-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.hpp
83 lines (59 loc) · 2.22 KB
/
camera.hpp
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
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once
#include <cmath>
#include "types/mat4.hpp"
#include "types/vec3.hpp"
class Camera
{
public:
blit::Mat4 get_look_matrix() const
{
auto front = look_at - position;
front.normalize();
auto side = front.cross(up);
auto up2 = side.cross(front);
auto mat = blit::Mat4::identity();
mat.v00 = side.x; mat.v01 = side.y; mat.v02 = side.z; mat.v03 = (-position).dot( side);
mat.v10 = up2.x; mat.v11 = up2.y; mat.v12 = up2.z; mat.v13 = (-position).dot( up2);
mat.v20 = -front.x; mat.v21 = -front.y; mat.v22 = -front.z; mat.v23 = (-position).dot(-front);
return mat;
}
static blit::Mat4 frustum_matrix(float left, float right, float bottom, float top, float near, float far)
{
auto mat = blit::Mat4::identity();
mat.v00 = (2.0f * near) / (right - left);
mat.v11 = (2.0f * near) / (top - bottom);
mat.v02 = (right + left) / (right - left);
mat.v12 = (top + bottom) / (top - bottom);
mat.v22 = -((far + near) / (far - near));
mat.v32 = -1.0f;
mat.v23 = -((2 * far * near) / (far - near));
mat.v33 = 0.0f;
return mat;
}
static blit::Mat4 orthographic_matrix(float left, float right, float bottom, float top, float near, float far)
{
auto mat = blit::Mat4::identity();
mat.v00 = 2.0f / (right - left);
mat.v11 = 2.0f / (top - bottom);
mat.v22 = -2.0f / (far - near);
mat.v03 = -((right + left) / (right - left));
mat.v13 = -((top + bottom) / (top - bottom));
mat.v23 = -((far + near) / (far - near));
return mat;
}
static blit::Mat4 orthographic_matrix(int width, int height, float near, float far)
{
// 0,0 -> w,h
return orthographic_matrix(0.0f, width, 0.0f, height, near, far);
}
static blit::Mat4 perspective_matrix(float near, float far, float fov, float aspect)
{
auto tan_fov = std::tan(fov);
auto near_h = near * tan_fov;
auto near_w = near_h * aspect;
return frustum_matrix(-near_w, near_w, near_h, -near_h, near, far);
}
blit::Vec3 position;
blit::Vec3 look_at;
blit::Vec3 up = {0.0f, 1.0f, 0.0f};
};