quick backup
[demo] / src / camera.cc
1 #include <math.h>
2
3 #include <gmath/gmath.h>
4
5 #include "camera.h"
6 #include "state_manager.h"
7
8 Camera::Camera() {}
9 Camera::~Camera() {}
10
11 void Camera::use() const
12 {
13         state_manager.set_state("st_view_matrix", get_view_matrix());
14 }
15
16 OrbitCamera::OrbitCamera()
17 {
18         phi = theta = distance = 0;
19 }
20
21 OrbitCamera::~OrbitCamera() {}
22
23 void OrbitCamera::set_orbit_params(float phi, float theta, float distance)
24 {
25         this->phi = phi;
26         this->theta = theta;
27         this->distance = distance;
28 }
29
30 Mat4 OrbitCamera::get_view_matrix() const
31 {
32         Mat4 view_matrix;
33         view_matrix.rotate_y(theta * (float)M_PI / 180);
34         view_matrix.rotate_x(phi * (float)M_PI / 180);
35         view_matrix.translate(Vec3(0, 0, -distance));
36
37         return view_matrix;
38 }
39
40 Mat4 calc_projection_matrix(float fov_deg, float aspect, float n, float f)
41 {
42         float fov = fov_deg / 180 * M_PI;
43
44         float tmp;
45         tmp = 1 / tan(fov / 2.0);
46
47         /* near - far clipping planes */
48         float range = n - f;
49
50         Mat4 pmat = Mat4(
51                         tmp/aspect,     0,              0,                                      0,
52                         0,                              tmp,    0,                                      0,
53                         0,                              0,              (f + n) / range,        -1,
54                         0,                              0,              2 * n * f / range,      0
55                     );
56
57         return pmat;
58 }