quick backup:
[demo] / src / camera.cc
index d7839b5..4db7286 100644 (file)
@@ -1,20 +1,53 @@
+#include <math.h>
+
 #include <gmath/gmath.h>
+
 #include "camera.h"
+#include "state_manager.h"
 
-Camera::Camera()
+Camera::Camera() {}
+Camera::~Camera() {}
+
+void Camera::use() const
+{
+       state_manager.set_state("st_view_matrix", get_view_matrix());
+}
+
+OrbitCamera::OrbitCamera()
 {
        phi = theta = distance = 0;
-       fov = 0;
-       m_projection = Mat4::identity;
 }
 
-Camera::Camera(float phi, float theta, float distance, float fov)
+OrbitCamera::~OrbitCamera() {}
+
+void OrbitCamera::set_orbit_params(float phi, float theta, float distance)
 {
        this->phi = phi;
        this->theta = theta;
        this->distance = distance;
+}
+
+Mat4 OrbitCamera::get_view_matrix() const
+{
+       Mat4 view_matrix;
+       view_matrix.translate(Vec3(0, 0, -distance));
+       view_matrix.rotate_x(phi * (float)M_PI / 180);
+       view_matrix.rotate_y(theta * (float)M_PI / 180);
 
-       this->fov = fov;
+       return view_matrix;
 }
 
-Camera::~Camera() {}
\ No newline at end of file
+Mat4 calc_projection_matrix(float f, float n, float aspect, float fov)
+{
+       float tmp;
+       tmp = 1 / tan(fov / 2.0);
+
+       Mat4 pmat = Mat4(
+                       -tmp/aspect,    0,              0,                                      0,
+                       0,                              tmp,    0,                                      0,
+                       0,                              0,      (f + n) / (n - f),      (2 * f * n) / (n - f),
+                       0,                              0,              -1,                                     0
+                   );
+
+       return pmat;
+}
\ No newline at end of file