diff options
author | talha <talha@talhaamir.xyz> | 2024-03-27 04:19:47 +0500 |
---|---|---|
committer | talha <talha@talhaamir.xyz> | 2024-03-27 04:19:47 +0500 |
commit | 6d21cde397ff246c053274c47fd33f3b84f68c6e (patch) | |
tree | 53de7afec887986140b0e5fdd7a004c134bd1ae8 /source/shaders | |
parent | 2a1557aa2b4d406c92703f60f2d34283fed5fcfb (diff) |
Added directional lighting, point lights
Diffstat (limited to 'source/shaders')
-rw-r--r-- | source/shaders/light_subject.fs.glsl | 58 | ||||
-rw-r--r-- | source/shaders/light_subject.vs.glsl | 3 |
2 files changed, 53 insertions, 8 deletions
diff --git a/source/shaders/light_subject.fs.glsl b/source/shaders/light_subject.fs.glsl index 54bf700..aa3cf36 100644 --- a/source/shaders/light_subject.fs.glsl +++ b/source/shaders/light_subject.fs.glsl @@ -1,45 +1,87 @@ #version 330 core + struct Material { + sampler2D diffuse; + sampler2D specular; + float shininess; +}; + +struct Light { vec3 ambient; vec3 diffuse; vec3 specular; - float shininess; + + vec3 position; }; -struct Light { +struct DirectionalLight { + vec3 direction; + vec3 ambient; vec3 diffuse; vec3 specular; +}; +struct PointLight { vec3 position; + + vec3 ambient; + vec3 diffuse; + vec3 specular; + + // attentuation factors + float kC; + float kL; + float kQ; }; +in vec2 texCoords; in vec3 fragNormal; in vec3 worldPosition; uniform Material material; uniform Light light; +uniform DirectionalLight dirLight; +uniform PointLight pointLight; uniform vec3 cameraPosition; uniform vec3 lightColor; -uniform sampler2D smilingTexture; -uniform sampler2D containerTexture; out vec4 FragColor; void main() { - vec3 ambientLight = light.ambient * material.ambient; + float lightDistance = length(pointLight.position - worldPosition); + float attenuationFactor = 1.0 / + (pointLight.kC + (pointLight.kL * lightDistance) + (pointLight.kQ * lightDistance * lightDistance)); + + vec3 ambientLight = attenuationFactor * pointLight.ambient * vec3(texture(material.diffuse, texCoords)); // @note: Diffuse calculations - vec3 lightDir = normalize(light.position - worldPosition); + //vec3 lightDir = normalize(light.position - worldPosition); + /* + @note: an explanation of why the light direction vector is taken from fragment to the + light source. + Basic LA really, we need to calculate the angle between the direction of the 2 vectors: + a. The direction at which light incidents with the fragment + b. The normal vector + The reason the light direction is taken from the fragment to the light source, is precisely so we can calculate + the angle between the normal and the direction at which light would hit. This if taken as starting from the light + source would actually be incorrect, since we would be calculating the angle between the light source in the direction + of the fragment and the normal. Consider what happens when it is directly above. The angle becomes 180, not 0. This + is because the normal moves in the direction opposite to the lights direction if taken this way, which is not what + we expect or want. + Reversing this, allows us to consider the angle at the point in which light hits the fragment, and the normal vector + of the fragment. + */ + vec3 lightDir = normalize(pointLight.position - worldPosition); float diffuseStrength = max(dot(lightDir, fragNormal), 0.0); - vec3 diffuseLight = light.diffuse * (diffuseStrength * material.diffuse); + vec3 diffuseLight = attenuationFactor * pointLight.diffuse * diffuseStrength * vec3(texture(material.diffuse, texCoords)); // @note: Specular calculations vec3 viewDir = normalize(cameraPosition - worldPosition); vec3 reflectDir = reflect(-lightDir, fragNormal); float specularity = max(dot(viewDir, reflectDir), 0.0); float shinePower = pow(specularity, material.shininess); - vec3 specularLight = light.specular * (shinePower * material.specular); + vec3 specularLight = attenuationFactor * pointLight.specular * shinePower * vec3(texture(material.specular, texCoords)); vec3 color = ambientLight + diffuseLight + specularLight; FragColor = vec4(color, 1.0); diff --git a/source/shaders/light_subject.vs.glsl b/source/shaders/light_subject.vs.glsl index 327e896..49e58d8 100644 --- a/source/shaders/light_subject.vs.glsl +++ b/source/shaders/light_subject.vs.glsl @@ -2,6 +2,7 @@ layout(location = 0) in vec3 position; layout(location = 1) in vec3 normal; +layout(location = 2) in vec2 inTexCoords; uniform mat4 Model; uniform mat4 View; @@ -9,10 +10,12 @@ uniform mat4 Projection; out vec3 fragNormal; out vec3 worldPosition; +out vec2 texCoords; void main() { gl_Position = Projection * View * Model * vec4(position, 1.0); worldPosition = vec3(Model * vec4(position, 1.0)); fragNormal = mat3(transpose(inverse(Model))) * normal; fragNormal = normalize(normal); + texCoords = inTexCoords; } |