How to use gluLookAt as a camera

Hello everybody!

I’ve been programming some stuff in JOGL. But now i have stumbled upon a problem.

I want to build a camera, to view a couple of openGL rendered objects.

Well what does the camera do now?

If you push ‘k’ or ‘l’, the camera will rotate left or right.
But if my angle hits -pi, or +pi, the camera is rotated!

Now i have read something about multiplying the view matrix with the matrix of the camera.
This leaves me extremely baffeled. I cannot find a good tutorial about it, and i don’t know how to invert it.

Could somebody please tell me how this works? I quite understand maths, but this confuses me a lot.

I have also added my source code (it might be messy), but maybe you can see what i have done wrong.

///////////////////////SOURCE CODE////////////////////////////////////////////////
import javax.media.opengl.;
import javax.media.opengl.glu.
;
import com.sun.opengl.util.;
import javax.swing.
;
import java.awt.event.;
import java.awt.
;

public class FirstPersonControl implements GLEventListener, KeyListener, MouseMotionListener {
private static final GLU glu = new GLU();

//MOVEMENT
public float posx = 0.0f, posz = 0.0f, angx = 0.0f, angz = 0.0f, angle = 0.0f;
public float WALK = 0.01f, RUN = 0.03f, SPEED;
public float ANGLE = 0.0f;
public boolean UP = false, DOWN = false, LEFT = false, RIGHT = false, SHIFT = false;
public boolean ROTL = false, ROTR = false;
//END OF MOVEMENT

GLCanvas glc;

public void display(GLAutoDrawable gld) {
	final GL gl = gld.getGL();
	
	//CONTROLS
	if(SHIFT == true) {
		SPEED = RUN;
	} else {
		SPEED = WALK;
	}
	if(UP == true) {
		posx += SPEED * (float)Math.sin(angle);
		posz += SPEED * (float)Math.cos(angle);
	}
	if(DOWN == true) {
		posx -= SPEED * (float)Math.sin(angle);
		posz -= SPEED * (float)Math.cos(angle);
	}
	if(LEFT == true) {
		//posx += SPEED;
	}
	if(RIGHT == true) {
		//posx -= SPEED;
	}
	if(ROTL == true) {
		angle += 0.005f;
	}
	if(ROTR == true) {
		angle -= 0.005f;
	}
	//END OF CONTROLS
	
	//ANGLE CONTROLS
	angx = 5.0f * (float)Math.sin(angle);
	angz = 5.0f * (float)Math.cos(angle);
	
	//END OF ANGLE CONTROLS
	gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
	
	gl.glMatrixMode(GL.GL_MODELVIEW);
	gl.glLoadIdentity();
	
	glu.gluLookAt(posx, 0, posz, posx + angx, 0, posz + angz + 5.0f, 0, 1, 0);
	
	gl.glPushMatrix();
		gl.glTranslatef(0.0f, 0.0f, 5.0f);
		
		gl.glBegin(GL.GL_TRIANGLES);
			// Front
			gl.glColor3f(0.0f, 1.0f, 1.0f); 
			gl.glVertex3f(0.0f, 1.0f, 0.0f);
			gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			gl.glVertex3f(1.0f, -1.0f, 1.0f);
			
			// Right Side Facing Front
			gl.glVertex3f(0.0f, 1.0f, 0.0f);
			gl.glVertex3f(1.0f, -1.0f, 1.0f);
			gl.glVertex3f(0.0f, -1.0f, -1.0f);
			
			// Left Side Facing Front
			gl.glVertex3f(0.0f, 1.0f, 0.0f);
			gl.glVertex3f(0.0f, -1.0f, -1.0f);
			gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			
			// Bottom
			gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			gl.glVertex3f(1.0f, -1.0f, 1.0f);
			gl.glVertex3f(0.0f, -1.0f, -1.0f);
		gl.glEnd();
	gl.glPopMatrix();
	
	gl.glPushMatrix();
		gl.glTranslatef(0.0f, 0.0f, -5.0f);
		
		gl.glBegin(GL.GL_TRIANGLES);
			// Front
			gl.glColor3f(0.0f, 0.0f, 1.0f); 
			gl.glVertex3f(0.0f, 1.0f, 0.0f);
			gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			gl.glVertex3f(1.0f, -1.0f, 1.0f);
			
			// Right Side Facing Front
			gl.glVertex3f(0.0f, 1.0f, 0.0f);
			gl.glVertex3f(1.0f, -1.0f, 1.0f);
			gl.glVertex3f(0.0f, -1.0f, -1.0f);
			
			// Left Side Facing Front
			gl.glVertex3f(0.0f, 1.0f, 0.0f);
			gl.glVertex3f(0.0f, -1.0f, -1.0f);
			gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			
			// Bottom
			gl.glColor3f(0.5f, 0.0f, 0.5f); 
			gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			gl.glVertex3f(1.0f, -1.0f, 1.0f);
			gl.glVertex3f(0.0f, -1.0f, -1.0f);
		gl.glEnd();
	gl.glPopMatrix();
	
	gl.glPushMatrix();
		gl.glTranslatef(5.0f, 0.0f, 0.0f);
		
		gl.glBegin(GL.GL_TRIANGLES);
			// Front
			gl.glColor3f(0.0f, 1.0f, 0.0f); 
			gl.glVertex3f(0.0f, 1.0f, 0.0f);
			gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			gl.glVertex3f(1.0f, -1.0f, 1.0f);
			
			// Right Side Facing Front
			gl.glVertex3f(0.0f, 1.0f, 0.0f);
			gl.glVertex3f(1.0f, -1.0f, 1.0f);
			gl.glVertex3f(0.0f, -1.0f, -1.0f);
			
			// Left Side Facing Front
			gl.glVertex3f(0.0f, 1.0f, 0.0f);
			gl.glVertex3f(0.0f, -1.0f, -1.0f);
			gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			
			// Bottom
			gl.glColor3f(0.5f, 0.0f, 0.5f); 
			gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			gl.glVertex3f(1.0f, -1.0f, 1.0f);
			gl.glVertex3f(0.0f, -1.0f, -1.0f);
		gl.glEnd();
	gl.glPopMatrix();
	
	gl.glPushMatrix();
		gl.glTranslatef(-5.0f, 0.0f, 0.0f);
		
		gl.glBegin(GL.GL_TRIANGLES);
			// Front
			gl.glColor3f(1.0f, 1.0f, 1.0f); 
			gl.glVertex3f(0.0f, 1.0f, 0.0f);
			gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			gl.glVertex3f(1.0f, -1.0f, 1.0f);
			
			// Right Side Facing Front
			gl.glVertex3f(0.0f, 1.0f, 0.0f);
			gl.glVertex3f(1.0f, -1.0f, 1.0f);
			gl.glVertex3f(0.0f, -1.0f, -1.0f);
			
			// Left Side Facing Front
			gl.glVertex3f(0.0f, 1.0f, 0.0f);
			gl.glVertex3f(0.0f, -1.0f, -1.0f);
			gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			
			// Bottom
			gl.glColor3f(0.5f, 0.0f, 0.5f); 
			gl.glVertex3f(-1.0f, -1.0f, 1.0f);
			gl.glVertex3f(1.0f, -1.0f, 1.0f);
			gl.glVertex3f(0.0f, -1.0f, -1.0f);
		gl.glEnd();
	gl.glPopMatrix();
}

public void init(GLAutoDrawable gld) {
	final GL gl = gld.getGL();
	
	gl.glShadeModel(GL.GL_SMOOTH);
	gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	gl.glClearDepth(1.0f);
	gl.glEnable(GL.GL_DEPTH_TEST);
	gl.glDepthFunc(GL.GL_LEQUAL);
	gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
}

public void reshape(GLAutoDrawable gld, int x, int y, int width, int height) {
	final GL gl = gld.getGL();
	
	if(height < 1) {
		height = 1;
	}
	
	final float ratio = (float)width / (float)height;
	gl.glMatrixMode(GL.GL_PROJECTION);
	gl.glLoadIdentity();
	glu.gluPerspective(50.0f, ratio, 1.0f, 1000.0f);
	gl.glMatrixMode(GL.GL_MODELVIEW);
	gl.glLoadIdentity();
}

public void displayChanged(GLAutoDrawable gld, boolean modeChanged, boolean deviceChanged) {}

public void keyPressed(KeyEvent e) {
	switch(e.getKeyCode()) {
		case KeyEvent.VK_W:
			UP = true;
			break;
		case KeyEvent.VK_S:
			DOWN = true;
			break;
		case KeyEvent.VK_A:
			LEFT = true;
			break;
		case KeyEvent.VK_D:
			RIGHT = true;
			break;
		case KeyEvent.VK_SHIFT:
			SHIFT = true;
			break;
		case KeyEvent.VK_K:
			ROTL = true;
			break;
		case KeyEvent.VK_L:
			ROTR = true;
			break;
		case KeyEvent.VK_ESCAPE:
		case KeyEvent.VK_F10:
			System.exit(0);
			break;
	}
	
	glc.repaint();
}

public void keyReleased(KeyEvent e) {
	switch(e.getKeyCode()) {
		case KeyEvent.VK_W:
			UP = false;
			break;
		case KeyEvent.VK_S:
			DOWN = false;
			break;
		case KeyEvent.VK_A:
			LEFT = false;
			break;
		case KeyEvent.VK_D:
			RIGHT = false;
			break;
		case KeyEvent.VK_SHIFT:
			SHIFT = false;
			break;
		case KeyEvent.VK_K:
			ROTL = false;
			System.out.println(angle);
			break;
		case KeyEvent.VK_L:
			ROTR = false;
			System.out.println(angle);
			break;
		}
	
}

public void keyTyped(KeyEvent e) {
}

public void mouseMoved(MouseEvent m) {
}

public void mouseDragged(MouseEvent m) {
}

public void getGLCanvas(GLCanvas glc) {
	this.glc = glc;
}

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Anyway, thanks for reading!

I hope somebody can help me, because i’m not sure how to continue

Sorry i solved it already!

I have used glTranslatef and glRotatef, instead of gluLookAt!