PDA

View Full Version : camera problems



Eber Kain
07-03-2001, 06:46 AM
here is the camera class ive been useing, i call cam.update() evert frame, and to draw an object i call

glLoadMatrixf(cam.m) ;
glMultMatrixf(object.m);
draw_object();

my camera is all messed up though, you can download this (home.earthlink.net/~eberkain/dl/alpha03.zip) to see what its doing, Am i on the wrong track or what, I also realise that doing all the glGetFloatv(); functions, and loading matrixs like i am isnt very efficent, but im just looking for good functionality, i will come back and optimise things later.

struct CameraS{
int mode;
virtual void init();
virtual void update();
virtual void draw_grid();
float e[15];//eyepoint
float p[15];//look at point
float u[15];//up point
float m[15];//compiled matrix
};
void CameraS::init() {
//Could need function for mode implementation later on
cam.update();
}
void CameraS::update() {
//Load the players matrix
glLoadMatrixf(ship[player].m);
//Move back and up
glTranslatef(0,3,-20);
//save the current matrix as the eyepoint
glGetFloatv(GL_MODELVIEW_MATRIX, cam.e);
//move up
glTranslatef(0,1,0);
//save as the up point
glGetFloatv(GL_MODELVIEW_MATRIX, cam.u);
//move back to the ship
glTranslatef(0,-1,25);
//save as look at point
glGetFloatv(GL_MODELVIEW_MATRIX, cam.p);
//reset the coord system
glLoadIdentity();
//Call look at routine
gluLookAt(cam.e[12],cam.e[13],cam.e[14],cam.p[12],cam.p[13],cam.p[14],cam.u[12],cam.u[13],cam.u[14]);
//save compiled matrix for camera
glGetFloatv(GL_MODELVIEW_MATRIX, cam.m);
}
void CameraS::draw_grid() {
//imbeded for loops to draw a grid for a movement reference.
for (int z = -10; z<10; z++) {
for (int y = -10; y<10; y++) {
glLoadMatrixf(cam.m);
glTranslatef(0,y,z);
glBegin(GL_POINTS);
for (int x = -10; x<10; x++) {
glColor3f(0.3,0.3,0.3);
glVertex2f(x,0);
}
glEnd();
}
}
}

Ritchie
07-03-2001, 01:20 PM
If I understand your code correctly you want to draw some kind of object that you want the camera to follow as you move it around in space. You do not have to build your own viewing matrix.

Instead use gluLookAt to construct it for you. You can use the object's coordinates as the centerx, centery, and centerz. Your eye coordinates are then linked to the center coordinates through some kind of formulae. Your Up vector is always 1 in y-direction as far as I can see, but nothing prohibits you from playing with these values too.

Hope this helps.

Ritchie



[This message has been edited by Ritchie (edited 07-03-2001).]