views:

402

answers:

2

I draw textures using the D3DXSprite.
I want to transform them(rotate, scale etc..) so I use the SetTransfrom method.
Should I, store the old transformation -> set a new one -> draw -> set the old one? I have a sprite class whice take care of the Draw and the Update methods. I mean something like this:

D3DXMatrix oldMatrix;
sprite->GetTransfrom(&oldMatrix);

D3DXMatrix newMatrix;
D3DXMatrixScaling(&newMatrix, 2.0f, 2.0f, 0.0f);

sprite->SetTransform(&newMatrix);
sprite->Draw(...);

sprite->SetTransform(&oldMatrix);

Good/Bad?

+1  A: 

What would be bad is to retrieve the oldMatrix and use it for computing a newMatrix (precision issues). It's better to recompute a fresh matrix at each new draw.

What you probably want to store for each of your sprites is a position, rotation and scale factor.

Stringer Bell
How can I recompute a new matrix?
Adir
You can use D3DXMatrixTransformation2D (assuming you have 2D sprites)
Stringer Bell
+1  A: 

TBH drawing 2 tris (Im assuming thats what your sprite is made of) changing a transform and drawing is HIGHLY inefficient.

You are far far better off manually transforming the 4 verts into 1 big dynamic vertex buffer and rendering everything in a oner. Same goes for the textures applied to the sprites. Keeping them in one big texture will save you many state changes and allow you to do your rendering far far faster overall.

Edit: Making multiple DrawPrimitive/DrawIndexedPrimitive calls is very slow. For each matrix you set you need to make a seperate draw call. The way to avoid this is to apply your matrix to each of the 4 vertices yourself and then copy them into a vertex buffer already transformed. This means you can make one draw call with with an identity world matrix set.

It is quite probable, in hindsight, that ID3DXSprite abstracts this away from you as you are unable to set the corners. Performance would truly suck if it didn't abstract this away from you.

It is very unlikely, however, that it abstracts away the texture problem. Thus it would be best to store all the frame of a sprite in one texture (and possibly all the frames of multiple sprites in one texture). So in this case you are best off passing one IDirect3DTexture and setting pSrcRect to the sub rect of which ever frame you are in rather than using seperate textures. The fewer texture you use the better performance will be.

Goz
Have any code sample? I havn't fully understood it
Adir
Maybe see also: http://blogs.msdn.com/shawnhar/archive/2010/03/22/point-sprites-in-xna-game-studio-4-0.aspx
Stringer Bell