gluLookAt() with translate and rotate
I,ve been trying to look around for an implementation similar to gluLookAt() that has been done completely with translates and rotates (which I am pretty sure can be done). I do realise gluLookAt() makes life easier but it also limits the amount of control one has over the camera. I have done a simple but working implementation similar to gluLookAt() using matrices but am still lost trying to pull it off using just rotations and translates. Any help will be appreciated. Thanks a lot.
Quote:Original post by rumeshkumar
I,ve been trying to look around for an implementation similar to gluLookAt() that has been done completely with translates and rotates (which I am pretty sure can be done). I do realise gluLookAt() makes life easier but it also limits the amount of control one has over the camera. I have done a simple but working implementation similar to gluLookAt() using matrices but am still lost trying to pull it off using just rotations and translates. Any help will be appreciated. Thanks a lot.
Heres a small example :
// A camera at { 5.0f, 1.0f, 5.0f }GLfloat g_mCamera[16] = { 1.0f, 0.0f, 0.0f, 5.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 5.0f, 0.0f, 0.0f, 0.0f, 1.0f ) ;GLvoid Render( ){ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ; glLoadIdentity( ) ; // Sets up a camera with the above specified matrix, 'g_mCamera' glMultMatrixf( g_mCamera ) ; // Multiplies your camera matrix with the identity matrix // Draw stuff // swap buffers}
The simplest solution is to perform a 'base change'...
Not that by default OpenGL uses an identity...this means that you are looking toward -z.
If you use look = -z, up = y, pos = 0 you get the identity matrix.
Matrix ComputeViewMatrix(const Vector& look, const Vector& up, const Vector& view_position){ Vector j = up; Vector k = -look Vector i = j ^ k; Matrix M = Matrix::IDENTITY; // rotation part M.SetCol(0, i); // copy i in [0x0] [0x4] [0x8] M.SetCol(1, j); // copy j in [0x1] [0x5] [0x9] M.SetCol(2, k); // copy k in [0x2] [0x6] [0xA] // translation part Vector t = M * (-view_position); M.SetRow(3, t ); // copy t in [0xC] [0xD] [0xE] return(M);}
Not that by default OpenGL uses an identity...this means that you are looking toward -z.
If you use look = -z, up = y, pos = 0 you get the identity matrix.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement