refactored light updating to account for lights in hidden nodes

This commit is contained in:
devZoGok
2024-09-13 19:46:57 +03:00
parent 1103e527a8
commit 0b99083bab
4 changed files with 25 additions and 12 deletions
+14 -5
View File
@@ -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<Node*> descendants;
rootNode->getDescendants(descendants);
descendants.push_back(rootNode);
vector<Light*> lights;
vector<Material*> 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<Node*> 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<Material*> 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");
+3 -1
View File
@@ -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;
+3 -3
View File
@@ -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();
+5 -3
View File
@@ -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);