Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How important is alignment in opengl vertex data in iOS

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

Misaligned vs Aligned vertex data

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.

like image 438
badweasel Avatar asked Oct 10 '12 12:10

badweasel


People also ask

What is vertex attribute in OpenGL?

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.

What is VAO OpenGL?

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.


1 Answers

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 structs 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 shorts or other types, you might find padding to become an issue. But if you stick with only floats you will be fine, and you will be in good company since most educational sources on OpenGL do this.

like image 52
Bill Abrams Avatar answered Oct 19 '22 22:10

Bill Abrams