提问者:小点点

C++OpenGL,为什么顶点数组不绑定顶点缓冲区?


我很确定顶点数组没有绑定顶点缓冲区,因为如果我注释掉取消绑定顶点缓冲区的那一行,它工作得非常好,这表明顶点数组没有正确绑定顶点缓冲区。 下面是代码(程序和窗口有一些抽象,但与问题无关):

GLuint va;
glGenVertexArrays(1, &va);
glBindVertexArray(va);

GLuint vb;
glGenBuffers(1, &vb);
glBindBuffer(GL_ARRAY_BUFFER, vb);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, (2 + 3) * sizeof(float), nullptr);
glBindAttribLocation(program.id(), 0, "i_position");
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, (2 + 3) * sizeof(float), (const void*)(2 * sizeof(float)));
glBindAttribLocation(program.id(), 1, "i_color");

glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0); //< if this line is commented out it works perfectly fine

program.bind();

while(window->isOpen())
{
    glfwPollEvents();

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glBindVertexArray(va);
    glBufferData(GL_ARRAY_BUFFER, 3 * (2 + 3) * sizeof(float), vertexes, GL_DYNAMIC_DRAW);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    window->update();
}

有人知道我做错了什么吗?


共1个答案

匿名用户

VAO不存储缓冲区绑定。 它只存储哪个缓冲区绑定到哪个属性。 如果需要缓冲区绑定本身(例如,对于GLBufferData),则必须自己绑定缓冲区。

还要注意,必须在链接程序对象GET之前调用GLBindatTribLocation

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++opengl|顶点|数组|绑定|顶点|缓冲区)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?