I've written a very simple OpenGL program displaying a white cube. To do the job done I recovered vertex position and indice data from an OBJ file that describes my cube. Here's the OBJ file content :
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
usemtl BoxMtl
s off
f 1 2 3
f 5 8 7
f 1 5 6
f 2 6 3
f 3 7 4
f 5 1 4
f 4 1 3
f 6 5 7
f 2 1 6
f 6 7 3
f 7 8 4
f 8 5 4
According to the OBJ file the vertex array is translated in C by :
static GLfloat position[24] =
{
1.000000, -1.000000, -1.000000,
1.000000, -1.000000, 1.000000,
-1.000000, -1.000000, 1.000000,
-1.000000, -1.000000, -1.000000,
1.000000, 1.000000, -0.999999,
0.999999, 1.000000, 1.000001,
-1.000000, 1.000000, 1.000000,
-1.000000, 1.000000, -1.000000
};
And the index array by :
static GLuint indices[36] = {
0, 1, 2,
4, 7, 6,
0, 4, 5,
1, 5, 2,
2, 6, 3,
4, 0, 3,
3, 0, 2,
5, 4, 6,
1, 0, 5,
5, 6, 2,
6, 7, 3,
7, 4, 3
};
Now the code to initialize the VBO for the vertex array :
GLuint vertex_vboId;
glGenBuffers(1, &vertex_vboId);
glBindBuffer(GL_ARRAY_BUFFER, vertex_vboId);
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), position, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
The one for the IBO :
GLuint indexId;
glGenBuffers(1, &indexId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36 * sizeof(GLuint), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
And finally the code to render the scene in the main loop (here the white cube):
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertex_vboId);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, OFFSET_BUFFER(0));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexId);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, OFFSET_BUFFER(0));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Here's the output :
Until here there's no problem ! But my objective is to render the same cube but this time with a texture (a uv map) and with a UNIQUE call of glDrawElements like above.
To do the job done, I've first created a uv mapped cube on Blender and exported a new OBJ file but, this time, with vertex texture (vt) like below :
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vt 0.626059 0.265705
vt 0.626059 0.487398
vt 0.404365 0.487398
vt 0.626060 0.930786
vt 0.404365 0.930786
vt 0.404365 0.709091
vt 0.847752 0.487397
vt 0.847753 0.709091
vt 0.626059 0.709091
vt 0.182672 0.487397
vt 0.626059 0.044011
vt 0.404366 0.265704
vt 0.182671 0.709091
vt 0.404366 0.044011
usemtl BoxMtl
s off
f 1/1 2/2 3/3
f 5/4 8/5 7/6
f 1/7 5/8 6/9
f 2/2 6/9 3/3
f 3/3 7/6 4/10
f 5/11 1/1 4/12
f 4/12 1/1 3/3
f 6/9 5/4 7/6
f 2/2 1/7 6/9
f 6/9 7/6 3/3
f 7/6 8/13 4/10
f 8/14 5/11 4/12
So I wonder if it's possible to use glDrawElements with two kinds of indices (v and vt) in a unique indice array (with a size of 72 this time). I tried several way without any success. Or maybe I have to use glDrawArrays... I'm really lost. Thanks very much in advance for your help.