# Drawing a cube

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???

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 ?

I use glut to intialize everything, but i gl quads to draw six squares. I also use the glvertext3f function to draw.

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

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

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&lt;15;i+=5)
for(int j=-10;j&lt;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);
``````

}