our_dick/src/engine/camera.cpp
2020-10-11 12:36:05 -07:00

107 lines
3.0 KiB
C++

/**
This file is a part of our_dick
Copyright (C) 2020 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "engine/camera.hpp"
#include "config.hpp"
#include "math/projection.hpp"
namespace egn{
camera_iface::camera_iface(const math::mat4<GLfloat>& proj, GLfloat n, GLfloat f):
m_projection_matrix(proj),
m_near(n), m_far(f){}
void camera_iface::set_position(const math::vec3<GLfloat>& pos){
object::set_position(pos);
m_update_flag |= VIEW_UPDATE;
}
void camera_iface::set_orientation(const math::quaternion<GLfloat>& orient){
object::set_orientation(orient);
m_update_flag |= VIEW_UPDATE;
}
const math::mat4<GLfloat>& camera_iface::get_projection_matrix()const{
if(m_update_flag & PROJ_UPDATE){
recalc_projection_matrix();
m_update_flag ^= PROJ_UPDATE;
}
return m_projection_matrix;
}
const math::mat4<GLfloat>& camera_iface::get_view_matrix()const{
if(m_update_flag & VIEW_UPDATE){
recalc_view_matrix();
m_update_flag ^= VIEW_UPDATE;
}
return m_view_matrix;
}
GLfloat camera_iface::get_near_plane()const{
return m_near;
}
GLfloat camera_iface::get_far_plane()const{
return m_far;
}
void camera_iface::set_near_plane(GLfloat n){
m_update_flag |= PROJ_UPDATE;
m_near = n;
}
void camera_iface::set_far_plane(GLfloat f){
m_update_flag |= PROJ_UPDATE;
m_far = f;
}
void camera_iface::recalc_view_matrix()const{
debug_print_verbose("Rebuilding view matrix\n");
m_view_matrix = m_orientation.to_mat4();
m_view_matrix.get(3, 0) = -m_position[0];
m_view_matrix.get(3, 1) = -m_position[1];
m_view_matrix.get(3, 2) = -m_position[2];
}
ortho_camera::ortho_camera(GLfloat w, GLfloat h, GLfloat n, GLfloat f):
camera_iface(math::ortho_projection(w, h, n, f), n, f),
m_width(w),
m_height(h){}
GLfloat ortho_camera::get_projection_width()const{
return m_width;
}
GLfloat ortho_camera::get_projection_height()const{
return m_height;
}
void ortho_camera::set_projection_width(GLfloat w){
m_update_flag |= PROJ_UPDATE;
m_width = w;
}
void ortho_camera::set_projection_height(GLfloat h){
m_update_flag |= PROJ_UPDATE;
m_height = h;
}
void ortho_camera::set_projection_box(GLfloat w, GLfloat h){
m_update_flag |= PROJ_UPDATE;
m_width = w;
m_height = h;
}
void ortho_camera::recalc_projection_matrix()const{
debug_print_verbose("Rebuilding orthographic projection matrix\n");
m_projection_matrix = math::ortho_projection(m_width, m_height, m_near, m_far);
}
}