Skip to main content
gamedev.net
🔒 Locked

Creating an index buffer

Started by flaXen 6 replies 250+ views
Original Post
flaXen
flaXen
Hmm.. I can handle locking, filling, and unlocking vertex buffers, but I need help with index buffers. The SDK documentation doesn''t help much. How do I specify the format of an index buffer when I create it? How do I lock, fill, and unlock it? Any code samples of this would be most useful. The examples in the SDK mostly just load their meshes from X files. Thanks! -- Dan
Sepsis
Sepsis
No worries pal...here is how it works...:

Basically, and index buffer holds the position of the different vertices in a vertex buffer. So it''s an interger offset in the vertex buffer. Lets say for example we are making a face the looks like a square or rectangle, made up of 2 basic right triangles, and 6 vertices.

If DrawIndexedPrimitive() draws the first triangle, it will start on the left vertex, draw a line to the upper vertex of the left triangle, then connect the upper with teh bottm right vertex, and finally with the two bottom vertices. If it starts drawing the second triangle, it will get the upper left vertex of the second triangle from teh cached data of the vertex buffer. Instead of drawing it twice, the index buffer uses the same vertex for the second triangle. Much faster. After drawing the left upper vertex, DrawIndexedPrimitive() will draw teh fourth and last vertex buffer and then reuse the bottom vertex from teh first triangle... If you have no idea what I just said...lets take a look at some code:

here is your basic Vertex Structure:

{ -1.0f, 1.0f, 0.0f, }, //vertex 0
{ 1.0f, 1.0f, 0.0f, }, //vertex 1
{ 1.0f, -1.0f, 0.0f, },//vertex 2
{ -1.0f, -1.0f, 0.0f,},//vertex 3

That vertex buffer only has to hold 4 vertices instead of 6 because we are using an index buffer.

Now you need to define the order in which your IndexBuffer will be drawn out...like so:

WORD Indices[] = {0, 1, 2, 0, 2, 3};

See...you draw the first vertex(0), then the second(1), then the third(2), then the first again(0), then the third(2), then the fourth(3)...works!

v0-----v1
| |
| |
| |
v3-----v2

Now in your render function:

1) Use your device to set your indices
m_pd3dDevice->SetIndices(m_PIndexBuffer, 0);

2) Last but not least...draw your primitives. This function works the same as the normal DrawPrimitives...you have to tell it how many to draw

m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 4, 0, 2);

4 being the number of vertices
and 2 being the number of primitives


Sepsis
Sepsis
Sorry..forgot to add 1 more thing...

It locks like a normal vertex buffer...just use your indexbuffer variables...
Sepsis
Sepsis
You create it like a normal VertexBuffer...

LPDIRECT3DINDEXBUFFER8
notehaha
notehaha
Could you show me another example that using index buffer as VertexBuffer offset?

Thanx,
Sepsis
Sepsis
Yeah..i''ll send you some code
Sepsis
Sepsis
Yeah..i''ll send you some code
notehaha
notehaha
my e-mail is :
notehaha@hotmail.com

Thanks,

Topic Locked

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

Sign in to reply to this topic.