Sign in to follow this  
thekiwimaddog

Texture Coords Translation

Recommended Posts

thekiwimaddog    155
Hi Everyone! I'm trying change the texture coords on an object and I'm having trouble doing this using D3DXMatrixTranslation() with the code below:

m_d3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );

D3DXMATRIX matTrans;
D3DXMatrixIdentity(&matTrans);
D3DXMatrixTranslation(&matTrans,66.6f,66.6f,0.0f);

m_d3dDevice->SetTransform( D3DTS_TEXTURE0, &matTrans );

Test.Render();
However, the following code works fine:
m_d3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );

D3DXMATRIX matTrans;
D3DXMatrixIdentity(&matTrans);

matTrans._31 = 66.6f;
matTrans._32 = 66.6f;

m_d3dDevice->SetTransform( D3DTS_TEXTURE0, &matTrans );
Does anyone have any ideas why D3DXMatrixTranslation() is not working?? Any help would be greatly appreciated! Many Thanks David

Share this post


Link to post
Share on other sites
Evil Steve    2017
Both bits of code should produce the exact same result. Have you tried comparing the generated matrices to see what the difference is?

Share this post


Link to post
Share on other sites
thekiwimaddog    155
Thanks for the reply Evil Steve!
It's Messing up for some reason as you can see:
D3DXMatrixTranslation(&matTrans,66.6f,66.6f,0.0f);
1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
0.0 0.0 1.0 0.0
66.6 66.6 0.0 1.0

What It Should Be:
1.0 0.0 0.0 0.0
0.0 1.0 0.0 0.0
66.6 66.6 1.0 0.0
0.0 0.0 0.0 1.0

Any Ideas Why Anyone??
Thanks
David

Share this post


Link to post
Share on other sites
kauna    2922
Hi,

The D3DXMatrixTranslation routine is working with 4x4 matrices where the translation is stored in different elements, as you have already noticed.

The texture coordinate transform function expects you to provide a 4x4 matrix where only the 3x3 part is used.

Hope you understand.

Best regards!

For reference the d3dmatrix structure which d3dxmatrix inherits:


typedef struct _D3DMATRIX {
union {
struct {
float _11, _12, _13, _14;
float _21, _22, _23, _24;
float _31, _32, _33, _34;
float _41, _42, _43, _44;

};
float m[4][4];
};
} D3DMATRIX;

Share this post


Link to post
Share on other sites
JohnnyCode    1046
any vector you want to transform with a translation matrix must be passed to transform function in [x,y,z,1.0] 4 dimensional vector thus you get

1*x + 0*y + 0*z + 1*66.6f=translatedvec.x;

0*x + 1*y + 0*z + 1*66.6f=translatedvec.y;

0*x + 0*y + 1*z + 1*66.6f=translatedvec.z;

the translation part of matrix is stored as a 4th column allways (row in dx case).
the 1-3*1-3 portion of matrix is for rotation component.

In case you transform direction vectors(normalized vectors) not position vectors,
you do not use [vector,1.0] but [vector,0.0] input vector.

Share this post


Link to post
Share on other sites
kauna    2922

Hi,

sorry for being too hard to understand.

What I meant to say that you cannot use D3DXMatrixTranslation in this case since it will place the translation in the elements matTrans._41,matTrans._42,matTrans._43.

The texture transformation matrix must have the translation in the elements matTrans._31,matTrans._32.

I hope you understand.

Best regards!

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by kauna
The texture coordinate transform function expects you to provide a 4x4 matrix where only the 3x3 part is used.
Really? Where does it say that? I can only find a reference to "Direct3D devices can transform the texture coordinates for vertices by applying a 4x4 matrix."

In any case - this is another reason to use vertex shaders; you have full control over what size of matrix you pass in [wink]

Share this post


Link to post
Share on other sites
kauna    2922
Quote:
Original post by Evil Steve
Really? Where does it say that? I can only find a reference to "Direct3D devices can transform the texture coordinates for vertices by applying a 4x4 matrix."


Another thread on this matter and here is an example of applying texture translation on Direct3d mobile.

I also suggest using the vertex shaders instead of fixed function pipeline.

Regards!

Share this post


Link to post
Share on other sites
Nik02    4348
Quote:
Original post by Evil Steve
Quote:
Original post by kauna
The texture coordinate transform function expects you to provide a 4x4 matrix where only the 3x3 part is used.
Really? Where does it say that? I can only find a reference to "Direct3D devices can transform the texture coordinates for vertices by applying a 4x4 matrix."

In any case - this is another reason to use vertex shaders; you have full control over what size of matrix you pass in [wink]


Although it's been a while since I wrote anything against the fixed pipeline, I can confirm that the size of the matrix does depend on the size of the texture coordinate vector to transform. For 2D texture coordinates, the area of the matrix that gets actually used is 3x3 where the last row is the translation. However, you need to use the upper-left area of a 4x4 matrix to pass the data as a parameter, since there aren't other matrix sizes available at the API level. The driver just ignores the extra rows and columns as appropriate. To be safe though, you should set the whole 4x4 to identity and then set the smaller subset as appropriate because it may be that some drivers actually do the whole 4x4 multiplication.

I guess the reasoning for this is that they didn't want to further overload the already-complex render state functions by introducing more matrix sizes. The hardware, on the other hand, understandably prefer as compact form as possible.

Like Steve said, vertex shaders make this kind of stuff a non-issue since you decide exactly what (and in which form) you feed to the hardware.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this