Animating Textures in DirectX 9

Started by
29 comments, last by busytree 17 years, 6 months ago
I looked for a long time and could not find any tutorials that show how to make animated textures. I want to animate my character as it moves across the screen, but I do not want to use sprites, so is there any way to do it without using sprites.
Advertisement
If you don't want to use sprites, then you are going to have to use textured Quads.

The big thing about animation is that you are going to have to track the time the texture is being displayed, then when it hits the length that you want it displayed, you switch to the next texture in the animation sequence.

A simple walk cycle can be done with 3 different textures. I would suggest using seperate textures at first to accomplish it. Then, you can save precious memory that textures eat up by using a single texture that serves as a sprite sheet (aka sprite atlas) and offset the UVs of your quad to accomodate the various images of the sequence.

If you need more help, just ask and I'll go further in depth if you can't find a solution.

Brandon
Ok thanks, I am going to try that right now and report my progress back here. Thanks :).
Hmm... I read about texture quads. I am using vertex buffers and loading the texture onto it. I thought since I have coordinates of my textures TU and TV. I can just directly manipulate the coordinates and make animations, no luck.

///////////////////////////////////////////////////////////struct CUSTOMVERTEX{   float x, y, z;      // The untransformed, 3D position for the vertex   float tu, tv;  };CUSTOMVERTEX g_Vertices[] =	{		{-1.0f, 2.0f,-1.0f,   0.0f, 0.0f },		{ 1.0f, 2.0f,-1.0f,  0.25f, 0.0f },		{-1.0f,-1.0f,-1.0f,   0.0f, 1.0f },		{ 1.0f,-1.0f,-1.0f,  0.25f, 1.0f }    };///////////////////////////////////////////////////////////


so I make CUSTOMVERTEX with world coordinates and texture coordinates. Then initialize and define g_Vertices.

Here is my problem

/////////////////////////////////////////////////////////////if(KEYDOWN(buffer, DIK_LEFT)) {				D3DXMatrixTranslation(&matTranslate, position.x-= 0.09f, position.y, position.z);		g_Vertices[8].tu += .25f;		g_Vertices[18].tu += .25f;		if(1.0f == g_Vertices[8].tu && 1.0f == g_Vertices[18].tu){			g_Vertices[8].tu = .25f;			g_Vertices[18].tu = .25f;		}			}/////////////////////////////////////////////////////////////

I am trying to invoke tu withen the g_Vertices just no luck. I tried various indexes and no change of texture. What am I missing? Time function?
It appears that you are trying to modify the Vertex Buffer without being inbetween a Lock() and Unlock(). Make sure to lock the buffer anytime you want to adjust the vertex x,y,z or u,v.

Alternatively you can DrawPrimiteUP without needing to lock/unlock the vertex buffer but feeding in the pointer to the buffer, size and stride.

Brandon
	      g_Vertices[1].tu += .25f;	      g_Vertices[3].tu += .25f;		  if(1.0f == g_Vertices[1].tu && 1.0f == g_Vertices[3].tu){			g_Vertices[1].tu = .25f;			g_Vertices[3].tu = .25f;		}	


ok so I placed my KEYDOWN conditions between the lock and unlock functions. So when I press the left arrow, the texture shrinks and expands. :/ Which is cool and all. I want my texture just to change position not shrink. How do I modify that? My mind is blank for the moment.
Heh, you got some UV animation going! Congrats, now you can make conveyor belts and really simple waves in water.

Your math is off for the UVs. As I stated first, work with DIFFERENT textures first just use SetTexture() when you press the keys, for now. After that we'll discuss sprite sheets.

Brandon
As for setTexture() technique, I found that I need to use timer of some sort, maybe getTime() function? I found it more tedious than my initial methods. I understand the concept of sprite. I have a picture that has 4 different motions for the character. So my initial setting is this

CUSTOMVERTEX g_Vertices[] =	{		{-1.0f, 2.0f,-1.0f,   0.0f, 0.0f },		{ 1.0f, 2.0f,-1.0f,  0.25f, 0.0f },		{-1.0f,-1.0f,-1.0f,   0.0f, 1.0f },		{ 1.0f,-1.0f,-1.0f,  0.25f, 1.0f }    };


which means g_Vertices[1].tu = .25f and g_Vertices[3].tu = 0.25f. So I cropped one quarter of the image to fit as a texture. When I set these tu to 0.50f, it will have two characters motions, instead of one different character motion. I want one character to fit in the texture. So what's going on there?
You need to move both texture co-ords across as below. This would give you the second quarter (across) of the base texture displayed on your quad.

CUSTOMVERTEX g_Vertices[] =	{		{-1.0f, 2.0f,-1.0f,   0.25f, 0.0f },		{ 1.0f, 2.0f,-1.0f,  0.50f, 0.0f },		{-1.0f,-1.0f,-1.0f,   0.25f, 1.0f },		{ 1.0f,-1.0f,-1.0f,  0.50f, 1.0f }    };
OH wow, once I did this:

[source lang=cpp]g_Vertices[0].tu += .25f;		g_Vertices[1].tu += .25f;		g_Vertices[2].tu += .25f;	    g_Vertices[3].tu += .25f;		if(1.0f == g_Vertices[1].tu && 1.0f == g_Vertices[3].tu){			g_Vertices[0].tu = .0f;			g_Vertices[1].tu = .25f;			g_Vertices[2].tu = .0f;			g_Vertices[3].tu = .25f;		}


I have the animation of my character as if it was sprite. :) Thank you. my question is what's happening exactly with texture coordinates of tu on all four vertices. Then my next question is what is the effecient way implementing timer, so that animation is not too fast? Is there more efficient of doing this?

This topic is closed to new replies.

Advertisement