From eb8b22d4edb857139b387d79fe7de994095c1aac Mon Sep 17 00:00:00 2001 From: devZoGok Date: Tue, 27 Dec 2022 16:11:10 +0200 Subject: [PATCH] opaque objects are rendered before transparent objects --- material.cpp | 10 ++++++++++ material.h | 1 + root.cpp | 41 +++++++++++++++++++++++++++++++---------- texture.cpp | 1 - texture.h | 3 ++- 5 files changed, 44 insertions(+), 12 deletions(-) diff --git a/material.cpp b/material.cpp index 8744baa..4d32b45 100755 --- a/material.cpp +++ b/material.cpp @@ -97,4 +97,14 @@ namespace vb01{ } } + + vector Material::getUniformsByType(Material::Uniform::Type type){ + vector uniforms; + + for(Uniform *uni : this->uniforms) + if(uni->type == type) + uniforms.push_back(uni); + + return uniforms; + } } diff --git a/material.h b/material.h index 2821d7b..8217779 100755 --- a/material.h +++ b/material.h @@ -145,6 +145,7 @@ namespace vb01{ void setVec4Uniform(std::string, Vector4); void setTexUniform(std::string, Texture*, bool); Uniform* getUniform(std::string); + std::vector getUniformsByType(Uniform::Type); inline Uniform* getUniform(int i){return uniforms[i];} inline void addIntUniform(std::string name, int value){uniforms.push_back(new IntUniform(name, value));} inline void addBoolUniform(std::string name, bool value){uniforms.push_back(new BoolUniform(name, value));} diff --git a/root.cpp b/root.cpp index 0c9545c..db2cfa9 100755 --- a/root.cpp +++ b/root.cpp @@ -173,30 +173,51 @@ namespace vb01{ glfwPollEvents(); } - //TODO replace sorting algorithm and sort only transparent objects + //TODO replace sorting algorithm void Root::updateNodeTree(Node *node){ - vector descendants; + vector descendants, opaqueNodes, transparentNodes; node->getDescendants(descendants); - int numDesc = descendants.size(); + for(Node *desc : descendants){ + if(!desc->getMeshes().empty()) + for(Mesh *mesh : desc->getMeshes()){ + Material *mat = mesh->getMaterial(); + vector uniforms = mat->getUniformsByType(Material::Uniform::TEXTURE); - for(int i = 0; i < numDesc; i++){ - Vector3 v1 = descendants[i]->getPosition() - camera->getPosition(); + if(!uniforms.empty()) + for(Material::Uniform *uni : uniforms){ + bool transparent = ((Material::TextureUniform*)uni)->value->isTransparent(); + (transparent ? transparentNodes : opaqueNodes).push_back(desc); + } + else + opaqueNodes.push_back(desc); + } + else + opaqueNodes.push_back(desc); + } + + for(Node *node : opaqueNodes) + node->update(); + + int numNodes = transparentNodes.size(); + + for(int i = 0; i < numNodes; i++){ + Vector3 v1 = transparentNodes[i]->getPosition() - camera->getPosition(); float a1 = v1.norm().getAngleBetween(camera->getDirection()); float d1 = v1.getLength() * cos(a1); - for(int j = i; j < numDesc; j++){ - Vector3 v2 = descendants[j]->getPosition() - camera->getPosition(); + for(int j = i; j < numNodes; j++){ + Vector3 v2 = transparentNodes[j]->getPosition() - camera->getPosition(); float a2 = v2.norm().getAngleBetween(camera->getDirection()); float d2 = v2.getLength() * cos(a2); if(d1 < d2) - swap(descendants[i], descendants[j]); + swap(transparentNodes[i], transparentNodes[j]); } } - for(Node *desc : descendants) - desc->update(); + for(Node *node : transparentNodes) + node->update(); } void Root::updateBloomFramebuffer(){ diff --git a/texture.cpp b/texture.cpp index 46cbb20..c97fdec 100755 --- a/texture.cpp +++ b/texture.cpp @@ -64,7 +64,6 @@ namespace vb01{ mixRatio = .1; for(int i = 0; i < numFrames; i++){ - bool png = false; int length = paths[i].length(); if(paths[i].substr(length - 4, string::npos) == ".png") diff --git a/texture.h b/texture.h index 9eb6588..beb1cb0 100755 --- a/texture.h +++ b/texture.h @@ -26,8 +26,9 @@ namespace vb01{ inline int getMipmapLevel(){return mipmapLevel;} inline float getMixRatio(){return mixRatio;} inline bool isCubemap(){return cubemap;} + inline bool isTransparent(){return png;} private: - bool cubemap = false; + bool cubemap = false, png = false; u32 *texture = nullptr; s64 lastUpdateTime = 0; int width, height, updateRate = 0, numFrames = 0, frameA = 0, frameB = 0, mipmapLevel = 1;