camera.zig (1854B)
1 const std = @import("std"); 2 const math = @import("math"); 3 4 pub const PerspectiveCamera = struct { 5 fovy: f32 = 60, 6 aspect: f32 = 1, 7 near: f32 = 0.01, 8 far: f32 = 100, 9 yaw: f32 = 90.0, 10 pitch: f32 = 0.0, 11 position: math.Vec4 = .{ 0, 0, 0, 1 }, 12 13 pub fn view(self: *const PerspectiveCamera) math.Mat4 { 14 const cos_yaw = @cos(std.math.degreesToRadians(self.yaw)); 15 const sin_yaw = @sin(std.math.degreesToRadians(self.yaw)); 16 const cos_pitch = @cos(std.math.degreesToRadians(self.pitch)); 17 const sin_pitch = @sin(std.math.degreesToRadians(self.pitch)); 18 const forward = math.normalize(math.Vec4{ 19 cos_yaw * cos_pitch, 20 sin_pitch, 21 sin_yaw * cos_pitch, 22 0, 23 }); 24 return math.lookToLH(self.position, forward, .{ 0, 1, 0, 0 }); 25 } 26 27 pub fn invView(self: *const PerspectiveCamera) math.Mat4 { 28 const cos_yaw = @cos(std.math.degreesToRadians(self.yaw)); 29 const sin_yaw = @sin(std.math.degreesToRadians(self.yaw)); 30 const cos_pitch = @cos(std.math.degreesToRadians(self.pitch)); 31 const sin_pitch = @sin(std.math.degreesToRadians(self.pitch)); 32 const forward = math.normalize(math.Vec4{ 33 cos_yaw * cos_pitch, 34 sin_pitch, 35 sin_yaw * cos_pitch, 36 0, 37 }); 38 return math.invLookToLH(self.position, forward, .{ 0, 1, 0, 0 }); 39 } 40 41 pub fn proj(self: *const PerspectiveCamera) math.Mat4 { 42 return math.perspectiveLH( 43 self.fovy, 44 self.aspect, 45 self.near, 46 self.far, 47 ); 48 } 49 50 pub fn invProj(self: *const PerspectiveCamera) math.Mat4 { 51 return math.invPerspectiveLH( 52 self.fovy, 53 self.aspect, 54 self.near, 55 self.far, 56 ); 57 } 58 };