Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Struggling With Vertex And Index Buffers In Direct3D

I've tried for many months to learn how IDirect3DVertexBuffer9 and IDirect3DIndexBuffer9 work. I've read multiple books, e-books and forums and I still can't get the hang of how they work. Can somebody help me understand how they work and how they link together?

PS: I've tried searching through the related questions but nothing interested me.

Thanks.

like image 819
DeadCapacitor Avatar asked Feb 03 '11 14:02

DeadCapacitor


People also ask

What are index buffers?

An index buffer is essentially an array of pointers into the vertex buffer. It allows you to reorder the vertex data, and reuse existing data for multiple vertices.

What is constant buffer directx?

A constant buffer allows you to efficiently supply shader constants data to the pipeline. You can use a constant buffer to store the results of the stream-output stage.

What is vertices and indices?

A vertex buffer can be thought of as an array of vertices, so the VB Index is simply the index into the vertex buffer for the target vertex. Similarly, an IB Index is an index into the index buffer.


2 Answers

This confused me at first too. Another way to think about this is visually (I'm a big visual thinker so maybe this will help you as well).

Expanding on zezba's example, let's say we want to draw a quad using two triangles:

Quad

As s/he pointed out, this can be done with just four vertices. So your vertex buffer would contain only FOUR entries. I'll label these {A, B, C, D}:

Quad with vertices: A, B, C, D

However, since graphics processors deal with triangles, we still need to define groupings of THREE vertices to tell the GPU how to create triangles out of the list of vertices already defined. This is the purpose of the index buffer.

You can think of the index buffer simply as a list of indices INTO the vertex buffer that defines triangles. So since we are forming two triangles, and each triangle needs three vertices, the index buffer will need SIX entries.

Ordering matters a bit here too. I won't go too much into that but let's just say I want to define my triangles counter clockwise. I'll define my two triangles as {B, A, C} and {B, C, D}. It's perfectly fine to reuse vertices for multiple triangles.

So my buffers end up looking like this:

Quad with vertex and index buffers

Hope this helps.

like image 74
Mark Hazlewood Avatar answered Oct 06 '22 03:10

Mark Hazlewood


IndexBuffers are used for memory & speed optimizations. A indexBuffer is a list of indices that index the vertices in the vertexBuffer.

So say I'm going to render a Flat Quad on the screen with 2 triangles. Each triangle takes up 3 vertices, so to render the quad with just a VertexBuffer I will need 6 vertices.

Now if I use a IndexBuffer though, I would only need to use 4 vertices(one for each corner of the quad). But I would need 6 indices, 3 indices for each triangle, that will index one of the corner vertices.

On large models this can save memory & greatly improve speed as the GPU will be processing fewer vertices.

Here is a site with some good sample code:: http://www.codesampler.com/dx9src.htm In there download the sample called "Indexed Geometry".

like image 27
zezba9000 Avatar answered Oct 06 '22 04:10

zezba9000