   LookAt matrix
  Hello everyone ,i am trying to make lookat matrix for my camera class i made it from what Blue book said but when i use my function i get some weird effects when i do camera rotation but when i use camera's vectors in gluLookAt* it works =( this is the code :template<class T>void Camera<T>::setuplookat(){static 3DVECTOR<T> f,up,s,u;static matrix<T> tra;f.set(targ.x-pos.x,targ.y-pos.y,targ.z-pos.z);f.normalize();up=upv;up.normalize();right.CrossProduct((targ-pos),up);s.CrossProduct(f,up);u.CrossProduct(s,f);LookAt.Unit(); tra.Unit();LookAt.arrMat[0]=s.x;LookAt.arrMat[4]=s.y;LookAt.arrMat[8]=s.z;LookAt.arrMat[1]=u.x;LookAt.arrMat[5]=u.y;LookAt.arrMat[9]=u.z;LookAt.arrMat[2]=-f.x;LookAt.arrMat[6]=-f.y;LookAt.arrMat[10]=-f.z;tra.arrMat[12]=-pos.x;tra.arrMat[13]=-pos.y; tra.arrMat[14]=-pos.z;LookAt=LookAt*tra;} tra matrix for the translating like what blue book said that after glMultMatrix(lookat) there is glTranslate*(-eyex,-eyey,-eyez)   
  Since the cross product of two unit vectors isn't necessarily a unit vector itself, you should normalize the s and u vectors after the cross product to make sure they are unit length.And that also means there is no need to normalize the up vector before the cross products aswell, although it doesn't make any difference with respect to how the function behaves. It's just an unnecessary normalization.I assume the vector and matrix functions are correct though. I'm not sure whether the LookAt matrix is constructed correct, but it looks OK.   ;  I think you also need LookAt.arrMat[15] = 1;There may be other mistakes here also, not sure. Any reason why you don't want to use gluLookAt()?Edit: nevermind, didn't see the unit matrix call.[Edited by - lightbringer on June 19, 2006 6:48:38 AM];  thank you for ur post ^^i tried normalizing s and u vector now i am getting much better results but somehow when i do rotation it happens on the target point not the camera itselftemplate<class T>void Camera<T>::setuplookat(){static 3DVECTOR<T> f,up,s,u;static matrix<T> tra;f.set(targ.x-pos.x,targ.y-pos.y,targ.z-pos.z);f.normalize();up=upv;//up.normalize();right.CrossProduct((targ-pos),up);s.CrossProduct(f,up);u.CrossProduct(s,f);s.normalize();u.normalize();LookAt.Unit(); tra.Unit();LookAt.arrMat[0]=s.x;LookAt.arrMat[4]=s.y;LookAt.arrMat[8]=s.z;LookAt.arrMat[1]=u.x;LookAt.arrMat[5]=u.y;LookAt.arrMat[9]=u.z;LookAt.arrMat[2]=-f.x;LookAt.arrMat[6]=-f.y;LookAt.arrMat[10]=-f.z;tra.arrMat[12]=-pos.x;tra.arrMat[13]=-pos.y; tra.arrMat[14]=-pos.z;LookAt=LookAt*tra;} about the matrix/vector class they work great i tested them thank you again ^^   ;  Could you post the code for how you multiply two matrices? Your problem sounds like wrong order of transformation, so it could be that the line LookAt=LookAt*tra; actually performs LookAt=tra*LookAt;.   ;  thank you :D it is like what u say LookAt=tra*LookAt ^^ thanks ;  But if you had to change the order, then it means your multiplication code IS multiplying them in the wrong order. The wrong order with respect to how OpenGL handles matrices that is.Maybe you intend to use it for some API independent code, but it's inconsistent with OpenGL at least. If you focus on OpenGL only, maybe you should concider rewriting your multiplication code to make the multiplication the other way around, for the sake of concistency.   ;  hmmm so u mean that the multiplication function that i made it isn't in opengl way ?my function is notmal-matix multiplication so do i need to change it?matrix <T>operator*(const matrix<T>  &v){static matrix<T> tmp;tmp.arrMat[0] = (this->arrMat[0]*v.arrMat[0]) + (this->arrMat[1]*v.arrMat[4]) + (this->arrMat[2]*v.arrMat[8]) + (this->arrMat[3]*v.arrMat[12]);tmp.arrMat[1] = (this->arrMat[0]*v.arrMat[1]) + (this->arrMat[1]*v.arrMat[5]) + (this->arrMat[2]*v.arrMat[9]) + (this->arrMat[3]*v.arrMat[13]);tmp.arrMat[2] = (this->arrMat[0]*v.arrMat[2]) + (this->arrMat[1]*v.arrMat[6]) + (this->arrMat[2]*v.arrMat[10])+ (this->arrMat[3]*v.arrMat[14]);tmp.arrMat[3] = (this->arrMat[0]*v.arrMat[3]) + (this->arrMat[1]*v.arrMat[7]) + (this->arrMat[2]*v.arrMat[11])+ (this->arrMat[3]*v.arrMat[15]);tmp.arrMat[4] = (this->arrMat[4]*v.arrMat[0]) + (this->arrMat[5]*v.arrMat[4]) + (this->arrMat[6]*v.arrMat[8]) + (this->arrMat[7]*v.arrMat[12]);tmp.arrMat[5] = (this->arrMat[4]*v.arrMat[1]) + (this->arrMat[5]*v.arrMat[5]) + (this->arrMat[6]*v.arrMat[9]) + (this->arrMat[7]*v.arrMat[13]);tmp.arrMat[6] = (this->arrMat[4]*v.arrMat[2]) + (this->arrMat[5]*v.arrMat[6]) + (this->arrMat[6]*v.arrMat[10])+ (this->arrMat[7]*v.arrMat[14]);tmp.arrMat[7] = (this->arrMat[4]*v.arrMat[3]) + (this->arrMat[5]*v.arrMat[7]) + (this->arrMat[6]*v.arrMat[11])+ (this->arrMat[7]*v.arrMat[15]);tmp.arrMat[8] = (this->arrMat[8]*v.arrMat[0]) + (this->arrMat[9]*v.arrMat[4]) + (this->arrMat[10]*v.arrMat[8]) + (this->arrMat[11]*v.arrMat[12]);tmp.arrMat[9] = (this->arrMat[8]*v.arrMat[1]) + (this->arrMat[9]*v.arrMat[5]) + (this->arrMat[10]*v.arrMat[9]) + (this->arrMat[11]*v.arrMat[13]);tmp.arrMat[10]= (this->arrMat[8]*v.arrMat[2]) + (this->arrMat[9]*v.arrMat[6]) + (this->arrMat[10]*v.arrMat[10])+ (this->arrMat[11]*v.arrMat[14]);tmp.arrMat[11]= (this->arrMat[8]*v.arrMat[3]) + (this->arrMat[9]*v.arrMat[7]) + (this->arrMat[10]*v.arrMat[11])+ (this->arrMat[11]*v.arrMat[15]);tmp.arrMat[12]= (this->arrMat[12]*v.arrMat[0]) + (this->arrMat[13]*v.arrMat[4]) + (this->arrMat[14]*v.arrMat[8]) + (this->arrMat[15]*v.arrMat[12]);tmp.arrMat[13]= (this->arrMat[12]*v.arrMat[1]) + (this->arrMat[13]*v.arrMat[5]) + (this->arrMat[14]*v.arrMat[9]) + (this->arrMat[15]*v.arrMat[13]);tmp.arrMat[14]= (this->arrMat[12]*v.arrMat[2]) + (this->arrMat[13]*v.arrMat[6]) + (this->arrMat[14]*v.arrMat[10])+ (this->arrMat[15]*v.arrMat[14]);tmp.arrMat[15]= (this->arrMat[12]*v.arrMat[3]) + (this->arrMat[13]*v.arrMat[7]) + (this->arrMat[14]*v.arrMat[11])+ (this->arrMat[15]*v.arrMat[15]);//this->operator=(tmp);return tmp;};  Yes, with respect to how OpenGL treats matrices, that code multiplies the matrices the wrong way. If you write A*B in the code, you get B*A.   ;  hmmm so i am going to change all operations to column major 00 04 08 1201 05 09 1302 06 10 1403 07 11 15 is that right?   