#version 430 core uniform sampler2D colorTexture; uniform sampler2D normalSampler; uniform vec3 sunPos; uniform vec3 sunColor; uniform float sunLightExp; uniform vec3 cameraPos; uniform float time; uniform vec3 reflectorPos; uniform vec3 reflectorDir; uniform vec3 reflectorColor; uniform float reflectorAngle; uniform float reflectorLightExp; vec3 normalizedVertexNormal; mat3 TBN; in vec3 vertexNormalOut; in vec3 vertexTangentOut; in vec3 vertexBitangentOut; in vec3 vertexPosOut; in vec2 vertexTexCoordOut; out vec4 outColor; vec4 calcPointLight(vec3 fragColor, vec3 lightPos, vec3 lightColor, float lightExp) { vec3 lightDir = normalize(vertexPosOut - lightPos); vec3 viewDir = normalize(cameraPos - vertexPosOut); // tangent space vec3 viewDirTS = TBN * viewDir; vec3 lightDirTS = TBN * lightDir; //tmp solution viewDir = normalize(viewDirTS); lightDir = normalize(lightDirTS); float lightDistance = length(vertexPosOut - lightPos); vec3 newLightColor = lightColor / pow(lightDistance, 2); float intensity = dot(normalizedVertexNormal, -lightDir); intensity = max(intensity, 0.0); vec3 reflectDir = reflect(lightDir, normalizedVertexNormal); float glossPow = 8; float specular = pow(max(dot(viewDir, reflectDir), 0.0), glossPow); float diffuse = intensity; vec3 resultColor = newLightColor * (fragColor * diffuse + specular ); return vec4(1 - exp(-resultColor * lightExp), 1.0); } vec4 calcSpotLight(vec3 fragColor, vec3 lightPos, vec3 lightColor, float lightExp) { vec3 reflectorLightDir = normalize(vertexPosOut - lightPos); 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); res = calcPointLight(fragColor, lightPos, lightColor, lightExp * intensity); } return res; } void main() { TBN = transpose(mat3(vertexTangentOut, vertexBitangentOut, vertexNormalOut)); vec3 textureColor = texture2D(colorTexture, vertexTexCoordOut).rgb; normalizedVertexNormal = normalize(vertexNormalOut); //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] normalizedVertexNormal = normalize(samplerNormal);// to avoid potential precision problems in sampler texture //tmp solution //normalizedVertexNormal = vec3(0, 0, 1); outColor = calcPointLight(textureColor, sunPos, sunColor, sunLightExp); outColor += calcSpotLight(textureColor, reflectorPos, reflectorColor, reflectorLightExp); //Debug //outColor = vec4(textureColor, 1); }