1#include "camera.hpp"
2
3#include "math.hpp"
4#include "renderer.hpp"
5
6Mat4 perspective(float fov, float aspect, float near_plane, float far_plane) {
7 // far + near / far - near 0 0 -(2.0f * far * near)/ far - near) \\
8 // 0 near_plane / right 0 0 \\
9 // 0 0 near_plane / top 0 \\
10 // 1.0f 0 0 0 \\
11
12 Mat4 r = Mat4::zeroed();
13
14 float top = near_plane * tan(fov * 0.5f);
15 float right = top * aspect;
16
17 // X = depth
18 r.m[0][0] = (far_plane + near_plane) / (far_plane - near_plane);
19 r.m[0][3] = 1.0f; // w row
20 r.m[3][0] = -(2.0f * far_plane * near_plane) / (far_plane - near_plane);
21 // Y = Right
22 r.m[1][1] = near_plane / right;
23 // Z = up
24 r.m[2][2] = near_plane / top;
25
26 return r;
27};
28
29Mat4 lookAt(Camera *camera) {
30 vec3f f = camera->forward;
31 vec3f r = camera->right;
32 vec3f u = camera->up;
33 vec3f p = camera->position;
34
35 Mat4 view = Mat4::identity();
36 // mind the transpose here since its a complete inverse
37 view = {
38 {{f.x, r.x, u.x, 0.0}, // X+ forward row
39 {f.y, r.y, u.y, 0.0}, // Y- right row
40 {f.z, r.z, u.z, 0.0}, // Z+ up row
41 {-(f.dot(p)), -(r.dot(p)), -(u.dot(p)), 1.0f}} // negate cam world pos translateions
42 };
43
44 return view;
45};