ios - OpenGL 4.1 中顶点数组对象未正确绑定(bind)

标签 ios macos opengl opengl-4

我正在将一些绘图代码从 OpenGLES 2.0 移植到 OpenGL 4.0,并且在绑定(bind)顶点数组对象时遇到问题。当我运行 glValidateProgram 时,出现此错误:验证失败:没有绑定(bind)顶点数组对象

这是我用于绘图的代码:

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 .

这一切都发生在主线程上。

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

最佳答案

弄清楚了 - 事实证明这是因为我使用了错误版本的 glGenVertexArray

我之前有过这样的声明:

#define glGenVertexArrays glGenVertexArraysAPPLE

事实证明,只有 OpenGL 3.0 之前的版本才需要这样做。

关于ios - OpenGL 4.1 中顶点数组对象未正确绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32067530/

相关文章:

ios - SpriteKit 移动到 X :duration from Current Stat

macos - webView不适用于osx

java - UnicodeFont 渲染似乎阻止了其他所有内容的渲染?

c++ - glDrawArrays 访问冲突

ios - 使用适用于 iOS Swift 3 的 REST API 从 AZURE 下载文件

ios - 使用 UIActivityViewController 在 Swift 3 中的 WhatsApp 上共享实时照片不起作用

c - pcap_loop 函数不是 "reactive"。为什么?

opengl - DX9 和 DX10) : what is the default pixel (and vertex) shader?(如果可能,还有 OpenGL。)

ios - 无法快速在 View Controller 之间传递值

macos - 如何在cocoa中获取正确的IPv6地址