PDA

View Full Version : The best way to draw a cube



mjriggio
11-17-2002, 03:57 PM
I'm new to openGL and am have been successful at drawing a cube http://www.opengl.org/discussion_boards/ubb/smile.gif However, I'm concerned about the efficiency of which I drew the cube. Currently, this is what I'm doing:

I'm using vertex arrays, and filling it with 24 vertices (4 for each face). I was kinda hoping I would be able to just specify 8 vertices total, and then have it just figure it out itself... http://www.opengl.org/discussion_boards/ubb/smile.gif Is this possible? If so, can someone provide an example for me to work from?

Also, I'm not sure if this matters, but I do plan on using lighting in my program, so I'm not sure if this throws off any possible solutions.

Finally, I was reading somewhere that it would be better to store it as a set of triangles instead of rectangles. Is this the case?

TIA,
-Mike

nexusone
11-17-2002, 05:46 PM
First you can make a routine that only takes 8 vertex points and makes a cube, but in the routine you will still have to have 24 vertex defined for the cube to be drawn.

I think Triangles are processed somewhat faster, then quads when using hardware.

As for lighting,



Originally posted by mjriggio:
I'm new to openGL and am have been successful at drawing a cube http://www.opengl.org/discussion_boards/ubb/smile.gif However, I'm concerned about the efficiency of which I drew the cube. Currently, this is what I'm doing:

I'm using vertex arrays, and filling it with 24 vertices (4 for each face). I was kinda hoping I would be able to just specify 8 vertices total, and then have it just figure it out itself... http://www.opengl.org/discussion_boards/ubb/smile.gif Is this possible? If so, can someone provide an example for me to work from?

Also, I'm not sure if this matters, but I do plan on using lighting in my program, so I'm not sure if this throws off any possible solutions.

Finally, I was reading somewhere that it would be better to store it as a set of triangles instead of rectangles. Is this the case?

TIA,
-Mike

ImpactDNI
11-17-2002, 06:24 PM
I Had this same thought when i started opengl... Heres 2 good ideas
#1, use the cube function, opengl has a built in cube drawing function =P
#2, use 2 matricies.
static float vertex[8][3] = {{x,y,z}, {x,y,z}, {x,y,z}, {x,y,z}, {x,y,z}, {x,y,z}, {x,y,z}, {x,y,z}};
// that is where you put your 8 verticies
static float link[6][4] = {0,1,2,3}, {4,5,6,7}, {0,1,5,4}, {1,2,6,5}, {2,3,7,6}, {3,0,7,4}};
// those are the order in which the verticies are linked
now draw each of of those quads
glBegin(GL_QUADS)
for (int a=0; a<6; a++){
glVertexf(vertex[link[a][0]][0], vertex[link[a][0]][1], vertex[link[a][0][2]);
glVertexf(vertex[link[a][1]][0], vertex[link[a][1]][1], vertex[link[a][1][2]);
glVertexf(vertex[link[a][2]][0], vertex[link[a][2]][1], vertex[link[a][2][2]);
glVertexf(vertex[link[a][3]][0], vertex[link[a][3]][1], vertex[link[a][3][2]);
}
glEnd();
// thats just written on the fly... but hopefully you get the idea... it draws the verticies based on what order you have them linked in the link[] matrix. You could do the same thing with triangles... just separate each face into 2 triangles

M/\dm/\n
11-19-2002, 12:18 AM
1st create vertex array with 8 vertices, 2nd create indices array, 3rd use DrawElements if I remember correct & by the way, you can put all this thing in display list http://www.opengl.org/discussion_boards/ubb/smile.gif