use these functions for rotating:
you need 2 4x4 matrices for this.
GLdouble mat[16];
GLdouble invmat[16];
the inverse matrix is used for camera translation, so if you want to view from the perspective of the object you’re translating, feed your camera object with the inverse matrix and do
glMultMatrixd(invmat);
glTranslatef(-x, -y, -z);
ok now the rotation functions:
void matxrot(GLdouble *mat, GLdouble *invmat, GLfloat rot)
{
GLdouble c=cos(grad2rad(rot));
GLdouble s=sin(grad2rad(rot));
GLdouble tmp[16],tmp1[16],tmp2[16];
tmp[0]=1; tmp[1]=0; tmp[2]=0; tmp[3]=0;
tmp[4]=0; tmp[5]=c; tmp[6]=s; tmp[7]=0;
tmp[8]=0; tmp[9]=-s; tmp[10]=c; tmp[11]=0;
tmp[12]=0; tmp[13]=0; tmp[14]=0; tmp[15]=1;
matmultrot(mat,tmp,tmp1);
tmp[0]=1; tmp[1]=0; tmp[2]=0; tmp[3]=0;
tmp[4]=0; tmp[5]=c; tmp[6]=-s; tmp[7]=0;
tmp[8]=0; tmp[9]=s; tmp[10]=c; tmp[11]=0;
tmp[12]=0; tmp[13]=0; tmp[14]=0; tmp[15]=1;
matmultrot(tmp,invmat,tmp2);
for(int i=0;i<16;i++)
{
mat[i]=tmp1[i];
invmat[i]=tmp2[i];
}
}
// y rotation relative to current orientation
void matyrot(GLdouble *mat, GLdouble *invmat, GLfloat rot)
{
GLdouble c=cos(grad2rad(rot));
GLdouble s=sin(grad2rad(rot));
GLdouble tmp[16],tmp1[16],tmp2[16];
tmp[0]=c; tmp[1]=0; tmp[2]=-s; tmp[3]=0;
tmp[4]=0; tmp[5]=1; tmp[6]=0; tmp[7]=0;
tmp[8]=s; tmp[9]=0; tmp[10]=c; tmp[11]=0;
tmp[12]=0; tmp[13]=0; tmp[14]=0; tmp[15]=1;
matmultrot(mat,tmp,tmp1);
tmp[0]=c; tmp[1]=0; tmp[2]=s; tmp[3]=0;
tmp[4]=0; tmp[5]=1; tmp[6]=0; tmp[7]=0;
tmp[8]=-s; tmp[9]=0; tmp[10]=c; tmp[11]=0;
tmp[12]=0; tmp[13]=0; tmp[14]=0; tmp[15]=1;
matmultrot(tmp,invmat,tmp2);
for(int i=0;i<16;i++)
{
mat[i]=tmp1[i];
invmat[i]=tmp2[i];
}
}
// z rotation relative to current orientation
void matzrot(GLdouble *mat, GLdouble *invmat, GLfloat rot)
{
GLdouble c=cos(grad2rad(rot));
GLdouble s=sin(grad2rad(rot));
GLdouble tmp[16],tmp1[16],tmp2[16];
tmp[0]=c; tmp[1]=s; tmp[2]=0; tmp[3]=0;
tmp[4]=-s; tmp[5]=c; tmp[6]=0; tmp[7]=0;
tmp[8]=0; tmp[9]=0; tmp[10]=1; tmp[11]=0;
tmp[12]=0; tmp[13]=0; tmp[14]=0; tmp[15]=1;
matmultrot(mat,tmp,tmp1);
tmp[0]=c; tmp[1]=-s; tmp[2]=0; tmp[3]=0;
tmp[4]=s; tmp[5]=c; tmp[6]=0; tmp[7]=0;
tmp[8]=0; tmp[9]=0; tmp[10]=1; tmp[11]=0;
tmp[12]=0; tmp[13]=0; tmp[14]=0; tmp[15]=1;
matmultrot(tmp,invmat,tmp2);
for(int i=0;i<16;i++)
{
mat[i]=tmp1[i];
invmat[i]=tmp2[i];
}
}
the 3rd parameter is always used to store the result matrix.
matmultrot is just a standard matrix multplication.
now this is how to move and rotate your object:
-
set both matrices to identity
-
rotate left/right:
rotatey(mat,invmat,angle_increment);
-
up/down: use rotatex
-
roll left/right: use rotatez
-
move forward/backward in respect to your orientation:
GLfloat dx=-mat[8];
GLfloat dy=-mat[9];
GLfloat dz=-mat[10];
x+=(dx)*spd;
y+=(dy)*spd;
z+=(dz)*spd;
where spd is your movement speed.
ah yes, set t identity only for initializing. if you want to control your camera directly using this, use invmat for 5) and mat for glMutlMatrixd
hope this was clear
eik
[This message has been edited by hoshi55 (edited 03-25-2002).]