From 1f2030751731c579779d236fbe4f37767050b118 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 20 Sep 2020 18:13:13 +0300 Subject: [PATCH] fixed lighting bug --- light.cpp | 26 ++++++++++++-------------- material.cpp | 10 +++++----- node.cpp | 31 +++++++++++++++---------------- particleEmitter.cpp | 2 +- 4 files changed, 33 insertions(+), 36 deletions(-) diff --git a/light.cpp b/light.cpp index 51faa97..31ae892 100755 --- a/light.cpp +++ b/light.cpp @@ -51,23 +51,26 @@ namespace vb01{ void Light::update(){ Root *root=Root::getSingleton(); Node *rootNode=root->getRootNode(); - std::vector descendants; + vector descendants; + vector lights; rootNode->getDescendants(rootNode, descendants); + descendants.push_back(rootNode); std::vector materials; Camera *cam=root->getCamera(); - int thisId=-1; - float fov=cam->getFov(),width=root->getWidth(),height=root->getHeight(); + + int thisId = -1; + float fov = cam->getFov(), width = root->getWidth(), height = root->getHeight(); for(Node *d : descendants){ - std::vector lights=d->getLights(); - for(int i=0;igetLights()) + lights.push_back(l); for(Mesh *m : d->getMeshes()) materials.push_back(m->getMaterial()); } - mat4 proj=mat4(1.), view=mat4(1.); + for(int i = 0; i < lights.size(); i++) + if(lights[i] == this) + thisId = i; + mat4 proj = mat4(1.), view = mat4(1.); renderShadow(descendants, proj, view); updateShader(materials, thisId, proj, view); @@ -144,11 +147,6 @@ namespace vb01{ shader->setVec3(color,"light["+to_string(thisId)+"].color"); shader->setFloat(nearPlane,"light["+to_string(thisId)+"].near"); shader->setFloat(farPlane,"light["+to_string(thisId)+"].far"); - shader->setInt(0,"diffuseMap"); - shader->setInt(1,"normalMap"); - shader->setInt(2,"specularMap"); - shader->setInt(3,"parallaxMap"); - shader->setInt(4,"environmentMap"); shader->setInt(5,"light["+to_string(thisId)+"].depthMapCube"); shader->setInt(6,"light["+to_string(thisId)+"].depthMap"); depthMap->select(type==POINT?5:6); diff --git a/material.cpp b/material.cpp index d3fbd4c..62fc48f 100755 --- a/material.cpp +++ b/material.cpp @@ -53,6 +53,11 @@ namespace vb01{ if(type==MATERIAL_2D){ shader->setBool(lightingEnabled,"lightingEnabled"); shader->setBool(normalMapEnabled,"normalMapEnabled"); + shader->setInt(0,"diffuseMap"); + shader->setInt(1,"normalMap"); + shader->setInt(2,"specularMap"); + shader->setInt(3,"parallaxMap"); + shader->setInt(4,"environmentMap"); } shader->setBool(texturingEnabled,"texturingEnabled"); if(type==MATERIAL_GUI){ @@ -61,11 +66,6 @@ namespace vb01{ shader->setVec4(diffuseColor,"diffuseColor"); } if(texturingEnabled){ - shader->setInt(0,"diffuseMap"); - shader->setInt(1,"normalMap"); - shader->setInt(2,"specularMap"); - shader->setInt(3,"parallaxMap"); - shader->setInt(4,"environmentMap"); for(Texture *t : diffuseMapTextures) t->update(0); for(Texture *t : normalMapTextures) diff --git a/node.cpp b/node.cpp index c6521f2..7e3b845 100755 --- a/node.cpp +++ b/node.cpp @@ -158,11 +158,11 @@ namespace vb01{ } void Node::getDescendants(Node *node, vector &descendants){ - for(int i=0;igetNumChildren();i++){ - if(node->getChild(i)->getNumChildren()>0) - getDescendants(node->getChild(i),descendants); + for(Node *child : node->getChildren()){ + if(child->getNumChildren() > 0) + getDescendants(child, descendants); else - descendants.push_back(node->getChild(i)); + descendants.push_back(child); } } @@ -290,21 +290,20 @@ namespace vb01{ } void Node::updateShaders(){ - Root *root=Root::getSingleton(); - Node *rootNode=Root::getSingleton()->getRootNode(); + Root *root = Root::getSingleton(); + Node *rootNode = Root::getSingleton()->getRootNode(); vector descendants; - rootNode->getDescendants(rootNode,descendants); + rootNode->getDescendants(rootNode, descendants); descendants.push_back(rootNode); - for(Node *n : descendants){ - vector meshes=n->getMeshes(); - for(Mesh *m : meshes){ - Material *mat=m->getMaterial(); - string str="const int numLights="+to_string(root->numLights>0?root->numLights:1)+";"; - if(mat){ - //mat->getShader()->editShader(true,'=',';',str); - mat->getShader()->editShader(Shader::FRAGMENT_SHADER,1,str); - } + for(Node *n : descendants){ + vector meshes = n->getMeshes(); + for(Mesh *m : meshes){ + Material *mat = m->getMaterial(); + string str = "const int numLights = " + to_string(root->numLights > 0 ? root->numLights : 1) + ";"; + + if(mat) + mat->getShader()->editShader(Shader::FRAGMENT_SHADER, 1, str); } } } diff --git a/particleEmitter.cpp b/particleEmitter.cpp index d36c09b..721e48f 100755 --- a/particleEmitter.cpp +++ b/particleEmitter.cpp @@ -46,7 +46,7 @@ namespace vb01{ vertices[5].pos=Vector3(size.x/2,size.y/2,0); vertices[5].texCoords=Vector2(1,1); - mat4 *matrices=new mat4[numParticles]; + matrices=new mat4[numParticles]; for(int i=0;i