our_dick/src/engine/object.cpp

137 lines
5.5 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/object.hpp"
#include "config.hpp"
#include "util/minmax.hpp"
namespace egn{
object_base::object_base(const math::vec3<GLfloat>& position):
m_position(position){}
object_base::object_base(const math::vec3<GLfloat>& position, const math::quaternion<GLfloat>& orientation):
m_position(position),
m_orientation(orientation){}
void object_base::translate(const math::vec3<GLfloat>& distance){
set_position(m_position + distance);
}
void object_base::rotate(const math::quaternion<GLfloat>& distance){
set_orientation(m_orientation * distance);
}
void object_base::scale(const math::vec3<GLfloat>& distance){
set_scale(math::vec3<GLfloat>{m_scale[0] * distance[0],
m_scale[1] * distance[1],
m_scale[2] * distance[2]});
}
void object_base::look_at(const math::vec3<GLfloat>& targ, const math::vec3<GLfloat>& up){
math::vec3<GLfloat> front((m_position - targ).normalize());
math::vec3<GLfloat> right(math::cross(up, front));
math::vec3<GLfloat> true_up(math::cross(front, right));
set_orientation(math::quaternion<GLfloat>{math::mat3<GLfloat>{right[0], true_up[0], front[0],
right[1], true_up[1], front[1],
right[2], true_up[2], front[2]}});
}
void object_base::set_position(const math::vec3<GLfloat>& pos){
m_update_flag |= (TRANSLATION_UPDATE | BOUNDING_VOLUME_UPDATE);
m_position = pos;
}
void object_base::set_orientation(const math::quaternion<GLfloat>& orient){
m_update_flag |= (ROTATION_UPDATE | BOUNDING_VOLUME_UPDATE);
m_orientation = orient;
}
void object_base::set_scale(const math::vec3<GLfloat>& scale){
m_update_flag |= (SCALE_UPDATE | BOUNDING_VOLUME_UPDATE);
m_scale = scale;
}
const math::mat4<GLfloat>& object_base::model_matrix()const{
if(m_update_flag & (SCALE_UPDATE | ROTATION_UPDATE | TRANSLATION_UPDATE)){
recalc_model_matrix();
m_update_flag &= (~(SCALE_UPDATE | ROTATION_UPDATE | TRANSLATION_UPDATE));
}
return m_model_matrix;
}
const math::vec3<GLfloat>& object_base::position()const{
return m_position;
}
const math::vec3<GLfloat>& object_base::scale()const{
return m_scale;
}
const math::quaternion<GLfloat>& object_base::orientation()const{
return m_orientation;
}
void object_base::recalc_model_matrix()const{
debug_print_verbose("Rebuilding model matrix\n");
m_model_matrix = m_orientation.to_mat4();
m_model_matrix.get(3, 0) = m_position[0];
m_model_matrix.get(3, 1) = m_position[1];
m_model_matrix.get(3, 2) = m_position[2];
}
aabb_object::aabb_object(const math::vec3<GLfloat>& position,
const math::vec3<GLfloat>& aabb_p1, const math::vec3<GLfloat>& aabb_p2):
object_base(position),
m_model_bounding_box(aabb_p1, aabb_p2),
m_world_bounding_box(m_model_bounding_box){}
aabb_object::aabb_object(const math::vec3<GLfloat>& position, const math::quaternion<GLfloat>& orientation,
const math::vec3<GLfloat>& aabb_p1, const math::vec3<GLfloat>& aabb_p2):
object_base(position, orientation),
m_model_bounding_box(aabb_p1, aabb_p2),
m_world_bounding_box(m_model_bounding_box){}
const aabb& aabb_object::get_model_bounding_box()const{
return m_model_bounding_box;
}
const aabb& aabb_object::get_bounding_box()const{
if(m_update_flag){
const auto& model_mat = model_matrix();
m_world_bounding_box.point1 = model_mat * math::vec4<float>(m_model_bounding_box.point1, 1.0f);
m_world_bounding_box.point2 = model_mat * math::vec4<float>(m_model_bounding_box.point2, 1.0f);
m_update_flag &= ~BOUNDING_VOLUME_UPDATE;
}
return m_world_bounding_box;
}
aabs_object::aabs_object(const math::vec3<GLfloat>& position, float aabs_r):
object_base(position),
m_model_bounding_sphere(aabs_r),
m_world_bounding_sphere(m_model_bounding_sphere){}
aabs_object::aabs_object(const math::vec3<GLfloat>& position, const math::quaternion<GLfloat>& orientation,
float aabs_r):
object_base(position, orientation),
m_model_bounding_sphere(aabs_r),
m_world_bounding_sphere(m_model_bounding_sphere){}
const aabs& aabs_object::get_model_bounding_sphere()const{
return m_model_bounding_sphere;
}
const aabs& aabs_object::get_bounding_sphere()const{
if(m_update_flag){
math::mat4<float> t_mat{1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
m_position.x(), m_position.y(), m_position.z(), 1};
m_world_bounding_sphere.point = t_mat * math::vec4<float>(m_model_bounding_sphere.point, 1.0f);
m_world_bounding_sphere.radius *= util::max(m_scale.x(), m_scale.y(), m_scale.z());
m_update_flag &= ~BOUNDING_VOLUME_UPDATE;
}
return m_world_bounding_sphere;
}
}