Actually ive tweaked it a bit and it seems to be working OK now, but I have a problem that something is altering the "roll" when im flying/looking around. Here is the code that manipulates the view in the Camera class:
void Camera::spinLeft(float angle){ Matrix mRotation; vector3f vNewDirection; rotationMatrix(DEG_TO_RAD(angle), vUp[0], vUp[1], vUp[2], mRotation); vNewDirection[0] = mRotation[0] * vDirection[0] + mRotation[4] * vDirection[1] + mRotation[8] * vDirection[2]; vNewDirection[1] = mRotation[1] * vDirection[0] + mRotation[5] * vDirection[1] + mRotation[9] * vDirection[2]; vNewDirection[2] = mRotation[2] * vDirection[0] + mRotation[6] * vDirection[1] + mRotation[10] * vDirection[2]; memcpy(vDirection, vNewDirection, sizeof(vector3f)); }void Camera::lookUp(float angle){ Matrix mRotation; vector3f vCross; // Cross Product vCross[0] = vUp[1]*vDirection[2] - vDirection[1]*vUp[2]; vCross[1] = -vUp[0]*vDirection[2] + vDirection[0]*vUp[2]; vCross[2] = vUp[0]*vDirection[1] - vDirection[0]*vUp[1]; rotationMatrix(DEG_TO_RAD(-angle), vCross[0], vCross[1], vCross[2], mRotation); vector3f vNewVect; // Inline 3x3 matrix multiply for rotation only vNewVect[0] = mRotation[0] * vDirection[0] + mRotation[4] * vDirection[1] + mRotation[8] * vDirection[2]; vNewVect[1] = mRotation[1] * vDirection[0] + mRotation[5] * vDirection[1] + mRotation[9] * vDirection[2]; vNewVect[2] = mRotation[2] * vDirection[0] + mRotation[6] * vDirection[1] + mRotation[10] * vDirection[2]; memcpy(vDirection, vNewVect, sizeof(GLfloat)*3); // Update pointing up vector vNewVect[0] = mRotation[0] * vUp[0] + mRotation[4] * vUp[1] + mRotation[8] * vUp[2]; vNewVect[1] = mRotation[1] * vUp[0] + mRotation[5] * vUp[1] + mRotation[9] * vUp[2]; vNewVect[2] = mRotation[2] * vUp[0] + mRotation[6] * vUp[1] + mRotation[10] * vUp[2]; memcpy(vUp, vNewVect, sizeof(GLfloat) * 3); }void Camera::rotationMatrix(float angle, float x, float y, float z, Matrix mMatrix){ float vecLength, sinResult, cosResult, oneMinusCos; float xx, yy, zz, xy, yz, zx, xs, ys, zs; // Scale vector vecLength = (float)sqrt( x*x + y*y + z*z ); // Rotation matrix is normalized x /= vecLength; y /= vecLength; z /= vecLength; sinResult = (float)sin(angle); cosResult = (float)cos(angle); oneMinusCos = 1.0f - cosResult; xx = x * x; yy = y * y; zz = z * z; xy = x * y; yz = y * z; zx = z * x; xs = x * sinResult; ys = y * sinResult; zs = z * sinResult; mMatrix[0] = (oneMinusCos * xx) + cosResult; mMatrix[4] = (oneMinusCos * xy) - zs; mMatrix[8] = (oneMinusCos * zx) + ys; mMatrix[12] = 0.0f; mMatrix[1] = (oneMinusCos * xy) + zs; mMatrix[5] = (oneMinusCos * yy) + cosResult; mMatrix[9] = (oneMinusCos * yz) - xs; mMatrix[13] = 0.0f; mMatrix[2] = (oneMinusCos * zx) - ys; mMatrix[6] = (oneMinusCos * yz) + xs; mMatrix[10] = (oneMinusCos * zz) + cosResult; mMatrix[14] = 0.0f; mMatrix[3] = 0.0f; mMatrix[7] = 0.0f; mMatrix[11] = 0.0f; mMatrix[15] = 1.0f;}
passiveMotionFunc:
void mouse(int x,int y){ if ( x != windowCenterX ) { float difference = (windowCenterX) - x; camera.spinLeft(difference/2.0); } if ( y != windowCenterY ) { float difference = (windowCenterY) - y; camera.lookUp(difference/2.0); } if ( x == windowCenterX && y == windowCenterY ) return; glutWarpPointer(windowCenterX,windowCenterY); }
When the application starts and I look around, it "tilts" the camera (z-axis rotation).
I cant seem to find the problem.
Thx