From 84cbd5e5c76cfbf88c2dea4d994c6adaf9168c07 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Fri, 22 Mar 2019 18:01:42 +0100 Subject: [PATCH] Fix transparancy check for nodes The previous method tried to read from meshes which had been freed from memory --- src/sceneGraph.cpp | 12 ++++++------ src/sceneGraph.hpp | 6 +++--- src/utilities/modelLoader.cpp | 5 ++++- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/sceneGraph.cpp b/src/sceneGraph.cpp index b27e9eb..eb053ab 100644 --- a/src/sceneGraph.cpp +++ b/src/sceneGraph.cpp @@ -14,7 +14,7 @@ void SceneNode::setMesh(const Mesh* mesh) { vertexArrayObjectID = cache[mesh]; VAOIndexCount = mesh->indices.size(); isVertexColored = ! mesh->colors.empty(); - m_gemoetry = mesh; + mesh_has_transparancy = mesh->has_transparancy; } void SceneNode::setTexture( const PNGImage* diffuse, @@ -28,7 +28,7 @@ void SceneNode::setTexture( isNormalMapped = false; isDisplacementMapped = false; isReflectionMapped = false; - t_diffuse = nullptr; + tex_has_transparancy = false; } if (diffuse) { @@ -36,7 +36,7 @@ void SceneNode::setTexture( cache[diffuse] = generateTexture(*diffuse); diffuseTextureID = cache[diffuse]; isTextured = true; - t_diffuse = diffuse; + tex_has_transparancy = diffuse->has_transparancy; } if (normal) { @@ -84,9 +84,9 @@ bool SceneNode::has_no_transforms() const { } bool SceneNode::has_transparancy() const { - return opacity < 1.0 - || t_diffuse && t_diffuse->has_transparancy - || m_gemoetry && m_gemoetry->has_transparancy; + return mesh_has_transparancy + || tex_has_transparancy + || opacity < 0.98; } SceneNode* SceneNode::clone() const { diff --git a/src/sceneGraph.hpp b/src/sceneGraph.hpp index 7f5a771..369fea5 100644 --- a/src/sceneGraph.hpp +++ b/src/sceneGraph.hpp @@ -82,9 +82,9 @@ struct SceneNode { float displacementCoefficient = 0.1; // in units uint reflectionTextureID; - // has_transparancy - const Mesh* m_gemoetry = nullptr; - const PNGImage* t_diffuse = nullptr; + // has_transparancy check + bool mesh_has_transparancy = false; + bool tex_has_transparancy = false; // shader flags bool isTextured = false; diff --git a/src/utilities/modelLoader.cpp b/src/utilities/modelLoader.cpp index b32cff1..8469db4 100644 --- a/src/utilities/modelLoader.cpp +++ b/src/utilities/modelLoader.cpp @@ -175,7 +175,8 @@ SceneNode* loadModelScene(const std::string& dirname, const std::string& filenam aimesh->mTextureCoords[0][i].y, }); } - + + mesh.has_transparancy = false; if (aimesh->GetNumColorChannels() >= 1) for (uint i=0; i < aimesh->mNumVertices; i++){ mesh.colors.push_back({ @@ -184,6 +185,8 @@ SceneNode* loadModelScene(const std::string& dirname, const std::string& filenam aimesh->mColors[0][i].b, aimesh->mColors[0][i].a, }); + if (aimesh->mColors[0][i].a < 255) + mesh.has_transparancy = true; } for (uint i=0; i < aimesh->mNumFaces; i++){