Answers:
我认为您将通过样本更好地了解它们的目的。通过阅读注释,您将了解如何使用VAO。
// BEGIN INITIALIZATION
// Define some vertex data
struct Vertex {
GLfloat position[3];
GLfloat texcoord[2];
};
Vertex vertexdata[NUM_VERTS] = { ... };
GLubyte indexdata[NUM_INDICES] = { 0, 1, 2, ... };
// Create and bind a VAO
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// Create and bind a BO for vertex data
GLuint vbuffer;
glGenBuffers(1, &vbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vbuffer);
// copy data into the buffer object
glBufferData(GL_ARRAY_BUFFER, NUM_VERTS * sizeof(Vertex), vertexdata, GL_STATIC_DRAW);
// set up vertex attributes
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, texcoord));
// Create and bind a BO for index data
GLuint ibuffer;
glGenBuffers(1, &ibuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibuffer);
// copy data into the buffer object
glBufferData(GL_ELEMENT_ARRAY_BUFFER, NUM_INDICES * sizeof(GLubyte), indexdata, GL_STATIC_DRAW);
// At this point the VAO is set up with two vertex attributes
// referencing the same buffer object, and another buffer object
// as source for index data. We can now unbind the VAO, go do
// something else, and bind it again later when we want to render
// with it.
glBindVertexArray(0);
// END INITIALIZATION
// BEGIN RENDER LOOP
// This is it. Binding the VAO again restores all buffer
// bindings and attribute settings that were previously set up
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, NUM_INDICES, GL_UNSIGNED_BYTE, (void*)0);
// END RENDER LOOP
GLfloat normal[3]
在Vertex类中添加并想将法线上传到客户端,该怎么办?
这是我最喜欢的链接:http : //www.songho.ca/opengl/gl_vertexarray.html
它应该可以帮助您更好地了解VAO和VBO之间的差异。另外,Id建议阅读OpenGL精讲中有关该主题的一章。它很好地详尽地解释了这些基础知识并提供了示例。
第9讲这里http://nccastaff.bournemouth.ac.uk/jmacey/CA1/index.html有一些相关内容(我的其他笔记也有),如果您在同一站点上查看NGL库,包含VAO行为的类