/** 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 . */ #include "engine/object.hpp" #include "config.hpp" #include "util/minmax.hpp" namespace egn{ object_base::object_base(const math::vec3& position): m_position(position){} object_base::object_base(const math::vec3& position, const math::quaternion& orientation): m_position(position), m_orientation(orientation){} void object_base::translate(const math::vec3& distance){ set_position(m_position + distance); } void object_base::rotate(const math::quaternion& distance){ set_orientation(m_orientation * distance); } void object_base::scale(const math::vec3& distance){ set_scale(math::vec3{m_scale[0] * distance[0], m_scale[1] * distance[1], m_scale[2] * distance[2]}); } void object_base::look_at(const math::vec3& targ, const math::vec3& up){ math::vec3 front((m_position - targ).normalize()); math::vec3 right(math::cross(up, front)); math::vec3 true_up(math::cross(front, right)); set_orientation(math::quaternion{math::mat3{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& pos){ m_update_flag |= (TRANSLATION_UPDATE | BOUNDING_VOLUME_UPDATE); m_position = pos; } void object_base::set_orientation(const math::quaternion& orient){ m_update_flag |= (ROTATION_UPDATE | BOUNDING_VOLUME_UPDATE); m_orientation = orient; } void object_base::set_scale(const math::vec3& scale){ m_update_flag |= (SCALE_UPDATE | BOUNDING_VOLUME_UPDATE); m_scale = scale; } const math::mat4& 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& object_base::position()const{ return m_position; } const math::vec3& object_base::scale()const{ return m_scale; } const math::quaternion& 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& position, const math::vec3& aabb_p1, const math::vec3& 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& position, const math::quaternion& orientation, const math::vec3& aabb_p1, const math::vec3& 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(m_model_bounding_box.point1, 1.0f); m_world_bounding_box.point2 = model_mat * math::vec4(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& 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& position, const math::quaternion& 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 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(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; } }