OpenGL ES: Vertex Buffer Objects and Vertex Array Objects

Discussion in 'iOS Programming' started by Blakeasd, Jul 11, 2013.

  1. Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #1
    Hello,

    I am following this tutorial for rendering a cube in OpenGL ES 2 with GLKit and custom shaders. I am having some trouble understanding some things. Take a look at this code snippet:

    Code:
     glGenBuffers(1, &_verticesVBO); 
        glBindBuffer(GL_ARRAY_BUFFER, _verticesVBO); 
        glBufferData(GL_ARRAY_BUFFER, sizeof(CubeVertexData), CubeVertexData, GL_STATIC_DRAW);     
        glBindBuffer(GL_ARRAY_BUFFER, 0);     
        
        glGenBuffers(1, &_indicesVBO); 
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indicesVBO); 
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(CubeIndicesData), CubeIndicesData, GL_STATIC_DRAW);     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);    
        
        GLint attribute;
        GLsizei stride = sizeof(GLfloat) * 3;
        glGenVertexArraysOES(1, &_VAO); 
        glBindVertexArrayOES(_VAO); 
        
        glBindBuffer(GL_ARRAY_BUFFER, _verticesVBO); 
        attribute = glGetAttribLocation(_program, "Position"); 
        glEnableVertexAttribArray(attribute);
        glVertexAttribPointer(attribute, 3, GL_FLOAT, GL_FALSE, stride, NULL);
    
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indicesVBO);
        glBindVertexArrayOES(0);
    
    
    
    How does the glVertexAttribPointer know what array (there is an indices array and a vertex array) to use for the Position attribute? Most of the code that I've done involves using the last parameter to pass in the array. In this tutorial it's NULL.

    Any assistance is much appreciated.

    Thanks!
     
  2. lastcall macrumors member

    lastcall

    Joined:
    Jan 10, 2013
    #2
    The docs tell you:
    Whatever buffer is currently bound to GL_ARRAY_BUFFER.
     
  3. Blakeasd thread starter macrumors 6502a

    Joined:
    Dec 29, 2009

Share This Page