12 Replies - 3137 Views - Last Post: 29 November 2010 - 10:52 PM Rate Topic: -----

#1 simpler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Animation with 2D textured quads

Posted 28 November 2010 - 01:39 PM

Hey I'm writing a 2D game with DirectX and have been using textured quads to draw my textures on the screen. Now i want to take it a step further and add animations to the player when he moves. I know how this is done when you use a Sprite but I don't wat to use them. So my question is;

How do i make animations with textured quads?

And, I would prefer to have the a texture atlas where is store the characters different animations. I might be totally lost, if so give me some tips.

Thanks,
simpler

Is This A Good Question/Topic? 0
  • +

Replies To: Animation with 2D textured quads

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Animation with 2D textured quads

Posted 28 November 2010 - 03:59 PM

Well you basically just do what you do with sprites, change the texture on the quad at for each animation frame. Create an array of textures that you can grab or loop through.
Was This Post Helpful? 0
  • +
  • -

#3 simpler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: Animation with 2D textured quads

Posted 28 November 2010 - 11:07 PM

I need to have a vertex shader, right?
Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Animation with 2D textured quads

Posted 29 November 2010 - 04:55 AM

Um..no, what gave you that idea? it would be a little overkill to use a shader for this :s

You need to load each frame of the animation into a seperate 2D texture and bind them as required, or put all frames into a single 2D texture and move the texture coordinates.
Was This Post Helpful? 0
  • +
  • -

#5 simpler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: Animation with 2D textured quads

Posted 29 November 2010 - 05:41 AM

Okey, i want to put all the frames in one texture. Is there a function or smth to cange the texture cooordinates?
Was This Post Helpful? 0
  • +
  • -

#6 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Animation with 2D textured quads

Posted 29 November 2010 - 06:17 AM

You say you know how to do this with sprites. Could you post some code or what your idea for this would be?
Was This Post Helpful? 0
  • +
  • -

#7 simpler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: Animation with 2D textured quads

Posted 29 November 2010 - 07:50 AM

I can't access the code right now but it looks like this:
(I have done a nested for()-loop to automaticly increment the top, left, right and bottom coordinates depending on wich frame im drawing. The loop isnt here but the i try to show the result of it:)

Character texture width and height = 64x64

FRAME 1:
partOfTextureToDraw.top = 0 left = 0 right = 64 bottom = 64
FRAME 2:
partOfTextureToDraw.top = 0 left = 64 right = 128 bottom = 64
FRAME 3:
partOfTextureToDraw.top = 0 left = 128 right = 192 bottom = 64

Sprite->Draw(
pTexture,
partOfTextureToDraw,
pCenter,
pPosition,
Color
);

In the game loop I update the coordintes of the rectangle and then simpy call the Draw() function to draw just that part of the texture. When using sprites it's just to have a RECT structure which holds what part of the texture to draw in the Draw()-function.

But when I use my textured quads I don't know how to do.
Maybe it's my vertex structure that needs to have texture coordinates as well? I feel kinda stuck

This post has been edited by simpler: 29 November 2010 - 07:51 AM

Was This Post Helpful? 0
  • +
  • -

#8 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Animation with 2D textured quads

Posted 29 November 2010 - 08:16 AM

Check out this link. A lot of it is a little overkill for what you are doing but the concepts are there and should get you started, especially the section on batching. Give it a try and then post some code showing your work, it'll be much easier to help then.
Was This Post Helpful? 0
  • +
  • -

#9 simpler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: Animation with 2D textured quads

Posted 29 November 2010 - 09:40 AM

Oh thanks alot stayscrips! I acctually followed that guide when I made my textured quads, but not untill now did I found out that the vertex structure acctually had U and V members representing the texture coordinates. And now i know how to set them as well, I think i can get started with the animation for real. Just one last question ofcourse ;)

Lets say this is my vertex structure(like in the example):
struct TLVERTEX
{
    float x;
    float y;
    float z;
    float rhw;
    D3DCOLOR colour;
    float u;
    float v;
};


How does the the compiler recognize that the 'u' and 'v' members represent texture coordinates? Is it the specific name of them, the order or something else?
Was This Post Helpful? 0
  • +
  • -

#10 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Animation with 2D textured quads

Posted 29 November 2010 - 10:45 AM

Glad you're making progress :)

Before you create your vertex struct you define how your vertex will be used like so:

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_COLOR | D3DFVF_TEX1)



Now when you use a custom vertex, in your case TLVERTEX, the compiler knows how it should be interpreted.

This post has been edited by stayscrisp: 29 November 2010 - 10:48 AM

Was This Post Helpful? 0
  • +
  • -

#11 simpler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: Animation with 2D textured quads

Posted 29 November 2010 - 12:40 PM

That really explains alot!
If I define this:

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_COLOR | D3DFVF_TEX1)


Does my vertex structure have to be in that specific order? Or can I do like this:

struct TLVERTEX
{
    float u;
    float v;
    float x;
    float y;
    float z;
    float rhw;
    D3DCOLOR colour;
};


Lastly,
in my book I read they don't do it like this. They do the vertex decleration like this:

D3DVERTEXELEMENT9 VertexPosElements[] = 
	{
		{0, 0,  D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
		D3DDECL_END()
	};	
	HR(gd3dDevice->CreateVertexDeclaration(VertexPosElements, &VertexPos::Decl));


Is that doing the same thing in another way? My book is about DirectX 9.0c, have things changed or is that code above doing something totally different?
Was This Post Helpful? 0
  • +
  • -

#12 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Animation with 2D textured quads

Posted 29 November 2010 - 12:54 PM

Yes vertex declarations are the new way of doing this and they are a lot better. At the minute it doesn't really matter which you use but if you were to move onto more complicated meshes then vertex declarations would be a better way to go. Also you need to use vertex declarations to effectively use vertex shaders.

I don't know if they need to be in a specific order, my guess is that they do (why don't you try it). It's definitely a lot less confusing to keep them in the order you declare them in.

You use vertex declarations like this:

LPDIRECT3DVERTEXDECLARATION9 pVertexDeclaration;
D3DVERTEXELEMENT9 decl[] =
{
    // your vertex elements
}; 

device->CreateVertexDeclaration(decl, &pVertexDelaration);



So first of all you declare a pointer to a vertex declaration ( LPDIRECT3DVERTEXDECLARATION9 is simply a typedef for IDirect3DVertexDeclaration* )

then you use the CreateVertexDeclaration function to make it point to the vertex elements.
Was This Post Helpful? 0
  • +
  • -

#13 simpler  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 02-March 10

Re: Animation with 2D textured quads

Posted 29 November 2010 - 10:52 PM

Thanks once again, starting to understand a bit more :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1