why use vertex buffer and index buffer

Started by
6 comments, last by PAiNIC 18 years, 7 months ago
drawing using a vertex buffer and index buffer is really confusing me. it is known that one of the advantages of using this method is that vertices could be shared by triangles and this would save memory. that is correct but what is confusing is that shared vertices might not have the same texture coordinates and same normals. before u draw from a vertex buffer u should specify the vertex format which means assigning it x,y,z (this is ok since it is the same), nx, ny, nz (this might differ between the two adjacent triangles), texture coordinates may also differ so u need to repeat the vertices again to take the above differences into account and this would defeat the purpose of using vertex buffers. I am trying to load an MS3D model, it saves the vertices into one place then it index these vertices when defining the triangles. each triangle has 3 sets of normals and texture coordinates for each corner and 3 indexes for the actual vertices as mentioned earlier. I can not just copy the vertices into a vertex buffer since a vertex may be shared by two triangles with different tex coordinates. the solution is create a vertex buffer that contains the whole set of the model's vertices. if that is the case why use indexed vertex buffer from the very beginning ? thanks
Advertisement
I can't say I know everything about this topic but I do have some ideas:
For the MS3D model-specific case, the index buffers take advantage of any vertices that do have the same position, normal and texture co-ordinates and also control the winding order for each triangle. I would expect that in the loaded vertexbuffer you would still find some vertices that have the same position, but different normals and/or texture co-ordinates.

As for why use indexed vertices instead of just vertex buffers, I'd presume it would be a memory thing. It takes less memory to store a number of vertices and an array of unsigned integers representing offsets into the vertices, than it would to store a bunch of duplicate vertices with position(3*32), normal(3*32) and texture coords(2*32) (256 bits per vertex compared to 32 bits per index).

Of course, index buffers are most useful if you DO have duplicate vertices and you want to avoid re-using them. For something like an MS3D model, where you're not sure exactly what vertices you're using, it'd be better to stick with vertex and index buffers I think. There's little to no performance difference between the two.
I have a model with ~500 vertices and ~3000 index. look at the numbers : from 3000 vertex to 500 vertex, this is a big saving but all of this is screwed because u can not create a vertex buffer of 500 vertex simply because u can not draw them unless they all have the same vertex format which means if more than one vertex are shared by a triangle must have the same texture coordinates and normals and this is not always the case. then u find yourself forced to create a buffer of 3000 vertices. at this case it does not make any sense to use indices since u r using the whole set. if u can convert the 500 to something between 500 and 3000 something like 1500, then it makes sense but I do not think this is doable.
The agp will perform some cache optimisations when using indexbuffers.
If you cannot share vertexes, then using indexes will gain nothing. However, models with no shared vertices are unusual.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Typically the normals and UVs will be shared from one triangle to the next. Without shared normals, you won't get smooth lighting as you pass from one triangle to the next. Without shared UVs, you won't get seamless texturing.

A difference in UVs and normals often occurs at a material change, or a sharp edge, such as going from one face of a cube to another. This is usually a very small percentage of vertices.

It doesn't take much before indicies pay off. Consider a cube. 6 faces, each with 2 triangles.

Non indexed = 6 faces * 3 vertex * 2 triangles per face = 36 vertices
Indexed = 6 faces * 4 vertices per face = 24 vertices.

Assume a fairly standard vertex size of 32 bytes. We need 8*32 bytes to store the extra vertices. Assume a fairly standard index size of 2 bytes. We need 36*2 bytes to store indices.

Even with a cube, you still save memory, and use fewer vertices. Rendering will also be faster as you transform less vertices because of the post-vertex-shader cache.
can I assume that shared vertices in a mesh group in ms3d have the same texture coordinates and normals ?
This depends on what MS3D file you're loading. Because MS3D is a model editor, some models will have vertices with the same position, normal and texcoords that haven't been welded together by the artist. In this case, MS3D will not implicitly treat these vertices as shared and will place both into the vertex buffer. All the primitive types (cubes, geospheres etc.) will be created with shared vertices and indexed, but user-created vertex geometry must have duplicate vertices welded. Hope that helps :)

This topic is closed to new replies.

Advertisement