Files
TDT4230/res/shaders/simple.frag
Fredrik Robertsen c635a0f0c2 working lights!
- fix positions of lights
- fix normal vector bug (mat4->mat3)
- change light color
2026-01-29 19:06:41 +01:00

47 lines
1.3 KiB
GLSL

#version 430 core
in layout(location = 0) vec3 normal;
in layout(location = 1) vec2 textureCoordinates;
in layout(location = 2) vec3 worldPositions;
uniform vec3 lightPositions[3];
uniform vec3 cameraPosition;
out vec4 color;
float rand(vec2 co) {
return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);
}
float dither(vec2 uv) {
return (rand(uv) * 2.0 - 1.0) / 256.0;
}
const vec3 objectColor = vec3(1.0, 1.0, 1.0);
const vec3 lightColor = vec3(0.5, 0.5, 0.5);
const float ambientStrength = 0.1;
const float specularStrength = 0.5;
const float shininess = 32.0;
void main()
{
vec3 norm = normalize(normal);
vec3 viewDir = normalize(cameraPosition - worldPositions);
vec3 ambient = ambientStrength * lightColor * objectColor;
vec3 result = ambient;
for (int i = 0; i < 3; i++) {
vec3 lightDir = normalize(lightPositions[i] - worldPositions);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;
vec3 halfwayDir = normalize(lightDir + viewDir); // little optimization (blinn-phong)
float spec = pow(max(dot(norm, halfwayDir), 0.0), shininess);
vec3 specular = specularStrength * spec * lightColor;
result += (diffuse + specular) * objectColor;
}
color = vec4(result, 1.0);
}