# Moving the Camera in a scene example

• 07-09-2013, 07:08 AM
Limingdu
Moving the Camera in a scene example
Does anyone have a simple quaterion camera (where the camera moves not the scene) example.

I was trying to implement zoom, panning, and rotating (similar to how Maya operates) and having a little trouble?
• 07-09-2013, 09:04 PM
GClements
Quote:

Originally Posted by Limingdu
Does anyone have a simple quaterion camera (where the camera moves not the scene) example.

Implementing a moveable camera boils down to inserting the inverse of the camera's transformation between the projection matrix and model-view matrix (i.e. either appending it to the projection matrix or prepending it to the model-view matrix; usually the latter).

OpenGL doesn't include a matrix inversion function. You could write one using e.g. Cramer's rule or Gaussian elimination (see e.g. wikipedia for details). If the matrix consists solely of a rotation, the matrix is orthonormal and the inverse is equal to the transpose, so you can use glMultTransposeMatrix() or transpose it yourself (it may already be transposed, as OpenGL uses column-major order while C code typically uses row-major order). If the matrix is constructed by a sequence of operations (translation, rotation, scale), you can invert the result by inverting the individual matrices (i.e. negating the translation and rotation, and using the reciprocal of the scale) and applying them in the reverse order. (A*B*C)^-1 = (C^-1)*(B^-1)*(A^-1).

Quote:

Originally Posted by Limingdu
I was trying to implement zoom, panning, and rotating (similar to how Maya operates) and having a little trouble?

You need to define what you mean by "zoom". It can refer to changing the field-of-view angle (this is what "zoom" means for a physical camera), in which case it needs to be applied to the projection matrix (e.g. by changing the fovy parameter in gluPerspective()). Or (for a perspective projection) it can refer to moving the camera forward or backward in the direction it's facing. Or it can refer to scaling (for a perspective projection, the scale needs to be applied after the perspective transformation, which is essentially the same as modifying the field-of-view angle).
• 07-10-2013, 07:39 AM
Limingdu
Your right.. I should have said dolly tumble track...
• 07-12-2013, 10:10 AM
JohnMarkem
You have your Eye Position (Ep), Look at Position (Lp) and Up vector (Up)

you have a vector from (1) Ep to your Lp and (2) Up so you need the cross product of (1) x(2)
To dolly and track you translate along this coordinate system.

To rotate around a your Look at Position

Code :

``` // -------------------------------------------------------------------------- public void roll(float angle) { Vector3f localZAxis = new Vector3f(Z_AXIS); orientation.rotate(localZAxis);   Quaternion q = new Quaternion(localZAxis, angle);   // Update camera's local left and up vectors. q.rotate(l); q.rotate(u);   // orientation = q * orientation. Quaternion.mult(q, orientation, orientation); }   // -------------------------------------------------------------------------- public void pitch(float angle) { Vector3f localXAxis = new Vector3f(X_AXIS); orientation.rotate(localXAxis);   Quaternion q = new Quaternion(localXAxis, angle);   // Update camera's local up and forward vectors. q.rotate(u); q.rotate(f);   // orientation = q * orientation. Quaternion.mult(q, orientation, orientation); }   // -------------------------------------------------------------------------- public void yaw(float angle) { Vector3f localYAxis = new Vector3f(Y_AXIS); orientation.rotate(localYAxis);   Quaternion q = new Quaternion(localYAxis, angle);   // Update camera's local left and forward vectors. q.rotate(l); q.rotate(f);   // orientation = q * orientation. Quaternion.mult(q, orientation, orientation); }```

I'm not sure if someone has a simple class that does what I just mentioned above or not?
That would be helpful.