X-Git-Url: http://git.mutantstargoat.com?p=demo;a=blobdiff_plain;f=src%2Fcamera.cc;h=0f7773e75be449e2b73226b1306a2034a507176c;hp=d7839b57322f0419c0b7b97e193458b299d25382;hb=63d7f3b0e70ab5e3d530c579b1881967c96b0b92;hpb=f906c6d5d6b93dcc4c0f3d54e679cf86c829b91e diff --git a/src/camera.cc b/src/camera.cc index d7839b5..0f7773e 100644 --- a/src/camera.cc +++ b/src/camera.cc @@ -1,20 +1,58 @@ +#include + #include + #include "camera.h" +#include "state_manager.h" + +Camera::Camera() {} +Camera::~Camera() {} + +void Camera::use() const +{ + state_manager.set_state("st_view_matrix", get_view_matrix()); +} -Camera::Camera() +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; +} - this->fov = fov; +Mat4 OrbitCamera::get_view_matrix() const +{ + Mat4 view_matrix; + view_matrix.rotate_y(theta * (float)M_PI / 180); + view_matrix.rotate_x(phi * (float)M_PI / 180); + view_matrix.translate(Vec3(0, 0, -distance)); + + return view_matrix; } -Camera::~Camera() {} \ No newline at end of file +Mat4 calc_projection_matrix(float fov_deg, float aspect, float n, float f) +{ + float fov = fov_deg / 180 * M_PI; + + float tmp; + tmp = 1 / tan(fov / 2.0); + + /* near - far clipping planes */ + float range = n - f; + + Mat4 pmat = Mat4( + tmp/aspect, 0, 0, 0, + 0, tmp, 0, 0, + 0, 0, (f + n) / range, -1, + 0, 0, 2 * n * f / range, 0 + ); + + return pmat; +} \ No newline at end of file