tangent space light for texture rendering
This commit is contained in:
parent
0a702e155d
commit
72fbf03ed2
@ -3,11 +3,12 @@
|
|||||||
uniform sampler2D colorTexture;
|
uniform sampler2D colorTexture;
|
||||||
uniform sampler2D normalSampler;
|
uniform sampler2D normalSampler;
|
||||||
|
|
||||||
|
uniform vec3 cameraPos;
|
||||||
|
|
||||||
uniform vec3 sunPos;
|
uniform vec3 sunPos;
|
||||||
uniform vec3 sunColor;
|
uniform vec3 sunColor;
|
||||||
uniform float sunLightExp;
|
uniform float sunLightExp;
|
||||||
|
|
||||||
uniform vec3 cameraPos;
|
|
||||||
uniform float time;
|
uniform float time;
|
||||||
|
|
||||||
uniform vec3 reflectorPos;
|
uniform vec3 reflectorPos;
|
||||||
@ -19,52 +20,40 @@ uniform float reflectorLightExp;
|
|||||||
vec3 normalizedVertexNormal;
|
vec3 normalizedVertexNormal;
|
||||||
mat3 TBN;
|
mat3 TBN;
|
||||||
|
|
||||||
in vec3 vertexNormalOut;
|
in vec3 vertexPosWld;
|
||||||
in vec3 vertexTangentOut;
|
|
||||||
in vec3 vertexBitangentOut;
|
|
||||||
in vec3 vertexPosOut;
|
|
||||||
in vec2 vertexTexCoordOut;
|
in vec2 vertexTexCoordOut;
|
||||||
|
in vec3 viewDirTS;
|
||||||
|
in vec3 sunLightDirTS;
|
||||||
|
in vec3 reflectorLightDirTS;
|
||||||
|
|
||||||
out vec4 outColor;
|
out vec4 outColor;
|
||||||
|
|
||||||
vec4 calcPointLight(vec3 fragColor, vec3 lightPos, vec3 lightColor, float lightExp) {
|
vec4 calcPointLight(vec3 fragColor, vec3 lightPos, vec3 lightDirTS, vec3 lightColor, float lightExp) {
|
||||||
vec3 lightDir = normalize(vertexPosOut - lightPos);
|
float lightDistance = length(vertexPosWld - 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);
|
vec3 newLightColor = lightColor / pow(lightDistance, 2);
|
||||||
|
|
||||||
float intensity = dot(normalizedVertexNormal, -lightDir);
|
float intensity = dot(normalizedVertexNormal, -lightDirTS);
|
||||||
intensity = max(intensity, 0.0);
|
intensity = max(intensity, 0.0);
|
||||||
|
|
||||||
vec3 reflectDir = reflect(lightDir, normalizedVertexNormal);
|
vec3 reflectDir = reflect(lightDirTS, normalizedVertexNormal);
|
||||||
|
|
||||||
|
|
||||||
float glossPow = 8;
|
float glossPow = 8;
|
||||||
float specular = pow(max(dot(viewDir, reflectDir), 0.0), glossPow);
|
float specular = pow(max(dot(viewDirTS, reflectDir), 0.0), glossPow);
|
||||||
|
|
||||||
float diffuse = intensity;
|
float diffuse = intensity;
|
||||||
vec3 resultColor = newLightColor * (fragColor * diffuse + specular );
|
vec3 resultColor = newLightColor * (fragColor * diffuse + specular );
|
||||||
return vec4(1 - exp(-resultColor * lightExp), 1.0);
|
return vec4(1 - exp(-resultColor * lightExp), 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
vec4 calcSpotLight(vec3 fragColor, vec3 lightPos, vec3 lightColor, float lightExp) {
|
vec4 calcSpotLight(vec3 fragColor, vec3 lightPos, vec3 lightDirTS, vec3 lightColor, float lightExp) {
|
||||||
vec3 reflectorLightDir = normalize(vertexPosOut - lightPos);
|
vec3 reflectorLightDir = normalize(vertexPosWld - lightPos);
|
||||||
float angleCos = dot(reflectorLightDir, reflectorDir);
|
float angleCos = dot(reflectorLightDir, reflectorDir);
|
||||||
float reflectorOutAngle = reflectorAngle + radians(10);
|
float reflectorOutAngle = reflectorAngle + radians(10);
|
||||||
float epsilon = cos(reflectorAngle) - cos(reflectorOutAngle);
|
float epsilon = cos(reflectorAngle) - cos(reflectorOutAngle);
|
||||||
vec4 res = vec4(0, 0, 0, 1);
|
vec4 res = vec4(0, 0, 0, 1);
|
||||||
if (angleCos > cos(reflectorOutAngle)) {
|
if (angleCos > cos(reflectorOutAngle)) {
|
||||||
float intensity = clamp((angleCos - cos(reflectorOutAngle)) / epsilon, 0.0, 1.0);
|
float intensity = clamp((angleCos - cos(reflectorOutAngle)) / epsilon, 0.0, 1.0);
|
||||||
res = calcPointLight(fragColor, lightPos, lightColor, lightExp * intensity);
|
res = calcPointLight(fragColor, lightPos, lightDirTS, lightColor, lightExp * intensity);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -72,22 +61,19 @@ vec4 calcSpotLight(vec3 fragColor, vec3 lightPos, vec3 lightColor, float lightEx
|
|||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
TBN = transpose(mat3(vertexTangentOut, vertexBitangentOut, vertexNormalOut));
|
|
||||||
|
|
||||||
vec3 textureColor = texture2D(colorTexture, vertexTexCoordOut).rgb;
|
vec3 textureColor = texture2D(colorTexture, vertexTexCoordOut).rgb;
|
||||||
|
|
||||||
normalizedVertexNormal = normalize(vertexNormalOut);
|
|
||||||
//get normal from normal sampler
|
//get normal from normal sampler
|
||||||
vec3 samplerNormal = texture2D(normalSampler, vertexTexCoordOut).xyz;
|
vec3 samplerNormal = texture2D(normalSampler, vertexTexCoordOut).xyz;
|
||||||
samplerNormal = 2 * samplerNormal - 1;//since sampler has values from [0, 1], but we want [-1, 1]
|
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
|
normalizedVertexNormal = normalize(samplerNormal);// to avoid potential precision problems in sampler texture
|
||||||
|
|
||||||
//tmp solution
|
//Debug
|
||||||
//normalizedVertexNormal = vec3(0, 0, 1);
|
//normalizedVertexNormal = vec3(0, 0, 1);
|
||||||
|
|
||||||
outColor = calcPointLight(textureColor, sunPos, sunColor, sunLightExp);
|
outColor = calcPointLight(textureColor, sunPos, sunLightDirTS, sunColor, sunLightExp);
|
||||||
|
|
||||||
outColor += calcSpotLight(textureColor, reflectorPos, reflectorColor, reflectorLightExp);
|
outColor += calcSpotLight(textureColor, reflectorPos, reflectorLightDirTS, reflectorColor, reflectorLightExp);
|
||||||
|
|
||||||
//Debug
|
//Debug
|
||||||
//outColor = vec4(textureColor, 1);
|
//outColor = vec4(textureColor, 1);
|
||||||
|
@ -9,19 +9,41 @@ layout(location = 4) in vec3 vertexBitangent;
|
|||||||
uniform mat4 transformation;
|
uniform mat4 transformation;
|
||||||
uniform mat4 modelMat;
|
uniform mat4 modelMat;
|
||||||
|
|
||||||
out vec3 vertexNormalOut;
|
uniform vec3 cameraPos;
|
||||||
out vec3 vertexTangentOut;
|
uniform vec3 sunPos;
|
||||||
out vec3 vertexBitangentOut;
|
uniform vec3 reflectorPos;
|
||||||
out vec3 vertexPosOut;
|
|
||||||
|
out vec3 vertexPosWld;
|
||||||
out vec2 vertexTexCoordOut;
|
out vec2 vertexTexCoordOut;
|
||||||
|
out vec3 viewDirTS;
|
||||||
|
out vec3 sunLightDirTS;
|
||||||
|
out vec3 reflectorLightDirTS;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = transformation * vec4(vertexPosition, 1.0);
|
gl_Position = transformation * vec4(vertexPosition, 1.0);
|
||||||
vertexNormalOut = (modelMat * vec4(vertexNormal, 0.0)).xyz;
|
vec3 normal = (modelMat * vec4(vertexNormal, 0.0)).xyz;
|
||||||
vertexTangentOut = (modelMat * vec4(vertexTangent, 0.0)).xyz;
|
vec3 tangent = (modelMat * vec4(vertexTangent, 0.0)).xyz;
|
||||||
vertexBitangentOut = (modelMat * vec4(vertexBitangent, 0.0)).xyz;
|
vec3 bitangent = (modelMat * vec4(vertexBitangent, 0.0)).xyz;
|
||||||
vertexPosOut = (modelMat * vec4(vertexPosition, 1.0)).xyz;
|
vertexPosWld = (modelMat * vec4(vertexPosition, 1.0)).xyz;
|
||||||
vertexTexCoordOut = vertexTexCoord;
|
vertexTexCoordOut = vertexTexCoord;
|
||||||
|
|
||||||
|
mat3 TBN = transpose(mat3(tangent, bitangent, normal));
|
||||||
|
|
||||||
|
//`lightPos - vertexPos` from tutorial is wrong?
|
||||||
|
//TODO why should we normalize here if we would normalize Tangent Space vectors later?
|
||||||
|
vec3 sunLightDir = normalize(vertexPosWld - sunPos);
|
||||||
|
vec3 reflectorLightDir = normalize(vertexPosWld - reflectorPos);
|
||||||
|
vec3 viewDir = normalize(cameraPos - vertexPosWld);
|
||||||
|
|
||||||
|
// tangent space
|
||||||
|
viewDirTS = TBN * viewDir;
|
||||||
|
sunLightDirTS = TBN * sunLightDir;
|
||||||
|
reflectorLightDirTS = TBN * reflectorLightDir;
|
||||||
|
|
||||||
|
//TODO should normilize here or in the fragment shader?
|
||||||
|
viewDirTS = normalize(viewDirTS);
|
||||||
|
sunLightDirTS = normalize(sunLightDirTS);
|
||||||
|
reflectorLightDirTS = normalize(reflectorLightDirTS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user