我一直在阅读一些有关VAO与VBO一起工作的信息。据我了解,当我们调用glVertexAttribPointer时,VAO仅存储有关VBO的状态信息。我的问题是,当调用glVertexAttribPointer时,它何时存储有关EBO的状态(用于索引图形)?
//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:
顶点数组对象(VAO)是OpenGL对象,它存储提供顶点数据所需的所有状态(下面有一个小例外)。它存储顶点数据的格式以及提供顶点数据数组的缓冲区对象(请参见下文)。
索引缓冲区绑定存储在VAO中。
对于GL_ARRAY_BUFFER
VAO,当您致电时将保存绑定glVertexAttribPointer
。基本上是因为GL_ARRAY_BUFFER
绑定不是VAO状态的一部分。因此,调用glBindBuffer(GL_ARRAY_BUFFER, vertexBufferHandle)
不会对VAO的状态产生任何影响。
因为GL_ELEMENT_ARRAY_BUFFER
不是这种情况:VAO(如果已绑定)将在调用时将索引缓冲区绑定保存到其状态glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferHandle)
。
opengl-tutorial.org上有很多很棒的有关OpenGL索引缓冲的教程,以及其他基本内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句