Sign in to follow this  
3TATUK2

Cg POM shader translation?

Recommended Posts

3TATUK2    714

This is a really good parallax occlusion mapping shader, but it's written in Cg ... Can someone please translate it to GLSL?

 

Also maybe try to elaborate on the needed parts if you happen to know? Such as IN.scale in whatever else

/*****************************
** relief mapping
*****************************/
// linear serach
//
// step equal intervals until 
// finding a point inside object
void ray_intersect_linear(
      in sampler2D relieftex,
      inout float3 p, 
      inout float3 v)
{
	const int search_steps = 10;

	v /= search_steps;

	for(int i=0; i<search_steps-1; i++)
	{
		float4 t = tex2D(relieftex,p.xy);
		if (p.z<t.w)
			p += v;
	}
}

// binary search
//
// serach around point for a closer
// intersection walking half distance
// at each step
void ray_intersect_binary(
      in sampler2D relieftex,
      inout float3 p, 
      inout float3 v)
{
	const int search_steps = 5;
   
	for(int i=0; i<search_steps; i++)
	{
		v *= 0.5;
		float4 t = tex2D(relieftex,p.xy);
		if (p.z<t.w)
			p += 2*v;
		p -= v;
	}
}

f2s_mrt mrt_relief(
	v2f IN,
	uniform sampler2D colortex	: TEXUNIT0,	// color texture map 
	uniform sampler2D relieftex	: TEXUNIT1,	// relief texture map 
	uniform float2 planes)			// near and far plane information
{
	f2s_mrt OUT;

	// view vector in tangent space
	float3 v = normalize(IN.view);

	// depth bias
	//float s = 1.0-v.z; s = 1.0-s*s; IN.scale.z *= s;

	// scale view into texture space scale
	v *= IN.scale.z/(IN.scale*v.z);

	// ray intersect depth map
	float3 p = float3(IN.texcoord,0);
	ray_intersect_linear(relieftex,p,v);
	ray_intersect_binary(relieftex,p,v);

	// get normal and color from textures
	float3 normal  = f3tex2D(relieftex,p.xy);
	float4 texture = f4tex2D(colortex,p.xy);

	// diffuse color (+ emission part)
	float4 diffuse = glstate.material.diffuse;
	diffuse.w = glstate.material.emission.x;

	// specular color + shininess
	float4 specular = glstate.material.specular;

	// expand normal into object space
	normal -= 0.5;
	normal  = normalize(normal.x*IN.tangent+normal.y*IN.binormal+normal.z*IN.normal);

	// convert normal back to [0,1] color space
	normal = normal*0.5+0.5;

	// output mrt colors
	OUT.color0 = normal;
	OUT.color1 = texture*diffuse;
	OUT.color2 = specular;

	// corrected depth into a separate color buffer
	float3 pos = IN.vpos+normalize(IN.vpos)*p.z*IN.scale.z/IN.view.z;
	float d = ((planes.x*pos.z+planes.y)/-pos.z);
	OUT.color3 = float_to_color(d);

	return OUT;
}

I got this shader directly from the author who used it for the POM in this video:

 

http://www.youtube.com/watch?v=EjQicKCP-zc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this