OpenGL Index Buffers

Recommended Posts

Well I finally got everything to render how I want it without the use of vertex buffers, shaders, etc. but now I want to speed things up a bit. Someone described to me "index buffers":
Quote:
 for a small example. lets say that you had a cube. this cube will have 8 verts and 12 triangles. if we were just to send each triangle down by itself, we would send the 3 verts it has and transform it and render the triangle. which will end up doing 24 transformations in order to render the cube. which in some cases this leads to extreme amount of replicated work. what we would like to do is only process each vertex once and reuse the work that we did. which is where index buffers come in Oh, that makes sense. so we can just send an array of 8 verts down and process each once. then when we need to render a triangle we instead specify each as 3 indexes into the array of 8 verts that we already processed Can these two (vertex buffers and index buffers) both be used at once? yes, you have to in order to index. so what you do is set up a vertex buffer the size of the total verticies in the object. and then a separate index buffer to specify the triangles. or a vertex buffer can be used by itself and just specify the triangles inline, but as I mentioned this will replicate some data
I have a few questions about index buffers being used with vertex buffers. 1) Where is everything stored while/after being transformed? So far I've only used glVertex and so I have to send the vertices for each triangle in groups of 3 - if the 3 vertices making up a triangle aren't sent one after another, the wrong triangle will be rendered. With index buffers being used with vertex buffers (correct me if I'm wrong) it sounds like you'd have to transform all the vertices for an object and store them to be used later when you render the triangles. How does this work? 2) What is the best way to store each vertex in files and RAM? I can think of two ways: the first is to have each vertex consist of position, normal, and texture coords, and have each triangle have 3 indexes for its vertices. The second way is to store 3 arrays: position, normal, and texture coords. Each triangle would have 9 indexes in this case, but it would seem to greatly reduce the number of duplicated componants. With the first method, if a vertex was shared by 3 triangles but the triangles were flat-shaded, since the normals would be different for them all then 3 different vertices would have to be transformed completely. However with the second way, since the normals would be the only difference, the position would not have to be transformed 3 times. Since I haven't used index buffers, I don't know which is best, or possible to do, or if I'm looking at this entirely the wrong way. 3) Also, the person who explained index buffers uses DirectX and not OpenGL. If anyone could point me to the OpenGL API for index buffers that'd be great.

Share on other sites
1) It is not stored in any permanent way. Why would you want to store it?

http://www.opengl.org/wiki/index.php/GL_ARB_vertex_buffer_object

Share on other sites
I don't want to store it permanently, but if a vertex is going to be used several times in different triangles, you have to temporarily store its transformed position, right? Also thanks for the link, I'll have a look at it right now.

Share on other sites
The driver/GPU temporarily stores previously processed vertex data in it's cache. It uses this cache to avoid multiple processing of the same data. But the it's size is limited so it can only do this if the vertex is close. I mean that in the indices list matching indexes have to be as close to each other as it can be. So if you have and IndexList of (0, 1, 2, 1, 2, 3, 3, 4, 1, ...) or similar than the driver/GPU can take advantage of the cached vertices. If you have a list like (0, 1, 2, 3, 4, ... a lot of index that not mach at all ... 1, 4, 3, 2, 234, 2 ...) then the driver/GPU can't take advantage of the cache, as it will long forget about the previously processed 1st, 2nd, 3rd and so on vertices.

Share on other sites
So index buffers only really help performance if the triangles that share vertices are drawn one after another?

Share on other sites
Yes, it's a very small cache.
It varies from GPU. 16 vertices for Geforce1/2, 24 vertices for Geforce 3/4.
I think it's 32 geforce fx, 48 geforce 6xxx

nVidia has released this lib to optimize meshes
http://developer.nvidia.com/object/nvtristrip_library.html

Share on other sites
Alright, so I've gotten to the point where I'm actually trying to implement these things. Vertex buffers seem fairly easy, I just looked through the NeHe tutorial, but I'm not sure how to do index buffers.

I have 3 buffers set up: vertex data, normal data, and texture coord data. Then I created a 4th buffer to store the vertex, normal, and texture IDs for each triangle. How do I draw the correct vertices, normals, and textures, from the 4th buffer?

By the way, NeHe states that "you should be aware that normals operate "one for one" with vertices, meaning that if you are using normals, each vertex should have an accompanying normal". Does this mean that I can't store the vertices seperate from the normals? If so then I will have to include the vertices and normals into one buffer and I'd prefer to keep them seperate so I can reduce memory usage if possible. Also it would lead to much more duplication of vertex and normal data which I wouldn't like very much.

Or does this just mean that each normal goes with a vertex, not with a triangle? If that is the case (which now I think it is because I noticed GL_NORMAL_ARRAY) I will be happy. =)

Share on other sites
1) The vertices are transformed on the fly. Graphics hardwares are modeled after a conceptual rendering pipeline. A rendering pipeline is composed of several stages, each of which may have a pipeline in itself. Why do we have pipelines you might ask? Well, the discussion is somehow lengthy and so I leave it to another time. But for our purposes, all you need to know is that each stage has its own set of responsibilities. The Geometry stage is responsible for transforming the geometry (among MANY other things). For triangle lists, it conceptually follows the following algorithm:
// Note: Every trio of indices in the index buffer represents a triangleFor every trio of indices (triangle) in the index buffer:  For every index of this trio:    Check the post TnL vertex cache for this index. Each index references one    vertex and one vertex only. If the index exists in the cache, the    vertex that it references has been transformed lately so we don't need to    re-transform it. The transformed vertex is stored in the cache.    If the vertex is inside the post TnL vertex cache      Fetch the transformed vertex and skip to the next iteration of the loop    else      Check the pre-transform vertex cache to see if the untransformed vertex      that we are going to transform has been fetched before, so we don't need      to re-fetch it from memory. This is totally different from the post TnL       cache (see the description below)          If the untransformed vertex is inside the pre-transform vertex cache        Fetch the untransformed vertex from the cache. This saves us a memory        access.      else        Fetch the untransformed vertex from video/AGP memory since we couldn't        find it inside the cache; look inside the vertex buffer and find the         untransformed vertex that this index references.      Now that we have the untransformed vertex, transform it and store the      results in the post TnL cache.

Note that there is nothing magical with a cache. A cache is simply a high performance memory that stores the results of a computation (or a memory access) in order to decrease a delay. They can be used anywhere. Actually we use them all the time. A simple variable that stores the results of a computation is a simple cache, sine we would have otherwise needed to recompute everything had we not stored the results in that variable. To that end, there's also another small cache, called the pre-transform cache (as oposed to post TnL or post-transform cache) which stores the vertices that have been lately fetched from memory.

So to sum it up:
The pre-transform vertex cache is a cache that stores the results of a memory access. It stores untransformed vertices that have been lately referenced by an index. To make good use of this cache, reorder your vertex buffer so vertices are relatively stored in the order that they are referenced. This is the cache that people mostly neglect.

The post-transform (also called post TnL) vertex cache is a cache that stores the results of a computation. It stores transformed vertices after the transformation pipeline is done with them. To make good use of this cache, reorder your index buffer so triangles are stored in an order which makes them share as many vertices as possible. This is the cache that people are mostly referring to when they refer to vertex caches in general.

Pretty confusing, isn't it? :) Just give it some time.

2) I suspect that the first approach yields better results, as its a better use of pre-transform cache.

Quote:
 Original post by V-manIt varies from GPU. 16 vertices for Geforce1/2, 24 vertices for Geforce 3/4.I think it's 32 geforce fx, 48 geforce 6xxxnVidia has released this lib to optimize mesheshttp://developer.nvidia.com/object/nvtristrip_library.html

I doubt there are any cards that have caches larger than 32 entries. As Hugues Hoppe showed in "Optimization of mesh locality for transparent vertex caching", its very unlikely that any hardware benefits from caches larger than 32 entries.

Besides, triangle strips are really outdated. I haven't used NvTriStrip, but it's unlikely that stripification of geometry yields better results than triangle lists that are optimized for good cache coherency.

[Edited by - Ashkan on January 4, 2008 2:52:17 AM]

Share on other sites
Quote:
 Original post by GumgoAlright, so I've gotten to the point where I'm actually trying to implement these things. Vertex buffers seem fairly easy, I just looked through the NeHe tutorial, but I'm not sure how to do index buffers.I have 3 buffers set up: vertex data, normal data, and texture coord data. Then I created a 4th buffer to store the vertex, normal, and texture IDs for each triangle. How do I draw the correct vertices, normals, and textures, from the 4th buffer?By the way, NeHe states that "you should be aware that normals operate "one for one" with vertices, meaning that if you are using normals, each vertex should have an accompanying normal". Does this mean that I can't store the vertices seperate from the normals? If so then I will have to include the vertices and normals into one buffer and I'd prefer to keep them seperate so I can reduce memory usage if possible. Also it would lead to much more duplication of vertex and normal data which I wouldn't like very much.Or does this just mean that each normal goes with a vertex, not with a triangle? If that is the case (which now I think it is because I noticed GL_NORMAL_ARRAY) I will be happy. =)

Use the link that I gave you. It explains a common usage pattern.
4th buffer? Don't put texture ID in a vertex buffer.

Each vertex should have 1 normal, 1 texcoord.
Example, you can't have 5 vertex, 2 normals, 3 texcoords. Have 5 of each.

The link I gave also shows how to make an index buffer.

Quote:
 Besides, triangle strips are really outdated. I haven't used NvTriStrip, but it's unlikely that stripification of geometry yields better results than triangle lists that are optimized for good cache coherency.

I didn't say to use or not to use GL_TRIANLE_STRIP.
You can use GL_TRIANGLES if you want.

Share on other sites
Quote:
 Each vertex should have 1 normal, 1 texcoord.Example, you can't have 5 vertex, 2 normals, 3 texcoords. Have 5 of each.

Hmm, okay. Well, I guess I misunderstood a bit and this complicates things some, but I'll have another look at the link.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628364
• Total Posts
2982269
• Similar Content

• Hi all,

I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?

• Hi, New here.
I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc....
I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn?
How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want?
Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn.
So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started.
Thanks,
Dejay Hextrix
• By mellinoe
Hi all,
First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
• By aejt
I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
I have these classes:
For GPU resources:
Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).
And my plan is to define everything into these XML documents to abstract away files:
Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
Factory classes for resources:
For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
Factory classes for assets:
Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).

Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
Thanks!
• By nedondev
I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
Thanks.

• 10
• 9
• 13
• 24
• 11