8

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.

badweasel
  • 2,349
  • 1
  • 19
  • 31
  • 2
    Are you sure the above isn't already aligned to a 4-byte boundary? If the code you show is using GLfloats, those are 32-bit (4 byte) data types, so no matter how many of them there are, they're always aligned to a 4-byte boundary. The guide is referring to shorts and other data types. – Brad Larson Oct 10 '12 at 16:14
  • I was wondering that. Thanks! You should put that as an answer so I can checkmark it. – badweasel Oct 11 '12 at 07:38
  • 2
    I could, but it doesn't answer the more fundamental question of how important byte alignment of vertex data is for performance. That might require some benchmarks to illustrate fully. – Brad Larson Oct 11 '12 at 15:44

1 Answers1

2

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.

Bill Abrams
  • 320
  • 2
  • 9
  • I'm iOS but I avoid the use of objective c structures for anything opengl. I typically use "static const GLfloat Vertex[]" and then load that into a VBO. Strangely, for 2d sprites I actually use a customized excel doc to create all my vertex data. For 3d I import the obj file and save it off as a binary file for quick loading in game. I'm willing to give you the checkmark just for the effort. Brad Larson's point though in the comment on my question was that it's not answerable without testing. Might be a valid point. – badweasel Oct 08 '13 at 11:43
  • Another note.. I hate this about stackoverflow, where you want to answer a question and you search.. then craft your answer only to realize the question is a year or two old. So obviously the answer I used was Brad's that I was already in 4-byte boundaries... a year ago. :) – badweasel Oct 08 '13 at 11:45
  • @badweasel Out of curiousity, what technique are you using in iOS to import obj and then serialize to binary? Are you leveraging GLKit or another library for the conversion, or are you converting the OBJ text data yourself? And then what method are you creating the binary with? – Bill Abrams Oct 08 '13 at 13:24
  • I don't use glkit at all. I wrote my own engine. It's too hard to explain here but if you email me at michael at badweasel dot com I'll tell you how and send you the project file. It's quite a cheat. – badweasel Oct 09 '13 at 00:39