Cameras

I’m quite confused with moving a 3D camera in space… Why is it that no matter how I move the camera or where I render the triangle strip, it is clipped at either -1 or +1 from the origin?

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glTranslatef(0.0f, 0.0f, ZDist);

glBegin(GL_TRIANGLE_STRIP);
glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-5.0f, 5.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-5.0f, -5.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(5.0f, 5.0f, 0.0f);
glColor3f(0.5f, 0.0f, 0.5f); glVertex3f(5.0f, -5.0f, 0.0f);
glEnd();

glutSwapBuffers();
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w / (GLfloat) h, 0.5, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

In you display func you are setting GL_PROJECTION to identity whereas in your reshape func you are setting it to gluPerspective(60.0, (GLfloat) w / (GLfloat) h, 0.5, 20.0);

Either remove these lines from the display func which is probably best
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

or add
gluPerspective(60.0, (GLfloat) w / (GLfloat) h, 0.5, 20.0);

right after the glLoadIdentity which is part of what I said to remove.

[This message has been edited by shinpaughp (edited 05-24-2003).]

Try simply :

void display(void){	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	//glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluLookAt(0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);	//glMatrixMode(GL_MODELVIEW);	//glLoadIdentity();	glTranslatef(0.0f, 0.0f, ZDist);	glBegin(GL_TRIANGLE_STRIP);		glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(-5.0f, 5.0f, 0.0f);		glColor3f(0.0f, 1.0f, 0.0f); glVertex3f(-5.0f, -5.0f, 0.0f);		glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(5.0f, 5.0f, 0.0f);		glColor3f(0.5f, 0.0f, 0.5f); glVertex3f(5.0f, -5.0f, 0.0f);	glEnd();	glutSwapBuffers(); }void reshape(int w, int h){	glViewport(0, 0, (GLsizei) w, (GLsizei) h);	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(60.0, (GLfloat) w / (GLfloat) h, 0.5, 20.0);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();}

Oops, sorry :

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
glTranslatef(0.0f, 0.0f, ZDist);
glBegin(GL_TRIANGLE_STRIP);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-5.0f, 5.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-5.0f, -5.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(5.0f, 5.0f, 0.0f);
glColor3f(0.5f, 0.0f, 0.5f);
glVertex3f(5.0f, -5.0f, 0.0f);
glEnd();

glutSwapBuffers();
}

Now it’s better.

Simply your reseting the axes twice with glLoadIdentity() and you don’t need to do that.

I’ve set the not needed lines in comment