it’s very simple with opengl.
you have to associate a modelview matrix with every object: this matrix will hold the information about heading and position.
you can build a function like this into the object class:
double mv[4][4];
void transform( rx,ry,rz,tx,ty,tz ) {
glMatrixMode(GL_MODELVIEW);
glLoadMatrixd((double *)mv);
glRotatef(rx,1,0,0);
glRotatef(ry,1,0,0);
glRotatef(rz,1,0,0);
glTranslatef(tx,ty,tz);
glGetDoublev(GL_MODELVIEW_MATRIX,(double *)mv);
}
mv is a private member var of your class.
this works by applying incremental rotations and traslations to the object’s modelview matrix.
into the application:
object.transform(rx,ry,rz,tx,ty,tz);
object.draw();
…even better, you can make the .draw() method to apply the current rotation and traslation only when it is called, so you can use a more intuitive interface, like this:
void rotate( x,y,z ) {
rot.x=x;
rot.y=y;
rot.z=z;
}
void translate( x,y,z ) {
xlt.x=x;
xlt.y=y;
xlt.z=z;
}
void draw() {
glMatrixMode(GL_MODELVIEW);
glLoadMatrixd((double *)mv);
glRotatef(rot.x,1,0,0);
glRotatef(rot.y,0,1,0);
glRotatef(rot.z,0,0,1);
glTranslatef(
xlt.x,
xlt.y,
xlt.z
);
glGetDoublev(GL_MODELVIEW_MATRIX,(double *)mv);
setmem(&rot,sizeof(rot),0);
setmem(&xlt,sizeof(xlt),0);
}
again, rot, xlt and mv are private members.
then your application will look something like this:
object.rotate(
user.rot.x,
user.rot.y,
user.rot.z,
);
object.translate(
user.xlt.x,
user.xlt.y,
user.xlt.z,
);
object.draw();
since you’re operating over the same matrix over and over, sometime (to say, every 30 frames) is good to renormalize the matrix.
also, note that translations will always be executed into object space, so if you translate -100 units along the Z axis, whatever will be your object orientation, it will travel forward.
is it good for you?
Dolo//\ightY
[This message has been edited by dmy (edited 04-27-2000).]
[This message has been edited by dmy (edited 04-27-2000).]