Search code examples
openglrotationmatrix-multiplicationoculusglulookat

Rotate camera in own coord using gluLookat and glMultMatrixf


I have to change a camera implementation built with gluLookat and glMultmatrixf.

The camera implemented is an arcball camera where the camera rotates around a point.

Orientation of the camera is by

//set projection
glMatrixMode(GL_PROJECTION);
gluPerspective (..);
//modelview
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(pCamera.x, pCamera.y, pCamera.z,
          pTarget.x, pTarget.y, pTarget.z,
          up.x, up.y, up.z);

And is followed by

glMultMatrixf(transformViewMatrix);

to move the camera around the point, where transformViewMatrix is calculated from mouse coord


I have to integrate Oculus Rift with the camera so the camera can rotate on spot instead of around a point.

I can get a rotation matrix from Oculus sensor, rotOculus.

I tried multiplying transforViewMatrix with rotOculus.

glMultMatrixf(transformViewMatrix*rotOculus);

but get that the object I am looking at is rotating on spot instead of the camera

then I tried to assign transformViewMatrix with rotOculus, but that is same as arcball rotation with mouse, just changed to Oculus.

I think in order to rotate camera on spot, I need to translate the camera to origin by pCamera, then glMultMatrixf(rotOculus)?

But I dont have access to how I can order glTranslate, gluLookAt, glMultMatrixf methods because those are implemented in a dll, I can only change pCamera, pTarget, up, and viewTransformMatrix.

Could anyone point how I could rotate camera on spot in this situation? Thank you.


Solution

  • You want to move the lookat vector, which is the line between pCamera and pTarget. You don't specify what types you're using, but the code should look something like this

    // Get the lookat vector
    vec3 lookAtVector = pTarget - pCamera;
    // Rotate by the HMD orientation
    lookAtVector = rotOculus * lookAtVector;
    
    // Add back the camera vector to get the worldspace target
    vec3 newTarget = lookAtVector + pCamera;
    
    // Upload the view matrix to GL
    gluLookAt(pCamera.x, pCamera.y, pCamera.z,
             newTarget.x, newTarget.y, newTarget.z,
             up.x, up.y, up.z);