Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / 3D Theory & Graphics / VS vs. PS vector normalization Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
mentalcalculator

April 01, 2005, 07:09 AM

Hey, i'm having a little problem with a basic direct3d effect file ( .fx ) that just does a simple dot3 bump mapping. Normalizing the light vector ( in tangent space ) in vertex shader is giving me very different results from normalizing it in pixel shader, and the worst thing is that normalizing it on pixel shader is resulting in a weird look while the vertex shader normalization is fine.

Here's the basics of the effect:

  1.  
  2. // light vector
  3. float3 lightPosition : LIGHTPOSITION;
  4.  
  5. struct VS_OUTPUT
  6. {
  7.     float4 Pos             : POSITION;
  8.     float3 LightVector : COLOR0;
  9.     float2 BaseUVs         : TEXCOORD0;
  10. };
  11.  
  12. ////////////////////////////// VERTEX SHADER
  13.  
  14. VS_OUTPUT VS11( float3 Pos : POSITION, float3 Norm : NORMAL, float2 texCoords : TEXCOORD0, float3 Tangent : TANGENT, float3 Binormal : BINORMAL )
  15. {
  16.     VS_OUTPUT Out = ( VS_OUTPUT ) 0;
  17.    
  18.     float4 posWorld = mul( float4( Pos, 1 ), World );
  19.  
  20.     float4 posView = mul( posWorld, View );
  21.  
  22.     float4 posProjection = mul( posView, Projection );
  23.  
  24.         float4 lightInObjectSpace = mul( float4( lightPosition, 1 ), InverseWorld );
  25.  
  26.         float3 lightVector = float3( lightInObjectSpace.x, lightInObjectSpace.y,
  27.  
  28. lightInObjectSpace.z ) - Pos;
  29.        
  30.         float3 lightInTangentSpace;
  31.         lightInTangentSpace.x = dot( lightVector, Tangent );
  32.         lightInTangentSpace.y = dot( lightVector, Binormal );
  33.         lightInTangentSpace.z = dot( lightVector, Norm );
  34.         //lightInTangentSpace = normalize( lightInTangentSpace );
  35.        
  36.         Out.BaseUVs             = texCoords;
  37.         Out.Pos                 = posProjection;
  38.         Out.LightVector = lightInTangentSpace;
  39.        
  40.         return Out;
  41. }
  42.  
  43.  
  44. ///////////////////////////// PIXEL SHADER
  45.  
  46. float4 PS20( VS_OUTPUT In ) : COLOR
  47. {
  48.         // normalize vectors interpolated along the surface
  49.         float3 lightVector = normalize( In.LightVector );
  50.        
  51.         // sample bump map
  52.         float3 pixelNormal = tex2D( bumpMapSample, In.BaseUVs );
  53.        
  54.         // put pixelNormal in [ -1, 1 ] range
  55.         pixelNormal = pixelNormal * 2 - 1;
  56.         pixelNormal = normalize( pixelNormal );
  57.        
  58.         // sample base texture
  59.         float3 baseColor = tex2D( baseSample, In.BaseUVs );
  60.                
  61.         // calculate diffuse illumination
  62.         float bumpDot = saturate( dot( lightVector, pixelNormal ) );
  63.  
  64.         // calculate pixel's final color
  65.         float4 finalColor;
  66.         //finalColor.rgb = lightColor * baseColor * bumpDot;
  67.         finalColor.rgb = lightVector;
  68.         finalColor.w   = 1;
  69.  
  70.     return finalColor;
  71. }  
  72.  


Anything wrong ?

Thanks!

 
NCCAChris

April 01, 2005, 08:59 AM

the light in tangent space calculation is wrong i think - its been a while since i have done this kind of work - got some code at home which might help...in the mean time just look at nvidia's examples

 
mentalcalculator

April 01, 2005, 09:06 AM

Hum... are you sure? Because i don't think it is wrong... :)

 
Scali

April 01, 2005, 09:22 AM

You put the lightvector in a COLOR register.
They only have a range of 0..1 and limited precision.
So if you don't normalize per-vertex (and scale/bias from -1..1 to 0..1 range), the components of your normal will probably be clamped.
Either keep the per-vertex normalization anyway, even though you normalize per-pixel, or use a texture register instead for the normalvector.

 
mentalcalculator

April 01, 2005, 09:26 AM

Ohhh...damn, what a mistake! It's now working! Thanks!

 
This thread contains 5 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.