顶点数组对象在OpenGL 4.1

时间:2015-08-18 08:39:28

标签: ios macos opengl opengl-4

我正在将一些绘图代码从OpenGLES 2.0移植到OpenGL 4.0,我正在运行让我的顶点数组对象绑定的问题。当我运行glValidateProgram时,我收到此错误:Validation Failed: No vertex array object bound

这是我用于绘图的代码:

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);

}

因此,似乎VAO在这一点上肯定会受到限制。使用以下代码生成VAO:

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;
}

我注意到的另一件事是我从glGenVertexArrays回来的VAO ID是一个很大的值,比如1606408096,在这个例子中,我的工作总是像1或2。

这一切都发生在主线上。

知道可能出现什么问题吗?

1 个答案:

答案 0 :(得分:2)

想出来 - 事​​实证明这是因为我使用了错误版本的glGenVertexArray

我之前有这个声明:

#define glGenVertexArrays glGenVertexArraysAPPLE

事实证明,这仅适用于3.0之前的OpenGL版本。