Context Navigation

Positioning a Camera with a User-Defined Matrix

Goal

Manually position a camera to view a scene.

Overview

To position a camera manually, we can use the viewer class setViewByMatrix() method. This call should be placed between the viewer.update and viewer.frame calls within the simulation loop. The following guidelines are useful:

Producer and all classes below osgGA::MatrixManipulator (in terms of abstraction) use 'Y' up coordinates. All others -including the Viewer class matrix manipulators - use 'Z' up.

The inverse of a position/orientation matrix can be used to orient a camera.

With these guidelines, the only steps required to position a camera manually are:

Create and initialize a matrix with the correct world position and orientation.

Get the inverse of this matrix and...

Provide a world up orientation. In this case by rotating from 'Y' up to 'Z' up.

The code

Set up position & orientation of matrix

To setup a matrix we can use the methods of the osg::Matrix class. In this case we’ll use a double-precision type, or osg::Matrixd. To position and orient this matrix, we can use the Matrix class methods makeTranslate() and makeRotate(). Both of these methods have a number of overloads for convenience. For this example we’ll use the version of makeRotate() that takes 3 degree/vector pairs. Rotation will be by the number degrees (in radians) about the axis specified by the vector. For translation we’ll use the version that simply takes x, y and z values. We’ll create a single matrix that represents a rotation and translation by post-multiplying the rotation matrix by the translation matrix. The code to achieve this is as follows.

Here's the code to set up a scene. The scene has a small terrain model and a tank at position (10,10,8).

Using the matrix to position the viewer’s camera

Using osgProducer::Viewer default settings, a call to a viewer instance’s update() method results in the viewer’s cameraGroup instance calling its setViewByMatrix method. Normally the argument to this method will be the inverse of the current matrix manipulator’s (trackball, drive, or fly) matrix. One way to change this behavior is to manually set the viewer’s matrix immediately after the viewer update() method is called. To use the camera position/orientation matrix we created above to manually position and orient the Viewer’s camera matrix, we need to first invert the matrix.
In addition to inverting the matrix, we also need to provide world orientation. Normally osgGA::MatrixManipulator matrices (used by osgProducer::Viewer class) represent a ‘Z’ up coordinate system. Producer and osg::Matrix class matrices (like those created above) normally represent a ‘Y’ up coordinate system. So after inverting the matrix from above, we also need to provide a rotation from ‘Y’ up to ‘Z’ up. This can be done by rotating -90 degrees about the x-axis. The code to do this is as follows: