Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: gluLootAt not working in this code..

  1. #1
    Newbie Newbie
    Join Date
    Oct 2013
    Posts
    3

    Unhappy gluLootAt not working in this code..

    Following code is working. but few things bothering me.
    1. when I run the code, gluLookAt command has no effect.
    2. z axis into the screen seems to be the positive.

    please help to solve these two dilemmas

    Code :
    #include <GL/glut.h>
    #include<GL/gl.h>
    #include<GL/glu.h>
     
    double rotate_y=0;
    double rotate_x=0;
     
    void reshape(int w, int h)
    {
    	glViewport(0,0,w,h);
     
    	glMatrixMode(GL_PROJECTION);
    		glLoadIdentity();
    		glOrtho(-5,5,-5,5,-5,5);
     
    		glLoadIdentity();
    		gluLookAt(0.2,0,-2,0,-0.5,0,0,1,0);
    }
     
    void display(void)
    {
    	glClearColor(0,0,0,0);
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    		glLoadIdentity();
    		glRotatef( rotate_x, 1.0, 0.0, 0.0 );
    		glRotatef( rotate_y, 0.0, 1.0, 0.0 );
     
     
    	glColor3f(0,1,0);
    	glBegin(GL_POLYGON);
    		glVertex3f(0.5,-0.5,-1);
    		glVertex3f(0.5,0.5,-1);
    		glVertex3f(-0.5,0.5,-1);
    		glVertex3f(-0.5,-0.5,-1);
    	glEnd();
     
    	glColor3f(1,0,0);
    	glBegin(GL_POLYGON);
    		glVertex3f(0.5,-0.5,-0.8);
    		glVertex3f(0.5,0.5,-0.8);
    		glVertex3f(-0.5,0.5,-0.8);
    		glVertex3f(-0.5,-0.5,-0.8);
    	glEnd();
     
    	glColor3f(0,0,1);
    	glBegin(GL_POLYGON);
    		glVertex3f(0.5,-0.5,-0.2);
    		glVertex3f(0.5,0.5,-0.2);
    		glVertex3f(-0.5,0.5,-0.2);
    		glVertex3f(-0.5,-0.5,-0.2);
    	glEnd();
     
    	glutSwapBuffers();
     
     
    }
     
    void specialKeys( int key, int x, int y ) {
     
      //  Right arrow - increase rotation by 5 degree
      if (key == GLUT_KEY_RIGHT)
        rotate_y += 5;
     
      //  Left arrow - decrease rotation by 5 degree
      else if (key == GLUT_KEY_LEFT)
        rotate_y -= 5;
     
      else if (key == GLUT_KEY_UP)
        rotate_x += 5;
     
      else if (key == GLUT_KEY_DOWN)
        rotate_x -= 5;
     
      //  Request display update
      glutPostRedisplay();
     
    }
     
    int main (int argc, char *argv[])
    {
    	glutInit(&argc, argv);
    	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    	glutInitWindowPosition(100,100);
    	glutInitWindowSize(500,500);
    	glutCreateWindow ("animating triangles");
    	glEnable(GL_DEPTH_TEST);
    	glutDisplayFunc(display);
    	glutReshapeFunc(reshape);
    	glutSpecialFunc(specialKeys);
    	glutMainLoop ();
    	return 0;
    }

  2. #2
    Intern Contributor
    Join Date
    Mar 2014
    Location
    San Jose, CA
    Posts
    58
    On problem 1, you're missing the call to set the matrix mode back to MODELVIEW after setting up the projection. Try to add that line in your reshape() function:
    Code :
    	glMatrixMode(GL_PROJECTION);
    		glLoadIdentity();
    		glOrtho(-5,5,-5,5,-5,5);
     
    	glMatrixMode(GL_MODELVIEW);
    		glLoadIdentity();
    		gluLookAt(0.2,0,-2,0,-0.5,0,0,1,0);

    On problem 2, your parameters to gluLookAt() specify that you're looking from (0.2, 0.0, -2.0) towards (0.0, -0.5, 0.0). Which means that you're looking primarily in the positive z direction (plus somewhat in the negative x direction), resulting in your positive z axis pointing into the screen.

  3. #3
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,143
    Of course, since it is called for the projection-matrix stack and after that annihilated by calling glLoadIdentity!
    After setting the projection, you should revert to Model-View matrix stack. gluLookAt() should be called in display function, right after glLoadIdentity.

  4. #4
    Newbie Newbie
    Join Date
    Oct 2013
    Posts
    3
    Thanks guys!

    what I noticed is that glutlookAt and glrotate doesn't work together...

  5. #5
    Senior Member OpenGL Pro Aleksandar's Avatar
    Join Date
    Jul 2009
    Posts
    1,143
    Quote Originally Posted by kawaikx15 View Post
    what I noticed is that glutlookAt and glrotate doesn't work together...
    Wrong conclusion! gluLookAt() is a combination of rotate/translate operations.
    Just use it as first operation (right after glLoadIdentity) in order to have a predictable result.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •