from depth texture to world space

hey guys,
i have a question regarding openGL/Cg. i posted it in the cgshaders.org forum, but i have a feeling that board gets a lot less exposure than this one, so here goes

so i’m using tex2DProj to get the float depth value from my shadow map (the shadow map texture is configured with: glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE) so that i don’t get back just 1/0) at some pixel. now i need to convert that depth back to worldspace. is the best way to do that to embed a form of “gluUnproject” into my fragment program, and then run it on each depth value i get back from tex2DProj? has anybody done this before with any success?

Yeah, you’ve got a point (S,T,Z), where S and T are the generated texcoords and Z is the depth sample. The viewport rectangle is (0,0,1,1), and the depth range is whatever you had when you rendered the depth texture, which is probably (0,1). The “modelview” and projection matrices are concatenated in your texture matrix, so invert that and you have everything you need to unproject your point.

– Tom

thanks tom. one more question – i know that when values are stored in the depth buffer, they’re first scaled and then biased. i assume the same happens when rendering to a depth texture. do i have to manually undo this scaling/biasing in my cg program, or will tex2D/tex2DProj take care of this for me?