Relief shader to share

The place for artists, modellers, level designers et al to discuss their approaches for creating content for OGRE.
User avatar
Quorthon3D
Greenskin
Posts: 100
Joined: Fri Aug 08, 2008 8:06 pm
Location: Belo Horizonte - Brazil

Relief shader to share

Post by Quorthon3D »

Hi.

Has anybody a script of relief to share ??
I found one here, but he works only in openGL.
http://www.ogre3d.org/forums/viewtopic. ... 68&start=0

thanks
Sorry for my fail english, I'm studing, I swear! =]
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Re: Relief shader to share

Post by jacmoe »

You are mistaken. CG is almost identical to HLSL.

<edit>
And, CG works in D3D as well.
</edit>
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
Quorthon3D
Greenskin
Posts: 100
Joined: Fri Aug 08, 2008 8:06 pm
Location: Belo Horizonte - Brazil

Re: Relief shader to share

Post by Quorthon3D »

No. I know that .cg works in both renders, but I tested the shader, and this works only in openGL, when renders on Direct3D, the object not appears.

So, I'm searching other script.

any help me, please.
Sorry for my fail english, I'm studing, I swear! =]
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Re: Relief shader to share

Post by jacmoe »

Try different profiles - I am no shader genius, but I strongly suspect arbfp1 to be OpenGL only?
Look in the Ogre manual for other profiles.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179

Re: Relief shader to share

Post by jacmoe »

Try profiles vs_2_0 arbvp1 for the vertex program and profiles ps_2_0 arbfp1 for the fragment program.
It just might work.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 116

Re: Relief shader to share

Post by mkultra333 »

I have a version of the one from the nvidia shader site that I'm working on now, got it running complete with the inbuilt shadow. Not on this computer, I'll have to post it later. That other shader on the thread does have opengl specific code it it, plus it does per vertex lighting so the specular doesn't come out very nice. The nvidia one is better, it's CG and doesn't specifically assume ogl, and it does per fragment lighting. I can run it in directx.

Alternately, copy the one from the nvidia shader site and make the following changes:
1: Removed the BINORMAL input and generate your own from TANGENT and NORMAL.
2: Change the part of the shader that has "-IN.binormal" to "+IN.binormal"

I think that was all... use tile of 1 and depth of 0.1 or so. And your bump map (which is in the alpha channel of your normal map) needs to be inverted compared what you might normally use, like a negative.

It looks pretty neat, there some catches though that I'm trying to modify (for instance, you can get weird gaps at edges of walls unless you use the right kind of relief pattern.)

Anyhow, I'll post all my code later anyway.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 116

Re: Relief shader to share

Post by mkultra333 »

Here's the shader.

relief_map.cg

Code: Select all

/*********************************************************************NVMH3****
*******************************************************************************
$Revision: #4 $

Copyright NVIDIA Corporation 2008
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED
*AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS
BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES
WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS,
BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY
LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

% This material shows and compares results from four popular and
% advanced schemes for emulating displaement mapping.  They are:
% Relief Mapping, Parallax Mapping, Normal Mapping, and Relief
% Mapping with Shadows.  Original File by Fabio Policarpo.

keywords: material bumpmap
date: 071005

Note: Strong discontinuties in the model geometric normal (e.g., very sharp
    differences from the normals in two or more parts of the same triangle)
    can cause unusual overall light-attentuation errors. Re-normalizing the
    rasterized normals in the fragment shader can correct this, but the case
    was considered rare enough that these extra steps were eliminated for
    code efficiency. If you see off lighting near sharp model edges, just
    normalize "IN.normal" in the calculation of the varible "att" (shared
    by all techniques).



To learn more about shading, shaders, and to bounce ideas off other shader
    authors and users, visit the NVIDIA Shader Library Forums at:

    http://developer.nvidia.com/forums/

*******************************************************************************
******************************************************************************/

// Modified for OGRE by mkultra333, 090529
// Got rid of BINORMAL input, now computed in vertex program.
// Changed "- tNorm.y*IN.binormal" to "+ tNorm.y*IN.binormal" in fragment program.
// Removed WorldITXf, WorldXf and ViewIXf 4x4 matrixes that weren't used.


/********** CONNECTOR STRUCTURES *****************/

struct AppVertexData {
    float4 pos		: POSITION;
    float4 color	: COLOR0;
    float3 normal	: NORMAL; // expected to be normalized
    float2 txcoord	: TEXCOORD0;
    float3 tangent	: TANGENT0; // pre-normalized
};

struct VertexOutput {
    float4 hpos		: POSITION;
    float2 UV		: TEXCOORD0;
    float3 vpos		: TEXCOORD1;
    float3 tangent	: TEXCOORD2;
    float3 binormal	: TEXCOORD3;
    float3 normal	: TEXCOORD4;
    float4 lightpos	: TEXCOORD5;
    float4 color	: COLOR0;
};

/*** SHADER FUNCTIONS **********************************************/

VertexOutput view_spaceVS(AppVertexData IN,
	uniform float4x4 WvpXf,
    uniform float4x4 ViewXf,
    uniform float4x4 WorldViewXf,
    uniform float TileCount,
    uniform float3 LampPos
) {
    VertexOutput OUT = (VertexOutput)0;
    // isolate WorldViewXf rotation-only part
    

    float3x3 modelViewRotXf;
    modelViewRotXf[0] = WorldViewXf[0].xyz;
    modelViewRotXf[1] = WorldViewXf[1].xyz;
    modelViewRotXf[2] = WorldViewXf[2].xyz;
    
    float4 Po = float4(IN.pos.xyz,1.0);
    OUT.hpos = mul(WvpXf,Po);
    
    // vertex position in view space (with model transformations)
    OUT.vpos = mul(WorldViewXf,Po).xyz;
    
    // light position in view space
    float4 Lw = float4(LampPos.xyz,1); // this point in world space
    OUT.lightpos = mul(ViewXf,Lw); // this point in view space
    
	// Calculate the binormal (NB we assume both normal and tangent are
	// already normalised)
	// NB looks like nvidia cross params are BACKWARDS to what you'd expect
	// this equates to NxT, not TxN
	float3 binormal = cross(IN.tangent, IN.normal);

    // tangent space vectors in view space (with model transformations)
    OUT.tangent = mul(modelViewRotXf,IN.tangent);
    OUT.binormal = mul(modelViewRotXf,binormal);
    OUT.normal = mul(modelViewRotXf,IN.normal);
    
    // copy color and texture coordinates
    OUT.color = IN.color; // currently ignored by all techniques
    OUT.UV = TileCount * IN.txcoord.xy;
    
    return OUT;
}

//// ray-intersect functions for relief mapping //////////

float ray_intersect_rm(			// use linear and binary search
      in sampler2D reliefmap,
      in float2 dp, 
      in float2 ds)
{
   const int linear_search_steps=15;
   
   // current size of search window
   float size = 1.0/linear_search_steps;
   // current depth position
   float depth = 0.0;
   // search front to back for first point inside object
   for( int i=0;i<linear_search_steps-1;i++ ) {
	float4 t = tex2D(reliefmap,dp+ds*depth);
	if (depth<t.w)
	    depth += size;
   }
   const int binary_search_steps=5;
   // recurse around first point (depth) for closest match
   for( int ii=0;ii<binary_search_steps;ii++ ) {
	size*=0.5;
	float4 t = tex2D(reliefmap,dp+ds*depth);
	if (depth<t.w)
	    depth += (2*size);
	depth -= size;
   }
   return depth;
}

float ray_intersect_rm_lin(	// only linear search for shadows
      in sampler2D reliefmap,
      in float2 dp, 
      in float2 ds)
{
   const int linear_search_steps=15;
   // current size of search window
   float size = 1.0/linear_search_steps;
   // current depth position
   float depth = 0.0;
   // search front to back for first point inside object
   for( int i=0;i<linear_search_steps-1;i++ ) {
	float4 t = tex2D(reliefmap,dp+ds*depth);
	if (depth<t.w)
	    depth += size;
   }
   return depth;
}

float4 relief_mapPS(VertexOutput IN,
		    uniform float Depth,
		    uniform float3 SurfaceColor,
		    uniform sampler2D ColorSampler : register(s1),
		    uniform sampler2D ReliefSampler : register(s0),
		    uniform float PhongExp,
		    uniform float3 SpecColor,
		    uniform float3 AmbiColor
) : COLOR
{
    // ray intersect in view direction
    float3 p = IN.vpos;
    float3 Vn = normalize(p);
    float a = dot(IN.normal,-Vn);
    float3 s  = float3(dot(Vn,IN.tangent.xyz), dot(Vn,IN.binormal.xyz), a);
    s  *= Depth/a;
    float2 ds = s.xy;
    float2 dp = IN.UV;
    float d  = ray_intersect_rm(ReliefSampler,dp,ds);
    // get rm and color texture points
    float2 uv = dp+ds*d;
    float3 texCol = tex2D(ColorSampler,uv).xyz;
    float3 tNorm = tex2D(ReliefSampler,uv).xyz - float3(0.5,0.5,0.5);
    tNorm = normalize(tNorm.x*IN.tangent +
		  tNorm.y*IN.binormal + 
		  tNorm.z*IN.normal);
    // compute light direction
    p += Vn*d/(a*Depth);
    float3 Ln = normalize(p-IN.lightpos.xyz);
    // compute diffuse and specular terms
    float att = saturate(dot(-Ln,IN.normal));
    float diff = saturate(dot(-Ln,tNorm));
    float spec = saturate(dot(normalize(-Ln-Vn),tNorm));
    spec = pow(spec,PhongExp);
    // compute final color
    float3 finalcolor = AmbiColor*texCol + 
	    att*(texCol*SurfaceColor*diff+SpecColor*spec);
    return float4(finalcolor.rgb,1.0);
}

float4 relief_map_shadowsPS(VertexOutput IN,
			    uniform float Depth,
			    uniform float3 SurfaceColor,
			    uniform sampler2D ColorSampler : register(s1),
					uniform sampler2D ReliefSampler : register(s0),
			    uniform float PhongExp,
			    uniform float3 SpecColor,
			    uniform float3 AmbiColor
) : COLOR
{
    // ray intersect in view direction
    float3 p = IN.vpos;
    float3 Vn = normalize(p);
    float a = dot(IN.normal,-Vn);
    float3 s = float3(dot(Vn,IN.tangent.xyz), dot(Vn,IN.binormal.xyz), a);
    s  *= Depth/a;
    float2 ds = s.xy;
    float2 dp = IN.UV;
    float d  = ray_intersect_rm(ReliefSampler,dp,ds);
    // get rm and color texture points
    float2 uv = dp+ds*d;
    float3 texCol = tex2D(ColorSampler,uv).xyz;
    float3 tNorm = tex2D(ReliefSampler,uv).xyz - float3(0.5,0.5,0.5);
    tNorm = normalize(tNorm.x*IN.tangent +
		    tNorm.y*IN.binormal + 
		    tNorm.z*IN.normal);
    // compute light direction
    p += Vn*d/(a*Depth);
    float3 Ln = normalize(p-IN.lightpos.xyz);
    // compute diffuse and specular terms
    float att = saturate(dot(-Ln,IN.normal));
    float diff = saturate(dot(-Ln,tNorm));
    float spec = saturate(dot(normalize(-Ln-Vn),tNorm));
    // ray intersect in light direction
    dp+= ds*d;
    a  = dot(IN.normal,-Ln);
    s  = float3(dot(Ln,IN.tangent.xyz),dot(Ln,IN.binormal.xyz),a);
    s *= Depth/a;
    ds = s.xy;
    dp -= ds*d;
    float dl = ray_intersect_rm_lin(ReliefSampler,dp,s.xy);
    if (dl<d-0.05) {		// if pixel in shadow
      diff *= dot(AmbiColor.xyz,float3(1.0,1.0,1.0))*0.333333;
      spec = 0;
    }
    spec = pow(spec,PhongExp);
    // compute final color
    float3 finalcolor = AmbiColor*texCol + 
	    att*(texCol*SurfaceColor*diff+SpecColor*spec);
    return float4(finalcolor.rgb,1.0);
}
ReliefMap.material

Code: Select all

vertex_program view_spaceVS cg         
{
   source relief_map.cg
   entry_point view_spaceVS
   profiles arbvp1 vs_1_1
}

fragment_program relief_mapPS cg         
{
   source relief_map.cg   
   entry_point relief_mapPS
   profiles ps_2_x arbfp1 fp30
}

fragment_program relief_map_shadowsPS cg         
{
   source relief_map.cg   
   entry_point relief_map_shadowsPS
   profiles ps_2_x arbfp1 fp30
}

material reliefMaterial
{
   technique
   {      
      pass
      {
         vertex_program_ref view_spaceVS
         {
						param_named_auto	WvpXf					worldviewproj_matrix
						param_named_auto	ViewXf				view_matrix
						param_named_auto	WorldViewXf		worldview_matrix
						param_named				TileCount			float 1
						param_named_auto	LampPos				light_position 0



         }
         fragment_program_ref relief_map_shadowsPS
         {
						param_named				Depth					float 0.1
						param_named_auto	SurfaceColor	light_diffuse_colour 0
						param_named				PhongExp			float 32.0
						param_named_auto	SpecColor			light_specular_colour 0
						param_named_auto	AmbiColor			ambient_light_colour 0
         }

         texture_unit NormalMap
         {
            texture exp00_nbi.tga
         }

         texture_unit DiffuseMap
         {
            texture exp00.tga
         }
      }
   }
}
And here's a pic of the effect, sorry it was a bit dark so I brightened it externally.

Image

I only got this running the other night, and there may be bugs. There are two version of the shader, one with shadows and one without.

The original is on this nvidia page, http://developer.download.nvidia.com/sh ... brary.html. As for the license, it says on that page "All the shaders in this library are provided free of charge for use in derivative works, whether academic, commercial, or personal" and has more in depth license info here, http://developer.download.nvidia.com/li ... icense.txt.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
User avatar
lordsme
Gremlin
Posts: 167
Joined: Sun Mar 11, 2007 1:11 pm
Location: Turin, Italy
x 10

Re: Relief shader to share

Post by lordsme »

This works perfectly, thank you!
My Portfolio
http://davidedigiannantonio.weebly.com

MESH - Mise-en-scene Helper
http://www.mesh-project.org/

ASALab @ Virtual Reality & Multimedia Park
http://www.vrmmp.it