PDA

View Full Version : Drawing a cube



11-15-2000, 04:34 PM
I feel so stupid. I can draw a triangle and a square but not just a square or just a cube. I'm using glut in C++ 6. I swap the buffers, resize, flush the screen, clear the colors, and translate. What am I doing wrong????

jc
11-15-2000, 10:08 PM
what method do you use to draw a cube ?? do you draw six quads ? or do you use the magic func glutsolidcube (or glutwirecube) ???? Could you send some code ?

11-16-2000, 10:21 AM
I use glut to intialize everything, but i gl quads to draw six squares. I also use the glvertext3f function to draw.

bsimser
11-16-2000, 10:33 AM
It depends on how you want to draw the cube. If you're using glut, then glutWireCube or glutSolidCube will work. Otherwise you'll have to create 6 faces yourself. Take a look at the cube.c that came with glut, it shows how to build the cube using polygons and is quite straight forward.

liB

Antonio
11-16-2000, 02:56 PM
Here goes a function that creates cubes using vertex arrays.

Even if you really want to use quads you can figure out the coordinates from the code.

Hope this helps,

Antonio www.fatech.com/tech (http://www.fatech.com/tech)

float vertices[] = {
// ground
-100.0f, 0.0f, -100.0f,
-100.0f, 0.0f, 100.0f,
100.0f, 0.0f, 100.0f,
100.0f, 0.0f, -100.0f,
// cube top ccw
-1.0f, 1.0f, -1.0f, //4
-1.0f, 1.0f, 1.0f, //5
1.0f, 1.0f, 1.0f, //6
1.0f, 1.0f, -1.0f, //7
// cube bottom cw
-1.0f, -1.0f, -1.0f, //8
-1.0f, -1.0f, 1.0f, //9
1.0f, -1.0f, 1.0f, //10
1.0f, -1.0f, -1.0f, //11
};


GLuint indices[] = {4,8,5,9,6,10,7,11, 10,9,11,8,7,4,6,5};


GLuint createDL() {
GLuint DL;

// Create the id for the list
DL = glGenLists(1);

// start list
glNewList(DL,GL_COMPILE);

glTranslatef(0.0f,2.1f,0.0f);
for(int i = -15;i<15;i+=5)
for(int j=-10;j<15;j+=5) {
glPushMatrix();
glTranslatef(i,0,j);
glColor3f(1.0f,0.0f,0.0f);
glDrawElements(GL_TRIANGLE_STRIP,8,GL_UNSIGNED_INT ,(const void *)indices);
glColor3f(0.0f,1.0f,0.0f);
glDrawElements(GL_TRIANGLE_STRIP,8,GL_UNSIGNED_INT ,(const void *)&(indices[8]));
glPopMatrix();
}

// endList
glEndList();

return(DL);
}