I am required to Using VBO to draw the cube by loading wavefront object. by drawing the triangles. here is the object:
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 1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -1.000000
vn -1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 0.0000
f 2//1 3//1 1//1
f 4//2 7//2 3//2
f 8//3 5//3 7//3
f 6//4 1//4 5//4
f 7//5 1//5 3//5
f 4//6 6//6 8//6
f 2//1 4//1 3//1
f 4//2 8//2 7//2
f 8//3 6//3 5//3
f 6//4 2//4 1//4
f 7//5 5//5 1//5
f 4//6 2//6 6//6
I also have a cooler array filled by myself which equals to 3 times number of face which is 36 indices
float colours[] = {
0.583f, 0.771f, 0.014f,
0.609f, 0.115f, 0.436f,
0.327f, 0.483f, 0.844f,
0.822f, 0.569f, 0.201f,
0.435f, 0.602f, 0.223f,
0.310f, 0.747f, 0.185f,
0.597f, 0.770f, 0.761f,
0.559f, 0.436f, 0.730f,
0.359f, 0.583f, 0.152f,
0.483f, 0.596f, 0.789f,
0.559f, 0.861f, 0.639f,
0.195f, 0.548f, 0.859f,
}
I store my vertices,normals,colours information into a array which is binding to VBO buffer:
float* vbo = new float[78]
which is in the order of vertices|normals|colours which is 24indices|18indices|36indices, I have been test it works very well
I use another array to store the vertices indices information which is 36indices ,for example: f 2//1 3//1 1//1 I store as 2/3/1 in my array
int *element = new int[36];
I have been test it works well ,I initial my vbo as follow:
static void init(void){
.......
Gluint vbo_id,index_id;
glGenBuffers(1,&vbo_id);
glGenBuffers(1,&index_id);
glBindBuffer(GL_ARRAY_BUFFER,vbo_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,index_id);
glBufferData(GL_ARRAY_BUFFER,sizeof(vbo),vbo,GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(element),element,GL_STATIC_DRAW); }
and my drawing is here
static void display(void){
.......
glBindBuffer(GL_ARRAY_BUFFER,vbo_id);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,index_id);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3,GL_FLOAT,0,0);
glNormalPointer(GL_FLOAT,0,(void*)(24*sizeof(float)));//binding
glColorPointer(3,GL_FLOAT,0,(void*)(42*sizeof(float)));
for(int i=0;i<12;i++){
glBegin(GL_TRIANGLES);
glArrayElement(element[i]);
glArrayElement(element[i+1]);
glArrayElement(element[i+2]);
glEnd();
}
/* have try this one too:
glDrawElements(GL_TRIANGLES,3,GL_INT,0) */
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
}
however there's nothing show on the screen. is my binding method correct, my instructor tells me when I draw with the indices , if I binding vertices and normals ,and colors correct , It will automatically matching by drawing with indices of vertices.