fixed lighting bug

This commit is contained in:
devZoGok
2021-10-19 19:06:41 +03:00
parent 756c58fc89
commit 1f20307517
4 changed files with 33 additions and 36 deletions
+12 -14
View File
@@ -51,23 +51,26 @@ namespace vb01{
void Light::update(){
Root *root=Root::getSingleton();
Node *rootNode=root->getRootNode();
std::vector<Node*> descendants;
vector<Node*> descendants;
vector<Light*> lights;
rootNode->getDescendants(rootNode, descendants);
descendants.push_back(rootNode);
std::vector<Material*> 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<Light*> lights=d->getLights();
for(int i=0;i<lights.size();i++){
if(lights[i]==this)
thisId=i;
}
for(Light *l : d->getLights())
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);
+5 -5
View File
@@ -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)
+15 -16
View File
@@ -158,11 +158,11 @@ namespace vb01{
}
void Node::getDescendants(Node *node, vector<Node*> &descendants){
for(int i=0;i<node->getNumChildren();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<Node*> descendants;
rootNode->getDescendants(rootNode,descendants);
rootNode->getDescendants(rootNode, descendants);
descendants.push_back(rootNode);
for(Node *n : descendants){
vector<Mesh*> 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<Mesh*> 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);
}
}
}
+1 -1
View File
@@ -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<numParticles;i++){
Particle *p=new Particle;