Texture transform question

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

Recommended Posts

I was looking into texture transforms using the fixed pipeline before jumping to implementing that as a vertex shader, and although I did make it work, I'd like to make sure I understand why it does... :) I tried making my texture scroll horizontally by building a matrix with D3DXMatrixTranslation(), but nothing happened after passing that matrix to D3D. After some failed attempts to make it work, I figured out that for texture transforms, translations are not stored in _41, _42 and _43, but rather _31 and _32? So am I right to assume that texture transforms actually use a 3x3 matrix and not a 4x4 matrix? If so, is there a function similar to D3DXMatrixTranslation() I can use to easily build such matrices? Also, I'm not 100% sure what D3DTTFF_COUNTx value to use. COUNT2 and COUNT3 seem to work fine in my current situation, so what's the difference really and why would they both work the same? Thanks!

Share on other sites
First, you need to assign a texture transformation to a state. This would set it to state 0.

pDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, _D3DTEXTURETRANSFORMFLAGS )

_D3DTEXTURETRANSFORMFLAGS is one of the flags as you mentioned... COUNT1, COUNT2, etc... Because of the fact that the transformation matrix is 4x4, and your texture coordinates are 2x2, obviously the multiplication can't be done. There's a bit of mathematics involved that I won't get into... Long story short, for 2-d texture coordinates, use the flag D3DTTFF_COUNT2. (1-d coordinates would use COUNT1, 3-d COUNT3, etc).

To transform 2-d coordinates, you want to edit entries _31 and _32 of the matrix (for U and V respectively).

Call pDevice-SetTransform( D3DTS_TEXTURE0 , &Mat ) with whichever texture state your texture is in.

pDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE ) when you're done, and you should be good to go. :)

Share on other sites
Thanks for the answer Doggan, but like I said, I got it working already, so the code itself is not the problem. What I want to know is why it seems to require a 3x3 matrix for translation rather than a 4x4 one that I create with the standard D3DX function, and how to build 3x3 matrices easily (D3DX style). Basically, it's the part where you said "there's a bit of mathematics involved that I won't get into" that I wanted to know... ;)

Anyone?

Share on other sites
Sorry, guess I just skimmed your question. I'll try to explain it now :)

For a World Matrix, you have a situation like this:

RightVec.x RightVec.y RightVec.z 0
UpVec.x UpVec.y UpVec.z 0
LookVec.x LookVec.y LookVec.z 0
Position.x Position.y Position.z 1

x, y, z, w, respectively. w is set to the identity matrix. This uses homogeneous coordinates, (x, y, z, 1). So, we have a 3-d matrix, but we use the 4th row for position (translation) vector.

Now we go to texture coordinates. The same principle applies. For 2-d texture coordinates (U, V) we have a 2 dimensional matrix, that we need to multiply by a 4 dimensional matrix (to perform translation/scaling/rotation of the texture). We use the form (u, v, 1, 0).

1 0 0 0
0 1 0 0
3 -5 1 0
0 0 0 1

...is a 2-d texture translation matrix. If we want the new values for U = .9, V = .1... Doing the calculations (using matrix multiplication):

U = .9*(m11) + .1*(m21) + 1*(m31) + 0*(m41)
= .9*1 + .1*0 + 1*3 + 0*0
= 3.9

V = .9*(m12) + .1*(m22) + 1*(m32) + 0*(m42)
= .9*0 + .1*1 + 1*-5 + 0*0
= -4.9

As you can see, the new values of U and V are shifted 3 and -5. If you want to scale the texture, you would change m11, m22 so that the texture coordinates are being multiplied by a value other than 1.

D3DTTFF_COUNT2 tells the pipeline that we are using 2-d texture coordinates, U and V. So, it knows that the homogeneous coordinates will be of the form (u, v, 1, 0). COUNT1 (1-d coordinates) would say (u, 1, 0, 0), where the 2nd row is used for translation.

Hope that helps.

• 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!

• 14
• 30
• 9
• 16
• 22