I’m writing a class in C++ that will handle drawing 3D models (ie triangles meshes) and I’m wondering how best to organize my data buffers.
Here’s what I know so far:
-
Using interleaved arrays speeds up the code. It makes use of spatial locality and increases cache hits. You can implement this by organizing vertices into
structs where eachstructholds the position/normal/texcoord/etc. information for that vertex. -
Using indexed arrays decreases memory consumption by storing each distinct vertex/normal/texcoord/etc. only once and then defining faces by references into those arrays, rather than redundantly specifying all of the information for each face. You can also implement this with a
structof indices into lists of vertex attributes.
My question: How should I best make use of both of these? Is it possible to do both? I’ve heard for both that you should always use them when you can, but haven’t found anything concerning using both at once, or one over the other.
My initial solution: I was going to implement a struct that had indices into the data arrays and pass an array of these structs, as well as the data arrays, as VBOs, essentially combining the two.
Am I on the right track? Is there a better way to do this? What should/shouldn’t I do? Is there anything I seem to be unaware of that would impact this decision?
Using interleaved arrays and indexes are not really related. It’s possible to do both things at the same time because they don’t really have much to do with each other.
Using interleaved arrays or not is just a choice if you want to pack your vertices like VTNVTNVTN… (vertex, texture, normal) in a single buffer, or as VVV, TTT, NNN in separate buffers.
Using indexes is just a decision if you have enough repeated vertices to justify the use of the index buffer. When making this decision it’s pretty much irrelevant if you’ve interleaved the vertices or not.
This is illegal. Note that you don’t get to have indices, you only get one index. You can’t sample vertex #0 at the same time you sample texcoord #1. The single index that you supply is the index into all of the buffers.
I suspect this is why you were initially confused, because you thought you could have multiple indices.