In OpenGL (OpenGL ES 2.0) can I use more than one GL_ELEMENT_ARRAY_BUFFER for different GL_ARRAY_BUFFER buffers? I'm reading "OpenGL ES 2.0 Programming Guide", Chapter 6 "Vertex attributes, arrays and buffer objects", there is source example: there are several GL_ARRAY_BUFFER (for position, normal, texture coords) and one GL_ELEMENT_ARRAY_BUFFER ("used to store element indices").
While I was writing question, I got that I can't send more then one indices array to glDrawElements, so if I use buffers, maybe only last binded GL_ELEMENT_ARRAY_BUFFER is using for drawing. But what about memory saving (what is purpose of glDrawElements)? I will illustrate problem that I faced.
There are 2 arrays (as GL_ARRAY_BUFFERs) - 8 vertices and 6 normals
GLfloat gVertexPositions[] =
{
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f
};
GLfloat gVertexNormals[] =
{
1.0f, 0.0f, 0.0f, // top
-1.0f, 0.0f, 0.0f, // bottom
0.0f, 1.0f, 0.0f, // right
0.0f, -1.0f, 0.0f, // left
0.0f, 0.0f, 1.0f, // back
0.0f, 0.0f, -1.0f // front
};
2 arrays of indices (as GL_ELEMENT_ARRAY_BUFFERs)
GLubyte gVertexPositionIndices[] =
{
0, 1, 2, // top
2, 3, 0,
0, 4, 1, // right
1, 4, 5,
5, 4, 7, // bottom
6, 5, 7,
2, 6, 7, // left
2, 7, 3,
1, 4, 2, // front
2, 4, 5,
0, 3, 4, // back
7, 4, 3
};
GLubyte gVertexNormalIndices[] =
{
0, 0, 0,
0, 0, 0,
2, 2, 2,
2, 2, 2,
1, 1, 1,
1, 1, 1,
3, 3, 3,
3, 3, 3,
5, 5, 5,
5, 5, 5,
4, 4, 4,
4, 4, 4
};
I set vertex attribute state
glBindAttribLocation(program, ATTRIB_POSITION, "a_position");
glBindAttribLocation(program, ATTRIB_NORMAL, "a_normal");
//.....
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[VBO_POSITION_INDICES]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * 36, gVertexPositionIndices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vboIds[VBO_POSITION_DATA]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * 8, gVertexPositions, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[VBO_NORMAL_INDICES]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte) * 36, gVertexNormalIndices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, vboIds[VBO_NORMAL_DATA]);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * 6, gVertexNormals, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[VBO_POSITION_INDICES]);
glBindBuffer(GL_ARRAY_BUFFER, vboIds[VBO_POSITION_DATA]);
glEnableVertexAttribArray(ATTRIB_POSITION);
glBindBuffer(GL_ARRAY_BUFFER, vboIds[VBO_NORMAL_DATA]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIds[VBO_NORMAL_INDICES]);
glEnableVertexAttribArray(ATTRIB_NORMAL);
glVertexAttribPointer(ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(ATTRIB_NORMAL, 3, GL_FLOAT, GL_FALSE, 0, 0);
Then draw
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, 0);
And screen is empty (because last GL_ELEMENT_ARRAY_BUFFER is using for "a_position" attribute, where all triplets have identical numbers)
All I want - program makes 36 vertices, sets their positions from gVertexPositions using gVertexPositionIndices and their normals from gVertexNormals using gVertexNormalIndices. I doubt that is possible, but I want to know exactly. And what will be the right way if that is impossible? Do I have to use 8*3 floats for positions, 36 bytes for indices and 36*3 floats for normals? So I can save memory only for position attribute?