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

 Home / 3D Theory & Graphics / Shadow map in Pixel shader texRECT(samlperRECT tex, float3 sz) 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.
 
koici

March 29, 2005, 06:53 AM

I m learning how to program shaders in Cg. I 'm working on shadow mapping. There is a function called texRECT(samlperRECT tex, float3 sz) that I use to perform a depth test. I think I have set up the texture correctly in openGl to perform the depth test. What I don't understand is the use of z (in the function texRECT). In Cg toolkit manual they say "z indicates a depth comparison value for shadow map lookups". What I am suppose to give to the function ???

And then the result of this function is a float4 but where is the result of the depth test ?

In fact I would like to do the same thing that in assembly code

TEX R0.x, fragment.position, texture[0], SHADOWRECT;

 
Reedbeta

March 29, 2005, 11:14 AM

I don't know CG, but there is probably a function called something like texRECTShadow(samplerRECT, float3) to do the shadow comparison. You put the Z value that you want to compare against into the Z-component of the texture coordinate, if this Z value is greater than the value stored in the texture it returns a vector of all 0's, otherwise a vector of all 1's.

 
SirKnight

April 14, 2005, 11:06 PM

The tex coords you use when doing the shadow map lookup should be the world space vertex transformed by a matrix that is the combination of light view matrix, light projection matrix and a scale/bias matrix to convert the range from [-1,1] to [0,1]. If you are not going to use a proj version of the texture lookup function then you will have to do the divide by w yourself to the tex coords before using them in the shadow map lookup. You should use texRECTproj so it does the divide by w for you.

Here is a snippet of a cg vertex program that I use for shadow mapping:

  1.  
  2. vertexOut vout;
  3.        
  4. mv  = glstate.matrix.modelview[0];
  5. mvp = glstate.matrix.mvp;
  6.        
  7. float4 epos = mul( mv, vert.pos );      // Eye-space vertex position
  8. float4 wpos = mul( cvi, epos    );      // Transform back to world space
  9. float4 proj = mul( tex, wpos    );  // Transform from world space to projected texture space
  10.  
  11. vout.hpos       = mul( mvp, vert.pos );
  12. vout.projCoords = proj;
  13. vout.tex0       = vert.tex0;
  14. vout.normal     = vert.normal;
  15. vout.v2light    = float4( lightPos.xyz - vert.pos.xyz, 1 );
  16. vout.view2v     = float4( vert.pos.xyz - eyePos.xyz, 1 );
  17.  
  18. return vout;
  19.  


BTW, cvi is the inverse camera view matrix and tex is the matrix that has the matrix products: [Scale/Bias]*[Light Proj]*[Light View].

And here is the fragment program (note I use 2D texures):
  1.  
  2. float4 finalColor;
  3.  
  4. float  shadow   = ( vert.projCoords.w < 0 ) ? 0 : tex2Dproj( shadowMap, vert.projCoords ).r;   
  5. float4 lightTex = ( vert.projCoords.w < 0 ) ? 0 : tex2Dproj( projLightTexture, vert.projCoords );      
  6.  
  7. float2 tex0 = vert.tex0.xy;
  8.  
  9. float4 Kd       = tex2D( rgbTexture,  tex0 );
  10. float4 Ks       = tex2D( specTexture, tex0 );
  11.  
  12. float3 normal  = normalize( vert.normal.xyz  );
  13. float3 v2light = normalize( vert.v2light.xyz );
  14. float3 view2v  = normalize( vert.view2v.xyz  );
  15.        
  16. float3 l2vert = -v2light;
  17.        
  18. float diffuse, specular;
  19.  
  20. float3 R = reflect( -v2light, normal );  
  21.  
  22. float4 lighting = float4( 0, 0, 0, 0 );
  23.        
  24. lighting = lit( dot( normal, v2light ), dot( R, -view2v ), specExp );
  25.  
  26. diffuse  = lighting.y;
  27. specular = lighting.z;
  28.  
  29. float3 finalShadow = shadow * lightTex.xyz;
  30.        
  31. finalColor  = float4( ( Kd.xyz * ambient ) + ( Kd.xyz * diffuse + Ks.xyz * specular ) * finalShadow, 1 );
  32.  
  33. return finalColor;
  34.  



-SirKnight

 
killah

May 25, 2005, 06:06 AM

If I'm using OpenGL and, once configured the camera, I want to transform "Camera View Matrix" to "Inverse Camera View Matrix" (your "cvi" variable) to send it to the vertex shader, which matrix I have to use? (MODELVIEW, PROJECTION...), at first I thought that I had to use MODELVIEW but I'm not sure at all...


Cheers

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