Let's say I have a have 10 different terrains. Each terrain have the same x and z coordinates, but they have different y coordinate, normals and colors. The indices used for element rendering is also the same.
The data looks like this:
std::vector<GLfloat> xzCoords;
std::vector<GLuint> indices;
std::vector<GLfloat> yCoords[10];
std::vector<GLfloat> normals[10];
std::vector<GLfloat> colors[10];
And the VBO and VAO IDs:
GLuint VAO_IDs[10];
GLuint VBO_xzCoord_IDs[10];
GLuint VBO_yCoord_IDs[10];
GLuint VBO_indices_IDs[10];
GLuint VBO_normals_IDs[10];
GLuint VBO_colors_IDs[10];
If I want one VAO for each of the terrain deffinitions, how would that look if the VBO for xz coordinates and indices are the same for each?
What I am doing now looks like this:
Setting up the buffers:
for (size_t i = 0; i < 10; i++){
//VAO
glGenVertexArrays(1, &VAO_IDs[i]);
glBindVertexArray(VAO_IDs[i]);
//VBO
glGenBuffers(1, &VBO_xzCoord_IDs[i]);
glGenBuffers(1, &VBO_yCoord_IDs[i]);
glGenBuffers(1, &VBO_indices_IDs[i]);
glGenBuffers(1, &VBO_normals_IDs[i]);
glGenBuffers(1, &VBO_colors_IDs[i]);
//xz coords
glBindBuffer(GL_ARRAY_BUFFER, VBO_xzCoord_IDs[i]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*xzCoords.size(), &xzCoords[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
//y coords
glBindBuffer(GL_ARRAY_BUFFER, VBO_yCoord_IDs[i]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*yCoords[i].size(), &yCoords[i][0], GL_STREAM_DRAW);
glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
//normals
glBindBuffer(GL_ARRAY_BUFFER, VBO_normals_IDs[i]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*VBO_normals_IDs[i].size(), &VBO_normals_IDs[i][0], GL_STREAM_DRAW);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(2);
//colors
glBindBuffer(GL_ARRAY_BUFFER, VBO_colors_IDs[i]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*VBO_colors_IDs[i].size(), &VBO_colors_IDs[i][0], GL_STREAM_DRAW);
glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(3);
//indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBO_indices_IDs[i]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*indices.size(), &indices[0], GL_STATIC_DRAW);
}
Rendering function:
for (size_t i = 0; i < 10; i++)
{
glBindVertexArray(VAO_IDs[i]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBO_indices_IDs[i]);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, (void*)0);
}
So as you can see, I create a new xz-coordinates and indices buffer for each VAO. I doubt this is the most effective way to do it. How should I do this?