From 0475f171100b9c5dd538dd63c2f9ecd7ebcb1179 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Fri, 22 Mar 2019 00:05:50 +0100 Subject: [PATCH] Small optimization, no having to recompute a matrix for all objects --- src/renderlogic.cpp | 8 ++++---- src/sceneGraph.cpp | 6 ++++++ src/sceneGraph.hpp | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/renderlogic.cpp b/src/renderlogic.cpp index 7ba8f72..9bafdb9 100644 --- a/src/renderlogic.cpp +++ b/src/renderlogic.cpp @@ -60,8 +60,10 @@ void initRenderer(GLFWwindow* window, CommandLineOptions options) { // traverses and updates matricies void updateNodeTransformations(SceneNode* node, mat4 transformationThusFar, mat4 const& V, mat4 const& P) { - mat4 transformationMatrix - = glm::translate(mat4(1.0), node->position) + mat4 M = (node->has_no_transforms()) + ? transformationThusFar + : transformationThusFar + * glm::translate(mat4(1.0), node->position) * glm::translate(mat4(1.0), node->referencePoint) * glm::rotate(mat4(1.0), node->rotation.z, vec3(0,0,1)) * glm::rotate(mat4(1.0), node->rotation.y, vec3(0,1,0)) @@ -69,8 +71,6 @@ void updateNodeTransformations(SceneNode* node, mat4 transformationThusFar, mat4 * glm::scale(mat4(1.0), node->scale) * glm::translate(mat4(1.0), -node->referencePoint); - mat4 M = transformationThusFar * transformationMatrix; - node->MV = V*M; node->MVP = P*node->MV; node->MVnormal = glm::inverse(glm::transpose(node->MV)); diff --git a/src/sceneGraph.cpp b/src/sceneGraph.cpp index b17de6e..b27e9eb 100644 --- a/src/sceneGraph.cpp +++ b/src/sceneGraph.cpp @@ -77,6 +77,12 @@ void SceneNode::setMaterial(const Material& mat, bool recursive) { child->setMaterial(mat, true); } +bool SceneNode::has_no_transforms() const { + return position.x == 0 && position.y == 0 && position.z == 0 + && rotation.x == 0 && rotation.y == 0 && rotation.z == 0 + && scale.x == 1 && scale.y == 1 && scale.z == 1; +} + bool SceneNode::has_transparancy() const { return opacity < 1.0 || t_diffuse && t_diffuse->has_transparancy diff --git a/src/sceneGraph.hpp b/src/sceneGraph.hpp index 4aa7042..7f5a771 100644 --- a/src/sceneGraph.hpp +++ b/src/sceneGraph.hpp @@ -42,6 +42,7 @@ struct SceneNode { const PNGImage* reflection=nullptr, bool texture_reset=true); void setMaterial(const Material& mat, bool recursive=false); + bool has_no_transforms() const; bool has_transparancy() const; SceneNode* clone() const;