Easy3D 2.6.1
|
A perspective or orthographic camera. More...
#include <easy3d/renderer/camera.h>
Public Types | |
enum | Type { PERSPECTIVE , ORTHOGRAPHIC } |
Enumerates the two possible types of Camera. More... | |
Public Member Functions | |
Camera () | |
Default constructor. | |
virtual | ~Camera () |
Destructor. | |
Camera (const Camera &camera) | |
Copy constructor. | |
Camera & | operator= (const Camera &camera) |
Assignment operator. | |
Position and orientation | |
vec3 | position () const |
Get the position of the camera. | |
vec3 | upVector () const |
Get the up vector of the camera. | |
vec3 | viewDirection () const |
Get the view direction of the camera. | |
vec3 | rightVector () const |
Get the right vector of the camera. | |
quat | orientation () const |
Get the orientation of the camera. | |
void | set_from_model_view_matrix (const mat4 &mv) const |
Sets the Camera's position() and orientation() from an OpenGL ModelView matrix. | |
void | set_from_calibration (float fx, float fy, float skew, float cx, float cy, const mat3 &R, const vec3 &t, bool convert=true) |
Defines the position(), orientation() and fieldOfView() of the camera from calibrated camera intrinsic and extrinsic parameters. This function assumes the camera parameters were obtained by standard camera calibration, in which image coordinates are denoted in pixels, with the origin point (0, 0) corresponding to the top-left corner of the image. The X axis starts at the left edge of an image and goes towards the right edge. The Y axis starts at the top of the image towards image bottom. All image pixels have non-negative coordinates. | |
void | set_from_calibration (const mat34 &proj) |
Defines the position(), orientation() and fieldOfView() of the camera from calibrated camera intrinsic and extrinsic parameters. This function assumes the camera parameters were obtained by standard camera calibration, in which image coordinates are denoted in pixels, with the origin point (0, 0) corresponding to the top-left corner of the image. The X axis starts at the left edge of an image and goes towards the right edge. The Y axis starts at the top of the image towards image bottom. All image pixels have non-negative coordinates. | |
void | set_projection_matrix (const mat4 &proj) |
Temporally change the projection matrix. | |
void | set_modelview_matrix (const mat4 &mv) |
Temporally change the modelview matrix. | |
void | setPosition (const vec3 &pos) const |
Set the position of the camera. | |
void | setOrientation (const quat &q) const |
Set the orientation of the camera. | |
void | setOrientation (float theta, float phi) const |
Set the orientation of the camera. | |
void | setUpVector (const vec3 &up, bool noMove=true) const |
Set the up vector of the camera. | |
void | setViewDirection (const vec3 &direction) const |
Set the view direction of the camera. | |
Positioning tools | |
void | lookAt (const vec3 &target) const |
Make the camera look at a target. | |
void | showEntireScene () const |
Show the entire scene. | |
void | fitSphere (const vec3 ¢er, float radius) const |
Move the camera so that entire sphere fits the screen. | |
void | fitBoundingBox (const vec3 &min, const vec3 &max) const |
Move the camera so that entire box fits the screen. | |
void | fitScreenRegion (int xmin, int ymin, int xmax, int ymax) const |
Moves the Camera so that the rectangular screen region fits the screen. | |
void | centerScene () const |
Moves the Camera so that its sceneCenter() is projected in the center of the screen. | |
KeyFrameInterpolator * | keyframe_interpolator () const |
Get the keyframe interpolator. | |
void | interpolateToLookAt (const vec3 &point) |
Perform keyframe interpolation to look at a point. | |
void | interpolateToFitScene () |
Perform keyframe interpolation to fit the scene. | |
void | interpolateTo (const Frame *frame, float duration) |
Perform keyframe interpolation to a frame. | |
Type | type () const |
Get the type of the camera. | |
float | fieldOfView () const |
Returns the vertical field of view of the camera (in radians). | |
float | horizontalFieldOfView () const |
Returns the horizontal field of view of the camera (in radians). | |
float | aspectRatio () const |
Returns the Camera aspect ratio defined by screenWidth() / screenHeight(). | |
int | screenWidth () const |
Returns the width (in pixels) of the Camera screen. | |
int | screenHeight () const |
Returns the height (in pixels) of the Camera screen. | |
float | pixelGLRatio (const vec3 &position) const |
Get the pixel GL ratio at a position. | |
float | zNearCoefficient () const |
Returns the coefficient which is used to set zNear() when the Camera is inside the sphere defined by sceneCenter() and zClippingCoefficient() * sceneRadius(). | |
float | zClippingCoefficient () const |
Returns the coefficient used to position the near and far clipping planes. | |
virtual float | zNear () const |
Get the near clipping plane distance. | |
virtual float | zFar () const |
Get the far clipping plane distance. | |
virtual void | getOrthoWidthHeight (float &halfWidth, float &halfHeight) const |
Get the orthographic width and height. | |
void | getFrustumPlanesCoefficients (float coef[6][4]) const |
Get the frustum planes coefficients. | |
void | getFrustumPlanesCoefficients2 (float coef[6][4]) const |
Get the frustum planes coefficients (alternative version). | |
void | setType (Type type) |
Set the type of the camera. | |
void | setFieldOfView (float fov) |
Set the field of view of the camera. | |
void | setHorizontalFieldOfView (float hfov) |
Sets the horizontalFieldOfView() of the Camera (in radians). | |
void | setFOVToFitScene () |
Set the field of view to fit the scene. | |
void | setAspectRatio (float aspect) |
Defines the Camera aspectRatio(). | |
void | setScreenWidthAndHeight (int width, int height) |
Set the screen width and height. | |
void | setZNearCoefficient (float coef) |
Set the zNear coefficient. | |
void | setZClippingCoefficient (float coef) |
Set the zClipping coefficient. | |
Scene radius and center | |
float | sceneRadius () const |
Returns the radius of the scene observed by the Camera. | |
vec3 | sceneCenter () const |
Returns the position of the scene center, defined in the world coordinate system. | |
float | distanceToSceneCenter () const |
Get the distance to the scene center. | |
void | setSceneRadius (float radius) |
Set the radius of the scene. | |
void | setSceneCenter (const vec3 ¢er) |
Set the center of the scene. | |
void | setSceneBoundingBox (const vec3 &min, const vec3 &max) |
Set the bounding box of the scene. | |
void | setPivotPoint (const vec3 &point) |
Set the pivot point of the camera. After this method is called, the Camera rotates around this point. | |
vec3 | pivotPoint () const |
Get the pivot point of the camera. | |
ManipulatedCameraFrame * | frame () const |
Returns the ManipulatedFrame attached to the Camera. | |
void | setFrame (ManipulatedCameraFrame *const mcf) |
Set the manipulated frame of the camera. | |
void | computeProjectionMatrix () |
Compute the projection matrix. | |
void | computeModelViewMatrix () |
Compute the model view matrix. | |
const mat4 & | projectionMatrix () const |
Get the projection matrix. | |
const mat4 & | modelViewMatrix () const |
Get the model view matrix. | |
mat4 | modelViewProjectionMatrix () const |
Get the model view projection matrix. | |
World to Camera coordinate systems conversions | |
vec3 | cameraCoordinatesOf (const vec3 &src) const |
Convert world coordinates to camera coordinates. | |
vec3 | worldCoordinatesOf (const vec3 &src) const |
Convert camera coordinates to world coordinates. | |
2D screen to 3D world coordinate systems conversions | |
Signal | frame_modified |
A signal indicating the frame has been modified. | |
vec3 | projectedCoordinatesOf (const vec3 &src, const Frame *frame=nullptr) const |
Returns the screen projected coordinates of a 3D point defined in the frame coordinate system. When frame in NULL (default), src is expressed in the world coordinate system. | |
vec3 | unprojectedCoordinatesOf (const vec3 &src, const Frame *frame=nullptr) const |
Returns the world unprojected coordinates of a point defined in the screen coordinate system. The result is expressed in the frame coordinate system. When frame is NULL (default), the result is expressed in the world coordinate system. | |
void | convertClickToLine (int x, int y, vec3 &orig, vec3 &dir) const |
Returns the coefficients of a 3D half-line passing through the Camera eye and pixel (x, y). | |
A perspective or orthographic camera.
A Camera defines some intrinsic parameters (fieldOfView(), position(), viewDirection(), upVector()...) and useful positioning tools that ease its placement (showEntireScene(), fitSphere(), lookAt()...). It exports its associated OpenGL projection and modelview matrices and can interactively be modified using the mouse.
The position() and orientation() of the Camera are defined by a ManipulatedFrame (retrieved using frame()). These methods are just convenient wrappers to the equivalent Frame methods. This also means that the Camera frame() can be attached to a Frame::referenceFrame() which enables complex Camera setups.
If a pivotPoint() has been set, the Camera observes a scene and rotates around its pivotPoint().
The type() of the Camera can be Camera::ORTHOGRAPHIC or Camera::PERSPECTIVE (see Type()). fieldOfView() is meaningless with Camera::ORTHOGRAPHIC.
The near and far planes of the Camera are fitted to the scene and determined from scene radius, scene center, and zClippingCoefficient() by the zNear() and zFar() methods. Reasonable values on the scene extends hence have to be provided to the Camera to correctly display the scene. High level positioning methods also use this information (showEntireScene(), centerScene()...).
A Camera holds KeyFrameInterpolator that can be used to save Camera positions and paths. You can interactively addKeyFrameToPath() to a given path. Use playPath() to make the Camera follow the path.
Use cameraCoordinatesOf() and worldCoordinatesOf() to convert to and from the Camera frame() coordinate system. projectedCoordinatesOf() and unprojectedCoordinatesOf() will convert from screen to 3D coordinates. convertClickToLine() is very useful for analytical object selection.
A Camera can also be used outside a viewer or even without OpenGL for its coordinate system conversion capabilities. Note however that some of them explicitly rely on the presence of a Z-buffer.
To use the camera, you need to do the following:
camera_->setScreenWidthAndHeight()
at both camera_->frame()->action_start()
on mouse down and camera_->frame()->action_end()
on mouse up. camera_->frame()->action_rotate()
on mouse move for rotation and camera_->frame()->action_translate()
on mouse move for translation and camera_->frame()->action_zoom()
on mouse move for zoomTo make the entire scene visible, call camera_->setSceneBoundingBox()
and camera_->showEntireScene()
;
To retrieve the model view projection matrix, call camera_->modelViewProjectionMatrix()
enum Type |
Enumerates the two possible types of Camera.
See type() and setType(). This type mainly defines different Camera projection matrix (see loadProjectionMatrix()). Many other methods (convertClickToLine(), projectedCoordinatesOf(), pixelGLRatio()...) are affected by this Type.
Enumerator | |
---|---|
PERSPECTIVE | Perspective camera. |
ORTHOGRAPHIC | Orthographic camera. |
|
inline |
Returns the Camera aspect ratio defined by screenWidth() / screenHeight().
When the Camera is attached to a Viewer, these values and hence the aspectRatio() are automatically fitted to the viewer's window aspect ratio using setScreenWidthAndHeight().
Convert world coordinates to camera coordinates.
src | The source coordinates in world space. |
void centerScene | ( | ) | const |
Moves the Camera so that its sceneCenter() is projected in the center of the screen.
This method simply projects the current position on a line passing through sceneCenter(). The orientation() and fieldOfView() are unchanged.
Returns the coefficients of a 3D half-line passing through the Camera eye and pixel (x, y).
The origin of the half line (eye position) is stored in orig
, while dir
contains the properly oriented and normalized direction of the half line. x
and y
are expressed as the origin defined in the upper left corner. Use screenHeight() - y - 1 to convert to OpenGL units.
x | The x coordinate of the click. |
y | The y coordinate of the click. |
orig | The origin of the line. |
dir | The direction of the line. |
float distanceToSceneCenter | ( | ) | const |
Get the distance to the scene center.
|
inline |
Returns the vertical field of view of the camera (in radians).
Value is set using setFieldOfView(). Default value is pi/4 radians. This value is meaningless if the Camera type() is Camera::ORTHOGRAPHIC. The field of view corresponds the one used in gluPerspective
(see manual). It sets the Y (vertical) aperture of the Camera. The X (horizontal) angle is inferred from the window aspect ratio (see aspectRatio() and horizontalFieldOfView()). Use setFOVToFitScene() to adapt the fieldOfView() to a given scene.
Move the camera so that entire box fits the screen.
min | The minimum corner of the bounding box. |
max | The maximum corner of the bounding box. |
void fitScreenRegion | ( | int | xmin, |
int | ymin, | ||
int | xmax, | ||
int | ymax ) const |
Moves the Camera so that the rectangular screen region fits the screen.
The rectangular screen region is defined in pixel units, with origin in the upper left corner. The Camera is translated (its orientation() is unchanged) so that rectangle
is entirely visible. Since the pixel coordinates only define a frustum in 3D, it's the intersection of this frustum with a plane (orthogonal to the viewDirection() and passing through the sceneCenter()) that is used to define the 3D rectangle that is eventually fitted.
xmin | The minimum x coordinate. |
ymin | The minimum y coordinate. |
xmax | The maximum x coordinate. |
ymax | The maximum y coordinate. |
void fitSphere | ( | const vec3 & | center, |
float | radius ) const |
Move the camera so that entire sphere fits the screen.
center | The center of the sphere. |
radius | The radius of the sphere. |
|
inline |
Returns the ManipulatedFrame attached to the Camera.
This ManipulatedFrame defines its position() and orientation() and can translate mouse events into Camera displacement. Set using setFrame().
void getFrustumPlanesCoefficients | ( | float | coef[6][4] | ) | const |
Get the frustum planes coefficients.
coef | The coefficients. The six 4-component vectors of coef respectively correspond to the left, right, near, far, top and bottom Camera frustum planes. Each vector holds a plane equation of the form: a*x + b*y + c*z + d = 0
a ,b , c, and d are the 4 components of each vector, in that order. |
void getFrustumPlanesCoefficients2 | ( | float | coef[6][4] | ) | const |
Get the frustum planes coefficients (alternative version).
coef | The coefficients. The six 4-component vectors of coef respectively correspond to the left, right, near, far, top and bottom Camera frustum planes. Each vector holds a plane equation of the form: a*x + b*y + c*z + d = 0
a ,b , c, and d are the 4 components of each vector, in that order. |
|
virtual |
Get the orthographic width and height.
halfWidth | The half width. |
halfHeight | The half height. |
|
inline |
Returns the horizontal field of view of the camera (in radians).
Value is set using setHorizontalFieldOfView() or setFieldOfView(). These values are linked by:
void interpolateTo | ( | const Frame * | frame, |
float | duration ) |
Perform keyframe interpolation to a frame.
frame | The frame to interpolate to. |
duration | The duration of the interpolation. |
void interpolateToLookAt | ( | const vec3 & | point | ) |
Perform keyframe interpolation to look at a point.
point | The point to look at. |
|
inline |
Get the keyframe interpolator.
void lookAt | ( | const vec3 & | target | ) | const |
Make the camera look at a target.
target | The target to look at. |
const mat4 & modelViewMatrix | ( | ) | const |
Get the model view matrix.
mat4 modelViewProjectionMatrix | ( | ) | const |
Get the model view projection matrix.
Assignment operator.
camera | The camera to assign. |
quat orientation | ( | ) | const |
Get the orientation of the camera.
vec3 pivotPoint | ( | ) | const |
Get the pivot point of the camera.
float pixelGLRatio | ( | const vec3 & | position | ) | const |
Get the pixel GL ratio at a position.
position | The position. |
vec3 position | ( | ) | const |
Get the position of the camera.
Returns the screen projected coordinates of a 3D point defined in the frame coordinate system. When frame
in NULL
(default), src
is expressed in the world coordinate system.
src | The source coordinates in world space. |
frame | The frame to use for the conversion. |
const mat4 & projectionMatrix | ( | ) | const |
Get the projection matrix.
vec3 rightVector | ( | ) | const |
Get the right vector of the camera.
|
inline |
Returns the position of the scene center, defined in the world coordinate system.
The scene observed by the Camera should be roughly centered on this position, and included in a sceneRadius() sphere. This approximate description of the scene permits a zNear() and zFar() clipping planes definition, and allows convenient positioning methods such as showEntireScene(). Default value is (0,0,0) (world origin). Use setSceneCenter() to change it.
|
inline |
Returns the radius of the scene observed by the Camera.
You need to provide such an approximation of the scene dimensions so that the Camera can adapt its zNear() and zFar() values. See the sceneCenter() documentation.
|
inline |
Returns the height (in pixels) of the Camera screen.
Set using setScreenWidthAndHeight(). This value is automatically fitted to the viewer's window dimensions when the Camera is attached to a Viewer.
|
inline |
Returns the width (in pixels) of the Camera screen.
Set using setScreenWidthAndHeight(). This value is automatically fitted to the Viewer's window dimensions when the Camera is attached to a viewer.
void set_from_calibration | ( | const mat34 & | proj | ) |
Defines the position(), orientation() and fieldOfView() of the camera from calibrated camera intrinsic and extrinsic parameters. This function assumes the camera parameters were obtained by standard camera calibration, in which image coordinates are denoted in pixels, with the origin point (0, 0) corresponding to the top-left corner of the image. The X axis starts at the left edge of an image and goes towards the right edge. The Y axis starts at the top of the image towards image bottom. All image pixels have non-negative coordinates.
proj | The projection matrix, which can be computed as P = K * M * [R|t], where R is a 3x3 matrix representing the camera rotation, and t is a 3D vector describing the camera translation (rotation first then translation). |
void set_from_calibration | ( | float | fx, |
float | fy, | ||
float | skew, | ||
float | cx, | ||
float | cy, | ||
const mat3 & | R, | ||
const vec3 & | t, | ||
bool | convert = true ) |
Defines the position(), orientation() and fieldOfView() of the camera from calibrated camera intrinsic and extrinsic parameters. This function assumes the camera parameters were obtained by standard camera calibration, in which image coordinates are denoted in pixels, with the origin point (0, 0) corresponding to the top-left corner of the image. The X axis starts at the left edge of an image and goes towards the right edge. The Y axis starts at the top of the image towards image bottom. All image pixels have non-negative coordinates.
fx | The focal length in the x direction. |
fy | The focal length in the y direction. |
skew | The skew coefficient. |
cx | The principal point in the x direction. |
cy | The principal point in the y direction. |
R | The rotation matrix. It denotes the coordinate system transformation from 3D world coordinates to 3D camera coordinates. |
t | The translation vector. It is the position of the origin of the world coordinate system expressed in the camera coordinate system. |
convert | True to convert from vision convention to OpenGL convention (i.e., invert Y and Z axes). This is because the camera coordinates of computer vision goes X right, Y down, Z forward, while the camera coordinates of OpenGL goes X right, Y up, Z inward. |
void set_from_model_view_matrix | ( | const mat4 & | mv | ) | const |
Sets the Camera's position() and orientation() from an OpenGL ModelView matrix.
This enables a Camera initialisation from another OpenGL application. After this method has been called, getModelViewMatrix() returns a matrix equivalent to mv
. Only the orientation() and position() of the Camera are modified.
mv | The model view matrix. |
void set_modelview_matrix | ( | const mat4 & | mv | ) |
Temporally change the modelview matrix.
mv | The modelview matrix. |
void set_projection_matrix | ( | const mat4 & | proj | ) |
Temporally change the projection matrix.
proj | The projection matrix. |
|
inline |
Defines the Camera aspectRatio().
This value is actually inferred from the screenWidth() / screenHeight() ratio. You should use setScreenWidthAndHeight() instead. This method might however be convenient when the Camera is not associated with a Viewer. It actually sets the screenHeight() to 100 and the screenWidth() accordingly. See also setFOVToFitScene().
aspect | The aspect ratio. |
void setFieldOfView | ( | float | fov | ) |
Set the field of view of the camera.
fov | The field of view in radians. |
void setFrame | ( | ManipulatedCameraFrame *const | mcf | ) |
Set the manipulated frame of the camera.
mcf | The manipulated frame. |
|
inline |
Sets the horizontalFieldOfView() of the Camera (in radians).
horizontalFieldOfView() and fieldOfView() are linked by the aspectRatio(). This method actually calls setFieldOfView(( 2.0 * std::atan (std::tan(hfov / 2.0) / aspectRatio()) )) so that a call to horizontalFieldOfView() returns the expected value.
hfov | The horizontal field of view in radians. |
void setOrientation | ( | const quat & | q | ) | const |
Set the orientation of the camera.
q | The orientation of the camera. |
void setOrientation | ( | float | theta, |
float | phi ) const |
Set the orientation of the camera.
theta | The theta angle. |
phi | The phi angle. |
void setPivotPoint | ( | const vec3 & | point | ) |
Set the pivot point of the camera. After this method is called, the Camera rotates around this point.
point | The pivot point. |
void setPosition | ( | const vec3 & | pos | ) | const |
Set the position of the camera.
pos | The position of the camera. |
Set the bounding box of the scene.
This method sets the scene center and radius from the bounding box.
min | The minimum corner of the bounding box. |
max | The maximum corner of the bounding box. |
void setSceneCenter | ( | const vec3 & | center | ) |
Set the center of the scene.
center | The center of the scene. |
void setSceneRadius | ( | float | radius | ) |
Set the radius of the scene.
radius | The radius of the scene. |
void setScreenWidthAndHeight | ( | int | width, |
int | height ) |
Set the screen width and height.
width | The width in pixels. |
height | The height in pixels. |
void setType | ( | Type | type | ) |
Set the type of the camera.
type | The type of the camera. |
void setUpVector | ( | const vec3 & | up, |
bool | noMove = true ) const |
Set the up vector of the camera.
up | The up vector. |
noMove | True to not move the camera. |
void setViewDirection | ( | const vec3 & | direction | ) | const |
Set the view direction of the camera.
direction | The view direction. |
|
inline |
Set the zClipping coefficient.
coef | The zClipping coefficient. |
|
inline |
Set the zNear coefficient.
coef | The zNear coefficient. |
|
inline |
Get the type of the camera.
A Camera::PERSPECTIVE Camera uses a classical projection mainly defined by its fieldOfView(). With a Camera::ORTHOGRAPHIC type(), the fieldOfView() is meaningless and the width and height of the camera frustum are inferred from the distance to the pivotPoint() using getOrthoWidthHeight(). Both types use zNear() and zFar() (to define their clipping planes) and aspectRatio() (for frustum shape).
Returns the world unprojected coordinates of a point defined in the screen coordinate system. The result is expressed in the frame
coordinate system. When frame
is NULL
(default), the result is expressed in the world coordinate system.
src | The source coordinates in screen space. |
frame | The frame to use for the conversion. |
vec3 upVector | ( | ) | const |
Get the up vector of the camera.
vec3 viewDirection | ( | ) | const |
Get the view direction of the camera.
Convert camera coordinates to world coordinates.
src | The source coordinates in camera space. |
|
inline |
Returns the coefficient used to position the near and far clipping planes.
The near (resp. far) clipping plane is positioned at a distance equal to zClippingCoefficient() * sceneRadius() in front of (resp. behind) the sceneCenter(). This guarantees an optimal use of the z-buffer range and minimizes aliasing. See the zNear() and zFar() documentations. Default value is square root of 3.0 (so that a cube of size sceneRadius() is not clipped). However, since the sceneRadius() is used for other purposes (see showEntireScene(), flySpeed(), ...) and you may want to change this value to define more precisely the location of the clipping planes. For a total control on clipping planes' positions, another option is to overload the zNear() and zFar() methods.
|
virtual |
Get the far clipping plane distance.
|
virtual |
Get the near clipping plane distance.
|
inline |
Returns the coefficient which is used to set zNear() when the Camera is inside the sphere defined by sceneCenter() and zClippingCoefficient() * sceneRadius().
In that case, the zNear() value is set to zNearCoefficient() * zClippingCoefficient() * sceneRadius(). See the zNear() documentation for details. Default value is 0.001, which is appropriate for most applications. In case you need a high dynamic ZBuffer precision, you can increase this value (~0.1). A lower value will prevent clipping of very close objects at the expense of the worst Z precision. Only meaningful when Camera type is Camera::PERSPECTIVE.