The OpenGL ES Programming guide discusses that you should avoid misaligned vertex data and gives an example of aligning the data to 4 byte blocks.
OpenGL ES Programming Guide
However in most demo material and tutorials I've found online I don't see anyone doing this. Example below is from a demo project on 71 Squared:
static const SSTexturedVertexData3D EnemyFighterVertexData[] = {
{/*v:*/{1.987003, -0.692074, -1.720503}, /*n:*/{0.946379, -0.165685, -0.277261}, /*t:*/{0.972816, 0.024320}},
And how would you do it anyway? Add a dummy 0.0 to the v and n and 2 of them to the t?
My guess is that if you're only loading the vertex data once per app it wouldn't matter much but if your binding and rebinding to different arrays on each rendered frame it would matter. My plan is to combine all my arrays into one anyway, but I'm curious.
A vertex attribute is an input variable to a shader that is supplied with per-vertex data. In OpenGL core profile, they are specified as in variables in a vertex shader and are backed by a GL_ARRAY_BUFFER . These variable can contain, for example, positions, normals or texture coordinates.
A Vertex Array Object (VAO) is an OpenGL Object that stores all of the state needed to supply vertex data (with one minor exception noted below). It stores the format of the vertex data as well as the Buffer Objects (see below) providing the vertex data arrays.
General a vertex is made up of floats
which are going to be aligned, so you don't need to get too concerned with that. In fact a lot of books use ordinary struct
s to hold vertex data, which are not guaranteed to be contiguous due to the padding issue, but if all you have are floats, that's fine since you aren't likely to have padding. Of course, some people (myself included) prefer to use the STL vector
from C++ but if you are on iOS, while you can use C++ you are probably using Objective-C thus contiguity in a non-struct way would involve normal C arrays which are not so fun to work with.
If you create custom structures to hold vertex data as well as other data for your drawable object, and you use short
s or other types, you might find padding to become an issue. But if you stick with only float
s you will be fine, and you will be in good company since most educational sources on OpenGL do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With