• FEATURED
• FEATURED
• FEATURED
• FEATURED
• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# what the hell tex2Dproj have done ?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1DeeFar  Members

Posted 10 August 2006 - 07:16 PM

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

### #2Thergothon  Members

Posted 11 August 2006 - 03:48 AM

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

### #3DeeFar  Members

Posted 12 August 2006 - 10:54 PM

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

### #4Thergothon  Members

Posted 13 August 2006 - 06:37 AM

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.

Posted 13 August 2006 - 06:51 AM

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 :(

### #6DeeFar  Members

Posted 13 August 2006 - 03:17 PM

Thanks people, thanks for your great help here

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.