7
\$\begingroup\$

Is it better to have all vertex data in one structure like this:

class MyVertex
{
  int x,y,z;
  int u,v;
  int normalx, normaly, normalz;
}

Or to have each component (location, normal, texture coordinates) in separate arrays/buffers? To me it always seemed logical to keep the data grouped together in one structure because they'd always be the same for each instance of a shared vertex and that seems to be true for things like character models (ex: the normal should be an average of adjacent normals for smooth lighting). One instance where this doesn't seem to work is other kinds of meshes like say a cube where the texture coordinates for each may be the same but that causes them to be different where the vertices are shared.

Are they normally kept separate? Won't this make them less space efficient if there needs to be an instance of texture coordinates and normals for each vertex (they won't be indexed)? Can OpenGL even handle this mixing of indexed (for location) vs non-indexed buffers in the same VBO?

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

It's typical to keep them together. Then when you're creating your VBO, you can easily generate an interleaved VBO with all the data. This is very fast and a typical method when creating VBOs.

I have a few different data structures for vertices. When I know there isn't going to be any data of a specific type, I will leave out that data type and just interleave the data types present. For example, some of my models have bone information interleaved with the vertex data. Others do not. So I have a vertex structure that has that extra information and one that doesn't. I use both and have separate draw calls for each to set the appropriate pointers.

\$\endgroup\$
2
  • \$\begingroup\$ I suppose what I'm trying to figure out is how can you also have the VBO indexed at the same time where one or more of the vertex attributes differs for a shared vertex \$\endgroup\$
    – kiba2
    Dec 18, 2012 at 0:56
  • \$\begingroup\$ If the vertex attributes differ, then it's not a shared vertex. They are just two vertices that share some data. \$\endgroup\$
    – House
    Dec 18, 2012 at 1:00
0
\$\begingroup\$

Given importance of locality of reference for GPU cache, the general consensus is that you should always default to interleaving vertex attributes unless circumstances prevent you from doing so... such as the (semi-)shared circumstances you mention. There are many sources citing this on SO.

Not interleaving becomes important where your data set is very large and you genuinely don't want to waste space / system bus bandwidth specifying interleaved attributes that are optional or could otherwise be deduced from a smaller data set; e.g. in a 10M vertex dataset, if you could use the same value for every group of 4 vertices therein, you'd probably want to do so. But for smaller datasets, the ease of interleaving and repeating your data would override the need for reduction.

Another reason not to interleave is when you want your data modular, for example where geometry (i.e. positions) remain the same, but colour, normal maps etc. change (think human character geometry but textured as, say a recently-turned Borg where implants have grown under the skin).

enter image description here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .