Hi,
I’m having a problem with implementing a water shader in my own engine. The shader is based on the free water tutorial on gametutorials.com and their demo works fine. If I implement this in my engine I get not the correct reflections/refractions back.
Here are 2 screens showing the problem:
this is how it shoull look at least nearly:
and this is how it looks like if I’m rotating my cam :
Here is my shader code (maybe it helps)
the vertex shader
varying vec4 refrCoords;
varying vec4 normCoords;
varying vec4 viewCoords;
varying vec4 viewTangetSpace;
varying vec4 lightTangetSpace;
uniform vec4 lightPos, cameraPos;
void main()
{
// Because we have a flat plane for water we already know the vectors for tangent space
vec4 tangent = vec4(1.0, 0.0, 0.0, 0.0);
vec4 normal = vec4(0.0, 1.0, 0.0, 0.0);
vec4 biTangent = vec4(0.0, 0.0, 1.0, 0.0);
// Calculate the vector coming from the vertex to the camera
vec4 viewDir = cameraPos - gl_Vertex;
// Compute tangent space for the view direction
viewTangetSpace.x = dot(viewDir, tangent);
viewTangetSpace.y = dot(viewDir, biTangent);
viewTangetSpace.z = dot(viewDir, normal);
viewTangetSpace.w = 1.0;
// Calculate the vector that the light hits the vertex
vec4 lightDir = lightPos - gl_Vertex;
// Compute tangent space for the light direction
lightTangetSpace.x = dot(lightDir, tangent);
lightTangetSpace.y = dot(lightDir, biTangent);
lightTangetSpace.z = dot(lightDir, normal);
lightTangetSpace.w = 1.0;
refrCoords = gl_MultiTexCoord1*1.50;
normCoords = gl_MultiTexCoord2*1.50;
// This calculates our current projection coordinates
viewCoords = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_Position = viewCoords;
}
the fragment shader
varying vec4 refrCoords;
varying vec4 normCoords;
varying vec4 viewCoords;
varying vec4 viewTangetSpace;
varying vec4 lightTangetSpace;
uniform sampler2D reflection;
uniform sampler2D refraction;
uniform sampler2D normalMap;
uniform sampler2D dudvMap;
uniform sampler2D depthMap;
uniform vec4 waterColor;
void main()
{
const float kShine = 64.0;
const float kDistortion = 0.015;
const float kRefraction = 0.09;
vec4 distOffset = texture2D(dudvMap, normCoords.xy) * kDistortion;
vec4 dudvColor = texture2D(dudvMap, vec2(refrCoords + distOffset));
dudvColor = normalize(dudvColor * 2.0 - 1.0) * kRefraction;
vec4 normalVector = texture2D(normalMap, vec2(refrCoords + distOffset));
normalVector = normalVector * 2.0 - 1.0;
normalVector.a = 0.0;
vec4 lightReflection = normalize( reflect(-lightTangetSpace, normalVector) );
vec4 invertedFresnel = vec4( dot(normalVector, lightReflection ) );
vec4 fresnelTerm = 1.0 - invertedFresnel;
vec4 projCoord = viewCoords / viewCoords.q;
projCoord = (projCoord + 1.0) * 0.5;
projCoord += dudvColor;
projCoord = clamp(projCoord, 0.001, 0.999);
vec4 reflectionColor = texture2D(reflection, projCoord.xy);
vec4 refractionColor = texture2D(refraction, projCoord.xy);
vec4 depthValue = texture2D(depthMap, projCoord.xy);
vec4 invDepth = 1.0 - depthValue;
refractionColor *= invertedFresnel * invDepth;
refractionColor += waterColor * depthValue * invertedFresnel;
reflectionColor *= fresnelTerm;
vec4 localView = normalize(viewTangetSpace);
float intensity = max(0.0, dot(lightReflection, localView) );
vec4 specular = vec4(pow(intensity, kShine));
gl_FragColor = refractionColor + reflectionColor + specular;
}
I should also say that I’m using a left handed coordinate system instead of the right handed one. Could this be the problem? If yes any idea how to solve this?
thx in advance.