Utility class to manipulate a camera. More...
#include <Inventor/ViewerComponents/SoCameraInteractor.h>
Public Member Functions | |
SoCameraInteractor (SoCamera *camera) | |
virtual void | zoom (float zoom) |
virtual void | dolly (float scaleFactor) |
virtual void | changeViewVolume (float height, float aspect) |
SoCamera * | getCamera () const |
SbVec3f | getFocalPoint () const |
void | setPosition (const SbVec3f &pos) |
void | setPosition (const SbVec2f &pos, float distFromEye, const SbViewportRegion &vpRegion) |
void | translate (const SbVec3f &trans) |
void | translate (const SbVec2f &displacement, const SbViewportRegion &vpRegion) |
void | pointAt (const SbVec3f &targetPoint) |
SbRotation | lookAt (const SbVec3f &position, const SbVec3f &target, const SbVec3f &up) |
SbVec3f | getDirectionVector () const |
void | roll (float angle) |
void | pivot (float angle) |
void | tilt (float angle) |
void | rotate (const SbRotation &rot) |
void | setOrientation (const SbRotation &rot) |
void | setRotationCenter (const SbVec3f &pos) |
void | setRotationAxis (const SbVec3f &axis) |
void | rotate (float angle) |
void | viewAxis (const SbVec3f &direction, const SbVec3f &up) |
void | viewX (bool reverse=false) |
void | viewY (bool reverse=false) |
void | viewZ (bool reverse=false) |
void | viewIn (const SbBox3f &bbox) |
void | viewIn (SoPath *path, const SbViewportRegion &vpRegion) |
void | viewIn (SoNode *node, const SbViewportRegion &vpRegion) |
void | viewAll (SoPath *path, const SbViewportRegion &vpRegion) |
void | viewAll (SoNode *node, const SbViewportRegion &vpRegion) |
SbVec3f | projectToPlane (const SbVec2f &point, float distFromEye, const SbViewportRegion &vpRegion) const |
SbVec2f | projectToScreen (const SbVec3f &point, const SbViewportRegion &vpRegion) const |
void | adjustClippingPlanes (SoNode *sceneRoot, const SbViewportRegion &vpRegion) |
void | zoomInWindow (const SbVec2f &topLeftCorner, const SbVec2f &bottomRightCorner, const SbViewportRegion &vpRegion) |
void | dollyInWindow (const SbVec2f &topLeftCorner, const SbVec2f &bottomRightCorner, const SbViewportRegion &vpRegion) |
void | dollyWithZoomCenter (const SbVec2f ¢erPos, float scaleFactor, const SbViewportRegion &vpRegion) |
void | activatePanning (const SbVec2f &initPos, const SbViewportRegion &vpRegion) |
void | pan (const SbVec2f &newPos, const SbViewportRegion &vpRegion) |
void | activateOrbiting (const SbVec2f &startPos) |
void | orbit (const SbRotation &rotation) |
void | orbit (const SbVec2f &newPos) |
void | pushCamera () |
SbBool | popCamera () |
void | synchronize (SoCamera *camera) |
Static Public Member Functions | |
static SoCameraInteractor * | getNewInstance (SoCamera *camera) |
Deprecated | |
| |
SoDEPRECATED SbVec3f | getViewportCenter () const |
This class provides some useful algorithms to manipulate a camera (translation, rotation, zoom, etc).
This class is not, strictly speaking, a true interactor. It does not respond to input events. However it provides the camera manipulation algorithms you need to build an interactor that provides behavior similar to the Open Inventor viewer classes, for example SoWinExaminerViewer. An interactor could handle system-dependent events or Open Inventor events (using SoEventCallback). It could also be triggered from the user interface (buttons, sliders, etc).
Start by creating an instance of SoCameraInteractor by giving the camera to be manipulated either to the constructor of SoCameraInteractor or to the getNewInstance() method. Then, for example, to provide examinerViewer-like rotation behavior:
To provide the viewer's automatic adjustment of near and far clip planes behavior, call the adjustClippingPlanes() method before each render traversal.
To see an example "viewer" implemented using this class, please see the SceneExaminer class provided as source code in the Open Inventor SDK:
[C++] Reference counting:
Create interactor and an event callback node that handles mouse events
SoPerspectiveCamera* camera = new SoPerspectiveCamera(); SoCameraInteractor* m_interactor = SoCameraInteractor::getNewInstance( camera ); SoEventCallback* eventCB = new SoEventCallback; eventCB->addEventCallback( SoMouseButtonEvent::getClassTypeId(), mouseEventCB ); eventCB->addEventCallback( SoLocation2Event::getClassTypeId(), mouseEventCB );
Handler for mouse events that implements "orbit" camera manipulation:
void mouseEventCB( void* userData, SoEventCallback* node ) { static m_buttonDown = false; const SoEvent* evt = node->getEvent(); if (SO_MOUSE_PRESS_EVENT( evt, BUTTON1 )) { buttonDown = true; SbVec2f position = evt->getNormalizedPosition( node->getAction()->getViewportRegion() ); m_interactor->activateOrbiting( position ); m_interactor->setRotationCenter( m_interactor->getFocalPoint() ); } else if (SO_MOUSE_RELEASE_EVENT( evt, BUTTON1 )) { m_buttonDown = false; } else if (evt->isOfType(SoLocation2Event::getClassTypeId())) { if (m_buttonDown) { SbVec2f position = evt->getNormalizedPosition( node->getAction()->getViewportRegion() ); m_interactor->orbit( position ); } } }
SceneInteractor, SceneExaminer
SoCameraInteractor::SoCameraInteractor | ( | SoCamera * | camera | ) |
Create a SoCameraInteractor for the specified camera.
The camera should be an SoPerspectiveCamera or SoOrthographicCamera object. Create an empty object if camera pointer is NULL or invalid
void SoCameraInteractor::activateOrbiting | ( | const SbVec2f & | startPos | ) |
Set the starting point for interactive orbiting.
This method should be called when starting an interaction. Typically this is triggered by a mouse button or touch event. On subsequent mouse move or touch events, call the orbit() method with the new cursor or touch position.
Orbit is often used in an "examiner" type of viewer to allow the user to rotate the camera around a point of interest in the scene. To implement orbit around the camera's "focal point", call setRotationCenter() with the value returned from getFocalPoint().
startPos | starting position, 2D point in normalized screen coordinates (0 <= x,y <= 1). Typically this value is obtained from the SoEvent method getNormalizedPosition(). |
void SoCameraInteractor::activatePanning | ( | const SbVec2f & | initPos, | |
const SbViewportRegion & | vpRegion | |||
) |
Set the starting point for interactive panning.
This method should be called when starting an interaction. Typically this is triggered by a mouse button or touch event. On subsequent mouse move or touch events, call the pan() method with the new cursor or touch position.
initPos | initial position in normalized screen coordinates (0 <= x,y <= 1). Typically this value is obtained from the SoEvent method getNormalizedPosition(). | |
vpRegion | the actual viewport region |
void SoCameraInteractor::adjustClippingPlanes | ( | SoNode * | sceneRoot, | |
const SbViewportRegion & | vpRegion | |||
) |
Adjust near and far clipping planes to minimize clipping of objects in the scene.
Equivalent to the auto clip planes feature in the viewer classes. Typically called before each render traversal.
virtual void SoCameraInteractor::changeViewVolume | ( | float | height, | |
float | aspect | |||
) | [virtual] |
Change camera's view volume.
Allows changing the view volume height and width with a single call. For a perspective camera, the height parameter is heightAngle in radians. For an orthographic camera, the height parameter is height in world coordinates.
height | new camera height or heightAngle | |
aspect | new camera aspect ratio |
virtual void SoCameraInteractor::dolly | ( | float | scaleFactor | ) | [virtual] |
Dolly - Makes the scene appear larger or smaller.
For a perspective camera, moves the camera forward or backward along the view vector (details below). For an orthographic camera, changes the height field to give a similar visual effect (moving an orthographic camera forward and backward has no visual effect). This is the preferred way to implement the visual effect of "zooming" in and out for a perspective camera. For a perspective camera, dolly means the same thing it means for a camera on a movie set.
Note: 'scaleFactor' is the inverse of what you might expect. To make the scene appear 2X larger, specify a scaleFactor of 0.5. This is because scaleFactor multiplies the camera 'focalDistance' or 'height'.
void SoCameraInteractor::dollyInWindow | ( | const SbVec2f & | topLeftCorner, | |
const SbVec2f & | bottomRightCorner, | |||
const SbViewportRegion & | vpRegion | |||
) |
Moves the camera forward or backward along the view vector to view the region defined by the given corners in normalized screen coordinates.
void SoCameraInteractor::dollyWithZoomCenter | ( | const SbVec2f & | centerPos, | |
float | scaleFactor, | |||
const SbViewportRegion & | vpRegion | |||
) |
Modifies the camera to ajust the view around the specified point.
The algorithm is the same as the dolly() method (for a perspective camera the position field is changed, for an orthographic camera the height field is changed) and the delta parameter is used the same way.
centerPos | zoom center in normalized screen coordinates (0 <= x,y <= 1). Typically this value is obtained from the SoEvent method getNormalizedPosition(). | |
scaleFactor | zoom scale | |
vpRegion | the actual viewport region |
SoCamera* SoCameraInteractor::getCamera | ( | ) | const |
Returns the associated camera.
SbVec3f SoCameraInteractor::getDirectionVector | ( | ) | const |
Returns the direction vector of the camera.
SbVec3f SoCameraInteractor::getFocalPoint | ( | ) | const |
Returns the camera's "focal point", located focalDistance units from the camera position along the view direction vector (defined by the camera's orientation field).
This is the reference point for some algorithms. For example, orbit() is often used in an "examiner" type of viewer to allow the user to rotate the camera around a point of interest in the scene. To implement orbit around the camera's focal point, call the setRotationCenter() method with the focal point before calling the orbit() method.
static SoCameraInteractor* SoCameraInteractor::getNewInstance | ( | SoCamera * | camera | ) | [static] |
Create an instance for the specified camera.
The camera should be an SoPerspectiveCamera or SoOrthographicCamera object. Returns null if the camera parameter is null.
SoDEPRECATED SbVec3f SoCameraInteractor::getViewportCenter | ( | ) | const |
Returns the viewport center.
On other words, returns the camera's "focal point", located focalDistance units from the camera position along the view direction vector ( orientation).
SbRotation SoCameraInteractor::lookAt | ( | const SbVec3f & | position, | |
const SbVec3f & | target, | |||
const SbVec3f & | up | |||
) |
Returns the rotation needed to point a camera at position toward the given target point while keeping the "up" direction of the camera parallel to the specified up vector.
This method does NOT modify the camera node. Set the returned rotation in the camera's orientation field.
position | of the camera | |
target | position of the target | |
up | up vector of the camera |
void SoCameraInteractor::orbit | ( | const SbVec2f & | newPos | ) |
Orbit the camera based on offset from the previous screen position.
This method can be used to implement an interactive orbit operation in response to mouse move or touch events. The activateOrbiting() method should be called at the start of the operation. This method uses an SbSphereSheetProjector to compute a rotation value based on the specified point and the previously specified point.
newPos | new position, 2D point in normalized screen coordinates (0 <= x,y <= 1). Typically this value is obtained from the SoEvent method getNormalizedPosition(). |
void SoCameraInteractor::orbit | ( | const SbRotation & | rotation | ) |
Orbit the camera by the specified rotation.
"Orbit" means to rotate the camera around a fixed point in 3D space. The rotation point is specified using the setRotationCenter() method. This method automatically calls the setRotationAxis() method with the axis defined by the SbRotation parameter.
Orbit is often used in an "examiner" type of viewer to allow the user to rotate the camera around a point of interest in the scene. Orbit based on mouse or touch events can be implemented using the orbit() method. This method can be used, for example, to animate the camera to a new viewpoint. To implement orbit around the camera's "focal point", call setRotationCenter() with the value returned from getFocalPoint().
rotation | Incremental rotation, i.e. this rotation is multiplied with the current camera orientation. |
void SoCameraInteractor::pan | ( | const SbVec2f & | newPos, | |
const SbViewportRegion & | vpRegion | |||
) |
Pan the camera based on offset from starting point.
This method can be used to implement an interactive panning operation in response to move move or touch events. The activatePanning() method should be called at the start of the operation.
newPos | new position in normalized screen coordinates (0 <= x,y <= 1). Typically this value is obtained from the SoEvent method getNormalizedPosition(). | |
vpRegion | the actual viewport region |
void SoCameraInteractor::pivot | ( | float | angle | ) |
Pivot the camera.
Incremental in-place rotation left/right (around the camera's up vector, i.e. local Y axis). Rotations are cumulative. Camera position does not change. Positive values pivot the camera left.
angle | angle in radians |
void SoCameraInteractor::pointAt | ( | const SbVec3f & | targetPoint | ) |
Sets the orientation of the camera so that it points toward the given target point while keeping the "up" direction of the camera parallel to the positive world coordinate y-axis.
If this is not possible, it uses the positive z-axis as "up". Equivalent to calling the SoCamera method pointAt().
targetPoint | target point |
SbBool SoCameraInteractor::popCamera | ( | ) |
Restore saved camera.
The values of the camera fields are restored to the last pushed values. Returns true if successful, false if the vector of pushed cameras is empty.
SbVec3f SoCameraInteractor::projectToPlane | ( | const SbVec2f & | point, | |
float | distFromEye, | |||
const SbViewportRegion & | vpRegion | |||
) | const |
Project specified 2D point on the plane parallel to the near plane that is at distFromEye units from the eye, see also SbViewVolume::getPlanePoint.
point | point to project in normalized screen coordinates (0 <= x,y <= 1) | |
distFromEye | the distance at which the point would be back projected | |
vpRegion | the actual viewport region |
SbVec2f SoCameraInteractor::projectToScreen | ( | const SbVec3f & | point, | |
const SbViewportRegion & | vpRegion | |||
) | const |
Map a 3D point in world coordinates to a 2D point in normalized screen coordinates (0 <= x,y <= 1), see also SbViewVolume::projectToScreen.
point | point to project in world coordinates | |
vpRegion | the actual viewport region |
void SoCameraInteractor::pushCamera | ( | ) |
Save current camera.
The values of the camera fields are saved and can be restored by calling popCamera().
void SoCameraInteractor::roll | ( | float | angle | ) |
Roll the camera.
Incremental in-place rotation around the view direction vector (camera's local Z axis). Rotations are cumulative. Camera position does not change. Positive values roll the camera counter-clockwise.
angle | angle in radians |
void SoCameraInteractor::rotate | ( | float | angle | ) |
Rotate the camera around the center specified by setRotationCenter() and the axis specified by setRotationAxis().
The default rotation axis is (0,0,0), so calling this method will not have any visible effect until a rotation axis is set. The rotation axis is automatically set by some methods, e.g. orbit().
angle | angle in radians |
void SoCameraInteractor::rotate | ( | const SbRotation & | rot | ) |
Rotate the camera.
Incremental in-place rotation. Rotations are cumulative. Camera position does not change (rotationCenter and rotationAxis do not apply).
rot | new rotation |
void SoCameraInteractor::setOrientation | ( | const SbRotation & | rot | ) |
Set the orientation of the camera.
Replaces any previous rotations applied to the camera. Camera position does not change.
rot | new orientation |
void SoCameraInteractor::setPosition | ( | const SbVec2f & | pos, | |
float | distFromEye, | |||
const SbViewportRegion & | vpRegion | |||
) |
Move camera to the specified position.
pos | new position (2D point in normalized screen coordinates: 0 <= x,y <= 1) | |
distFromEye | the distance at which the point would be back projected. | |
vpRegion | the actual viewport region |
void SoCameraInteractor::setPosition | ( | const SbVec3f & | pos | ) |
Move camera to the specified position.
pos | new position in world coordinates |
void SoCameraInteractor::setRotationAxis | ( | const SbVec3f & | axis | ) |
Set the axis of rotation.
Default is (0, 0, 0). This vector is used by the rotate(float) method.
axis | new rotations axis |
void SoCameraInteractor::setRotationCenter | ( | const SbVec3f & | pos | ) |
Set the center of rotation.
Default is (0, 0, 0). This position is used by the rotate(float) and orbit() methods.
pos | position in world coordinates |
void SoCameraInteractor::synchronize | ( | SoCamera * | camera | ) |
Copy field values from the specified camera to the camera of this interactor.
If the two cameras are not the same type, common fields are copied and the height or heightAngle field is computed.
void SoCameraInteractor::tilt | ( | float | angle | ) |
Tilt the camera.
Incremental in-place rotation up/down (around the camera's local X axis). Rotations are cumulative. Camera position does not change. Positive values tilt the camera up.
angle | angle in radians |
void SoCameraInteractor::translate | ( | const SbVec2f & | displacement, | |
const SbViewportRegion & | vpRegion | |||
) |
Move camera by the specified 2D displacement.
displacement | 2D displacement in normalized screen coordinates: 0 <= x,y <= 1 | |
vpRegion | the actual viewport region |
void SoCameraInteractor::translate | ( | const SbVec3f & | trans | ) |
Move camera by the specified 3D displacement.
trans | translation value in world coordinates |
void SoCameraInteractor::viewAll | ( | SoNode * | node, | |
const SbViewportRegion & | vpRegion | |||
) |
void SoCameraInteractor::viewAll | ( | SoPath * | path, | |
const SbViewportRegion & | vpRegion | |||
) |
Moves and rotates the camera to be aligned with the given direction vector while keeping the up direction of the camera parallel to the specified up vector.
The camera position and orientation are changed to look at the point of interest, but the focalDistance is not changed.
direction | The new view direction vector: this is the vector from the camera's position to the target point. Any future call to the method getDirectionVector() will return the same vector. | |
up | The new up direction vector |
void SoCameraInteractor::viewIn | ( | SoNode * | node, | |
const SbViewportRegion & | vpRegion | |||
) |
Move camera to the center of the scene defined by the specified node.
Compute the bounding box of the scene and move the camera to the center of this box. The near and far clipping planes will be adjusted to see the inside of the box.
node | root node of the scene | |
vpRegion | the actual viewport region |
void SoCameraInteractor::viewIn | ( | SoPath * | path, | |
const SbViewportRegion & | vpRegion | |||
) |
Move camera to the center of the scene defined by the specified path.
Compute the bounding box of the scene and move the camera to the center of this box. The near and far clipping planes will be adjusted to see the inside of the box.
path | path to the scene | |
vpRegion | the actual viewport region |
void SoCameraInteractor::viewIn | ( | const SbBox3f & | bbox | ) |
Move camera to the center of the specified box.
The near and far clipping planes will be adjusted to see the inside of the box. If the box is empty, nothing happens.
void SoCameraInteractor::viewX | ( | bool | reverse = false |
) |
NOTE: The camera position is not changed, so the camera may not be looking toward the scene.
Calling viewAll() will move the camera so that the scene is visible again.
reverse | If false, rotate to look in the -X direction, else in +X direction. |
void SoCameraInteractor::viewY | ( | bool | reverse = false |
) |
Rotate camera to look parallel to the Y axis with +Z up.
NOTE: The camera position is not changed, so the camera may not be looking toward the scene. Calling viewAll() will move the camera so that the scene is visible again.
reverse | If false, rotate to look in the -Y direction, else in +Y direction. |
void SoCameraInteractor::viewZ | ( | bool | reverse = false |
) |
Rotate camera to look parallel to the Z axis with +Y up.
NOTE: The camera position is not changed, so the camera may not be looking toward the scene. Calling viewAll() will move the camera so that the scene is visible again.
reverse | If false, rotate to look in the -Z direction, else in +Z direction. |
virtual void SoCameraInteractor::zoom | ( | float | zoom | ) | [virtual] |
Zoom - Makes the scene appear larger or smaller.
Sets the camera's heightAngle field (for a perspective camera) or height field (for an orthographic camera) to the specified value. For a perspective camera, the value is in radians and the default is 0.785398. For an orthographic camera the value is in World Coordinates and the default is 2.
Conceptually, zoom means approximately the same thing as it does for a real camera - changing the "focal length" of the lens. Making the heightAngle (or height) larger corresponds to using a wide-angle lens, so the scene appears smaller. Making the heightAngle (or height) smaller corresponds to using a telephoto lens, so the scene appears larger.
NOTE: For a perspective camera, we strongly recommend implementing the visual effect of "zoom" by moving the camera closer to, or farther away from, the scene (see dolly()). Very small and very large values of heightAngle can produce undesirable results.
zoom | new zoom value |
void SoCameraInteractor::zoomInWindow | ( | const SbVec2f & | topLeftCorner, | |
const SbVec2f & | bottomRightCorner, | |||
const SbViewportRegion & | vpRegion | |||
) |
Zooms to the region defined by the given corners in normalized screen coordinates.
topLeftCorner | top left corner in normalized screen coordinates (0 <= x,y <= 1) | |
bottomRightCorner | bottom right corner in normalized screen coordinates (0 <= x,y <= 1) | |
vpRegion | the actual viewport region |