OpenGL ES Xcode Template Example -- Indices

Discussion in 'iOS Programming' started by Blakeasd, Jun 16, 2013.

  1. Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello,

    In the default Xcode 4.x OpenGL ES iOS template, why doesn't Apple use indices when drawing the two cubes? How does OpenGL know what order to draw the coordinates. Also, Apple uses normal values in with the vertex data -- how does OpenGL know what is a normal coordinate vs a vertex??

    Thanks!
     
  2. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #2
    They don't use indices because they always draw both cubes. Arrays of indices let you hop around in your index data, "connecting the dots" in different ways. That isn't needed in this case.

    The drawing is done with glDrawArrays, which draws all the triangle data that was set up.

    The vertex data in normals are interleaved because that makes rendering much faster. That gives the GPU locality of reference when it loads the data to draw a given triangle. If you had separate arrays of vertexes and normals the GPU would have to load the vertexes for a triangle from on area of memory, and the normal from another, which means it would have to load from main video memory to cache memory much more frequently.

    It knows where the vertex data is vs where the normal data is because the program tells it in the setupGL method.

    Specifically the paired calls to glEnableVertexAttribArray and glVertexAttribPointer tell it first where the data for the vertexes is located at each entry in the array, and then where the normals are located. Note the "BUFFER_OFFSET" macros at the end of each call to glVertexAttribPointer. Those specify offset into each entry in the vertex array where the vertex data and normal data are located.
     

Share This Page