summaryrefslogtreecommitdiff
path: root/glm/camera.ha
diff options
context:
space:
mode:
authorLassi Pulkkinen <lassi@pulk.fi>2024-10-31 03:11:21 +0200
committerLassi Pulkkinen <lassi@pulk.fi>2024-10-31 03:51:35 +0200
commitae44478b30d890fe0fb04022f44d474dcdcc3f9d (patch)
tree5f462459ae4b47d22114eed717d1382d08cf4dfe /glm/camera.ha
Initial commit (import old repo)HEADmain
Diffstat (limited to 'glm/camera.ha')
-rw-r--r--glm/camera.ha71
1 files changed, 71 insertions, 0 deletions
diff --git a/glm/camera.ha b/glm/camera.ha
new file mode 100644
index 0000000..b648232
--- /dev/null
+++ b/glm/camera.ha
@@ -0,0 +1,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;
+};