shadow maps and fragment program

How do I use these together? As I understand it the way shadow maps work is by projecting the texture over the scene normally, but instead of using the colour value they compute it using Cv = textureCoord.r >= Cf ? 1 : 0.

While my texture coordinates for s and t are valid it seems r is always 1 in my fragment program… Actually r doesn’t seem to be 1, it is far greater than 1. It seems I need to normalize it.

How do I normalize my r coordinate if this is the case?

How did you project your vertices to the shadow map? Normally, you project them with following transformations:

(s,t,r,q) = BiasMat * Projectionmatrix(LightSource) * Modelviewmatrix(LightSource)

Projectionmatrix(LightSource) and ModelviewMatrix(LightSource) are the matrices you used to render the scene from the light source.

0.5 0 0 0.5
0 0.5 0 0.5 = BiasMat
0 0 0.5 0.5
0 0 0 0

With this matrix you transform the coordinate system from window/screen space to texture space (is that the correct expression?).

By doing these transformations correctly, you normally have a r-coordinate between 0 and 1. Maybe you forgot to do a homogeneous divide in your vertex program.

[This message has been edited by mako (edited 07-31-2003).]

[This message has been edited by mako (edited 07-31-2003).]

Originally posted by titan:
[b]How do I use these together? As I understand it the way shadow maps work is by projecting the texture over the scene normally, but instead of using the colour value they compute it using Cv = textureCoord.r >= Cf ? 1 : 0.

How do I normalize my r coordinate if this is the case?[/b]

The q-coordinate in (s,t,r,q) is not necessarily 1.0 when you receive it after interpolation in the fragment program. Try to divide (s,t,r,q) by q. It worked for me when I did shadow mapping and PCF in a fragment program.

  • SFRuckus

Originally posted by SFRuckus:
Try to divide (s,t,r,q) by q. It worked for me when I did shadow mapping and PCF in a fragment program.

Of course. I don’t know why I wasn’t doing that. While this does give me a grey similar to the that of the texture I can’t do the a valid compare of coordinate.z (after *(1/q)) with the result of the TXP function. Any idea what I could be missing?

Originally posted by titan:
Of course. I don’t know why I wasn’t doing that. While this does give me a grey similar to the that of the texture I can’t do the a valid compare of coordinate.z (after *(1/q)) with the result of the TXP function. Any idea what I could be missing?

Here is what I do: shCoord holds projected and biased position of fragment (relative to the light). The shadow map is in texture[0].
After

TEMP R;
TXP R.z, fragment.texcoord[0], texture[0], 2D;
SLT R.w, shCoord.z, R.z;

I have R.w = 0 if in shadow and R.w = 1 otherwise.