From 1103e527a8a7e622318208d6f436d734d6eb4863 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Wed, 11 Sep 2024 20:23:25 +0300 Subject: [PATCH] added angle usage, attenuation, additive lighting properties --- light.cpp | 4 ++++ light.h | 12 +++++++++++- texture.frag | 49 +++++++++++++++++++++++++++++++++++++------------ 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/light.cpp b/light.cpp index 4984e62..db080fe 100755 --- a/light.cpp +++ b/light.cpp @@ -185,6 +185,7 @@ namespace vb01{ shader->setVec3(color, "lights[" + to_string(thisId) + "].color"); shader->setFloat(nearPlane, "lights[" + to_string(thisId) + "].near"); shader->setFloat(farPlane, "lights[" + to_string(thisId) + "].far"); + shader->setBool(additiveLighting, "lights[" + to_string(thisId) + "].additive"); int depthMapId = 10; shader->setInt(depthMapId, "lights[" + to_string(thisId) + "].depthMap"); @@ -194,9 +195,12 @@ namespace vb01{ switch(type){ case POINT: shader->setVec3(position, "lights[" + to_string(thisId) + "].pos"); + shader->setInt((int)attenuation, "lights[" + to_string(thisId) + "].attenuation"); + shader->setFloat(radius, "lights[" + to_string(thisId) + "].radius"); 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"); break; case DIRECTIONAL: shader->setMat4(proj * view, "lights[" + to_string(thisId) + "].lightMat"); diff --git a/light.h b/light.h index 1732d94..93d47b5 100755 --- a/light.h +++ b/light.h @@ -17,6 +17,7 @@ namespace vb01{ class Light : public Animatable, public Attachable{ public: enum Type{POINT, DIRECTIONAL, SPOT, AMBIENT}; + enum Attenuation{QUADRATIC, LINEAR, NONE}; Light(Type, std::string = ""); ~Light(); @@ -31,7 +32,14 @@ namespace vb01{ inline void setOuterAngle(float outerAngle){this->outerAngle = outerAngle;} inline void setShadowNearPlane(float nearPlane){this->nearPlane = nearPlane;} inline void setShadowFarPlane(float farPlane){this->farPlane = farPlane;} + inline void setAttenuation(Attenuation at){this->attenuation = at;} + inline void setRadius(float r){this->radius = r;} + inline void setAdditiveLighting(bool al){this->additiveLighting = al;} + inline void setUseAngle(bool ua){this->useAngle = ua;} + inline Attenuation getAttenuation(){return attenuation;} inline Vector3 getAttenuationValues(){return attenuationValues;} + inline bool isAdditiveLighting(){return additiveLighting;} + inline float getRadius(){return radius;} inline float getInnerAngle(){return innerAngle;} inline float getOuterAngle(){return outerAngle;} inline float getShadowNearPlane(){return nearPlane;} @@ -43,10 +51,12 @@ namespace vb01{ void updateShader(std::vector, int, glm::mat4&, glm::mat4&); Type type; + bool additiveLighting = true, useAngle = true; + Attenuation attenuation = Attenuation::QUADRATIC; Shader *depthMapShader; Texture *depthMap = nullptr; Vector3 color = Vector3::VEC_IJK, attenuationValues = Vector3(1.8, .7, 1); - float innerAngle = .707, outerAngle = .714, nearPlane = .1, farPlane = 100; + float innerAngle = .707, outerAngle = .714, nearPlane = .1, farPlane = 100, radius = 0; unsigned int depthmapFBO, depthMapSize = 1024; }; } diff --git a/texture.frag b/texture.frag index 38a3aa4..b8023e1 100755 --- a/texture.frag +++ b/texture.frag @@ -15,10 +15,11 @@ layout (location = 1) out vec4 BrightColor; //0-POINT,1-DIRECTIONAL,2-SPOT struct Light{ - int type; + bool useAngle, additive; + int type, attenuation; vec3 pos, color, direction; float innerAngle, outerAngle; - float a, b, c, near, far; + float a, b, c, near, far, radius; sampler2D depthMap; samplerCube depthMapCube; mat4 lightMat; @@ -33,7 +34,7 @@ struct Texture{ uniform Texture textures[4]; uniform samplerCube environmentMap; uniform Light lights[numLights]; -uniform bool lightingEnabled, texturingEnabled, normalMapEnabled, specularMapEnabled, castShadow, environmentMapEnabled; +uniform bool lightingEnabled, constLightingEnabled, texturingEnabled, normalMapEnabled, specularMapEnabled, castShadow, environmentMapEnabled; uniform vec4 diffuseColor, specularColor; uniform float shinyness, specularStrength; uniform vec3 camPos; @@ -96,20 +97,36 @@ void main(){ vec3 diffuseCol = vec3(0), specularCol = vec3(0); if(checkLights){ + bool addedLighting = false; + for(int i = 0; i < numLights; i++){ vec3 lightDir = vec3(0), viewDir = normalize(camPos - fragPos); - float attenuation = 1.0, coef = 1; + float attenuation = 0, coef = 1; + float factor = 0; + bool canAdd = false; if(lights[i].type == 0){ lightDir = normalize(lights[i].pos - fragPos); + float dist = length(lights[i].pos - fragPos), radius = lights[i].radius; - float a = lights[i].a, b = lights[i].b, c = lights[i].c; - float dist = length(fragPos - lights[i].pos); - - attenuation = 1.0 / (a * dist * dist + b * dist + c); + if(lights[i].attenuation == 0){ + float a = lights[i].a, b = lights[i].b, c = lights[i].c; + attenuation = 1.0 / (a * dist * dist + b * dist + c); + factor = (lights[i].useAngle ? max(dot(lightDir, normalize(normal)), 0.) : 1) * attenuation; + } + else if(lights[i].attenuation == 1 && dist < radius && radius > 0){ + canAdd = !lights[i].additive; + factor = 1.0 - dist / lights[i].radius; + } + else if(lights[i].attenuation == 2 && dist < radius && radius > 0){ + canAdd = !lights[i].additive; + factor = 1; + } } - else if(lights[i].type == 1) + else if(lights[i].type == 1){ lightDir = -normalize(lights[i].direction); + factor = max(dot(lightDir, normalize(normal)), 0.) * attenuation * coef; + } else if(lights[i].type == 2){ lightDir = normalize(lights[i].pos - fragPos); float innerAngle = lights[i].innerAngle; @@ -121,10 +138,18 @@ void main(){ if(angle > lights[i].outerAngle) continue; - } - float factor = (lights[i].type < 3 ? (max(dot(lightDir, normalize(normal)), 0.) * attenuation * coef) : 1); - diffuseCol += lights[i].color * factor; + factor = max(dot(lightDir, normalize(normal)), 0.) * attenuation * coef; + } + else factor = 1; + + + if(lights[i].additive) + diffuseCol += lights[i].color * factor; + else if(!lights[i].additive && constLightingEnabled && !addedLighting && canAdd){ + addedLighting = true; + diffuseCol += lights[i].color * factor; + } if(specularMapEnabled){ float specularSample = texture(textures[2].pastTexture, texCoords).r;