summaryrefslogtreecommitdiff
path: root/glm/camera.ha
blob: b6482327c74a904b3da7bf6eaa821754a8609ea8 (plain)
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
68
69
70
71
use math;

export fn ortho(
	m: *m4,
	left: f32,
	right: f32,
	bottom: f32,
	top: f32,
	znear: f32,
	zfar: f32,
) void = {
	m4_set_zero(m);

	const rl = 1.0 / (right - left);
	const tb = 1.0 / (top - bottom);
	const nf = -1.0 / (zfar - znear);

	m[0][0] = 2.0 * rl;
	m[1][1] = 2.0 * tb;
	m[2][2] = 2.0 * nf;
	m[3][0] =-(right + left) * rl;
	m[3][1] =-(top + bottom) * tb;
	m[3][2] = (zfar + znear) * nf;
	m[3][3] = 1.0;
};

export fn lookat(eye: *v3, center: *v3, up: *v3) m4 = {
	const f = v3_sub(center, eye);
	v3_normalize(&f);
	const s = v3_crossn(&f, up);
	const u = v3_cross(&s, &f);

	const res = m4_new(1.0);
	res[0][0] = s[0];
	res[0][1] = u[0];
	res[0][2] = -f[0];
	res[1][0] = s[1];
	res[1][1] = u[1];
	res[1][2] = -f[1];
	res[2][0] = s[2];
	res[2][1] = u[2];
	res[2][2] = -f[2];
	res[3][0] = -v3_dot(&s, eye);
	res[3][1] = -v3_dot(&u, eye);
	res[3][2] = v3_dot(&f, eye);
	res[0][3] = 0.0;
	res[1][3] = 0.0;
	res[2][3] = 0.0;
	res[3][3] = 1.0;
	return res;
};


export fn perspective(
	m: *m4,
	fovy_in_deg: f32,
	aspect_ratio: f32,
	znear: f32,
	zfar: f32
) void = {
	m4_set_zero(m);

	const f = 1.0 / math::tanf64(fovy_in_deg * 0.5): f32;
	const f1 = 1.0 / (znear - zfar);

	m[0][0] = f / aspect_ratio;
	m[1][1] = f;
	m[2][2] = (znear + zfar) * f1;
	m[2][3] = -1.0;
	m[3][2] = 2.0 * znear * zfar * f1;
};