Can't Render Cube

I know this is a really simple problem and I’ve been looking through the red book but I just can’t seem to get a cube to render in my app.

Basically, in my Render() function I have this:

void Render() {
	bool running = true;
	SDL_Event event;

	while (running) {
		if (SDL_PollEvent(&event)) {
			switch (event.type) {
				case SDL_QUIT:
					running = false;
					break;
				case SDL_KEYDOWN:
					if (event.key.keysym.sym == SDLK_ESCAPE)
						running = false;
					break;
				default:
					break;
			}
		}

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
		glLoadIdentity();
		gluLookAt(camX, camY, camZ, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
		DrawCube();

		SDL_GL_SwapBuffers();
	}
}

My DrawCube() function looks like this:

void DrawCube() {
	glBegin(GL_QUADS);
		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f( 1.0f, 1.0f, -1.0f);
		glVertex3f(-1.0f, 1.0f, -1.0f);
		glVertex3f(-1.0f, 1.0f, 1.0f);
		glVertex3f( 1.0f, 1.0f, 1.0f);

		glColor3f(1.0f, 0.5f, 0.0f);
		glVertex3f( 1.0f, -1.0f, 1.0f);
		glVertex3f(-1.0f, -1.0f, 1.0f);
		glVertex3f(-1.0f, -1.0f, -1.0f);
		glVertex3f( 1.0f, -1.0f, -1.0f);

		glColor3f(1.0f, 0.5f, 0.0f);
		glVertex3f( 1.0f, -1.0f, 1.0f);
		glVertex3f(-1.0f, -1.0f, 1.0f);
		glVertex3f(-1.0f, -1.0f, -1.0f);
		glVertex3f( 1.0f, -1.0f, -1.0f);

		glColor3f(1.0f, 1.0f, 0.0f);
		glVertex3f( 1.0f, -1.0f, -1.0f);
		glVertex3f(-1.0f, -1.0f, -1.0f);
		glVertex3f(-1.0f, 1.0f, -1.0f);
		glVertex3f( 1.0f, 1.0f, -1.0f);

		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f(-1.0f, 1.0f, 1.0f);
		glVertex3f(-1.0f, 1.0f, -1.0f);
		glVertex3f(-1.0f, -1.0f, -1.0f);
		glVertex3f(-1.0f, -1.0f, 1.0f);
		
		glColor3f(1.0f, 0.0f, 1.0f);
		glVertex3f( 1.0f, 1.0f,-1.0f);
		glVertex3f( 1.0f, 1.0f, 1.0f);
		glVertex3f( 1.0f, -1.0f, 1.0f);
		glVertex3f( 1.0f, -1.0f, -1.0f);
	glEnd();
}

Believe me, I know this is simple but it’s annoying me. I’m fairly sure it’s got something to do with glLoadIdentity() or maybe because I haven’t included a glTranslatef(). The thing is, according to the red book, glTranslatef() moves things from the origin but my gluLookAt() is looking right at the origin so I shouldn’t have to move it.

<cowbrain>,
Did you activate GL_MODELVIEW matrix before calling your render function?

If not, add glLoadMatrix(GL_MODELVIEW) right before glLoadIdentity().

And, the second and third quad are identical. Fix this one too.

There is to little code to look at, in the render code you use glLoadIdentity() and then gluLookAt(), but on what matrix are you doing this ? have you setup the projection matrix correct, if so have you switched to modelview matrix mode before using gluLookAt ?

from the snippet you posted i would guess that you didn’t setup the projection matrix. there should be something like

glMatrixmode(GL_PROJECTION);
glLoadidentity();
glOrtho(-2., 2., -2., 2., -1., 1.);

btw: it is glMatrixMode(GL_MODELVIEW), not glLoadMatrix(GL_MODELVIEW)

Yeah, I’d already set glMatrixMode(GL_MODELVIEW).

I should probably say that camX and camY = 0.0f and camZ = 10.0f. I forgot to put that in my original post.

just a tip: if you’re in a progress of learning and something doesn’t work- make a step back. if you can’t render a cube, try to render a quad. if the quad doesn’t show up, try a line or maybe even a point.

It’s interesting you should say that, actually, because I have a DrawAxis() function too which renders fine and looks like this:

void DrawAxis() {
		glBegin(GL_LINES);
			glColor3f(0.7f, 0.0f, 0.0f);
			glVertex3f(0.0f, 0.0f, 0.0f);
			glVertex3f(0.2f, 0.0f, 0.0f);
		glEnd();

		glBegin(GL_LINES);
			glColor3f(0.0f, 0.7f, 0.0f);
			glVertex3f(0.0f, 0.0f, 0.0f);
			glVertex3f(0.0f, 0.2f, 0.0f);
		glEnd();

		glBegin(GL_LINES);
			glColor3f(0.0f, 0.0f, 7.0f);
			glVertex3f(0.0f, 0.0f, 0.0f);
			glVertex3f(0.0f, 0.0f, 0.2f);
		glEnd();
}

It’s called immediately before DrawCube() in my Render() function.

Incidentally, the cube doesn’t render whether I have the DrawAxis() call there or not.

I’ve sorted it. I’m not sure why but I only had five sides in my DrawCube() function (I deleted the one I repeated instead of changing it). Now I’ve added the final side it renders fine.

Thanks for everyone’s help. I’m glad it wasn’t me just going nuts =o)