VAO在macOS上呈现正常,但在Linux上呈现INVALID_OPERATION

时间:2017-07-12 14:54:44

标签: c++ linux macos qt opengl

我有一个在Qt Widgets应用程序中使用OpenGL的应用程序。我曾经在macOS上开发它,在切换到Linux之前一切正常。现在当我调用glBindVertexArray(mesh->getVao());时,OpenGL会吐出INVALID_OPERATION。

使用AMD的CodeXL,我确定mesh->getVao()已加入2。我还用它来获取所有OpenGL调用的列表。在检查它时,似乎生成ID为2的VAO,并且呼叫列表中没有glDeleteVertexArrays。我甚至注释掉了删除顶点数组的代码。

The call list, when it generates the VAO

OpenGL文档声明glBindVertexArray失败的唯一原因是,如果没有给出零或生成的VAO。

尽管VAO存在,glBindVertexArray可以吐出INVALID_OPERATION还有其他可能的原因吗?为什么它可以在macOS上工作但在Linux上不能工作?

一些代码示例,如果有帮助

网格渲染

void renderMesh(const Resource::ResourceMesh *mesh) {
    //Set up textures
    for (unsigned int i = 0; i < mesh->getTextures().size(); i++) {
        glActiveTexture(GL_TEXTURE0 + i);
        mesh->getTextures().at(i)->getTexture()->bind();
    }
    static const int texIDs[32] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
    glUniform1iv(shaderTexID, MAX_SHADER_TEXTURES, texIDs);

    //Draw the mesh
    glBindVertexArray(mesh->getVao());
    qDebug() << "mesh == nullptr:" << (mesh == nullptr);
    glDrawElements(GL_TRIANGLES, mesh->getIndices().size(), GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}

VAO生成

//In ResourceMesh.hpp

protected:
    QVector<Model::Vertex> vertices;
    QVector<unsigned int> indices;
    QVector<Resource::ResourceTexture*> textures;

    GLuint vao;
    GLuint vbo;
    GLuint ebo;

//In ResourceMesh.cpp

void ResourceMesh::generateGlBuffers() {
    glGenVertexArrays(1, &vao);
    glGenBuffers(1, &vbo);
    glGenBuffers(1, &ebo);

    glBindVertexArray(vao);

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Model::Vertex), &vertices[0], GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);

    //Vertex positions
    glEnableVertexAttribArray(GLManager::VertexAttribs::VERTEX_POSITION);
    glVertexAttribPointer(GLManager::VertexAttribs::VERTEX_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Model::Vertex), (void*) offsetof(Model::Vertex, position));

    //Vertex normals
    glEnableVertexAttribArray(GLManager::VertexAttribs::VERTEX_NORMAL);
    glVertexAttribPointer(GLManager::VertexAttribs::VERTEX_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(Model::Vertex), (void*) offsetof(Model::Vertex, normal));

    //Vertex texture coordinates
    glEnableVertexAttribArray(GLManager::VertexAttribs::VERTEX_TEX_COORD);
    glVertexAttribPointer(GLManager::VertexAttribs::VERTEX_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(Model::Vertex), (void*) offsetof(Model::Vertex, texCoord));

    glBindVertexArray(0);
}

如果有帮助,这里是CodeXL的屏幕截图,在glBindVertexArray打破

AMD CodeXL

1 个答案:

答案 0 :(得分:2)

在评论中解决 - 结果是在错误的上下文中创建了VAO。我想Qt的macOS实现共享一切的上下文,而在Linux上,它们是分开的。