render-zig

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

commit 90b9db3b68b366a0a54bb1770ac600d859f8feb5
parent 04d09de0b45d9d57e623811aa525fd7ad9f0aca2
Author: Christian Ermann <christianermann@gmail.com>
Date:   Sun,  8 Dec 2024 22:22:14 -0800

Add cube sphere

Diffstat:
Asrc/cubesphere.zig | 29+++++++++++++++++++++++++++++
Msrc/main.zig | 10++++++++++
2 files changed, 39 insertions(+), 0 deletions(-)

diff --git a/src/cubesphere.zig b/src/cubesphere.zig @@ -0,0 +1,29 @@ +const std = @import("std"); + +const Mesh = @import("mesh.zig"); +const generateCube = @import("cube.zig").generateCube; +const f32x3_normalize = @import("camera.zig").f32x3_normalize; +const f32x3_mul = @import("camera.zig").f32x3_mul; + +const f32x3 = @Vector(3, f32); +const f32x4 = @Vector(4, f32); + +pub fn generateCubeSphere( + n_subdivisions: u32, + allocator: std.mem.Allocator, +) !Mesh { + const mesh = try generateCube(n_subdivisions, allocator); + const up = f32x3{ 0, 1, 0 }; + for ( + mesh.positions, + mesh.normals, + mesh.tangents, + mesh.bitangents, + ) |*position, *normal, *tangent, *bitangent| { + normal.* = f32x3_normalize(.{ position[0], position[1], position[2] }); + position.* = .{ normal[0], normal[1], normal[2], 1.0 }; + tangent.* = f32x3_normalize(f32x3_mul(up, normal.*)); + bitangent.* = f32x3_mul(normal.*, tangent.*); + } + return mesh; +} diff --git a/src/main.zig b/src/main.zig @@ -434,6 +434,9 @@ pub fn main() !void { const generateCube = @import("cube.zig").generateCube; const cube_mesh = try generateCube(1, allocator); + const generateCubeSphere = @import("cubesphere.zig").generateCubeSphere; + const cubesphere_mesh = try generateCubeSphere(16, allocator); + var rp = try MeshRenderPipeline.init( &app, .{ @@ -466,6 +469,7 @@ pub fn main() !void { var transform_2 = Transform{}; var transform_3 = Transform{ .offset = .{ 3, 3, 3 } }; var transform_4 = Transform{ .offset = .{ -3, -3, 0 } }; + var transform_5 = Transform{ .offset = .{ 3, -3, 1 } }; var render_mesh = try render_data.addMesh( &transform_1, @@ -491,6 +495,12 @@ pub fn main() !void { allocator, ); + _ = try render_data.addMesh( + &transform_5, + &cubesphere_mesh, + allocator, + ); + const corners = mesh.bboxVertices(); const bbox_indices = Mesh.bboxIndices();