Results 1 to 8 of 8

Thread: whats wrong with gluLookAt?

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    8

    whats wrong with gluLookAt?

    following gluLookat does not work....can any1 see the problem?

    Code :
    #include <stdio.h>
    #include <stdlib.h>
    #include <GL/glut.h>
     
    #define ESCAPE 27 //use escape key to exit program
     
    GLint Xsize = 400;
    GLint Ysize = 400;
    GLint window_name; //name of our window
     
     
    GLvoid InitWindow(GLfloat Width, GLfloat Height)
    {
     
    	glViewport(0, 0, Width, Height);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluPerspective(45.0,Width/Height,0.0, 200.0);
    	glMatrixMode(GL_MODELVIEW);
     
    }
     
     
    GLvoid ResizeScene(GLint Width, GLint Height)
    {
    	if (Height == 0) Height = 1; //prevent division by zero
    	if (Width == 0) Width = 1; 
    	InitWindow(Width, Height); //reset perspective projection
    }
     
     
     
    GLvoid DrawScene()
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();	
    	glTranslatef(0.0f, 0.0f, -100.0f);
    	glColor3ub(255, 0, 0);		
    	glutSolidSphere(3.0f, 15, 15);
    	glutSwapBuffers();
    }
     
     
     
    void NormalKey(GLubyte key, GLint x, GLint y)
    {
    	switch( key)
    	{
    		case ESCAPE:
    			printf("Escape pressed, exiting..\n");
    			glutDestroyWindow(window_name);
    			exit(0);
    			break; 
     
    		case 'd': 
    			glMatrixMode(GL_MODELVIEW);
    			glLoadIdentity();
    			gluLookAt(10.0f, 0.0f, -100.0f, 0.0f, 0.0f, -100.0f, 0.0f, 0.0f, 1.0f);	
    			break;	
     
    		default:
    			break;
    	}
    }
     
     
    int main(int argc, char **argv)
    {
    	glutInit(&amp;argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    	glutInitWindowSize(Xsize, Ysize);	
    	glutInitWindowPosition(0,0);
    	window_name = glutCreateWindow("Title");
     
    	//Initialise window
    	InitWindow(Xsize, Ysize);	
     
    	//Callback functions (Resize & Draw screen)
    	glutReshapeFunc(ResizeScene);
    	glutDisplayFunc(DrawScene);
    	//glutIdleFunc(DrawScene);	
    	//Callback function when a key is pressed	
    	glutKeyboardFunc(NormalKey);
     
    	glutMainLoop();
    	return 1;
     
    }

  2. #2
    Senior Member Regular Contributor
    Join Date
    Feb 2002
    Posts
    374

    Re: whats wrong with gluLookAt?

    AFAICS you are resetting your modelview matrix in your draw function, overwriting gluLookat's changes to it.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    8

    Re: whats wrong with gluLookAt?

    OK, I understand, but the thing is I have now removed the glutIdleFunc, and the problem is that if I remove glLoadIdentity() from the drawfunction it works fine, but if I include it, then it does not work....any ideas why this happens?


  4. #4
    Senior Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: whats wrong with gluLookAt?

    "then it does not work"
    and what happens? consecutive gluLookAt calls will keep multiplying new matrix with old one. glLoadIdentity prevents this. if it doesnt work that your camera isnt moving its because maybe your values are static for gluLookAt and in the other case they get concatenated together. something like that.

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    8

    Re: whats wrong with gluLookAt?

    It seems to me that your confused with what the problem actually is...

    my gluLookAt works...

    the problem is that it only works if I don't use glLoadIdentity() function in my draw function.

    As far as I know, glLoadIdentity() simply resets the matrix, so why does my program not work when I use glLoadIdentity()?

  6. #6
    Junior Member Newbie
    Join Date
    Oct 2002
    Location
    norway
    Posts
    17

    Re: whats wrong with gluLookAt?

    One way of "fixing" this is to preserve the matrix set up by gluLookAt by using the matrix stack.

    Code :
    GLvoid DrawScene()
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glMatrixMode(GL_MODELVIEW);
    	glPushMatrix();
    	glTranslatef(0.0f, 0.0f, -100.0f);
    	glColor3ub(255, 0, 0);		
    	glutSolidSphere(3.0f, 15, 15);
    	glPopMatrix();
    	glutSwapBuffers();
    }

  7. #7
    Junior Member Newbie
    Join Date
    Jan 2009
    Posts
    8

    Re: whats wrong with gluLookAt?

    yea, thanks...i solved it by using push and pop matrix

    but one thing I don't understand is what's the difference between glLoadIdentity() and pushing and popping???....I thought they were the same thing?

  8. #8
    Senior Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: whats wrong with gluLookAt?

    i think you didnt understood my post. from the start.

    "so why does my program not work when I use glLoadIdentity()?"
    because your params to gluLookAt are static! if glLoadIdentity resets the matrix and you give again those values your camera will be at same position as it was.

    "As far as I know, glLoadIdentity() simply resets the matrix"
    correct. more correct, it sets the matrix to "identity".

    "my gluLookAt works..."
    if it works without glLoadIdentity then because you keep "multiplying" the previous lookat matrix with the new one. thus old values are "concatenated" with the old ones and you get the camera movement.

    to fix this either do it without glLoadIdentity or add/substract from gluLookAt values on key hit.

    gluLookAt(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, oldValue += 1.0f, 0.0f, 1.0f, 0.0f);

    "glLoadIdentity() and pushing and popping???"
    saves the current matrix with push and restores it later with pop. glLoadIdentity sets it to identity matrix so no transformation takes place.

Similar Threads

  1. whats wrong with this code?
    By chrisx16x2008 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 03-12-2009, 08:50 AM
  2. Not sure whats wrong
    By Discoloda in forum OpenGL: GLSL
    Replies: 0
    Last Post: 10-31-2005, 08:58 AM
  3. Whats wrong with this code
    By Anish in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 07-25-2005, 04:28 AM
  4. Whats wrong with this ?
    By freestyler in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 08-27-2002, 05:11 AM
  5. Whats wrong with this!!
    By imported_Sean in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-29-2001, 03:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean