Can't see cube using gluLookAt

Hey all,

I’ve written this simple piece of code that involves a cube rotating about the origin. From what I understand, I’ve set up my camera behind the cube and have the camera looking in the direction of the cube:

#include <gl/glut.h> // GLUT header file
#include <gl/gl.h> // OpenGL header file
#include <math.h>

float _angle = 0.0;

void init()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluLookAt(0.5, 0.5, -5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
	gluPerspective(45.0,1.0,1.0,15.0);
}

void display() 
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the background

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity(); //set current matrix to identity

	glPushMatrix();
	glRotatef(_angle, 0.0, 0.0, 1.0); //Rotation: theta = 30 
	glBegin(GL_LINE_LOOP);
		glVertex3f( 0., 0., 0.); glVertex3f( 0., 1., 0.);
		glVertex3f( 1., 1., 0.); glVertex3f( 1., 0., 0.);
		glEnd();
	glBegin(GL_LINE_LOOP);
		glVertex3f( 0., 0., 1.); glVertex3f( 1., 0., 1.);
		glVertex3f( 1., 1., 1.); glVertex3f( 0., 1., 1.);
	glEnd();
	glBegin(GL_LINES);
		glVertex3f( 0., 0., 0.); glVertex3f( 0., 0., 1.);
		glVertex3f( 0., 1., 0.); glVertex3f( 0., 1., 1.);
		glVertex3f( 1., 0., 0.); glVertex3f( 1., 0., 1.);
		glVertex3f( 1., 1., 0.); glVertex3f( 1., 1., 1.);
	glEnd();
	glPopMatrix();

	glFlush();
}

void update(int value) {
	_angle += 2.0f;
	if (_angle > 360) {
		_angle -= 360;
	}
	glutPostRedisplay(); //causes the window to be redrawn
	glutTimerFunc(25, update, 0);
}

void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutCreateWindow("Q1");
init();
glutDisplayFunc(display);
glutTimerFunc(25, update, 0); //Add a timer
glutMainLoop();
} 

Now, if I omit the gluLookAt line and replace it with glTranslatef(0.0,0.0,-5.0);, then I can see the cube so I know the problem is with gluLookAt. I’ve been playing around with it but I just can’t figure it out. Why can’t I see anything the way I have it set up right now?

Thanks,

Canadian0469

There are two problems with your code.

  1. Matrices that define how you look at something typically belong to the modelview matrix and not the projection matrix.

  2. Most matrix calls perform a post-multiplication. This means that the matrix you call last is executed first on the data. So as it is now you’re performing a projection followed by a transform instead of vice versa.

Thanks again -NiCo-!

#2 fixed my problem right away.

As for #1, if I do

glMatrixMode(GL_MODELVIEW);
gluLookAt(0.5, 0.5, -5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,1.0,1.0,15.0);

instead, it doesn’t work anymore… Is this what you were suggesting? I’m confused by this first point you made because so far in all OpenGL code I’ve seen (in my class anyway) all calls to glPerspective and gluLookAt followed the glMatrixMode(GL_PROJECTION); command…

What aren’t I understanding?

Thanks again,

Canadian0469

That’s probably because you’re resetting the modelview matrix to the identity matrix in the display call. Note that, even for gluLookAt, you need to call glLoadIdentity first if you want to avoid accumulating postmultiplications.