Normal maps + Skinned animation shader

Problems building or running the engine, queries about how to use features etc.
Post Reply
Chaster
OGRE Expert User
OGRE Expert User
Posts: 557
Joined: Wed May 05, 2004 3:19 pm
Location: Portland, OR, USA
Contact:

Normal maps + Skinned animation shader

Post by Chaster »

Is anyone using the code from the wiki for normal mapping with hardware skinning (http://www.ogre3d.org/wiki/index.php/No ... d_Specular)
? I ask because I am having a little problem getting it to work. At shader compile time, It complains about something not being defined which is clearly defined in the shader.. (in other words, it says it doesn't know what something is, but I have told it...)

Here's my (very slightly modified) version:

Code: Select all

vertex_program AnimatedNormalSpecular_VP hlsl
{
   source animatedNormalSpecular.hlsl
   entry_point main_vp
   target vs_2_0
   column_major_matrices false		//required for hlsl skinning
   
   includes_skeletal_animation true

   default_params
   {
      param_named_auto worldviewprojmatrix worldviewproj_matrix
      param_named_auto light_position light_position_object_space 0
      param_named_auto eye_position camera_position_object_space
      
      param_named_auto worldMatrix3x4Array world_matrix_array_3x4
      param_named_auto viewProjectionMatrix viewproj_matrix
      param_named_auto invworldmatrix inverse_world_matrix
   }
}

fragment_program AnimatedNormalSpecular_FP hlsl
{
	source animatedNormalSpecular.hlsl
	entry_point main_fp
	target ps_2_0
	
	default_params
	{ 
		param_named_auto lightDiffuse light_diffuse_colour 0
		param_named_auto ambientLight ambient_light_colour
		param_named_auto specularLight light_specular_colour 0
		param_named specular_power float 64
		param_named bumpiness float 1
	}
}


material GruntFemaleBlue
{
	technique
	{
		pass Single Pass
		{
			vertex_program_ref AnimatedNormalSpecular_VP
			{
			}

			fragment_program_ref AnimatedNormalSpecular_FP
			{
			}
			shadow_caster_vertex_program_ref HardwareSkinningTwoWeightsShadowCaster		
			{
			}
			
			//diffuse map
			texture_unit
			{
				texture_alias base_map
				texture MedBodyDiffusemap.png
				filtering linear linear linear
			}

			//normal map
			texture_unit
			{
				texture_alias bump_map
				texture MedBodyNormalmap.png
				filtering linear linear linear
			}
			
		            // specular map
			  texture_unit specular_map
			  {
				texture_alias specular_map
				texture specular.png
			  }

		}

	}
}
I am trying to get it to work, but I keep getting a shader compile time error from ogre saying that it doesn't know what the worldviewprojmatrix is, but it's defined in the .hlsl file, so I don't know why Ogre can't find it..:

Code: Select all

void main_vp(    
		float4 position :     POSITION,
		float2 uv :    TEXCOORD0, 
		float3 normal:       NORMAL,
		float3 tangent:      TANGENT0,
   
		float4 blendIdx : BLENDINDICES,
		float4 blendWgt : BLENDWEIGHT,
		
		out	float4 oPosition :     POSITION,
		out	float2 oUV :		TEXCOORD0,
		out	float3 oLightVector: TEXCOORD1,
		out	float3 oHalfAngle :   TEXCOORD2,

		uniform float4x4 worldviewprojmatrix,
		uniform float4 light_position,
		uniform float4 eye_position,
		uniform float3x4   worldMatrix3x4Array[60],
		uniform float4x4 viewProjectionMatrix,
 		uniform float4x4  invworldmatrix
   )
{
	// Calculate the pixel position using the perspective matrix.
	oUV = uv;   
	
	// transform by indexed matrix
	   float4 blendPos = float4(0,0,0,0);
	   int i;
	   for (i = 0; i < 2; ++i)
	   {
		      blendPos += float4(mul(worldMatrix3x4Array[blendIdx[i]], position).xyz, 1.0) * blendWgt[i];
	   }
	   // view / projection
	oPosition = mul(viewProjectionMatrix, blendPos);
	

	   // transform normal
	   float3 newnormal = float3(0,0,0);
	   for (i = 0; i < 2; ++i)
	   {
		      newnormal += mul((float3x3)worldMatrix3x4Array[blendIdx[i]], normal) *      blendWgt[i];
	   }
	  newnormal = mul((float3x3)invworldmatrix, newnormal); 
	   newnormal = normalize(newnormal);

	   // transform tangent
	   float3 newtangent = float3(0,0,0);
	   for (i = 0; i < 2; ++i)
	   {
		      newtangent += mul((float3x3)worldMatrix3x4Array[blendIdx[i]], tangent) *      blendWgt[i];
	   }
	  newtangent = mul((float3x3)invworldmatrix, newtangent); 
	   newtangent = normalize(newtangent);
	  
	  
	  
	  
	float3 binormal = cross(newtangent, newnormal);
	float3x3 rotation = float3x3(newtangent, binormal, newnormal);

	 // Calculate the light vector in object space,
	// and then transform it into texture space.
	float3 temp_lightDir0 = normalize(light_position.xyz -  (blendPos * light_position.w));
	temp_lightDir0 = normalize(mul(rotation, temp_lightDir0));
	oLightVector = temp_lightDir0;


	// Calculate the view vector in object space,
	// and then transform it into texture space.
	float3 eyeDir = normalize(eye_position - blendPos);
	eyeDir = normalize(mul(rotation, eyeDir.xyz));

	// Calculate the half angle
	oHalfAngle = oLightVector + eyeDir;

}





float4 lightDiffuse ;
float4 ambientLight;
float4 specularLight;
float specular_power;
float bumpiness;
sampler base_map;
sampler bump_map;
sampler specular_map;

struct PS_INPUT_STRUCT
{
   float2 uv:     TEXCOORD0;
   float3 light_vector: TEXCOORD1;
   float3 half_angle:   TEXCOORD2;
};

struct PS_OUTPUT_STRUCT
{
   float4 color0:       COLOR0;
};

PS_OUTPUT_STRUCT main_fp( PS_INPUT_STRUCT psInStruct )
{
   PS_OUTPUT_STRUCT psOutStruct; 

  float3 base = tex2D( base_map, psInStruct.uv );
  float3 bump = tex2D( bump_map, psInStruct.uv );
  float specularLevel = tex2D(specular_map, psInStruct.uv).r;

  //normalise
  float3 normalized_light_vector = normalize( psInStruct.light_vector );
  float3 normalized_half_angle = normalize( psInStruct.half_angle );
   
  // "Smooth out" the bump based on the bumpiness parameter.
  // This is simply a linear interpolation between a "flat"
  // normal and a "bumped" normal.  Note that this "flat"
  // normal is based on the texture space coordinate basis.
  float3 smooth = { 0.5f, 0.5f, 1.0f };
  bump = lerp( smooth, bump, bumpiness );
  bump = normalize( ( bump * 2.0f ) - 1.0f );

  // These dot products are used for the lighting model
  // equations.  The surface normal dotted with the light
  // vector is denoted by n_dot_l.  The normal vector
  // dotted with the half angle vector is denoted by n_dot_h.
  float4 n_dot_l = dot( bump, normalized_light_vector );
  float4 n_dot_h = dot( bump, normalized_half_angle );

  // Calculate the resulting pixel color,
  // based on our lighting model.
  // Ambient + Diffuse + Specular
  psOutStruct.color0.rgb =
     ( base * ambientLight) +
     ( base * lightDiffuse * max( 0, n_dot_l ) ) +
     ( specularLight * specularLevel * pow( max( 0, n_dot_h ), specular_power ) );
  psOutStruct.color0.a = 1.0f; //** Set the alpha component manually

  return psOutStruct;
}

Any help? I'm a bit rusty on my shaders, so it's probably something very simple, but I'm sitting here scratching my head...

Chaster

[/code]
User avatar
tuan kuranes
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 2653
Joined: Wed Sep 24, 2003 8:07 am
Location: Haute Garonne, France
x 4
Contact:

Post by tuan kuranes »

The error message state that you're not using the variable "worldviewprojmatrix" in your shader main_vp code, hence it cannot bind it.

Comment its declaration it in both Ogre program shader declaration and in shader function header.
Chaster
OGRE Expert User
OGRE Expert User
Posts: 557
Joined: Wed May 05, 2004 3:19 pm
Location: Portland, OR, USA
Contact:

Post by Chaster »

Well, that was a big "duh" on my part... Thanks Tuan... Now to figure out why it still doesn't work for me.. LOL... (it's compiling fine, just not doing the normal maps.. I think I gotta go back and relearn shader stuff again..)

Thanks again,

Chaster
lmas19820607
Halfling
Posts: 72
Joined: Wed Dec 06, 2006 7:35 am

Post by lmas19820607 »

I found another error in this material...
In the main_vp:

// Calculate the view vector in object space,
// and then transform it into texture space.
float3 eyeDir = normalize(eye_position - blendPos);
eyeDir = normalize(mul(rotation, eyeDir.xyz));

The blendPos is in world space, but here needs object space blendPos.

It'll cause that the specular can't move when camera moves.

But I don't know how to fix it. I try to add this,
blendPos.xyz = mul((float3x3)invworldmatrix, blendPos.xyz);

doesn's work...
User avatar
manowar
Orc
Posts: 419
Joined: Thu Apr 07, 2005 2:11 pm
Location: UK
Contact:

Post by manowar »

I have been using this shader in my app (even modified it to get two lights...) and it was working fine, until I upgraded to shoggoth where the lighting gets totally wrong.

I tried the model in Lexi viewer and its fine too. I tried to use cg instead of hlsl but I get the same issues with lighting.

I even tried to hardcode the light position in the shader itself but nothing helps. It looks like something is not passed correctly from the vertex shader to the pixel shader.The position of the light has no effect at all, or at least is not passed correctly to the pixel shader for some reason.

Is there anything to change to make it work with shoggoth ? It would be nice if someone could quicky try this shader with shoggoth and let me know if it works. Could it be a bug somewhere in shoggoth ?

Thanks for your help

PS. I hve not looked much at the specular problem because our model does not need it :)
User avatar
Wolfmanfx
OGRE Team Member
OGRE Team Member
Posts: 1525
Joined: Fri Feb 03, 2006 10:37 pm
Location: Austria - Leoben
x 99
Contact:

Post by Wolfmanfx »

Maybe it has something todo with this problem http://www.ogre3d.org/phpBB2/viewtopic. ... highlight= ?
User avatar
manowar
Orc
Posts: 419
Joined: Thu Apr 07, 2005 2:11 pm
Location: UK
Contact:

Post by manowar »

Well this the problem in the other thread is different, it seems to be related with GL only. My application is running with d3d renderer.
So annoying problem :(
Chaster
OGRE Expert User
OGRE Expert User
Posts: 557
Joined: Wed May 05, 2004 3:19 pm
Location: Portland, OR, USA
Contact:

Post by Chaster »

I still haven't fixed this problem either. I had to attend to other more pressing issues and so let this one sit. I've just recently converted our project to Shoggoth, and am still working on other issues, but I need to get this fixed (or no normal mapped characters! yikes) eventually. Anyone who has insight, please post/share, k?

Chaster
Post Reply