I have some vertex data. Positions, normals, texture coordinates. I probably loaded it from a .obj file or some other format. But each piece of vertex data has its own index. Can I render this mesh data using OpenGL/Direct3D?

1 Answers 11

up vote 58 down vote accepted

In the most general sense, no. OpenGL and Direct3D only allow one index per vertex; the index fetches from each stream of vertex data. Therefore, every unique combination of components must have its own separate index.

So if you have a cube, where each face has its own normal, you will need to replicate the position and normal data a lot. You will need 24 positions and 24 normals, even though the cube will only have 8 unique positions and 6 unique normals.

Your best bet is to simply accept that your data will be larger. A great many model formats will use multiple indices; you will need to fixup this vertex data before you can render with it. Many mesh loading tools, such as Open Asset Importer, will perform this fixup for you.

GL 3.x and D3D10

For D3D10/OpenGL 3.x-class hardware, it is possible to avoid performing fixup and use multiple indexed attributes directly. However, be advised that this will likely decrease rendering performance.

The following discussion will use the OpenGL terminology, but Direct3D v10 and above has equivalent functionality.

The idea is to manually access the different vertex attributes from the vertex shader. Instead of sending the vertex attributes directly, the attributes that are passed are actually the indices for that particular vertex. The vertex shader then uses the indices to access the actual attribute through one or more buffer textures.

Attributes can be stored in multiple buffer textures or all within one. If the latter is used, then the shader will need an offset to add to each index in order to find the corresponding attribute's start index in the buffer.

Regular vertex attributes can be compressed in many ways. Buffer textures have fewer means of compression, allowing only a relatively limited number of vertex formats (via the image formats they support).

Please note again that any of these techniques may decrease overall vertex processing performance. Therefore, it should only be used in the most memory-limited of circumstances, after all other options for compression or optimization have been exhausted.

OpenGL ES 3.0 provides buffer textures as well.

8 upvote
Best information about this subject I have found ! Thanks – Ray Hulha
There's an article about this here (Chapter 21 - Programmable Vertex Pulling), but it's not directly accessible. There is code though. – jozxyqk
Is this slow because it's not sequential access of buffer? – Samik
@Samik: Indexed access of any kind is going to be non-sequential; that's kinda the point. The performance difference usually comes into play for hardware that has actual hardware support for vertex pulling. AMD's GCN-based architecture does not, so they have to patch your vertex shader based on your VAO in order to create the illusion of having hardware vertex pulling. So doing it manually yourself probably won't slow you down any. – Nicol Bolas
You don't need buffer textures. You can use regular textures. In other words you can do this in DirectX9, OpenGL 2.1. Live example here //allinonescript.com/a/22009385/128511 Note: not saying you should do this. Only that it's fully possible. – gman

Not the answer you're looking for? Browse other questions tagged or ask your own question.