Too much math!!!!
The problem is very simple but you should know something (a bit) about matrix and vector. Of course you need matrix and vector class...it should impossible without!
The general problem is a base-change (I dont know if this is the English term)...
Suppose you have an observer in position POS that look in the direction LOOK and have an UP vector.
To 'compute' the transforming matrix is simpy
matrix.LoadIdentity(); vector3 side = look^up; // cross matrix.SetCol(0, -side ); matrix.SetCol(1, up ); matrix.SetCol(2, -look ); matrix.SetRow(3, -(matrix*pos) );
Note that in this 'code' I've used OpenGL notation (as you can see by the translation ROW). So you should pay attention to the product matrix * pos (matrix-column * vector-column).
A GL translation is
1 0 0 0
0 1 0 0
0 0 1 0
Tx Ty Tz 1
For example I've used another notation and to load a gl matrix I must transpose it before
To use the matrix you must call glLoadMatrix(camera_matrix) as first command (you know the reverse order) before object transform.
Of course an analog problem is to set an object in position POS with direction LOOK...the solution is similar
matrix.LoadIdentity(); vector3 side = look^up; // cross matrix.SetRow(0, side ); // we use row and not col! matrix.SetRow(1, up ); matrix.SetRow(2, look ); matrix.SetRow(3, pos );
In this case you must call glMultMatrix(object_matrix) as last.
To move your camera/object viewer you can create some functions like Roll(angle), Pitch(angle),....
Do you know how setup a rotation angle?
This is my code (transposed version)
// I get the code from OpenGL spec...// but I must transpose it!// The rotation is performed around a vector v// with an angle iangle// AC_REAL are float (#define type)// acCos is a look up table and iangle is an// integer AC_REAL cosA = acCos(iangle); // cos(angle)AC_REAL sinA = acSin(iangle);AC_REAL icosA = (1-cosA); if(v.Len2()!=(AC_REAL)1.0){ v.Normalize();} AC_REAL x = v.m_x;AC_REAL y = v.m_y;AC_REAL z = v.m_z; AC_REAL xsinA = x * sinA;AC_REAL ysinA = y * sinA;AC_REAL zsinA = z * sinA; // m_elem is a float[16] array in the matrix object AC_REAL* m = m_elem; m[0] = cosA + x*x*icosA;m[1] = x*y*icosA - zsinA;m[2] = x*z*icosA + ysinA;m[3] = 0.0f; m[4] = x*y*icosA + zsinA;m[5] = cosA + y*y*icosA;m[6] = y*z*icosA - xsinA;m[7] = 0.0f; m[8] = x*z*icosA - ysinA;m[9] = y*z*icosA + xsinA;m[10] = cosA + z*z*icosA;m[11] = 0.0f; m[12] = 0.0f;m[13] = 0.0f;m[14] = 0.0f;m[15] = 1.0f;
I spent some time to check my matrix op with OpenGL ones and they work!
Your problem : SOLVED!
Ciao!
>
[edited by - blizzard999 on April 26, 2003 7:19:33 PM]