From 6a1fd050744e7363f1de15bee6c4880cfa7a1c53 Mon Sep 17 00:00:00 2001 From: scrawl Date: Thu, 16 Jun 2016 17:32:27 +0200 Subject: [PATCH] Fix the coordinate frame for eyeDir, minor cleanup (Bug #3440) --- files/shaders/objects_fragment.glsl | 13 +++++++------ files/shaders/terrain_fragment.glsl | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/files/shaders/objects_fragment.glsl b/files/shaders/objects_fragment.glsl index 935479e44..b0d51afae 100644 --- a/files/shaders/objects_fragment.glsl +++ b/files/shaders/objects_fragment.glsl @@ -71,23 +71,24 @@ void main() vec3 normalizedNormal = normalize(passNormal); vec3 normalizedTangent = normalize(passTangent); vec3 binormal = cross(normalizedTangent, normalizedNormal); - mat3 tbn = mat3(normalizedTangent, binormal, normalizedNormal); + mat3 tbnTranspose = mat3(normalizedTangent, binormal, normalizedNormal); - vec3 viewNormal = gl_NormalMatrix * normalize(tbn * (normalTex.xyz * 2.0 - 1.0)); + vec3 viewNormal = gl_NormalMatrix * normalize(tbnTranspose * (normalTex.xyz * 2.0 - 1.0)); #else vec3 viewNormal = gl_NormalMatrix * normalize(passNormal); #endif #if @parallax - vec3 cameraPos = osg_ViewMatrixInverse[3].xyz; - vec3 eyeDir = normalize(cameraPos - (osg_ViewMatrixInverse * vec4(passViewPos, 1)).xyz); - vec2 offset = getParallaxOffset(eyeDir, tbn, normalTex.a); + vec3 cameraPos = (gl_ModelViewMatrixInverse * vec4(0,0,0,1)).xyz; + vec3 objectPos = (gl_ModelViewMatrixInverse * vec4(passViewPos, 1)).xyz; + vec3 eyeDir = normalize(cameraPos - objectPos); + vec2 offset = getParallaxOffset(eyeDir, tbnTranspose, normalTex.a); adjustedDiffuseUV += offset; // only offset diffuse for now, other textures are more likely to be using a completely different UV set #if @diffuseMapUV == @normalMapUV // fetch a new normal using updated coordinates normalTex = texture2D(normalMap, adjustedDiffuseUV); - viewNormal = gl_NormalMatrix * normalize(tbn * (normalTex.xyz * 2.0 - 1.0)); + viewNormal = gl_NormalMatrix * normalize(tbnTranspose * (normalTex.xyz * 2.0 - 1.0)); #endif #endif diff --git a/files/shaders/terrain_fragment.glsl b/files/shaders/terrain_fragment.glsl index 14f00a2e1..d05a94687 100644 --- a/files/shaders/terrain_fragment.glsl +++ b/files/shaders/terrain_fragment.glsl @@ -42,21 +42,22 @@ void main() vec3 tangent = vec3(1.0, 0.0, 0.0); vec3 binormal = normalize(cross(tangent, normalizedNormal)); tangent = normalize(cross(normalizedNormal, binormal)); // note, now we need to re-cross to derive tangent again because it wasn't orthonormal - mat3 tbn = mat3(tangent, binormal, normalizedNormal); + mat3 tbnTranspose = mat3(tangent, binormal, normalizedNormal); - vec3 viewNormal = normalize(gl_NormalMatrix * (tbn * (normalTex.xyz * 2.0 - 1.0))); + vec3 viewNormal = normalize(gl_NormalMatrix * (tbnTranspose * (normalTex.xyz * 2.0 - 1.0))); #else vec3 viewNormal = normalize(gl_NormalMatrix * passNormal); #endif #if @parallax - vec3 cameraPos = osg_ViewMatrixInverse[3].xyz; - vec3 eyeDir = normalize(cameraPos - (osg_ViewMatrixInverse * vec4(passViewPos, 1)).xyz); - adjustedUV += getParallaxOffset(eyeDir, tbn, normalTex.a); + vec3 cameraPos = (gl_ModelViewMatrixInverse * vec4(0,0,0,1)).xyz; + vec3 objectPos = (gl_ModelViewMatrixInverse * vec4(passViewPos, 1)).xyz; + vec3 eyeDir = normalize(cameraPos - objectPos); + adjustedUV += getParallaxOffset(eyeDir, tbnTranspose, normalTex.a); // update normal using new coordinates normalTex = texture2D(normalMap, adjustedUV); - viewNormal = normalize(gl_NormalMatrix * (tbn * (normalTex.xyz * 2.0 - 1.0))); + viewNormal = normalize(gl_NormalMatrix * (tbnTranspose * (normalTex.xyz * 2.0 - 1.0))); #endif vec4 diffuseTex = texture2D(diffuseMap, adjustedUV);