Sign in to follow this  

gluLookAt: Why isn't this working?

This topic is 4110 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

For purposes of testing I tried throwing together some test code which draws a spinning triangle and a spinning square. In theory, hitting [a] would move the camera up (and so move all objects on screen down); except I can't seem to get this code to work, and I can't figure out why, even after comparing the code to various tutorials. Can anyone explain what I'm doing wrong? To clarify, it draws the objects correctly, except it seems that nothing I do can move the camera. Thanks.
#include "main.h"
#include "camera.h"

double xpos = 0;
double ypos = 0;
double zpos = 10;

void ShapeChange(int newWidth, int newHeight)
{
	if(newHeight == 0)
		newHeight = 1;

	// Set the viewport to be the entire window
	glViewport(0, 0, newWidth, newHeight);

	// Reset the coordinate system before modifying
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	float ratio = (float)newWidth/(float)newHeight;

	gluPerspective(45, ratio, 0.1, 100.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(xpos, ypos, zpos, xpos, ypos, zpos-1, 0.0, 1.0, 0.0);
}

void RenderScene(void)
{
	static float rotPyramid = 0;
	static float rotCube = 0;

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glPushMatrix();
	glLoadIdentity();
	glTranslatef(-1.5f,0.0f,-10.0f);
	glRotatef(rotPyramid, 0.0f, 1.0f, 0.0f);
	//glRotatef(135.0f, 0.0f, 1.0f, 0.0f);
	glColor3f(0.5f, 0, 0);
	glBegin(GL_TRIANGLES);						// Drawing Using Triangles
		glVertex3f( 0.0f, 1.0f, 0.0f);				// Top
		glVertex3f(-1.0f,-1.0f, 0.0f);				// Bottom Left
		glVertex3f( 1.0f,-1.0f, 0.0f);				// Bottom Right
	glEnd();
	
	glLoadIdentity();
	glTranslatef(1.5f, 0.0f, -10.0f);
	glRotatef(rotCube, 1.0f, 0.0f, 0.0f);
	//glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
	glColor3f(0, 0.5f, 0);
	glBegin(GL_QUADS);
		glVertex3f(-1.0, 1.0, 0.0f);
		glVertex3f(1.0, 1.0, 0.0f);
		glVertex3f(1.0, -1.0, 0.0f);
		glVertex3f(-1.0, -1.0, 0.0f);
	glEnd();
	
	glPopMatrix();
	glutSwapBuffers();

	rotPyramid += 25.0f;
	rotCube -= 1.0f;

}

void KeyProc(unsigned char key, int x, int y)
{
	if (key == 27) exit(0); // esc
	if (key == 97) // a
	{
		ypos++; 
		glLoadIdentity();
		gluLookAt(xpos, ypos, zpos, xpos, ypos, zpos-1, 0.0, 1.0, 0.0);
	}
}

void main(int argc, char **argv)
{
	//CCamera camera; // this won't work
	//CCamera* pCamera = new CCamera; // nor will this
	//CCamera* pCamera = CCamera::Instance(); // but this is 100% cool (doesn't actually do anything yet)

	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(640,480);
	glutCreateWindow("The Prinny Game... yeah");

	glutDisplayFunc(RenderScene);
	glutIdleFunc(RenderScene);
	glutReshapeFunc(ShapeChange);
	glutKeyboardFunc(KeyProc);

//	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
//	glClearDepth(1.0f);
	glEnable(GL_DEPTH_TEST);
//	glDepthFunc(GL_LEQUAL);
//	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//	glShadeModel(GL_SMOOTH);

	glutMainLoop();
}

Share this post


Link to post
Share on other sites
Well, one problem is that you're calling LoadIdentity() before you render your scene. Thus, anything you did previously to the Modelview matrix (for instance, changing it using gluLookAt) will be undone.

Cheers,
--Brian

Share this post


Link to post
Share on other sites
Quote:
Original post by Nairb
Well, one problem is that you're calling LoadIdentity() before you render your scene. Thus, anything you did previously to the Modelview matrix (for instance, changing it using gluLookAt) will be undone.

Cheers,
--Brian


Isn't that what glPushmatrix() (and the later glPopMatrix()) is supposed to accomplish? Save the modelview, draw stuff, then restore the view?

Or have I fundamentally misinterpreted how this stuff is supposed to work? :(

Share this post


Link to post
Share on other sites
You're a little off.

glPushMatrix saves the current matrix and glPopMatrix retrieves it (using a stack).

So here's what your code is doing:

push matrix to stack
clear matrix
translate/rotate
draw
pop previous matrix to stack

So you're saving the gluLookAt, then tossing it out just before you draw, then retrieving it just after.

I imagine you want to use glPushMatrix and glPopMatrix so that the rotations/translations of the individual objects is independent. To do that, put a push before the rotation and translation and a pop after the drawing of that object, and get rid of the glLoadIdentity in that function entirely.
In other words:

push matrix
translate/rotate
draw
pop matrix

push matrix
translate/rotate
draw
pop matrix

Share this post


Link to post
Share on other sites
Quote:
Original post by Nairb
You're a little off.

glPushMatrix saves the current matrix and glPopMatrix retrieves it (using a stack).

So here's what your code is doing:

push matrix to stack
clear matrix
translate/rotate
draw
pop previous matrix to stack

So you're saving the gluLookAt, then tossing it out just before you draw, then retrieving it just after.

I imagine you want to use glPushMatrix and glPopMatrix so that the rotations/translations of the individual objects is independent. To do that, put a push before the rotation and translation and a pop after the drawing of that object, and get rid of the glLoadIdentity in that function entirely.
In other words:

push matrix
translate/rotate
draw
pop matrix

push matrix
translate/rotate
draw
pop matrix


Ooohhhh, okay. Yup, pretty much a misinterpretation on my part of how the system works, right after I read your post I had an epiphany and saw how I was thinking wrong. I thought gluLookAt actually... damn, I don't know what I was thinking, I thought it moved objects already drawn or something.

Here's what I changed the relevant sections to, for reference.


void RenderScene(void)
{
static float rotPyramid = 0;
static float rotCube = 0;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glTranslatef(-1.5f,0.0f,-10.0f);
glRotatef(rotPyramid, 0.0f, 1.0f, 0.0f);
//glRotatef(135.0f, 0.0f, 1.0f, 0.0f);
glColor3f(0.5f, 0, 0);
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd();
glPopMatrix();

glPushMatrix();
glTranslatef(1.5f, 0.0f, -10.0f);
glRotatef(rotCube, 1.0f, 0.0f, 0.0f);
//glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
glColor3f(0, 0.5f, 0);
glBegin(GL_QUADS);
glVertex3f(-1.0, 1.0, 0.0f);
glVertex3f(1.0, 1.0, 0.0f);
glVertex3f(1.0, -1.0, 0.0f);
glVertex3f(-1.0, -1.0, 0.0f);
glEnd();
glPopMatrix();

glutSwapBuffers();

rotPyramid += 25.0f;
rotCube -= 1.0f;

}

void KeyProc(unsigned char key, int x, int y)
{
if (key == 27) exit(0); // esc
if (key == 97) // a
{
ypos++;
glLoadIdentity();
gluLookAt(xpos, ypos, zpos, xpos, ypos, zpos-1, 0.0, 1.0, 0.0);
}
}

Share this post


Link to post
Share on other sites

This topic is 4110 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this