Depth Corrected Parallax Mapping shader for TDG3D:
Code:
[VP test]
varying vec3 lightVec;
varying vec3 eyeVec;
varying vec3 et;
varying vec3 V_M;
varying vec4 glVertex;
varying vec2 glTexCoord0;
//*> TBNtangent Tangent
attribute vec3 Tangent;
//*> TBNnormal Normal
attribute vec3 Normal;
//*> TBNbinormal BiNormal
attribute vec3 BiNormal;
mat3 glNormalMatrixInverse(void)
{
return mat3( gl_ModelViewMatrix[0][0], gl_ModelViewMatrix[1][0], gl_ModelViewMatrix[2][0],
gl_ModelViewMatrix[0][1], gl_ModelViewMatrix[1][1], gl_ModelViewMatrix[2][1],
gl_ModelViewMatrix[0][2], gl_ModelViewMatrix[1][2], gl_ModelViewMatrix[2][2] );
}
void main(void)
{
glTexCoord0 = gl_MultiTexCoord0.st;
glVertex = gl_Vertex;
V_M = normalize(-glNormalMatrixInverse() * gl_ModelViewMatrix[3].xyz - glVertex.xyz);
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
vec3 t = normalize(gl_NormalMatrix * Tangent);
vec3 b = normalize(gl_NormalMatrix * BiNormal);
vec3 n = normalize(gl_NormalMatrix * Normal);
vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
vec3 tmpVec = gl_LightSource[0].position.xyz - vVertex;
lightVec.x = dot(tmpVec, t);
lightVec.y = dot(tmpVec, b);
lightVec.z = dot(tmpVec, n);
tmpVec = -vVertex;
eyeVec.x = dot(tmpVec, t);
eyeVec.y = dot(tmpVec, b);
eyeVec.z = dot(tmpVec, n);
}
[FP test]
//*> sampler2D 0 colorMap
uniform sampler2D colorMap;
//*> sampler2D 1 heightMap
uniform sampler2D heightMap;
//*> sampler2D 2 normalMap
uniform sampler2D normalMap;
varying vec3 lightVec;
varying vec3 eyeVec;
varying vec3 V_M;
varying vec4 glVertex;
varying vec2 glTexCoord0;
const float howmuch = 1.5;
const float invRadius = 0.0005;
void main (void)
{
float distSqr = dot(lightVec, lightVec);
float att = clamp(1.0 - invRadius * sqrt(distSqr), 0.0, 1.0);
vec3 lVec = lightVec * inversesqrt(distSqr);
vec3 vVec = normalize(eyeVec);
float height = texture2D ( heightMap, glTexCoord0.xy ).b;
float h = 0.03 * (1.0-height) - 0.015;
vec2 tex = glTexCoord0.xy - vVec.xy * h * howmuch / vVec.z;
vec4 base = texture2D(colorMap, tex);
vec3 bump = normalize( texture2D(normalMap, tex).xyz * 2.0 - 1.0);
float diffuse = max( dot(lVec, bump), 0.0 );
vec4 vertex = glVertex + vec4(2.0*height * V_M, 0.0);
vec4 p = gl_ModelViewProjectionMatrix * vertex;
gl_FragColor = vec4( base.rgb*diffuse*att,1.0 );
gl_FragDepth = 0.5 * (p.z / p.w + 1.0);
}
This shader alters OpenGL depth buffer to make parallax effect more realistic. Enjoy!
Bookmarks