Jump to content
  • Advertisement
Sign in to follow this  
3TATUK2

Cg POM shader translation?

This topic is 1738 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:

 

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!