2024-01-11 00:53:24 +01:00
|
|
|
#version 430 core
|
|
|
|
|
|
|
|
uniform sampler2D colorTexture;
|
|
|
|
uniform sampler2D normalSampler;
|
|
|
|
|
2024-01-12 01:34:37 +01:00
|
|
|
uniform vec3 cameraPos;
|
|
|
|
|
2024-01-11 00:53:24 +01:00
|
|
|
uniform vec3 sunPos;
|
|
|
|
uniform vec3 sunColor;
|
|
|
|
uniform float sunLightExp;
|
|
|
|
|
|
|
|
uniform float time;
|
|
|
|
|
|
|
|
uniform vec3 reflectorPos;
|
|
|
|
uniform vec3 reflectorDir;
|
|
|
|
uniform vec3 reflectorColor;
|
|
|
|
uniform float reflectorAngle;
|
|
|
|
uniform float reflectorLightExp;
|
|
|
|
|
|
|
|
vec3 normalizedVertexNormal;
|
|
|
|
|
2024-01-12 01:34:37 +01:00
|
|
|
in vec3 vertexPosWld;
|
2024-01-11 00:53:24 +01:00
|
|
|
in vec2 vertexTexCoordOut;
|
2024-01-12 01:34:37 +01:00
|
|
|
in vec3 viewDirTS;
|
|
|
|
in vec3 sunLightDirTS;
|
|
|
|
in vec3 reflectorLightDirTS;
|
2024-01-11 00:53:24 +01:00
|
|
|
|
|
|
|
out vec4 outColor;
|
|
|
|
|
2024-01-12 01:34:37 +01:00
|
|
|
vec4 calcPointLight(vec3 fragColor, vec3 lightPos, vec3 lightDirTS, vec3 lightColor, float lightExp) {
|
|
|
|
float lightDistance = length(vertexPosWld - lightPos);
|
2024-01-11 00:53:24 +01:00
|
|
|
vec3 newLightColor = lightColor / pow(lightDistance, 2);
|
|
|
|
|
2024-01-12 01:34:37 +01:00
|
|
|
float intensity = dot(normalizedVertexNormal, -lightDirTS);
|
2024-01-11 00:53:24 +01:00
|
|
|
intensity = max(intensity, 0.0);
|
|
|
|
|
2024-01-12 01:34:37 +01:00
|
|
|
vec3 reflectDir = reflect(lightDirTS, normalizedVertexNormal);
|
2024-01-11 00:53:24 +01:00
|
|
|
|
|
|
|
float glossPow = 8;
|
2024-01-12 01:34:37 +01:00
|
|
|
float specular = pow(max(dot(viewDirTS, reflectDir), 0.0), glossPow);
|
2024-01-11 00:53:24 +01:00
|
|
|
|
|
|
|
float diffuse = intensity;
|
|
|
|
vec3 resultColor = newLightColor * (fragColor * diffuse + specular );
|
|
|
|
return vec4(1 - exp(-resultColor * lightExp), 1.0);
|
|
|
|
}
|
|
|
|
|
2024-01-12 01:34:37 +01:00
|
|
|
vec4 calcSpotLight(vec3 fragColor, vec3 lightPos, vec3 lightDirTS, vec3 lightColor, float lightExp) {
|
|
|
|
vec3 reflectorLightDir = normalize(vertexPosWld - lightPos);
|
2024-01-11 00:53:24 +01:00
|
|
|
float angleCos = dot(reflectorLightDir, reflectorDir);
|
|
|
|
float reflectorOutAngle = reflectorAngle + radians(10);
|
|
|
|
float epsilon = cos(reflectorAngle) - cos(reflectorOutAngle);
|
|
|
|
vec4 res = vec4(0, 0, 0, 1);
|
|
|
|
if (angleCos > cos(reflectorOutAngle)) {
|
|
|
|
float intensity = clamp((angleCos - cos(reflectorOutAngle)) / epsilon, 0.0, 1.0);
|
2024-01-12 01:34:37 +01:00
|
|
|
res = calcPointLight(fragColor, lightPos, lightDirTS, lightColor, lightExp * intensity);
|
2024-01-11 00:53:24 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
vec3 textureColor = texture2D(colorTexture, vertexTexCoordOut).rgb;
|
|
|
|
|
|
|
|
//get normal from normal sampler
|
|
|
|
vec3 samplerNormal = texture2D(normalSampler, vertexTexCoordOut).xyz;
|
|
|
|
samplerNormal = 2 * samplerNormal - 1;//since sampler has values from [0, 1], but we want [-1, 1]
|
2024-01-11 04:07:32 +01:00
|
|
|
normalizedVertexNormal = normalize(samplerNormal);// to avoid potential precision problems in sampler texture
|
2024-01-11 00:53:24 +01:00
|
|
|
|
2024-01-12 01:34:37 +01:00
|
|
|
//Debug
|
2024-01-11 00:53:24 +01:00
|
|
|
//normalizedVertexNormal = vec3(0, 0, 1);
|
|
|
|
|
2024-01-12 01:34:37 +01:00
|
|
|
outColor = calcPointLight(textureColor, sunPos, sunLightDirTS, sunColor, sunLightExp);
|
2024-01-11 00:53:24 +01:00
|
|
|
|
2024-01-12 01:34:37 +01:00
|
|
|
outColor += calcSpotLight(textureColor, reflectorPos, reflectorLightDirTS, reflectorColor, reflectorLightExp);
|
2024-01-11 00:53:24 +01:00
|
|
|
|
|
|
|
//Debug
|
|
|
|
//outColor = vec4(textureColor, 1);
|
|
|
|
}
|
|
|
|
|