问题描述
我一直在阅读有关VAO如何与VBO一起工作的信息.据我了解,当我们调用glVertexAttribPointer时,VAO仅存储有关VBO的状态信息.我的问题是,什么时候它会存储有关EBO的状态(用于索引图形),当调用glVertexAttribPointer时,它是否同时保存两种状态?
I've been reading a bit about how VAOs work with VBOs. As far as I understand, the VAO only stores state information about a VBO when we call glVertexAttribPointer. My question is, when does it store state about an EBO (for indexed drawing), does it save state for both when glVertexAttribPointer is called?
//Vertices of the rectangle
std::vector<GLfloat> vertices
{
0.5f, 0.5f, 0.0f, // Top Right
0.5f, -0.5f, 0.0f, // Bottom Right
-0.5f, 0.5f, 0.0f, // Top Left
-0.5f, -0.5f, 0.0f, // Bottom Left
};
//Indices of the triangle
std::vector<GLuint> indices
{
0, 1, 3,
0, 3, 2
};
GLuint VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO); //Bind the VAO
//Bind the buffers
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0); //Unbind the VAO
//Supply Index Buffer information
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
//Custom shader class that stores the program to be used at render time
Shader shader("..path_to_shader\\shaders\\vertexshader.vert", "..path_to_shader\\shaders\\fragmentshader.frag");
while (!glfwWindowShouldClose(window))
{
glUseProgram(shader.Program());
glBindVertexArray(VAO); //Bind the VAO to draw.
glClearBufferfv(GL_COLOR, 0, &color[0]);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
glfwPollEvents();
glfwSwapBuffers(window);
glBindVertexArray(0);
glUseProgram(0);
}
推荐答案
来自 opengl.org :
对于GL_ARRAY_BUFFER
,当您调用glVertexAttribPointer
时,VAO将保存绑定.基本上是因为GL_ARRAY_BUFFER
绑定不是VAO状态的一部分.因此,调用glBindBuffer(GL_ARRAY_BUFFER, vertexBufferHandle)
不会对VAO的状态产生任何作用.
For GL_ARRAY_BUFFER
the VAO will save the binding when you call glVertexAttribPointer
. It is basically because the GL_ARRAY_BUFFER
binding is not part of the VAO's state. So calling glBindBuffer(GL_ARRAY_BUFFER, vertexBufferHandle)
won't do anything with the VAO's state.
对于GL_ELEMENT_ARRAY_BUFFER
并非如此:VAO(如果已绑定)将在您调用glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferHandle)
时将索引缓冲区绑定保存到其状态.
For GL_ELEMENT_ARRAY_BUFFER
it's not the case:the VAO (if bound) will save your index buffer binding to it's state when you call glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferHandle)
.
关于OpenGL 索引缓冲和 opengl-tutorial.org 上的另一基本内容.
There are great tutorials about OpenGL indexed buffering and another basic stuff on opengl-tutorial.org.
这篇关于VAO和元素缓冲区对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!