Small optimization, no having to recompute a matrix for all objects

This commit is contained in:
Peder Bergebakken Sundt 2019-03-22 00:05:50 +01:00
parent a5bfa4c237
commit 0475f17110
3 changed files with 11 additions and 4 deletions

View File

@ -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));

View File

@ -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

View File

@ -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;