PDA

View Full Version : Rotate camera around scene or the likes



truetaurus
10-25-2009, 04:13 AM
Hi am trying to rotate a camera around my scene I have, the camera position is in this function:



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?

marshats
10-25-2009, 06:49 AM
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 (http://publib.boulder.ibm.com/infocenter/systems/index.jsp?topic=/com.ibm.aix.opengl/doc/openglrf/gluLookAt.htm) 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.


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:


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 (http://glprogramming.com/red/chapter03.html)