Utility class to manipulate a camera. More...
#include <Inventor/ViewerComponents/SoCameraInteractor.h>
Public Member Functions | |
SoCameraInteractor (SoCamera *camera) | |
Create a SoCameraInteractor for the specified camera. | |
virtual void | zoom (float zoom) |
Zoom - Makes the scene appear larger or smaller. | |
virtual void | dolly (float scaleFactor) |
Dolly - Makes the scene appear larger or smaller. | |
virtual void | changeViewVolume (float height, float aspect) |
Change camera's view volume. | |
SoCamera * | getCamera () const |
Returns the associated camera. | |
SbVec3f | 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). | |
void | setPosition (const SbVec3f &pos) |
Move camera to the specified position. | |
void | setPosition (const SbVec2f &pos, float distFromEye, const SbViewportRegion &vpRegion) |
Move camera to the specified position. | |
void | translate (const SbVec3f &trans) |
Move camera by the specified 3D displacement. | |
void | translate (const SbVec2f &displacement, const SbViewportRegion &vpRegion) |
Move camera by the specified 2D displacement. | |
void | 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. | |
SbRotation | 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. | |
SbVec3f | getDirectionVector () const |
Returns the direction vector of the camera. | |
void | roll (float angle) |
Roll the camera. | |
void | pivot (float angle) |
Pivot the camera. | |
void | tilt (float angle) |
Tilt the camera. | |
void | rotate (const SbRotation &rot) |
Rotate the camera. | |
void | setOrientation (const SbRotation &rot) |
Set the orientation of the camera. | |
void | setRotationCenter (const SbVec3f &pos) |
Set the center of rotation. | |
void | setRotationAxis (const SbVec3f &axis) |
Set the axis of rotation. | |
void | rotate (float angle) |
Rotate the camera around the center specified by setRotationCenter() and the axis specified by setRotationAxis(). | |
void | viewAxis (const SbVec3f &direction, const SbVec3f &up) |
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. | |
void | viewX (bool reverse=false) |
NOTE: The camera position is not changed, so the camera may not be looking toward the scene. | |
void | viewY (bool reverse=false) |
Rotate camera to look parallel to the Y axis with +Z up. | |
void | viewZ (bool reverse=false) |
Rotate camera to look parallel to the Z axis with +Y up. | |
void | viewIn (const SbBox3f &bbox) |
Move camera to the center of the specified box. | |
void | viewIn (SoPath *path, const SbViewportRegion &vpRegion) |
Move camera to the center of the scene defined by the specified path. | |
void | viewIn (SoNode *node, const SbViewportRegion &vpRegion) |
Move camera to the center of the scene defined by the specified node. | |
void | viewAll (SoPath *path, const SbViewportRegion &vpRegion) |
Move the camera to view the scene defined by the given path. | |
void | viewAll (SoNode *node, const SbViewportRegion &vpRegion) |
Move the camera to view the scene defined by the given node. | |
SbVec3f | 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. | |
SbVec2f | 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. | |
void | adjustClippingPlanes (SoNode *sceneRoot, const SbViewportRegion &vpRegion) |
Adjust near and far clipping planes to minimize clipping of objects in the scene. | |
void | zoomInWindow (const SbVec2f &topLeftCorner, const SbVec2f &bottomRightCorner, const SbViewportRegion &vpRegion) |
Zooms to the region defined by the given corners in normalized screen coordinates. | |
void | 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 | dollyWithZoomCenter (const SbVec2f ¢erPos, float scaleFactor, const SbViewportRegion &vpRegion) |
Modifies the camera to ajust the view around the specified point. | |
void | activatePanning (const SbVec2f &initPos, const SbViewportRegion &vpRegion) |
Set the starting point for interactive panning. | |
void | pan (const SbVec2f &newPos, const SbViewportRegion &vpRegion) |
Pan the camera based on offset from starting point. | |
void | activateOrbiting (const SbVec2f &startPos) |
Set the starting point for interactive orbiting. | |
void | orbit (const SbRotation &rotation) |
Orbit the camera by the specified rotation. | |
void | orbit (const SbVec2f &newPos) |
Orbit the camera based on offset from the previous screen position. | |
void | pushCamera () |
Save current camera. | |
SbBool | popCamera () |
Restore saved camera. | |
void | synchronize (SoCamera *camera) |
Copy field values from the specified camera to the camera of this interactor. | |
Deprecated | |
SoDEPRECATED SbVec3f | getViewportCenter () const |
Returns the viewport center. | |
Public Member Functions inherited from SoRefCounter | |
void | ref () const |
Adds a reference to an instance. | |
void | unref () const |
Removes a reference from an instance. | |
void | unrefNoDelete () const |
unrefNoDelete() should be called when it is desired to decrement the reference count, but not delete the instance if this brings the reference count to zero. | |
int | getRefCount () const |
Returns current reference count. | |
void | lock () const |
lock this instance. | |
void | unlock () const |
unlock this instance. | |
Static Public Member Functions | |
static SoCameraInteractor * | getNewInstance (SoCamera *camera) |
Create an instance for the specified camera. | |
Utility class to manipulate a camera.
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:
Handler for mouse events that implements "orbit" camera manipulation:
SceneInteractor, SceneExaminer
Definition at line 121 of file SoCameraInteractor.h.
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 |
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 |
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 |
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 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::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::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 | ( | 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::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::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 | ( | 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::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::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 |
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 |