Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/foundations/physics/camera.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -35,14 +35,34 @@ 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(
math.matrix.perspectiveProjection(cfg.fovy, s, 0.01, 750),
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,
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
};
}
Expand Down
1 change: 1 addition & 0 deletions src/foundations/scenes/look_at/LookAt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/foundations/scenes/plane_distance/PlaneDistance.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/foundations/scenes/plane_distance/PlaneDistanceUI.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down