PDA

View Full Version : Camera Class



David Doria
10-29-2008, 08:07 AM
I am trying to make an easy way to rotate and zoom in a scene.

The problem with this code is that the view doesn't change. When I output the camera parameters at each display() call, they are changing the way they should. Is there any blatant problem with why the screen is not updating?

Thanks,

Dave



void display(void)
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
DrawAxes(5, true);

Camera.Look();

glutSwapBuffers();
}




void CameraMouseMotion(int x, int y)
{
int verbosity = 1;

const int dx = x - Mouse.X;
const int dy = y - Mouse.Y;

if ( (dx == 0) && (dy == 0) )
return;

if (Mouse.Middle) //zoom
{
//Moving the mouse up zooms in
//Moving the mouse down zooms out
double scale = ((double)dy * .01);
geom_Vector3 MotionAlongView = Camera.ViewDirection().Normalized() * scale;
Camera.Position = Camera.Position + MotionAlongView;
Camera.ViewDepth += MotionAlongView.Magnitude();
}
else if (Mouse.Left) //rotate
{
double scale = .01;
Camera.theta += scale * dx;
Camera.phi += scale * dy;
}
else if (Mouse.Right) //translate
{
//moving the mouse left and right pans
//moving the mouse up and down moves the camera up and down
double scale = .01;
geom_Vector3 V(double(dx) * scale, double(dy) * scale, 0);
Camera.Position = Camera.Position + V;
}

Mouse.X = x;
Mouse.Y = y;

glutPostRedisplay();
}




void CameraClass::Look()
{
//cout << *this;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(0, 10, 10, 0, 0, 0, 0, 1, 0);
gluLookAt(Position.getX(), Position.getY(), Position.getZ(), CenterOfView().getX(), CenterOfView().getY(), CenterOfView().getZ(), 0, 1, 0);

//eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz
}

Rosario Leonardi
10-29-2008, 10:01 AM
You first draw and then you move the camera.
Try this.



void display(void)
{
// glClearColor(1,1,1,1);
// you can set the clear color only once in the init function,
// when you have a valid context
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
// Useless, the camera sets the matrix

Camera.Look();

// if you want to move your object you need to use
// a glMultMatrix here (or use translate and rotate function)
DrawAxes(5, true);
glutSwapBuffers();
}


You also have to set the GL_PROJECTION MATRIX to something more relevant then the identity matrix. ;)

ps: the camera motion function don't make any sense to me. :(

David Doria
10-29-2008, 10:09 AM
Ah that was mistake just leaving identity in the projection matrix. Drawing the scene after setting the camera fixed the problem (still not sure why that makes a difference?)

Also, what do you not understand about the camera motion functions? I am just trying to move the camera around based on mouse inputs. How is this usually done?

Dave

Rosario Leonardi
10-29-2008, 10:22 AM
openGL work sequentially, and every time you draw something is drawn on the frame buffer using the current states.
So if you do this:

glLoadIdentity()
drawSomething(); // this will not moves
moveCamera();
drawSomethingElse(); // this is draw using the camera transformation

Read the NeHe tutorial about camera and transformation.

The projection matrix is used to set the z near and far plane, also is used to set the screen ratio and the camera FOV. If you don't set the projection matrix you will have a very distorted image. You can also create some advanced tricks.

The camera functions... bah... checkit. A correct camera motion function should take care of the current orientation of the camera. But I don't want to ruins the pleasure or programming. :)

David Doria
10-29-2008, 10:25 AM
Yea thats what I did.... set the position and orientation of the camera...