From e07ea55a6ab7c886aa5800c1934e85b93c2a3b46 Mon Sep 17 00:00:00 2001 From: btipling Date: Sat, 17 Aug 2024 13:05:40 -0700 Subject: [PATCH] Add ability to set camera heading Set it for plane distance scene so that it looks at the side the plane is pointing --- src/foundations/physics/camera.zig | 27 +++++++++++++++++-- src/foundations/scenes/look_at/LookAt.zig | 1 + .../scenes/plane_distance/PlaneDistance.zig | 4 +-- .../scenes/plane_distance/PlaneDistanceUI.zig | 2 +- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/foundations/physics/camera.zig b/src/foundations/physics/camera.zig index 3c74ae4..91f7bc0 100644 --- a/src/foundations/physics/camera.zig +++ b/src/foundations/physics/camera.zig @@ -15,7 +15,7 @@ pub fn Camera(comptime T: type, comptime IntegratorT: type) type { camera_orientation: math.rotation.Quat = .{ 1, 0, 0, 0 }, cursor_pos: math.vector.vec3 = .{ 0, 0, 0 }, cursor_mode: bool = false, - use_camera: bool = false, + use_camera: bool = true, fly_mode: bool = false, persp_m: math.matrix, mvp: math.matrix, @@ -35,7 +35,14 @@ pub fn Camera(comptime T: type, comptime IntegratorT: type) type { const world_right: math.vector.vec3 = .{ 0, 0, 1 }; const world_forward: math.vector.vec3 = .{ 0, 1, 0 }; - pub fn init(allocator: std.mem.Allocator, cfg: *config, scene: T, integrator: IntegratorT, pos: math.vector.vec3) *Self { + pub fn init( + allocator: std.mem.Allocator, + cfg: *config, + scene: T, + integrator: IntegratorT, + pos: math.vector.vec3, + heading: ?f32, + ) *Self { const cam = allocator.create(Self) catch @panic("OOM"); const s = @as(f32, @floatFromInt(cfg.width)) / @as(f32, @floatFromInt(cfg.height)); const mvp = math.matrix.transformMatrix( @@ -43,6 +50,19 @@ pub fn Camera(comptime T: type, comptime IntegratorT: type) type { math.matrix.leftHandedXUpToNDC(), ); + var camera_heading: math.rotation.Quat = .{ 1, 0, 0, 0 }; + if (heading) |h| { + std.debug.print("setting heading\n", .{}); + const a: math.rotation.AxisAngle = .{ + .angle = h, + .axis = world_up, + }; + var q = math.rotation.axisAngleToQuat(a); + q = math.vector.normalize(q); + q = math.rotation.multiplyQuaternions(camera_heading, q); + camera_heading = math.vector.normalize(q); + } + cam.* = .{ .allocator = allocator, .camera_pos = pos, @@ -52,6 +72,8 @@ pub fn Camera(comptime T: type, comptime IntegratorT: type) type { .movement = undefined, .scene = scene, .integrator = integrator, + .camera_orientation = camera_heading, + .camera_orientation_heading = camera_heading, }; cam.movement = physics.movement.init(cam.camera_pos, 0, .none); cam.updateCameraMatrix(); @@ -371,6 +393,7 @@ pub fn Camera(comptime T: type, comptime IntegratorT: type) type { .{ .program = p, .uniform = uniform }, ) catch @panic("OOM"); rhi.setUniformMatrix(p, uniform, self.mvp); + self.updateMVP(); } }; } diff --git a/src/foundations/scenes/look_at/LookAt.zig b/src/foundations/scenes/look_at/LookAt.zig index 4445436..d49a28f 100644 --- a/src/foundations/scenes/look_at/LookAt.zig +++ b/src/foundations/scenes/look_at/LookAt.zig @@ -29,6 +29,7 @@ pub fn init(allocator: std.mem.Allocator, cfg: *config) *LookAt { lkt, integrator, .{ 1, 3.5, 1 }, + null, ); errdefer cam.deinit(allocator); const grid = scenery.Grid.init(allocator); diff --git a/src/foundations/scenes/plane_distance/PlaneDistance.zig b/src/foundations/scenes/plane_distance/PlaneDistance.zig index e7734f4..bdd1194 100644 --- a/src/foundations/scenes/plane_distance/PlaneDistance.zig +++ b/src/foundations/scenes/plane_distance/PlaneDistance.zig @@ -30,7 +30,8 @@ pub fn init(allocator: std.mem.Allocator, cfg: *config) *PlaneDistance { cfg, pd, integrator, - .{ 0, 0, 0 }, + .{ 0, 200, -100 }, + std.math.pi * 0.75, ); errdefer cam.deinit(allocator); const grid = scenery.Grid.init(allocator); @@ -88,7 +89,6 @@ pub fn updatePlane(self: *PlaneDistance, m: math.matrix) void { const n = math.vector.normalize(math.vector.crossProduct(u, v)); const d: f32 = math.vector.dotProduct(math.vector.negate(n), q); self.plane = math.geometry.Plane.init(n, d); - self.plane.debug(); } pub fn updatePlaneTransform(self: *PlaneDistance, prog: u32) void { diff --git a/src/foundations/scenes/plane_distance/PlaneDistanceUI.zig b/src/foundations/scenes/plane_distance/PlaneDistanceUI.zig index 39c5241..86d8de5 100644 --- a/src/foundations/scenes/plane_distance/PlaneDistanceUI.zig +++ b/src/foundations/scenes/plane_distance/PlaneDistanceUI.zig @@ -4,7 +4,7 @@ rotation: [3]f32 = .{ std.math.pi, std.math.pi, }, -translate: [3]f32 = .{ -100, 100, -100 }, +translate: [3]f32 = .{ -100, -100, -100 }, updated: bool = true, const PlaneDistanceUI = @This();