Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

what the hell tex2Dproj have done ?

Started by DeeFar Aug 11, 2006 at 1:16 AM 4 replies 11k views
Original Post
DeeFar
DeeFar
I am really wondered what the hell tex2Dproj have done.(My Eng is poor.......) For example: I want to find a point in 3Dspace where it will be drawn on my screen(or maybe a texture) by UV coordinate. First i should transform the coordinate;
float4 vPtInProjSpace = vPtInObjSpace * matWorld * matView * matProj;
Then i want to use tex2Dproj to help me find the pixel drawn by that point.
float4 color = tex2Dproj( sShadowMap, vPtInProjSpace );
Of course this way is totally wrong, but i know how to do it by tex2D:
float2 vTexCoord = float2( vPtInObjSpace.x / vPtInObjSpace.w / 2 + 0.5, (- vPtInObjSpace.y) / vPtInObjSpace.w / 2 + 0.5 );
float4 color = tex2D( sShadowMap, vTexCoord );
And this works well. Luckily i find the right way by using tex2Dproj in this sample:
float fTexOffs = 0.5 + (0.5 / (float)SHADOW_MAP_SIZE);
D3DXMATRIX matTexAdj( 0.5f,     0.0f,     0.0f, 0.0f,
                      0.0f,     -0.5f,    0.0f, 0.0f,
                      0.0f,     0.0f,     1.0f, 0.0f,
                      fTexOffs, fTexOffs, 0.0f, 1.0f );
float4 color = tex2Dproj( sShadowMap, vPtInProjSpace * matTexAdj);
So there is my question: what the matTexAdj is ? and how tex2Dproj works ? Anyone give me a hand T_T
Thergothon
Thergothon
matTexAdj scales a point in the world*view*projection space of the light from being -1 <= x <= 1, -1 <= y < = 1, to 0 <= x <= 1, 0 <= y <=1, so that the point has a corresponding uv in the correct range.

as far as I know, tex2Dproj, performs a perspective divide on the coordinates passed to it, so that it gets the correct texel from the map (which is in the lights perspective space).
DeeFar
DeeFar
thanks a lot guy, I'm really not good at matrix. I think i should pay more time on that. What's more, does this mean i can gain a better performance with using tex2Dproj and matrix
Thergothon
Thergothon
Yes, you get better performance using tex2Dproj. This is because when you multiply the light space matrix by the texture adjustment matrix, it means you only have to do a single matrix multiply in the shader, followed by a call to tex2Dproj. If you didn't do this, you need a matrix multiply to convert the point to light space, and then perform the perspective divide followed by the 0.5 multiply and 0.5 addition to move the point into texture space for your tex2D call. So yeah, in short, use tex2Dproj.
TheAdmiral
TheAdmiral
The sample demonstrates (more or less) the most efficient way to perform projective texturing. Naturally, the scaling transformation is best done by matrix and both tex2Dproj & tex2D assemble to a texld (1 slot under ps_1_4) so performing the arithmetic manually is just burning up cycles.
You could save yourself a single instruction by turning the mat4x4 into a mat4x3 (as the w component undergoes an identity transformation anyway) if that's important to you.

Regards
Admiral

Edit: Thergothon beat me :(
Ring3 Circus - Diary of a programmer, journal of a hacker.
DeeFar
DeeFar
Thanks people, thanks for your great help here

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.