render-zig

A 3D rendering engine written in Zig
git clone git://git.christianermann.dev/render-zig
Log | Files | Refs

cubesphere.zig (927B)


      1 const std = @import("std");
      2 
      3 const Mesh = @import("mesh.zig");
      4 const generateCube = @import("cube.zig").generateCube;
      5 const f32x3_normalize = @import("camera.zig").f32x3_normalize;
      6 const f32x3_mul = @import("camera.zig").f32x3_mul;
      7 
      8 const f32x3 = @Vector(3, f32);
      9 const f32x4 = @Vector(4, f32);
     10 
     11 pub fn generateCubeSphere(
     12     n_subdivisions: u32,
     13     allocator: std.mem.Allocator,
     14 ) !Mesh {
     15     const mesh = try generateCube(n_subdivisions, allocator);
     16     const up = f32x3{ 0, 1, 0 };
     17     for (
     18         mesh.positions,
     19         mesh.normals,
     20         mesh.tangents,
     21         mesh.bitangents,
     22     ) |*position, *normal, *tangent, *bitangent| {
     23         normal.* = f32x3_normalize(.{ position[0], position[1], position[2] });
     24         position.* = .{ normal[0], normal[1], normal[2], 1.0 };
     25         tangent.* = f32x3_normalize(f32x3_mul(up, normal.*));
     26         bitangent.* = f32x3_mul(normal.*, tangent.*);
     27     }
     28     return mesh;
     29 }