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