As mentioned here, multiple VAOs can share a single VBO. However, can multiple VAOs share a single EBO?
For example, I want to create 2 rectangles. So, I do the following:
const GLuint indices[6] = {
0, 1, 2,
0, 3, 2
};
// First
glGenVertexArrays (1, &VAO1);
glBindVertexArray (VAO1);
glGenBuffers (1, &EBO1);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO1);
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// creating VBO, setting attributes etc
// Second
glGenVertexArrays (1, &VAO2);
glBindVertexArray (VAO2);
glGenBuffers (1, &EBO2);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO2);
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER)
is called twice for different EBOs with same data. As I understand, this leads to double GPU memory consumption and takes twice loading time.
So, would the following be correct?
const GLuint indices[6] = {
0, 1, 2,
0, 3, 2
};
glGenBuffers (1, &EBO);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
// First
glGenVertexArrays (1, &VAO1);
glBindVertexArray (VAO1);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO);
// creating VBO, setting attributes etc
// Second
glGenVertexArrays (1, &VAO2);
glBindVertexArray (VAO2);
glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, EBO);