Sign in to follow this  
Stru

Sprite Engine in 3D

Recommended Posts

I made a 2D sprite engine in Managed DirectX 9 and .NET with textured quads. And It does eveything I want rotation, alpha-blending, texture blending, scaling, multiple quads for each object, etc. However, I am unhappy with the performance of the engine. And, I am pretty sure I know the cause of the poor performance. Every frame I regenerate all the vertices based on object data then render them all and I have to do it all over again next time. I know there is a better way (i.e. generate all the geometry and move around a camera and only update the geometry that is changing) I am kinda new to Direct3D and I dont really know the best way to set this up. I looked at the 2D in DX9 tutorial but it didnt really make sense to me. Also, some of my objects will be made up of multiple textured quads which I didn't think the method in the tutorial can handle. I know i have to do some matrix stuff that I am kinda dreading. I am also kinda lost as to how the vertex buffer locking would work with this method. My question is... Could someone please give me some simple pseudocode for this kind of engine? I would really really appreciate any help/explanation you could give me. Particularly code dealing with all the vertices and locking them.

Share this post


Link to post
Share on other sites
If you are worried about performance using your own textured quads, try using ID3DXSprite. It batches everything, so it actually only makes one DrawPrimitive call per frame, which can really improve performance (espeicially if you are rendering a lot of quads). Also, it's really easy to use. Here is all you have to do:

// Load-time
(1) Create the ID3DXSprite object, using the D3DXCreateSprite factory function.

(2) Load your textures

// Run-time
(1) Call ID3DXSprite::Begin()

(2) For each texture you want to render
   (a) Build the translation matrix
   (b) Call ID3DXSprite::Draw()

(3) Call ID3DXSprite::End()

Share this post


Link to post
Share on other sites
I cant find a lot of documentation on ID3DXSprites but I want to know that they capable of doing everything I need before I start trying to code. Right now I am using TriangleStrips as sprites. I can change each vertex's color/tranparency individually, do 3 textures in a single pass and rotate and scale them however I want. And, I can make non-square sprites, (although I am willing to sacrifice non-square sprites if ID3DXSprite can do the rest of the stuff I need).

Also when you say it batches everything, I assume you mean batches the ones with the same texture. If the sprites have different textures will it automatically batch the ones with the same texture?, Or does it batch everything together?, or do I have sort them by texture myself. (I can batch them that way with my current engine if I wanted to although that isnt really my bottleneck). The problem is that my sprites use a lot of different textures and it will be a real pain to have to worry about doing the same texture at the same time which I suspect is the case with ID3DXSprite. And, if I use ID3DXSprite do I still have a vertex buffer to worry about or is it taken care of for me?

Sorry for all the questions but Microsoft's documentation/examples/samples are terrible.

Share this post


Link to post
Share on other sites
Check out ID3DXSprite in the SDK. It has very good info on it. Also for an example, see my post in gamedev.net post

As far as batching goes with the sprites, you can set that parameter when you call Sprite->Begin(D3DXSPRITE_SORT_TEXTURE); as such. You can also sort other ways. Check out the SDK for others.

You would use Sprite->SetTransform(&Matrix) where Matrix contains the typical transformation matrix. This allows translation, rotation, and scaling I believe, which you can set using the helper D3DXMatrix... functions.

The vertex buffer is taken care of for you.

Also, using the renderstates as your normally would, you can change the transparency. I believe when you call Sprite->Draw, the last member is the color of the sprite to draw. Two things to remember about render states are:

1) Start setting your render states AFTER the Sprite->Begin() statement.
2) If you set a render state, then call Draw, then change that same render state to something else...realize that the last change made to that render state will be the one used when you say Sprite->End(), which actually draws the sprites. Therefore, if you need to set a render state, try to batch them as much as possible, and when you need to change a render state, call Sprite->Flush() and it will flush the vertices into the pipeline, so you can change the render state safely and continue preparing the remaining sprites.

I do not believe you can make non-square sprites (without transparency).

Chris

Share this post


Link to post
Share on other sites
Thanks so much for answering my questions. I think I am starting to understand these things.

As for the SDK, well you must have a different version because my SDK is totally worthless it has about 1 sentence describing each function in the Sprite Class and thats it. No further explanation at all. Evidently Microsoft doesn't expect you to figure it out by yourself...

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