Archived

This topic is now archived and is closed to further replies.

Normie

VertexBuffers, Locking, and 2D (D3D)

Recommended Posts

Ok, after going though near every tutorial and article I can find on drawing 2D in D3D, I've figured out a few things (!): 1. VertexBuffers are used to draw vertices, and thus polygons, textures, etc. 2. You lock the buffer before writing data to it. 3. Locking and unlocking to move/draw new vertices/sprites is expensive on performance. Before I read/figured this out (I can't remember which), I thought of creating a sprite class with a buffer for each one. Then I realized that would mean a different buffer for each and every set of vertices (You'd think that'd be obvious, eh? ), and that would get pretty expensive on the (un)locking. So I realized I should use only one, large buffer, maybe two, for my entire 2D app. However, I have absolutely no idea how to do this, object-oriented or otherwise. Help (or, hell, even an example) would be most appreciated. [EDIT] Oh yeah, and if you planned to give an example *nudge *, i'm trying my damnedest to avoid globals, just because a past program taught me that it's evil bad juju. -Normie "But time flows like a river... and history repeats." ... So...what about beaver dams? Edited by - normie on July 24, 2001 11:41:04 PM

Share this post


Link to post
Share on other sites
If you like check out my D3DXSprite wrapper. It uses the D3DXSprite interface which supports scaling, alpha, colorkeying and rotation, I find it just as good as using Quads - if not more accurate. Its set up so every seperate bitmap is referenced by numbers and uses its own texture.

  Downloads:  ZeroOne Realm

Share this post


Link to post
Share on other sites
Normie, I actually came upon the exact same idea as you. Have one giant vertex buffer where all the sprite vertices are stored, and then have each separate sprite add to that buffer, and store its index.

You''d accomplish this with static class variables... you''d need a few. The pointer to the vertex buffer, a class reference counter (create the vertex buffer for the first class instance, if they''ve all been destroyed, kill the vertex buffer, etc.), the current vertex buffer position.

It would work very efficiently, I would think, especially for 2D games with static vertices. I''ll implement it sometime.

-----------------
The Goblin (madgob@aol.com)
-----------------
"Before critisizing somebody, walk a mile in their shoes. That way, when you do critisize them, not only will you be a mile away, but you''ll also have their shoes!"

Share this post


Link to post
Share on other sites
read the article on this site. You can have one buffer with 4 vertices and just reuse it with different matrices, or have a buffer of all vertices that don''t change, or both - use a big buffer for all unchanging vertices and a small, reusable buffer for everything that does. Likewise if there are objects that move together and always have the same matrices applied, you could batch them accordingly......

Share this post


Link to post
Share on other sites
hmmmmm something I''ve been pondering for a while...

What if we had two objects: One to store a pool of used textures,
the other for VertexBuffers...

now since it''s an expensive operation to switch textures and/or vertex buffers, it only makes sense to combine as many similar primitives as possible (using the same texture) into the same vertexbuffer...

So why not assign a unique key to each vertex buffer in the vertexbuffer pool and one for each texture in the texture pool...THEN upon creation of any entity, assign it one of the keys (matching the texture filename with the filename stored in the texture pool perhaps)..

?? would this work ???

Erik

Share this post


Link to post
Share on other sites