Rotate camera around a point in scene while looking at a different point

1.2k views Asked by At

Here's the problem statement:

I'm trying to setup my camera in Open scene graph such that it's looking at a given point but when I rotate it (using a trackballmanipulator), it should rotate around a different spin center in space.

So basically the camera is looking at one point but rotating around a different point in scene.

Now from my research so far I gathered that I need to apply some transformations to my camera. But I don't quite understand what transformations I should be applying.

Can anyone please help me with the math? I'm still trying to learn the CG math.

I have the following information with me:

eye (camera position): (eyeX, eyeY, eyeZ)

center (model center): (refX, refY, refZ)

up : (upX, upY, upZ)

Spin center/spin axis: (spinX, spinY, spinZ)

Code snippet:

osg::Vec3d eye = osg::Vec3d(cameraPos.at(0), cameraPos.at(1), cameraPos.at(2));

osg::Vec3d viewVector = osg::Vec3d(cameraViewDirection.at(0), cameraViewDirection.at(1), cameraViewDirection.at(2));

osg::Vec3d up = osg::Vec3d(cameraUpDirection.at(0), cameraUpDirection.at(1), cameraUpDirection.at(2));

osg::Vec3d modelCenter(eye + viewVector);

osgGA::CameraManipulator *cameraManipulator = view->getCameraManipulator();
osgGA::TrackballManipulator *trackBallManipulator = dynamic_cast<osgGA::TrackballManipulator *>(cameraManipulator);dynamic_cast<osgGA::TrackballManipulator *>(cameraManipulator);

//Transform camera values??

//Apply the viewpoint

trackBallManipulator->setTransformation(transformedEye, transformedCenter, transformedUp);
1

There are 1 answers

0
Björn Blissing On

You can define a home position for the Trackball manipulator. This is the position and orientation that the manipulator will go back to when pressing space or when you use the home(double time) function of the manipulator.

This home position is defined using three vectors; eye, center and up.

  • Eye - where in the world the camera is positioned.
  • Center - at what point in the world the camera is pointing towards.
  • Up - unit vector defining which direction the up-side of the camera should be.

Lets say that we have an object positioned at coordinates (1.0, 1.0, 0.0) and we want the trackball manipulator to use this the center of rotation. We want the camera to be positioned a bit back and up, so we define this as the eye vector as (0.0, 5.0, -10.0), ie 5 units up and 10 units back. And finally we want the up direction of the camera to be aligned with the Y-axis, then the Up vector will be (0.0, 1.0, 0.0).

A small code snippet could look like this:

// Create a new Trackball manipulator
osg::ref_ptr<osgGA::TrackballManipulator> manipulator = new osgGA::TrackballManipulator;
// Connect the trackball manipulator to the viewer
viewer.setCameraManipulator(manipulator);

// Set the desired home coordinates for the manipulator
osg::Vec3d eye(0.0, 5.0, -10.0);
osg::Vec3d center(1.0, 1.0, 0.0);
osg::Vec3d up(0.0, 1.0, 0.0);

// Make sure that OSG is not overriding our home position
manipulator->setAutoComputeHomePosition(false);
// Set the desired home position of the Trackball Manipulator
manipulator->setHomePosition(eye, center, up);
// Force the camera to move to the home position
manipulator->home(0.0);

// Start Viewer
while (!viewer.done()) {
    viewer.run();
}