Drawing a cube

Hi!

When i want to draw a cube why is this code correct

	glBegin(GL_QUADS);
	// top
	glColor3f(1.0f, 0.0f, 0.0f);
	glNormal3f(0.0f, 1.0f, 0.0f);
	glVertex3f(-0.5f, 0.5f, 0.5f);
	glVertex3f(0.5f, 0.5f, 0.5f);
	glVertex3f(0.5f, 0.5f, -0.5f);
	glVertex3f(-0.5f, 0.5f, -0.5f);
	
	glEnd();
	
	glBegin(GL_QUADS);
	// front
	glColor3f(0.0f, 1.0f, 0.0f);
	glNormal3f(0.0f, 0.0f, 1.0f);
	glVertex3f(0.5f, -0.5f, 0.5f);
	glVertex3f(0.5f, 0.5f, 0.5f);
	glVertex3f(-0.5f, 0.5f, 0.5f);
	glVertex3f(-0.5f, -0.5f, 0.5f);
	
	glEnd();
	
	glBegin(GL_QUADS);
	// right
	glColor3f(0.0f, 0.0f, 1.0f);
	glNormal3f(1.0f, 0.0f, 0.0f);
	glVertex3f(0.5f, 0.5f, -0.5f);
	glVertex3f(0.5f, 0.5f, 0.5f);
	glVertex3f(0.5f, -0.5f, 0.5f);
	glVertex3f(0.5f, -0.5f, -0.5f);
	
	glEnd();
	
	glBegin(GL_QUADS);
	// left
	glColor3f(0.0f, 0.0f, 0.5f);
	glNormal3f(-1.0f, 0.0f, 0.0f);
	glVertex3f(-0.5f, -0.5f, 0.5f);
	glVertex3f(-0.5f, 0.5f, 0.5f);
	glVertex3f(-0.5f, 0.5f, -0.5f);
	glVertex3f(-0.5f, -0.5f, -0.5f);
	
	glEnd();
	
	glBegin(GL_QUADS);
	// bottom
	glColor3f(0.5f, 0.0f, 0.0f);
	glNormal3f(0.0f, -1.0f, 0.0f);
	glVertex3f(0.5f, -0.5f, 0.5f);
	glVertex3f(-0.5f, -0.5f, 0.5f);
	glVertex3f(-0.5f, -0.5f, -0.5f);
	glVertex3f(0.5f, -0.5f, -0.5f);
	
	glEnd();
	
	glBegin(GL_QUADS);
	// back
	glColor3f(0.0f, 0.5f, 0.0f);
	glNormal3f(0.0f, 0.0f, -1.0f);
	glVertex3f(0.5f, 0.5f, -0.5f);
	glVertex3f(0.5f, -0.5f, -0.5f);
	glVertex3f(-0.5f, -0.5f, -0.5f);
	glVertex3f(-0.5f, 0.5f, -0.5f);
	
	glEnd();

and this for e.g. not

glBegin(GL_QUADS);
	// top
	glColor3f(1.0f, 0.0f, 0.0f);
	glNormal3f(0.0f, 1.0f, 0.0f);
	glVertex3f(-0.5f, 0.5f, 0.5f);
	glVertex3f(0.5f, 0.5f, 0.5f);
	glVertex3f(0.5f, 0.5f, -0.5f);
	glVertex3f(-0.5f, 0.5f, -0.5f);
	
	glEnd();
	
	glBegin(GL_QUADS);
	// front
	glColor3f(0.0f, 1.0f, 0.0f);
	glNormal3f(0.0f, 0.0f, 1.0f);
	glVertex3f(-0.5f, -0.5f, 0.5f);
	glVertex3f(0.5f, -0.5f, 0.5f);
	glVertex3f(0.5f, 0.5f, 0.5f);
	glVertex3f(-0.5f, 0.5f, 0.5f);
	
	glEnd();
	
	glBegin(GL_QUADS);
	// right
	glColor3f(0.0f, 0.0f, 1.0f);
	glNormal3f(1.0f, 0.0f, 0.0f);
	glVertex3f(0.5f, -0.5f, 0.5f);
	glVertex3f(0.5f, -0.5f, -0.5f);
	glVertex3f(0.5f, 0.5f, -0.5f);
	glVertex3f(0.5f, 0.5f, 0.5f);
	
	glEnd();
	
	glBegin(GL_QUADS);
	// left
	glColor3f(0.0f, 0.0f, 0.5f);
	glNormal3f(-1.0f, 0.0f, 0.0f);
	glVertex3f(-0.5f, -0.5f, 0.5f);
	glVertex3f(-0.5f, 0.5f, 0.5f);
	glVertex3f(-0.5f, 0.5f, -0.5f);
	glVertex3f(-0.5f, -0.5f, -0.5f);
	
	glEnd();
	
	glBegin(GL_QUADS);
	// bottom
	glColor3f(0.5f, 0.0f, 0.0f);
	glNormal3f(0.0f, -1.0f, 0.0f);
	glVertex3f(-0.5f, -0.5f, 0.5f);
	glVertex3f(0.5f, -0.5f, 0.5f);
	glVertex3f(0.5f, -0.5f, -0.5f);
	glVertex3f(-0.5f, -0.5f, -0.5f);
	
	glEnd();
	
	glBegin(GL_QUADS);
	// back
	glColor3f(0.0f, 0.5f, 0.0f);
	glNormal3f(0.0f, 0.0f, -1.0f);
	glVertex3f(0.5f, 0.5f, -0.5f);
	glVertex3f(0.5f, -0.5f, -0.5f);
	glVertex3f(-0.5f, -0.5f, -0.5f);
	glVertex3f(-0.5f, 0.5f, -0.5f);
	
	glEnd();

glFrontFace(GL_CCW) and glEnable(GL_CULL_FACE) are set! I don’t unterstand why the second example has mistakes, because there are also all Quads counterclockwise drawn! Please help me! I don’t understand why example 1 works and example 2 not!!!

Thanks in advange!
Mat!!!

As I don’t want to go trough so many vertex coordinates: What exactly is the error that the code
produces?

When i am rotating the cube about the x, y and z-axis some quads aren’t drawn, and some quads are overlapped together!

:slight_smile:

Not directly related, but you can do that with better performance :

	glBegin(GL_QUADS);
	// top
	glColor3f(1.0f, 0.0f, 0.0f);
	glNormal3f(0.0f, 1.0f, 0.0f);
	glVertex3f(-0.5f, 0.5f, 0.5f);
	glVertex3f(0.5f, 0.5f, 0.5f);
	glVertex3f(0.5f, 0.5f, -0.5f);
	glVertex3f(-0.5f, 0.5f, -0.5f);
	
	// front
	glColor3f(0.0f, 1.0f, 0.0f);
	glNormal3f(0.0f, 0.0f, 1.0f);
	glVertex3f(0.5f, -0.5f, 0.5f);
	glVertex3f(0.5f, 0.5f, 0.5f);
	glVertex3f(-0.5f, 0.5f, 0.5f);
	glVertex3f(-0.5f, -0.5f, 0.5f);
	
	// right
	glColor3f(0.0f, 0.0f, 1.0f);
	glNormal3f(1.0f, 0.0f, 0.0f);
	glVertex3f(0.5f, 0.5f, -0.5f);
	glVertex3f(0.5f, 0.5f, 0.5f);
	glVertex3f(0.5f, -0.5f, 0.5f);
	glVertex3f(0.5f, -0.5f, -0.5f);
	
        [...]

	glEnd();

Maybe plot the coordinates on a paper ? It is late here and coordinates are hard to read…

Do you use the z-buffer? I once encountered a problem similiar to yours when using the Z-buffer incorrectly.

Silver

Don´t know if you have depth testing set?.

glEnable(GL_DEPTH_TEST);

Also, have you set up Frustrum or Perspective and set it to the projection matrix? To use the code below, declare the following variable where all the other variables are declared in the drawing section ; int initGlProjection = 0;

Then, use something like this:

if (initGlProjection == 0)
{
glEnable(GL_DEPTH_TEST);
/Put the projection in the projection Matrix/
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-0.1333, 0.1333, -0.1, 0.1, 0.5, 25.0);
/* Get back into modelview matrix to draw objects*/
glMatrixMode (GL_MODELVIEW);*/
/Don´t execute section again/
initGlProjection = 1;
}

         For a simple example like drawing a cube, the above will work. I´ve done it like that. Instead of frustrum, you can use the perspective projection. Just an idea...I´m just starting out too.

BeginOpenGL

Do you mean glEnable(GL_DEPTH_TEST)! if so, yes!!!
If not, how do i enable/disable it?

bye!

oh i probably had a problem with this function

void setView() {
	
	glViewport(0,0, g_width, g_height);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(54.0, g_width/g_height, 0.1, 1000.0);

// changed from
	gluPerspective(54.0, g_width/g_height, -1.0, 1000.0);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
}

Now i also know how to draw the cube. Top, front and left -face must be drawn counterclockwise and back, bottom and right in clockwise!

Thanks guys :wink: !