Skip to main content
gamedev.net
🔒 Locked

Vertex Buffer question

Started by ChaosPhoenix Nov 1, 2005 at 10:32 AM 4 replies 500+ views
Original Post
ChaosPhoenix
ChaosPhoenix
So, I've done a lot of network programming but decided to expand my graphic programming knowledge. I've run into an issue that none of the books I have really explain well. When you lock the vertex buffer, it takes in a double void pointer as the source of your vertices. This double void pointer has no size and I see people just memcpy'ing random amounts of data into this unknown sized pointer. So, A. Wouldn't copying data into an unknown-sized, double void pointer buffer cause a crash? Why does this seem to work in sample programs? Is the data being written to already allocated blocks of memory that DirectX just manages? B. Is it better to create a contiguous block of memory with all your vertices per a given context, then locking the buffer and pointing it to the already populated vertice list, the drawing? C. What is the basic flow for using vertex buffers to draw various geometry with various textures? Sorry for the rather noobish questions and thanks for your help. :)
dave
dave
Hi bud,

Vertex Buffers:

- When you create a vertex buffer you should know how big you want it to be. memcpy'ing is dangerous to everyone, but it is the only way of getting vertices into a buffer.
- Copying to an area of memory via a void* pointer is always dangerous because it is easy to copy over something else. Above all this it is completely down to the user to make sure that this is all valid copying.

The general flow is to build a vertex buffer with all thr renderable objects in it. Then set the textures and shaders and then using DrawPrimitive() render n amount of triangles from the vertex buffer. It is also important to note that building vertex buffers per-frame is slow. So the entire static scene should be built before the game gets underway.

Now when you become more experienced you should move to TRIANGLESTRIPS instead of TRIANGLLISTS you should also be making an index buffer. Then using DrawIndexedPrimitive. Now that is a rough overview, but using the forum search you should be able to find more information. There is also alot indexed by google and in the DirectX SDK examples and articels.

Hope that helps mate.

ace
ChaosPhoenix
ChaosPhoenix
That did help quite a bit, thanks.

I do have a follow up question.

Obviously only static geometry(such as level meshes and such) are the only things you can keep static from frame to frame. What about character objects and particles? Should you rebuild their vertex buffer each frame? I.e. 1 lock for static geometry, then 1 lock for all dynamic geometry?

I know lock and unlock are expensive operations so you want to limit their calls, but I'm not really sure how you could only do a single lock/unlock per frame.

Thanks again for all the help, I'll make sure to do some more reading in my freetime tonight.
hplus0603
hplus0603
Yes, you typically re-build the vertex buffer of a particle system each frame.

For characters, you can either do that, or you can keep the geometry data in a static buffer and use vertex shader skinning.

When you know you re-build every frame, using DYNAMIC vertex buffers with DISCARD/NOOVERWRITE lock usage is the preferred mechanism; this lets the graphics card "stream" vertices.
enum Bool { True, False, FileNotFound };
Armadon
Armadon
Let's assume a scenario.
A particle system.
A particle system needs to update particles each frame and thus for dead/new particles the buffer that holds them needs to be updated. Dynamic buffers are excellent for this as they are made to do this kind of lock/unlocking operations.

So what you will do is have a vertex buffer of let's say 256 particles.
You will have a batch that you will update/render... let's say 64 and just lock that part of the buffer and do some updating.
A schematic of the vertex buffer.
[0-64-128-192-256]
So for each frame you could lock different parts of the vertex buffer and update and render particles in batches.

I hope this makes some sense.
I hope this helps.
Take care.
ChaosPhoenix
ChaosPhoenix
Okay guys, thanks. That makes sense.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.