diff --git a/include/engine/object.hpp b/include/engine/object.hpp index 4f91aa3..74af426 100644 --- a/include/engine/object.hpp +++ b/include/engine/object.hpp @@ -39,8 +39,11 @@ namespace egn{ math::vec3 m_position; //track current positon in world space math::quaternion m_orientation; //track current model space rotation math::vec3 m_scale{1.0f, 1.0f, 1.0f}; //track model space scale + private: mutable math::mat4 m_model_matrix; //compile all the above info into a matrix + protected: mutable int m_update_flag = NO_UPDATE; //whether or not to update the matrix upon access + //make the update flag protected so subclasses can share it public: object_base() = default; @@ -62,10 +65,10 @@ namespace egn{ virtual void set_orientation(const math::quaternion& orient); virtual void set_scale(const math::vec3& scale); - const math::mat4& get_model_matrix()const; - const math::vec3& get_position()const; - const math::vec3& get_scale()const; - const math::quaternion& get_orientation()const; + const math::mat4& model_matrix()const; + const math::vec3& position()const; + const math::vec3& scale()const; + const math::quaternion& orientation()const; protected: void recalc_model_matrix()const; @@ -75,6 +78,7 @@ namespace egn{ { protected: aabb m_model_bounding_box; //model coordinate bounding volume + private: mutable aabb m_world_bounding_box; //world coordinate bounding volume public: using object_base::object_base; @@ -97,6 +101,7 @@ namespace egn{ { protected: aabs m_model_bounding_sphere; //model coordinate bounding volume + private: mutable aabs m_world_bounding_sphere; //world coordinate bounding volume public: using object_base::object_base; diff --git a/src/engine/object.cpp b/src/engine/object.cpp index c8908c0..3fc17ae 100644 --- a/src/engine/object.cpp +++ b/src/engine/object.cpp @@ -59,20 +59,20 @@ namespace egn{ m_update_flag |= (SCALE_UPDATE | BOUNDING_VOLUME_UPDATE); m_scale = scale; } - const math::mat4& object_base::get_model_matrix()const{ + 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::get_position()const{ + const math::vec3& object_base::position()const{ return m_position; } - const math::vec3& object_base::get_scale()const{ + const math::vec3& object_base::scale()const{ return m_scale; } - const math::quaternion& object_base::get_orientation()const{ + const math::quaternion& object_base::orientation()const{ return m_orientation; } @@ -99,9 +99,9 @@ namespace egn{ } const aabb& aabb_object::get_bounding_box()const{ if(m_update_flag){ - recalc_model_matrix(); - m_world_bounding_box.point1 = m_model_matrix * math::vec4(m_model_bounding_box.point1, 1.0f); - m_world_bounding_box.point2 = m_model_matrix * math::vec4(m_model_bounding_box.point2, 1.0f); + 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; diff --git a/src/play_state.cpp b/src/play_state.cpp index b73a6f9..18dc73a 100644 --- a/src/play_state.cpp +++ b/src/play_state.cpp @@ -97,7 +97,7 @@ void play_state::handle_input(const egn::input_event& ev){ for(size_t i = 0;i < m_scene.renderables.size();++i){ square& sq = static_cast(*m_scene.renderables[i]); - const math::vec3& sq_pos = sq.get_position(); + const math::vec3& sq_pos = sq.position(); if((projected1.x() > (sq_pos.x() - 1.0f) && projected1.x() < (sq_pos.x() + 1.0f)) && (projected1.y() > (sq_pos.y() - 1.0f) && projected1.y() < (sq_pos.y() + 1.0f))) { diff --git a/src/square.cpp b/src/square.cpp index 0b49281..d2433ed 100644 --- a/src/square.cpp +++ b/src/square.cpp @@ -50,7 +50,7 @@ void square::set_color(const math::vec4& color){ } void square::render(gfx::shader_program& shader){ - shader.set_uniform("model_mat", get_model_matrix()); + shader.set_uniform("model_mat", model_matrix()); shader.set_uniform("model_color", m_color_filter); shader.set_uniform("texture1", *m_model->mesh(0).material().get_material((int)m_active_value)); m_model->mesh(0).vertex().render(shader);