Problem with gluLookAt

Started by jacekcichy, October 15, 2009, 06:36:19

Previous topic - Next topic

jacekcichy

Hello everybody!! :)

I was reading some post about gluLookAt method but I still have problem. I would like to move my "camera" using keyboard, but it doesn't work. Please help me :)

initMethod:
private static void initSystem(){
		try {
			//tworzymy displej chyba ramke gry
		    Display.setDisplayMode(chosenMode);
		    Display.setTitle("TestKamery");
		    Display.create();
		    //depth buffer sobie tworzymy
			GL11.glClearDepth(1.0f);
			GL11.glEnable(GL11.GL_DEPTH_TEST);
			GL11.glDepthFunc(GL11.GL_LEQUAL);
			//kolor tla
			GL11.glClearColor(0,0,0,0);
			//inicjacja modelu shadera
			GL11.glShadeModel(GL11.GL_SMOOTH);
		    
			//projekcja perspektywy
			GL11.glViewport(0, 0, targetWidth, targetHeight);
			//przypisujemy m.rzutowania -> m.jednostkowa
			GL11.glMatrixMode(GL11.GL_PROJECTION);
		    GL11.glLoadIdentity();
		    
		    GLU.gluPerspective(45.0f,targetWidth / targetHeight,1.0f,1000.0f);
						
		    GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);	
		    	    
		    //culling
		    // ustaw Culling Face To Back Face
		    GL11.glCullFace(GL11.GL_BACK);                     
	        GL11.glEnable(GL11.GL_CULL_FACE);
	        	        
		} catch (LWJGLException e) {
		    Sys.alert("Unable to create display.","");
		    System.exit(0);
		}				
	}


GameLoop:
private static void run(){
		boolean gameRunning = true;
						
		
		//petla gry
		while (gameRunning) {
			
			// wywoluj funkcje gamelogic
		    GameLogic();
		    		    
			// czyscimy obraz i Depth Buffer
			GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);          
									
			GL11.glMatrixMode(GL11.GL_MODELVIEW);
			GLU.gluLookAt(PozX, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
			GL11.glLoadIdentity();
			
			//reset ukladu wspolrzednych do 0.0.0
			GL11.glLoadIdentity();
						
			GL11.glTranslatef(0, 0, -200f);
			GL11.glRotatef(RotZ, 0, 1, 0);
						
			GL11.glBegin(GL11.GL_TRIANGLES);
				GL11.glColor3f(0.5f,0.2f,0.1f);
	        	GL11.glVertex3i(-10, -10, 0);
	        	GL11.glVertex3i(10, -10, 0);
	        	GL11.glVertex3i(0, 20, -10);
	        	GL11.glColor3f(0.1f,0.2f,0.5f);
	        	GL11.glVertex3i(0, 20, -10);
	        	GL11.glVertex3i(10, -10, 0);
	        	GL11.glVertex3i(0, -10, -20);
	        	GL11.glColor3f(0.2f,0.5f,0.1f);
	        	GL11.glVertex3i(0, -10, -20);
	        	GL11.glVertex3i(-10, -10, 0);
	        	GL11.glVertex3i(0, 20, -10);
	        	GL11.glColor3f(0.2f,0.1f,0.5f);
	        	GL11.glVertex3i(-10, -10, 0);
	        	GL11.glVertex3i(0, -10, -20);
	        	GL11.glVertex3i(10, -10, 0);
	        GL11.glEnd();           
            
		    
            // now tell the screen to update
			Display.update();
					 
		    // sprawdzamy czy uzytkownik niechce wyjsc, jezeli tak to wychodzimy z petli 
		    if (Display.isCloseRequested()) {
		    	gameRunning = false;
		        Display.destroy();
		        System.exit(0);
		    }			    
		}		    		    
	}

Ciardhubh

Quote from: jacekcichy on October 15, 2009, 06:36:19
...
			GL11.glMatrixMode(GL11.GL_MODELVIEW);
			GLU.gluLookAt(PozX, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
			GL11.glLoadIdentity();
			
			//reset ukladu wspolrzednych do 0.0.0
			GL11.glLoadIdentity();
...


You are overwriting whatever gluLookAt has set up with the identity matrix, twice.

Use:
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GLU.gluLookAt(PozX, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

and push pop from and to the matrix stack afterwards rather than glLoadIdentity.

See also:
http://www.opengl.org/resources/faq/technical/viewing.htm

jacekcichy

It works!! ;D Thanks a lot Ciardhubh!
I have a little request could You show me in my code use of Push and PopMatrix method? It will help me to understand this stack operation.








Ciardhubh

That's fairly easy. By calling glPushMatrix(), you make a copy of the current matrix and put in on the matrix stack for the current matrix mode. Then you call your translates, rotates, scales and draw what you want to draw. Afterwards you call glPopMatrix() and the previous matrix will be active again, undoing all matrix manipulations like translate since the last push.

E.g.:
glPushMatrix
// translate, rotate, scale
// draw
glPopMatrix

jacekcichy