How to treat a camera in OpenGL

How can I move a camera in OpenGL?
Most of the tutorials tell that you use only modelViewMatix. They say the final image is the same when you move a camera or a object.

Anyway I tried.
I made one matrix which expresses camera’s position and rotation, I call it ‘cameraViewMatrix’.
    GLKMatrix4 cameraViewMatrix = GLKMatrix4Identity;

    cameraViewMatrix = GLKMatrix4Translate(cameraViewMatrix, cameraX, cameraY, cameraZ);//translation

    cameraViewMatrix = GLKMatrix4RotateX(cameraViewMatrix, cameraRotX);//rotation
    cameraViewMatrix = GLKMatrix4RotateY(cameraViewMatrix, cameraRotY);
    cameraViewMatrix = GLKMatrix4RotateZ(cameraViewMatrix, cameraRotZ);

    modelViewMatrix = GLKMatrix4Multiply(cameraViewMatrix,modelViewMatrix);
If you changed the modelViewMatrix before it was passed to GPU, you would not have to change much code, I mean you don’t have to pass the camera matrix to GPU (shader) directly.

These are the results :

↑translation → rotation

↑rotation → translation

Of course the camera is moving, so no centrifugal force is applied to any objects in both movies.
I found I can easily treat a camera like this in OpenGL.
Develop | Comments(0) | Trackback(0)