Search code examples
iosmacosopenglopengl-4

Vertex array object not binding correctly in OpenGL 4.1


I'm in the process of porting some drawing code from OpenGLES 2.0 to OpenGL 4.0, and I'm running issues getting my vertex array object to bind. When I run glValidateProgram I get this error: Validation Failed: No vertex array object bound

Here's the code I'm using for drawing:

void RendererRender(RendererRef* renderer) {

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glUseProgram(program);

    glViewport(0, 0, renderer->viewportWidth, renderer->viewportHeight);
    glClearColor(0.2, 0.3, 0.4, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(M_PI/4.0, 1.0f, 0.1f, 10000.0f);

    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeLookAt(0,0,-10,
                                            0,0,0,
                                            0,1,0);

    glUniformMatrix4fv(modelViewMatrixUniformLocation, 1, GL_FALSE, modelViewMatrix.m);
    glUniformMatrix4fv(projectionMatrixUniformLocation, 1, GL_FALSE, projectionMatrix.m);

    glGetError();

    glBindVertexArray(triangleVAOId);
    if(!validateProgram(program)){
        NSLog(@"Error validating program");
        glGetError();
    }

    glPointSize(10.0f);
    glDrawElements(GL_POINTS, 3, GL_UNSIGNED_SHORT, NULL);

}

So it seems as if the VAO should definitely be bound at this point. The VAO is generated using the following code:

typedef struct {
    GLfloat position[3];
} Vertex;


GLuint buildVAO(int vertexCount, Vertex* vertexData, GLushort* indexData, BOOL hasTexture)
{

    GLuint vaoId;
    GLuint indexId;

    //generate the VAO & bind
    glGenVertexArrays(1, &vaoId);
    glBindVertexArray(vaoId);

    GLuint positionBufferId;

    //generate the VBO & bind
    glGenBuffers(1, &positionBufferId);
    glBindBuffer(GL_ARRAY_BUFFER, positionBufferId);

    //populate the buffer data
    glBufferData(GL_ARRAY_BUFFER,
                 vertexCount*sizeof(Vertex),
                 vertexData,
                 GL_DYNAMIC_DRAW);

    //setup the attribute pointer to reference the vertex position attribute
    glEnableVertexAttribArray(kVertexPositionAttributeLocation);
    glVertexAttribPointer(kVertexPositionAttributeLocation,
                          kVertexSize,
                          kPositionVertexTypeEnum,
                          GL_FALSE,
                          sizeof(Vertex),
                          (void*)offsetof(Vertex, position));

    }

    //create & bind index information
    glGenBuffers(1, &indexId);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexId);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, vertexCount*sizeof(GLushort), indexData, GL_DYNAMIC_DRAW);

    //restore default state
    glBindVertexArray(0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    return vaoId;
}

The other thing that I'm noticing is that the VAO id I get back from glGenVertexArrays is a large value, like 1606408096, where in the example I have working it's always something like 1 or 2.

This is all happening on the main thread.

Any idea what could be going wrong?


Solution

  • Figured it out - it turns out it was because I was using the wrong version of glGenVertexArray.

    I had this declaration earlier:

    #define glGenVertexArrays glGenVertexArraysAPPLE
    

    It turns out this is only necessary on OpenGL versions before 3.0.