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 2 of 2

Thread: Rotate camera around scene or the likes

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2009
    Posts
    6

    Rotate camera around scene or the likes

    Hi am trying to rotate a camera around my scene I have, the camera position is in this function:

    Code :
    void reshape(int width, int height)
     
    {
     
    	glViewport(0, 0, (GLsizei) width, (GLsizei) height);
     
     
     
    	glMatrixMode(GL_PROJECTION);
     
    	glLoadIdentity();
     
     
     
    	/*glOrtho(-(float) 2.0*CUBE_SIZE, (float) 2.0*CUBE_SIZE, -(float) 2.0*CUBE_SIZE,
     
    	           (float) 2.0*CUBE_SIZE, zm,  (float)40.0*CUBE_SIZE);*/
     
     
    	glFrustum(-(float) 2.0*CUBE_SIZE, (float) 2.0*CUBE_SIZE, -(float) 2.0*CUBE_SIZE, 
     
    			   (float) 2.0*CUBE_SIZE, zm,  (float)40.0*CUBE_SIZE);
     
    	glMatrixMode(GL_MODELVIEW);
     
     
     
    }

    How can I rotate the camera but still stay looking at the center of my scene?

  2. #2
    Member Regular Contributor
    Join Date
    Mar 2007
    Location
    CA
    Posts
    418

    Re: Rotate camera around scene or the likes

    You have described the viewing volume and how it is mapped to the pixels of your monitor. Everything you have is correct but it is just not describing the camera position ... To place and orient the camera apply the gluLookAt function to the GL_MODELVIEW matrix with center (x,y,z)=(0,0,0) or whatever the center of your scene is in global coordinates.
    Code :
    	  void gluLookAt( GLdouble eyeX,
    			  GLdouble eyeY,
    			  GLdouble eyeZ,
    			  GLdouble centerX,
    			  GLdouble centerY,
    			  GLdouble centerZ,
    			  GLdouble upX,
    			  GLdouble upY,
    			  GLdouble upZ )
    so you would add this add to the _beginning_ of your the render display function a camera as follows:
    Code :
    void display()
    {
            //for example to spin the camera located in the xy-plane
            // around the z-axis
            // the camera stays looking at the center 0,0,0 all the time
            // and is located a distance .5 from the center
            static angle = 0.0;
            angle +=  360./10.;
     
            // set camera parameters
            GLdouble eyeX=.5*cos(angle);
    	GLdouble eyeY=.5*sin(angle);
    	GLdouble eyeZ=0.;
    	GLdouble centerX=0;
    	GLdouble centerY=0;
    	GLdouble centerZ=0;
    	GLdouble upX=0.;
    	GLdouble upY=0.;
    	GLdouble upZ=1.;
     
    	glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
     
            gluLookAt(eyeX,eyeY,eyeZ,
                      centerX,centery,centerZ,
                      upX,upY,upZ);
     
            // start drawing your scene objects after gluLookAt
            //... glPushMatrix ... glPopMatrix ..
    }
    Note, your reshape(int width, int height) function is fine and should remain unchanged. The camera goes into your display() function since you want to change it from frame to frame for animation effect.

    A detailed but well worth reading chapter on openGL camera, transformations, etc is OpenGL Redbook, Chapter 3

Posting Permissions

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