首页 > 解决方案 > 在 DSA 代码的更新循环中是否使用 glBindVertexArray

问题描述

我正在将一些opengl代码转换为DSA,并且在任何地方都编写了不绑定顶点数组,因为它在DSA中继承。所以我用

GLuint VBO, VAO, indexBuffer;
glCreateVertexArrays(1, &VAO);
glCreateBuffers(1, &indexBuffer);
glCreateBuffers(1, &VBO);
...

而不是 glGen... 对应物。

但是现在我只是想知道是否有在更新循环中绑定顶点数组的 DSA 版本。我现在有

    while (!glfwWindowShouldClose(window)) {
       glfwPollEvents();

       glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
       glClear(GL_COLOR_BUFFER_BIT);
       // Draw the triangle
       ourShader.Use();
       const auto Textureloc = glGetUniformLocation(ourShader.Program, "ourTexture");
       glBindTextureUnit(Textureloc, texture);

         glBindVertexArray(VAO);
         glDrawElements(GL_TRIANGLES, 9, GL_UNSIGNED_INT, 0);
         glBindVertexArray(0);
         glBindTextureUnit(0, 0);

       glfwSwapBuffers(window);
   }

这很好用。但是,如果我不将 替换glBindVertexArray为其他东西,我会在以后的编码中遇到一些有趣的问题吗?就像我替换glBindTextureglBindTextureUnit

标签: openglopengl-4vertex-array

解决方案


总结一下,按照问题中的示例编写更新循环是完全正确的。在更新循环中使用VAO时应该绑定它。但是当您使用 DSA 时,您在修改它们时不需要绑定 VAO(也不是 VBO 或索引缓冲区)。因此,没有其他选择glDrawElements可以像 eg 这样将 VAO id 作为参数glVertexArrayElementBuffer


推荐阅读