get height, camera + cow movement with keys
[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 theta, float phi, 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.translation(-position);
34         view_matrix.rotate_y(theta * (float)M_PI / 180);
35         view_matrix.rotate_x(phi * (float)M_PI / 180);
36         view_matrix.translate(Vec3(0, 0, -distance));
37
38         return view_matrix;
39 }
40
41 Mat4 calc_projection_matrix(float fov_deg, float aspect, float n, float f)
42 {
43         float fov = fov_deg / 180 * M_PI;
44
45         float tmp;
46         tmp = 1 / tan(fov / 2.0);
47
48         /* near - far clipping planes */
49         float range = n - f;
50
51         Mat4 pmat = Mat4(
52                         tmp/aspect,     0,              0,                                      0,
53                         0,                              tmp,    0,                                      0,
54                         0,                              0,      (f + n) / range,        -1,
55                         0,                              0,              2 * n * f / range,      0
56                     );
57
58         return pmat;
59 }
60
61 void OrbitCamera::set_position(float x, float y, float z)
62 {
63         position.x = x;
64         position.y = y;
65         position.z = z;
66 }