Reputation: 51
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?
Upvotes: 3
Views: 2255
Reputation: 171
I hope I'm not too late, but what I do is set the normals on a per-vertex basis within the vertex array, then use the appropriate strides and buffer offsets when declaring the vertex attributes. Here's my code declaring the vertices and normals:
GLfloat BlockVertexData[144] = {
//Right side...
//Vertices... //Normals...
1.0f, 2.0f, 0.5f, 1.0f, 0.0f, 0.0f, //B
1.0f, 2.0f, -0.5f, 1.0f, 0.0f, 0.0f, //F
1.0f, -2.0f, 0.5f, 1.0f, 0.0f, 0.0f, //D
1.0f, -2.0f, -0.5f, 1.0f, 0.0f, 0.0f, //H
//Front side...
-1.0f, 2.0f, 0.5f, 0.0f, 0.0f, 1.0f, //A
1.0f, 2.0f, 0.5f, 0.0f, 0.0f, 1.0f, //B
-1.0f, -2.0f, 0.5f, 0.0f, 0.0f, 1.0f, //C
1.0f, -2.0f, 0.5f, 0.0f, 0.0f, 1.0f, //D
//Left side...
-1.0f, 2.0f, 0.5f, -1.0f, 0.0f, 0.0f, //A
-1.0f, 2.0f, -0.5f, -1.0f, 0.0f, 0.0f, //E
-1.0f, -2.0f, 0.5f, -1.0f, 0.0f, 0.0f, //C
-1.0f, -2.0f, -0.5f, -1.0f, 0.0f, 0.0f, //G
//Back side...
-1.0f, 2.0f, -0.5f, 0.0f, 0.0f, -1.0f, //E
1.0f, 2.0f, -0.5f, 0.0f, 0.0f, -1.0f, //F
-1.0f, -2.0f, -0.5f, 0.0f, 0.0f, -1.0f, //G
1.0f, -2.0f, -0.5f, 0.0f, 0.0f, -1.0f, //H
//Top side...
-1.0f, 2.0f, -0.5f, 0.0f, 1.0f, 0.0f, //E
1.0f, 2.0f, -0.5f, 0.0f, 1.0f, 0.0f, //F
-1.0f, 2.0f, 0.5f, 0.0f, 1.0f, 0.0f, //A
1.0f, 2.0f, 0.5f, 0.0f, 1.0f, 0.0f, //B
//Bottom side...
-1.0f, -2.0f, -0.5f, 0.0f, -1.0f, 0.0f, //G
1.0f, -2.0f, -0.5f, 0.0f, -1.0f, 0.0f, //H
-1.0f, -2.0f, 0.5f, 0.0f, -1.0f, 0.0f, //C
1.0f, -2.0f, 0.5f, 0.0f, -1.0f, 0.0f //D };
GLuint BlockIndicesData[36] = {
//Right side...
2, 0, 3, 0, 1, 3,
//Front side...
6, 4, 7, 4, 5, 7,
//Left side...
11, 10, 8, 8, 9, 11,
//Back side...
15, 14, 12, 12, 13, 15,
//Top side...
19, 18, 16, 16, 17, 19,
//Bottom side...
23, 22, 20, 20, 21, 23 };
And here's the code declaring the attributes:
// The stride shows that there are 6 floats in each row.
GLsizei stride = 6 * sizeof(GLfloat);
GLuint attribute;
attribute = glGetAttribLocation(program, "VertexPosition");
glEnableVertexAttribArray(attribute);
glVertexAttribPointer(attribute, 3, GL_FLOAT, GL_FALSE, stride, 0);
attribute = glGetAttribLocation(self.program, "VertexNormal");
glEnableVertexAttribArray(attribute);
// The sixth parameter indicates the buffer offset, so here there were 3 floats preceding it, so this indicates it.
glVertexAttribPointer(attribute, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid *)(sizeof(GLfloat) * 3));
I know that this may occupy more memory, but maybe someone can come up with a better solution. This is what I can think of to solve your problem.
Upvotes: 1