Texture Rotation in DX9

Started by
2 comments, last by yewbie 12 years, 5 months ago
I am supplying the U and V coordinates for my texture like below, what I would like to do is have the texture rotate from left to right but stay on my quad.
I haven't really been able to find much information about this, A lot of people suggest using a translation matrix, but I am defining my vertices directly and nothing goes through translation.

How was this done in YE OLDE times such as the texture "scrolling" done in games like doom, duke3d, etc?




TLVERTEX* vertices; //my vertices

vertices[0].colour = D3DCOLOR_ARGB (Alpha,Light,Light,Light);
vertices[0].x = (float)x;
vertices[0].y = (float)y;
vertices[0].z = 0.0f;
vertices[0].rhw = 1.0f;
vertices[0].u = 0.0f ;//1 - 0
vertices[0].v = 0.0f ;//1 - 0

vertices[1].colour = D3DCOLOR_ARGB (Alpha,Light,Light,Light);
vertices[1].x = (float)x + 64;
vertices[1].y = (float)y;
vertices[1].z = 0.0f;
vertices[1].rhw = 1.0f;
vertices[1].u = 1.0f ;//0 - 1
vertices[1].v = 0.0f ;//0 - 0

vertices[2].colour = D3DCOLOR_ARGB (Alpha,Light,Light,Light);
vertices[2].x = (float)x + 64;
vertices[2].y = (float)y + 64;
vertices[2].z = 0.0f;
vertices[2].rhw = 1.0f;
vertices[2].u = 1.0f ;//0 - 1
vertices[2].v = 1.0f ;//0 - 0

vertices[3].colour = D3DCOLOR_ARGB (Alpha,Light,Light,Light);
vertices[3].x = (float)x;
vertices[3].y = (float)y + 64;
vertices[3].z = 0.0f;
vertices[3].rhw = 1.0f;
vertices[3].u = 0.0f ;//0 - 1
vertices[3].v = 1.0f ;//0 - 0
Advertisement
The translation (or any other transformation matrix) is meant to be used on the (2D) texcoord. For a simple left to right scroll, just add an offset between [0..1] for u and set the texture addressing mode to wrap. E.g. (pseudo-code):

float elapsedTime = ...; // total elapsed time for current frame
float offset = frac(elapsedTime); // scale the argument to adjust speed, frac then "mods" it to [0..1]
//...
vertices[0].u = 0.0f + offset;
//...
vertices[1].u = 1.0f + offset;
// etc.


With shaders, of course, no need to recreate the vertex buffer: You could do this in a vertex shader providing the offset as a uniform.
Oh, thanks I tried this but didn't even think about the texture wrapping mode!
I will give it a shot and report back, thanks!
That works, thanks!

This topic is closed to new replies.

Advertisement