3.1.3 Mesh::initBuffer()
For full source code, please directly jump to the end of this page.
In this step, we are going to initialise vertex buffers as we did when drawing triangles and the cube.
Set up a Vertex Array Object
Now we need support rendering of multiple models. Each model has its own vertex buffers.
We need Vertex Array Object which remembers the configuration of vertex buffers.
GLuint vao;
glGenVertexArrays(1, &vao);
GLuint vertBufID;
glGenBuffers(1, &vertBufID);
glBindBuffer(GL_ARRAY_BUFFER, vertBufID);
glBindVertexArray(vao);
buffers.push_back(vao);Set up the Vertex Buffers
The following glGenBuffer() and glVertexArrayAttribute() callings are similar to those in triangle rendering, but now weare using vector of glm::vec3 for vertices and vector of GLuint for indices, so we need to change a bit.
// set buffer data to triangle vertex and setting vertex attributes
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, 0);
// set normal attributes
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, (void *) (sizeof(float) * 3));Create and Set up the Index Buffer
Finally, need to unbind vertex array
Full Source Code
Last updated