View Full Version : Curved Relief Mapping

06-23-2006, 02:11 PM
Hi guys!

I ve been trying to port this cg shader to glsl... relief mapping is one thing but the curved is another... I believe it is a must to understand and frankly... it is amazing... check this out:

if anyone is interested in helping digging this out... fun is on its way! :)

here is what ShaderDesigner as to offer as a starter:


uniform mat4 matWorldView;
uniform mat4 matView;
uniform mat4 matWorldViewProjection;

attribute vec3 tangent;
attribute vec3 binormal;

varying vec2 texCoord;
varying vec3 eyeSpaceVert;
varying vec3 eyeSpaceTangent;
varying vec3 eyeSpaceBinormal;
varying vec3 eyeSpaceNormal;
varying vec3 eyeSpaceLight;

void main(void)
eyeSpaceVert = (gl_ModelViewMatrix * gl_Vertex).xyz;
// eyeSpaceLight = (gl_ModelViewMatrix * vec4(gl_LightSource[0].position.xyz,1.0)).xyz;
eyeSpaceLight = vec4(gl_LightSource[0].position.xyz,1.0).xyz;

eyeSpaceTangent = gl_NormalMatrix * tangent;
eyeSpaceBinormal = gl_NormalMatrix * binormal;
eyeSpaceNormal = gl_NormalMatrix * gl_Normal;

texCoord = gl_MultiTexCoord0.xy;

gl_Position = ftransform();

uniform vec4 ambient;
uniform vec4 specular;
uniform vec4 diffuse;
uniform float shine;
uniform float depth;
uniform float tile;
uniform int linear_search_steps;
uniform int binary_search_steps;

varying vec2 texCoord;
varying vec3 eyeSpaceVert;
varying vec3 eyeSpaceTangent;
varying vec3 eyeSpaceBinormal;
varying vec3 eyeSpaceNormal;
varying vec3 eyeSpaceLight;

uniform sampler2D reliefmap;
uniform sampler2D texmap;
uniform sampler2D gloss;

float ray_intersect_rm(in vec2 dp,in vec2 ds);

void main(void)
vec4 t,c;
vec3 p,v,l,s;
vec2 dp,ds,uv;
float d,a;

// ray intersect in view direction
p = eyeSpaceVert;
v = normalize(p);
a = dot(eyeSpaceNormal,-v);
s = normalize(vec3(dot(v,eyeSpaceTangent),dot(v,eyeSpa ceBinormal),a));
s *= depth/a;
dp = texCoord*tile;
ds = s.xy;
d = ray_intersect_rm(dp,ds);

// get rm and color texture points

// expand normal from normal map in local polygon space
t.xyz=normalize(t.x*eyeSpaceTangent+t.y*eyeSpaceBi normal+t.z*eyeSpaceNormal);

// compute light direction
p += v*d*a;

// ray intersect in light direction
dp+= ds*d;
a = dot(eyeSpaceNormal,-l);
s = normalize(vec3(dot(l,eyeSpaceTangent),dot(l,eyeSpa ceBinormal),a));
s *= depth/a;
ds = s.xy;
dp-= ds*d;
float dl = ray_intersect_rm(dp,s.xy);
float shadow = 1.0;
vec3 specular_shadow=specular.xyz;
if (dl<d-0.05) // if pixel in shadow

// compute diffuse and specular terms
float att=max(0.0,dot(-l,eyeSpaceNormal));
float diff=shadow*max(0.0,dot(-l,t.xyz));
float spec=max(0.0,dot(normalize(-l-v),t.xyz));

// compute final color
vec4 finalcolor;
att*(c.xyz*diffuse.xyz*diff+specular_shadow*pow(sp ec,shine));
gl_FragColor = vec4(finalcolor.rgb,1.0);


float ray_intersect_rm(in vec2 dp, in vec2 ds)
//const int linear_search_steps=20;
//const int binary_search_steps=5;
float depth_step=1.0/float(linear_search_steps);

// current size of search window
float size=depth_step;
// current depth position
float depth=0.0;
// best match found (starts with last position 1.0)
float best_depth=1.0;

// search front to back for first point inside object
for( int i=0;i<linear_search_steps-1;i++ )
vec4 t=texture2D(reliefmap,dp+ds*depth);

if (best_depth>0.996) // if no depth found yet
if (depth>=t.w)
best_depth=depth; // store best depth

// recurse around first point (depth) for closest match
for( int i=0;i<binary_search_steps;i++ )
vec4 t=texture2D(reliefmap,dp+ds*depth);
if (depth>=t.w)

return best_depth;

06-27-2006, 02:35 AM
I have to know, has anyone gotten the Typhoon Labs relief shader implemented into their program perfectly?

I have been trying to implement it, but it turns out more like bump mapping than relief. So it is not doing any overlapping/occlusion with the texture.

If someone has it working, can you tell me what you had to change, if anything, and the attribute and uniform variables you are using in your shader.


06-27-2006, 09:17 PM
for my part, most of the work was to calculate the tangent and bitangents client side correctly... send them via vertex attributes and thats it!

06-29-2006, 08:31 PM
Hmm, that must be what I am doing wrong.

Is there any reason why I must use vertex attributes and not just generate them in the shader?


07-11-2006, 03:39 PM
Is there any reason why I must use vertex attributes and not just generate them in the shader? Assuming you are refering to tangents and binormals, to compute them in the shader you would need either:
1) For each vertex, a list of all neighboring vertices and their texture coordinates and normals, OR
2) A differentiable formula for the surface from which you could compute the gradient in the shader.

Either way, you'd be needlessly recomputing the tangents and binormals each frame. Performance-wise, you'd be better off computing them once in a preprocessing stage and passing them in as vertex attributes. But if your mesh is deformable or otherwise non-static, you'd need to recompute the tangent space basis each time the surface changes.

05-14-2012, 08:45 AM
I'm interested in this technique. Unfortunately, the original ressources are offline:


Does anyone by chance have this file and would be so kind to upload it to a one click hoster and send me the link? That would be awesome.


05-14-2012, 09:00 AM

This should be the same thing. BTW, 2 second Google search...

05-14-2012, 09:17 AM
I know (R)CSM and it's not the same thing. It does not support correct silhouettes (as it is there). Also, there's no z-buffer correction for shadow mapping in it. I refer to this paper


and I'd really like to have the corresponding demos / ressouces that once were available.

05-14-2012, 09:51 AM
Well, I guess you're right. Still the papers complement each other quite well. ;)

Manuel Oliveira's page also links to the dead links above. I guess the easiest and most rewarding way to get this straight is to implement it yourself.

05-14-2012, 10:49 AM
Yes, but I'm struggling a bit with that. Especially the pre-processing (calculating the quadric coefficients).