# what the hell tex2Dproj have done ?

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 );

So there is my question: what the matTexAdj is ? and how tex2Dproj works ? Anyone give me a hand T_T

##### Share on other sites
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).

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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

Edit: Thergothon beat me :(

##### Share on other sites
Thanks people, thanks for your great help here

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 15
• 21
• 26
• 11