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);
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.
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: