From 0b99083bab7c4ec6dc706ac4b0df0f4d1b8d60b9 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Fri, 13 Sep 2024 19:46:57 +0300 Subject: [PATCH] refactored light updating to account for lights in hidden nodes --- light.cpp | 19 ++++++++++++++----- light.h | 4 +++- node.cpp | 6 +++--- texture.frag | 8 +++++--- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/light.cpp b/light.cpp index db080fe..0f423e2 100755 --- a/light.cpp +++ b/light.cpp @@ -75,32 +75,39 @@ namespace vb01{ glBindFramebuffer(GL_FRAMEBUFFER, *(Root::getSingleton()->getFBO())); } - void Light::update(){ + void Light::update(bool render){ Root *root = Root::getSingleton(); Node *rootNode = root->getRootNode(); vector descendants; rootNode->getDescendants(descendants); descendants.push_back(rootNode); + vector lights; vector materials; for(Node *d : descendants){ for(Light *l : d->getLights()) lights.push_back(l); - for(Mesh *m : d->getMeshes()) - materials.push_back(m->getMaterial()); + for(Mesh *mesh : d->getMeshes()) + materials.push_back(mesh->getMaterial()); } int thisId = -1; + for(int i = 0; i < lights.size(); i++) if(lights[i] == this) thisId = i; + for(Material *mat : materials){ + Shader *shader = mat->getShader(); + shader->use(); + shader->setBool(render, "lights[" + to_string(thisId) + "].render"); + } + mat4 proj = mat4(1.), view = mat4(1.); renderShadow(descendants, proj, view); updateShader(materials, thisId, proj, view); - } void Light::renderShadow(std::vector descendants, mat4 &proj, mat4 &view){ @@ -176,8 +183,10 @@ namespace vb01{ glViewport(0, 0, root->getWidth(), root->getHeight()); } + //TODO replace depth map id literals void Light::updateShader(vector materials, int thisId, mat4 &proj, mat4 &view){ Vector3 position = node->localToGlobalPosition(Vector3::VEC_ZERO), direction = node->getGlobalAxis(2); + for(Material *m : materials){ Shader *shader = m->getShader(); shader->use(); @@ -200,7 +209,7 @@ namespace vb01{ shader->setFloat(attenuationValues.x, "lights[" + to_string(thisId) + "].a"); shader->setFloat(attenuationValues.y, "lights[" + to_string(thisId) + "].b"); shader->setFloat(attenuationValues.z, "lights[" + to_string(thisId) + "].c"); - shader->setBool(useAngle, "lights[" + to_string(thisId) + "].additive"); + shader->setBool(useAngle, "lights[" + to_string(thisId) + "].useAngle"); break; case DIRECTIONAL: shader->setMat4(proj * view, "lights[" + to_string(thisId) + "].lightMat"); diff --git a/light.h b/light.h index 93d47b5..9fe4424 100755 --- a/light.h +++ b/light.h @@ -21,7 +21,9 @@ namespace vb01{ Light(Type, std::string = ""); ~Light(); - void update(); + void update(bool); + inline Vector3 getColor(){return color;} + inline Type getLightType(){return type;} inline void setColor(Vector3 color){this->color = color;} inline void setAttenuationValues(float a, float b, float c){ attenuationValues.x = a; diff --git a/node.cpp b/node.cpp index ce478d9..f49aaaf 100755 --- a/node.cpp +++ b/node.cpp @@ -58,10 +58,10 @@ namespace vb01{ if(!render) break; } - if(render){ - for(Light *l : lights) - l->update(); + for(Light *l : lights) + l->update(render); + if(render){ for(Mesh *m : meshes) m->update(); diff --git a/texture.frag b/texture.frag index b8023e1..75de987 100755 --- a/texture.frag +++ b/texture.frag @@ -15,7 +15,7 @@ layout (location = 1) out vec4 BrightColor; //0-POINT,1-DIRECTIONAL,2-SPOT struct Light{ - bool useAngle, additive; + bool useAngle, additive, render; int type, attenuation; vec3 pos, color, direction; float innerAngle, outerAngle; @@ -100,8 +100,10 @@ void main(){ bool addedLighting = false; for(int i = 0; i < numLights; i++){ + if(!lights[i].render) continue; + vec3 lightDir = vec3(0), viewDir = normalize(camPos - fragPos); - float attenuation = 0, coef = 1; + float attenuation = 1, coef = 1; float factor = 0; bool canAdd = false; @@ -125,7 +127,7 @@ void main(){ } else if(lights[i].type == 1){ lightDir = -normalize(lights[i].direction); - factor = max(dot(lightDir, normalize(normal)), 0.) * attenuation * coef; + factor = max(dot(lightDir, normalize(normal)), 0.) * attenuation; } else if(lights[i].type == 2){ lightDir = normalize(lights[i].pos - fragPos);