1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
const std = @import("std");
pub const Mat4 = [16]f32;
pub const identity: Mat4 = .{ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
pub fn multiply(a: Mat4, b: Mat4) Mat4 {
var out: Mat4 = @splat(0);
for (0..4) |col| for (0..4) |row| {
for (0..4) |k| out[col * 4 + row] += a[k * 4 + row] * b[col * 4 + k];
};
return out;
}
fn sub(a: [3]f32, b: [3]f32) [3]f32 {
return .{ a[0] - b[0], a[1] - b[1], a[2] - b[2] };
}
fn dot(a: [3]f32, b: [3]f32) f32 {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
fn cross(a: [3]f32, b: [3]f32) [3]f32 {
return .{ a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0] };
}
fn unit(a: [3]f32) [3]f32 {
const n = @sqrt(dot(a, a));
return .{ a[0] / n, a[1] / n, a[2] / n };
}
pub const Camera = struct {
eye: [3]f32,
target: [3]f32,
up: [3]f32 = .{ 0, 1, 0 },
fov: f32 = std.math.pi / 3.0,
near: f32 = 0.1,
far: f32 = 300,
aspect_ratio: ?f32 = null,
orthographic: ?struct { xmag: f32, ymag: f32 } = null,
/// Right-handed, column-major; depth range 0..1, camera forward -Z.
pub fn matrix(c: Camera, aspect: f32) Mat4 {
const z = unit(sub(c.eye, c.target));
const x = unit(cross(c.up, z));
const y = cross(z, x);
const view: Mat4 = .{ x[0], y[0], z[0], 0, x[1], y[1], z[1], 0, x[2], y[2], z[2], 0, -dot(x, c.eye), -dot(y, c.eye), -dot(z, c.eye), 1 };
const scale = 1 / @tan(c.fov / 2);
const d = c.near - c.far;
const projection: Mat4 = if (c.orthographic) |o|
.{ 1 / o.xmag, 0, 0, 0, 0, 1 / o.ymag, 0, 0, 0, 0, 1 / d, 0, 0, 0, c.near / d, 1 }
else if (std.math.isInf(c.far))
.{ scale / (c.aspect_ratio orelse aspect), 0, 0, 0, 0, scale, 0, 0, 0, 0, -1, -1, 0, 0, -c.near, 0 }
else
.{ scale / (c.aspect_ratio orelse aspect), 0, 0, 0, 0, scale, 0, 0, 0, 0, c.far / d, -1, 0, 0, c.near * c.far / d, 0 };
return multiply(projection, view);
}
};
test "perspective maps near and far to zero and one" {
const camera: Camera = .{ .eye = .{ 0, 0, 0 }, .target = .{ 0, 0, -1 } };
const m = camera.matrix(1);
try std.testing.expectApproxEqAbs(@as(f32, 0), (m[10] * -camera.near + m[14]) / camera.near, 0.00001);
try std.testing.expectApproxEqAbs(@as(f32, 1), (m[10] * -camera.far + m[14]) / camera.far, 0.00001);
}
test "orthographic and infinite perspective keep valid depth" {
var camera: Camera = .{ .eye = .{ 0, 0, 0 }, .target = .{ 0, 0, -1 }, .orthographic = .{ .xmag = 2, .ymag = 3 } };
const ortho = camera.matrix(1);
try std.testing.expectApproxEqAbs(@as(f32, 0), ortho[10] * -camera.near + ortho[14], 0.00001);
try std.testing.expectApproxEqAbs(@as(f32, 1), ortho[10] * -camera.far + ortho[14], 0.00001);
camera.orthographic = null;
camera.far = std.math.inf(f32);
const infinite = camera.matrix(1);
for (infinite) |v| try std.testing.expect(std.math.isFinite(v));
try std.testing.expectApproxEqAbs(@as(f32, 0), (infinite[10] * -camera.near + infinite[14]) / camera.near, 0.00001);
}
|