VS
normal = normalize((gl_ModelViewMatrix* vec4(gl_Normal.xyz,0.0)).xyz);
tex_tan = cross(normal.xyz,vec3(0,-1,0));
tex_cotan = cross(normal.xyz,tex_tan);
tex_uv = gl_MultiTexCoord0.xy;
For the fragment shader, we need to first get the direction of the texturing by using the derivatives before we can compute the pertubed normal vector as follows:
FS
vec3 bump=vec3(-1.0,-1.0,-1.0)+2.0*texture2D(texBump,tex_uv).xyz;
vec2 tdx=normalize(dFdx(tex_uv)); // tex coord derivative in x
vec2 tdy=normalize(dFdy(tex_uv)); // tex coord derivative in y
vec3 n =bump.z*normal // Z-direction of the normalmap
+bump.x*(tdx.x*tex_tan+tdx.y*tex_cotan) // X-direction
+bump.y*(tdy.x*tex_tan+tdy.y*tex_cotan);// Y-direction
n=normalize(n);
Its obviously an approximation, but so far I am satisfied with the result.
Note that mirrored UV's will need special treatment which is not included above.
Update: Using the geometry shader for computing the (tdx.x*tex_tan+tdx.y*tex_cotan) terms might improve the performance.
Note that mirrored UV's will need special treatment which is not included above.
Update: Using the geometry shader for computing the (tdx.x*tex_tan+tdx.y*tex_cotan) terms might improve the performance.