Issues with gluLookAt

Started by Richie551, August 27, 2012, 01:24:01

Previous topic - Next topic

Richie551

So once again I am back to these forums for more help.  I finally decided to begin working with the 3D aspects of openGL, and have run into issues with gluLookAt.  The following line of code to *supposively* aim the camera at a rendered cube.
GLU.gluLookAt(200, 100, 0, 500, 400, 0, 0, 0, 0);

However, the camera does not aim at this cube.
The cube is created using the following code pieces.
This creates a cube entity and puts it at a specified position
cube = new Cube(this, 500, 400, 0);

And this is what draws the actual cube as of current
GL11.glTranslatef(x, y, z);
		
		GL11.glBegin(GL11.GL_QUADS);
		{
			//Six Sides Needed
			
			//Back
			GL11.glColor3f(1, 0, 0);
			
			GL11.glVertex3f(0, 0, 50);
			GL11.glVertex3f(0, 50, 50);
			GL11.glVertex3f(50, 50, 50);
			GL11.glVertex3f(50, 0, 50);
			
			//Front
			GL11.glColor3f(1, 1, 0);
			
			GL11.glVertex3f(0, 0, 0);
			GL11.glVertex3f(0, 50, 0);
			GL11.glVertex3f(50, 50, 0);
			GL11.glVertex3f(50, 0, 0);
			
			//Bottom
			GL11.glColor3f(0.5f, 0.5f, 1);
			
			GL11.glVertex3f(0, 0, 0);
			GL11.glVertex3f(50, 0, 0);
			GL11.glVertex3f(50, 0, 50);
			GL11.glVertex3f(0, 0, 50);
			
			//Top
			GL11.glColor3f(1, 0.5f, 0.5f);
			
			GL11.glVertex3f(0, 50, 0);
			GL11.glVertex3f(50, 50, 0);
			GL11.glVertex3f(50, 50, 50);
			GL11.glVertex3f(0, 50, 50);
			
			//Side
			GL11.glColor3f(1, 1, 1);
			
			GL11.glVertex3f(0, 0, 0);
			GL11.glVertex3f(0, 50, 0);
			GL11.glVertex3f(0, 50, 50);
			GL11.glVertex3f(0, 0, 50);
			
			//Side
			GL11.glColor3f(0.5f, 0.5f, 0.5f);
			
			GL11.glVertex3f(50, 0, 0);
			GL11.glVertex3f(50, 50, 0);
			GL11.glVertex3f(50, 50, 50);
			GL11.glVertex3f(50, 0, 50);
			
		}
		GL11.glEnd();

This was some quick code I wrote up to draw a simple cube.
The question is, why does this gluLookAt code not look at the cube?
Sorry if I'm missing something very obvious, gluLookAt has me very confused, I understand what it should do, but I'm confused as to how I should do it.
Strangely enough, this piece of code partially works, but not as I need it to.
GLU.gluLookAt(500, 400, -50, 500, 400, 0, 1, 0, 0);

Any help on this matter would be appriciated, thanks.

Fool Running

I think your problem is your "up vector" (the last 3 arguments to gluLookAt() ). This tells the function which way is up so it can put the camera in an orientation that looks correct. I'm guessing passing (0, 0, 0) does bad things to the algorithm.

Assuming that you want +y axis to be up, then you should have (0, 1, 0) as your last 3 arguments.

Also keep in mind that you will be looking at one corner of the cube since you have a corner at (0, 0, 0).
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Richie551

Thank you for the responce Fool Running.  I have in the meantime been madly researching the GLU.gluLookAt function, and so far, have had better results using translations and rotations, but I am determined to get it working using gluLookAt.  By making some modifications, I got to a point of being able to atleast have the cube on the screen rotated the incorrect way.

Using this code piece:
GLU.gluLookAt(0, 400, 0, 500, 400, 0, 0, 1, 0);

I am still not quite grasping GLU.gluLookAt yet though.
For some reason I am, once in this position, unable to do anything else with that gluLookAt command without making the cube vanish.  For example, if I try to get the cube moved to the left or the right by changing the eyeX the cube disappears.  When using the centerX, a similar thing happens.
Can anyone help me as to using gluLookAt properly to get correct results?
I am stumped as to how to do it.
Any help would be appriciated

Fool Running

Could you paste your entire render method?

EDIT: My best guess is that maybe you're attempting to use this on a projection made with glOrtho or that you aren't setting up your projection matrix at all.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Richie551

You were right, fool running, that I am using glOrtho, am I not supposed to be using glOrtho?  I always thought that was the 'thing' to use for 2D and 3D implementations.  Am I supposed to be doing something different for 3D implementations?
By the way, here is all of my code relating to the rendering:
This is the setup piece that pretty much sets everything up such as the projection matrix:
public void init()
	{
		try {
			Mouse.setGrabbed(true);
			
			Display.setTitle(TITLE);
			Display.setDisplayMode(new DisplayMode(width, height));
			Display.create();
			
			GL11.glMatrixMode(GL11.GL_PROJECTION);
			GL11.glLoadIdentity();
			
			GL11.glOrtho(0, width, height, 0, -1000, 1000);
			
			GL11.glMatrixMode(GL11.GL_MODELVIEW);
			GL11.glLoadIdentity();
		} catch (LWJGLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		initEnt();
	}

This is the piece that handles rendering of cubes, I have a few extra bits and bobs commented out for if I ever wanted to add textures to the cube:
public void draw(float x, float y, float z, float rot)
	{
		//GL11.glEnable(GL11.GL_TEXTURE_2D);
		
		GL11.glEnable(GL11.GL_DEPTH_TEST);
		GL11.glDepthFunc(GL11.GL_NEAREST);
		
		GL11.glPushMatrix();
		GL11.glTranslatef(x, y, z);
		
		GL11.glBegin(GL11.GL_QUADS);
		{
			//Six Sides Needed
			
			//Back
			GL11.glColor3f(1, 0, 0);
			
			GL11.glVertex3f(0, 0, 50);
			GL11.glVertex3f(0, 50, 50);
			GL11.glVertex3f(50, 50, 50);
			GL11.glVertex3f(50, 0, 50);
			
			//Front
			GL11.glColor3f(1, 1, 0);
			
			GL11.glVertex3f(0, 0, 0);
			GL11.glVertex3f(0, 50, 0);
			GL11.glVertex3f(50, 50, 0);
			GL11.glVertex3f(50, 0, 0);
			
			//Bottom
			GL11.glColor3f(0.5f, 0.5f, 1);
			
			GL11.glVertex3f(0, 0, 0);
			GL11.glVertex3f(50, 0, 0);
			GL11.glVertex3f(50, 0, 50);
			GL11.glVertex3f(0, 0, 50);
			
			//Top
			GL11.glColor3f(1, 0.5f, 0.5f);
			
			GL11.glVertex3f(0, 50, 0);
			GL11.glVertex3f(50, 50, 0);
			GL11.glVertex3f(50, 50, 50);
			GL11.glVertex3f(0, 50, 50);
			
			//Side
			GL11.glColor3f(1, 1, 1);
			
			GL11.glVertex3f(0, 0, 0);
			GL11.glVertex3f(0, 50, 0);
			GL11.glVertex3f(0, 50, 50);
			GL11.glVertex3f(0, 0, 50);
			
			//Side
			GL11.glColor3f(0.5f, 0.5f, 0.5f);
			
			GL11.glVertex3f(50, 0, 0);
			GL11.glVertex3f(50, 50, 0);
			GL11.glVertex3f(50, 50, 50);
			GL11.glVertex3f(50, 0, 50);
			
		}
		GL11.glEnd();
		
		GL11.glPopMatrix();
		
	}

And this is what runs in my loop and controls the gluLookAt
while(!Display.isCloseRequested() && !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
		{
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
			
			GL11.glMatrixMode(GL11.GL_MODELVIEW);
			GL11.glLoadIdentity();
			
			
			GLU.gluLookAt(0, 400, 0, 500, 400, 0, 0, 1, 0);
			/*
			GL11.glTranslatef(45, 360, 0);

			GL11.glRotated(45, 0, 1, 0);
			GL11.glRotated(roty, 1, 0, 0);
			*/
			
			frameLoop();
			
			Display.update();
		}
		Display.destroy();
		System.exit(-1);

I have absolutely no idea what is wrong, any help is appriciated.

Fool Running

Yes, gluLookAt does not work well with glOrtho views. See http://stackoverflow.com/questions/1401326/gluperspective-vs-gluortho2d for an explanation of the differences (first place I found). Basically, glOrtho is used for 2D stuff and gluPerspective is usually used for 3D stuff.

What you want to do is set up a 3D perspective by changing your glOrtho call to something like:
gluPerspective(60.0f, width / (float)height, 0.4f, 1000f);

Hope that helps ;D
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Richie551

Thank you a lot for all the help you gave me Fool Running.  After doing some reading and messing around with these concepts, I now understand gluLookAt and gluPerspective, and why I shouldn't use glOrtho for a non-2D view.  I do have a question though, for the glPerspective example you gave me, why did you put 0.4 as the ZNear, and why did you make height a float?  I noticed in my experimenting that if height were not a float value, it skews the display, why is that?  Again, thanks for the help you have been giving me Fool Running.

Fool Running

zNear is how close an object can get to the camera before being clipped. This is typically as large as you can make it while still being able to see stuff at a reasonable closeness. The reason for this is that the z-buffer used to determine whether an object is closer/further then another object when rendering has a set accuracy (say 16 or 24 bits). The closer the zNear gets to 0.0, the more this accuracy is used up by objects that are close to the camera. See http://www.opengl.org/archives/resources/faq/technical/depthbuffer.htm for more information.

The reason that height is cast to a float is because otherwise it would produce a result of 1 (since it's using integer math). This is the aspect ratio of the width to the height so it is best expressed as a float (unless the height happens to be an exact multiple of the width).
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D