From 835b5897cfaef36b132ff6875cf78ae153212c79 Mon Sep 17 00:00:00 2001 From: Tobias Frisch <tfrisch@uni-koblenz.de> Date: Mon, 10 Oct 2022 18:26:09 +0200 Subject: [PATCH] Adjust shaders for normal mapping Signed-off-by: Tobias Frisch <tfrisch@uni-koblenz.de> --- .../NormalMapping/shaders/NormalMapping.frag | 33 +++++++++---- .../NormalMapping/shaders/NormalMapping.vert | 46 ++++++------------- 2 files changed, 38 insertions(+), 41 deletions(-) diff --git a/demos/NormalMapping/shaders/NormalMapping.frag b/demos/NormalMapping/shaders/NormalMapping.frag index 87fef02..2f36489 100644 --- a/demos/NormalMapping/shaders/NormalMapping.frag +++ b/demos/NormalMapping/shaders/NormalMapping.frag @@ -33,20 +33,37 @@ layout(set=0, binding=4) uniform texturePropsBuffer { int useColorTexture; }; -layout(location = 0) in vec3 passLightVector; -layout(location = 1) in vec3 passEyeVector; -layout(location = 2) in vec2 passUV; +layout(location = 0) in vec3 passPosition; +layout(location = 1) in vec3 passNormal; +layout(location = 2) in vec3 passTangent; +layout(location = 3) in vec2 passUV; layout(location = 0) out vec4 fragmentColor; void main(){ - + vec3 normal = normalize(passNormal); + vec3 tangent = normalize(passTangent); + vec3 bitangent = cross(tangent, normal); + /***************** Diffuse ******************************************************/ // local normal, in tangent space - vec3 normal = texture(sampler2D(normalTexture, colorSampler), passUV).rgb * 2.0 - 1.0; + vec3 normalWeights = texture(sampler2D(normalTexture, colorSampler), passUV).rgb * 2.0 - 1.0; + + mat3 tbn = transpose(mat3( + tangent, + bitangent, + normal + )); + + normal = tbn * normalWeights; - // direction of the light (from the fragment to the light) in tangent space - vec3 lightVector = normalize(passLightVector); + vec3 lightVector; + vec3 light_camcoord = (light.pos).xyz; + if (light.pos.w > 0.001f) { + lightVector = normalize(light_camcoord - passPosition); + } else { + lightVector = normalize(light_camcoord); + } //compute the diffuse lighting factor float cos_phi = max(dot(normal, lightVector), 0); @@ -56,7 +73,7 @@ void main(){ vec3 reflection = normalize(reflect(-lightVector, normal)); // eye vector in tangent space - vec3 eyeVector = normalize(passEyeVector); + vec3 eyeVector = normalize(-passPosition); // compute the specular lighting factor float cos_psi_n = pow(max(dot(reflection, eyeVector), 0.0f), mat.shininess); diff --git a/demos/NormalMapping/shaders/NormalMapping.vert b/demos/NormalMapping/shaders/NormalMapping.vert index d4feaf4..f83070f 100644 --- a/demos/NormalMapping/shaders/NormalMapping.vert +++ b/demos/NormalMapping/shaders/NormalMapping.vert @@ -5,8 +5,6 @@ layout(location = 1) in vec3 normal; layout(location = 2) in vec2 uv; layout(location = 3) in vec3 tangent; -//vec3 tangent = vec3(0); - layout(set=0, binding=0) uniform matrixBuffer { mat4 modelMatrix; mat4 viewMatrix; @@ -26,42 +24,24 @@ layout(set=0, binding=2) uniform lightBuffer { vec3 lightAmbient; }; -layout(location = 0) out vec3 passLightVector; -layout(location = 1) out vec3 passEyeVector; -layout(location = 2) out vec2 passUV; +layout(location = 0) out vec3 passPosition; +layout(location = 1) out vec3 passNormal; +layout(location = 2) out vec3 passTangent; +layout(location = 3) out vec2 passUV; -void main(){ +void main() { gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1); - /*********** Calculate matrix that goes from camera space to tangent space **********/ - mat4 normalMatrix = transpose(inverse(viewMatrix * modelMatrix)); - vec3 normal_cs = normalize((normalMatrix * vec4(normal,0)).xyz); - vec3 tangent_cs = normalize((normalMatrix * vec4(tangent,0)).xyz); - vec3 bitangent_cs = cross(tangent_cs, normal_cs); - - mat3 tbn = transpose(mat3( - tangent_cs, - bitangent_cs, - normal_cs - )); - - /*********** Calculate position in camera space *************************************/ - vec3 position_cs = (viewMatrix * modelMatrix * vec4(position, 1)).xyz; - - /*********** Calculate light vector (tangent space) *********************************/ - // light position in world coordinates - vec3 lightPosition_cs = (light.pos).xyz; - - // calcluate vector that goes from the vertex to the light, in tangent space - passLightVector = tbn * normalize(lightPosition_cs - position_cs); - - /*********** Calculate eye vector (tangent space) **********************************/ - // calculate eye vector in camera space - vec3 eye_cs = normalize(-position_cs); + //transform the position correctly into view space + //and pass it to the fragment shader + passPosition = (viewMatrix * modelMatrix * vec4(position, 1)).xyz; - // calculate eye vector in tangent space - passEyeVector = tbn * eye_cs; + //transform the normal and tangent correctly into view space + //and pass it to the fragment shader + mat3 normalMatrix = mat3(transpose(inverse(viewMatrix * modelMatrix))); + passNormal = normalize(normalMatrix * normal); + passTangent = normalize(normalMatrix * tangent); /*********** Pass uv of the vertex *************************************************/ // no special space for this one -- GitLab