diff options
| author | Thomas Guillermo Albers Raviola <thomas@thomaslabs.org> | 2026-01-16 23:02:32 +0100 |
|---|---|---|
| committer | Thomas Guillermo Albers Raviola <thomas@thomaslabs.org> | 2026-01-16 23:02:32 +0100 |
| commit | 6b8af9cf83851c075c6c9514b1deaa931c2b19a4 (patch) | |
| tree | 428986b49c32e21d3f7a3c2dfa41858ae0153209 /09-september/resources/shaders/shader.frag | |
Initial commit
Diffstat (limited to '09-september/resources/shaders/shader.frag')
| -rw-r--r-- | 09-september/resources/shaders/shader.frag | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/09-september/resources/shaders/shader.frag b/09-september/resources/shaders/shader.frag new file mode 100644 index 0000000..44e579c --- /dev/null +++ b/09-september/resources/shaders/shader.frag @@ -0,0 +1,56 @@ +#version 420 + +in vec2 Fragment_UV; +in vec4 Fragment_Color; +in vec3 toLightVector[4]; +in vec3 toEyeVector; + +layout(location = 0) out vec4 out_color; + +uniform vec4 lightColor[4]; +uniform vec3 attenuation[4]; +uniform sampler2D Texture; +uniform sampler2D normalMap; + +void main() +{ + vec4 totalDiffuse = vec4(0.0, 0.0, 0.0, 1.0); + vec4 totalSpecular = vec4(0.0, 0.0, 0.0, 1.0); + + /* We get the normal from the normal map and we transform it into world space */ + vec3 normal = normalize( (255.0 / 128.0 * texture(normalMap, Fragment_UV) - 1.0).xyz ); + + for(int i = 0; i < 4; i++) + { + /*Light Attenuation*/ + float dist = length(toLightVector[i]); + float attFactor = attenuation[i].x + (attenuation[i].y * dist) + (attenuation[i].z * dist * dist); + vec3 unitToLightVector = normalize(toLightVector[i]); + + /*Diffuse lighting*/ + + /*La intensidad es el cos entre la normal y el vector hacia la luz*/ + float brightness = dot(unitToLightVector, normalize(normal)); + /*No queremos luz negativa ni mayor a 1*/ + brightness = max(brightness, 0.0); + totalDiffuse = totalDiffuse + (brightness * lightColor[i]) / attFactor; + + /* Specular lighting */ + + /* Reflejamos el vector hacia la luz (su inverso) en la normal */ + vec3 Vector_ReflectedLight = reflect(-unitToLightVector, normal); + + /* La luz especular es el cos del angulo entre el vector hacia el ojo y la luz reflectada en la normal */ + float specularity = clamp( dot(Vector_ReflectedLight, toEyeVector), 0, 1 ); + float reflectivity = 0.5; + /* Que tan grande es el specular highlight */ + specularity = pow(specularity, 10); + totalSpecular = totalSpecular + clamp( reflectivity * specularity * lightColor[i], 0, 1) / attFactor; + } + + /* Ambient Light */ + totalDiffuse = max(totalDiffuse, 0.2); + + /* Juntamos todo para el color final*/ + out_color = totalDiffuse * texture(Texture, Fragment_UV) + totalSpecular; +} |
