Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]
Public Member Functions | Protected Attributes

vl::Camera Class Reference

Represents a virtual camera defining, among other things, the point of view from which scenes can be rendered. More...

#include <Camera.hpp>

Inheritance diagram for vl::Camera:
vl::Object

List of all members.

Public Member Functions

virtual const char * className ()
 Returns the name of the class.
 Camera ()
 Constructs a perspective projecting camera with FOV = 60.0, Near Plane = 0.05, Far Plane = 10000.0.
void computeNearFarOptimizedProjMatrix (const Sphere &scene_bounding_sphere)
 The near and far clipping planes are adjusted to fit the provided scene_bounding_sphere.
void computeFrustumPlanes ()
 Computes the Camera's frustum planes in world space.
void applyModelViewMatrix (const mat4 &model_matrix) const
 Loads the GL_MODELVIEW matrix with the Camera's view matrix multiplied by the specified model matrix.
void applyViewMatrix () const
 Loads the GL_MODELVIEW matrix with the Camera's view matrix.
void applyProjMatrix () const
 Loads the GL_PROJECTION matrix with the Camera's projection matrix.
Real aspectRatio () const
 Returns the aspect ratio computed as viewport()->width()/viewport()->height().
void setFOV (Real fov)
 The field of view of the camera.
Real fov () const
 The field of view of the camera.
void setNearPlane (Real nearplane)
 The near clipping plane.
Real nearPlane () const
 The near clipping plane.
void setFarPlane (Real farplane)
 The far clipping plane.
Real farPlane () const
 The far clipping plane.
void setFrustum (const Frustum &frustum)
 The view frustum of the camera used to perform frustum culling.
const Frustumfrustum () const
 The view frustum of the camera used to perform frustum culling.
Frustumfrustum ()
 The view frustum of the camera used to perform frustum culling.
void setViewport (Viewport *viewport)
 The viewport bound to a camera.
Viewportviewport ()
 The viewport bound to a camera.
const Viewportviewport () const
 The viewport bound to a camera.
void followTransform (Transform *transform)
 Bind the camera to a Transform.
const TransformfollowedTransform () const
 Returns the Transform bound to a camera.
TransformfollowedTransform ()
 Returns the Transform bound to a camera.
void setViewMatrix (const mat4 &mat)
 Sets the Camera's view matrix.
const mat4viewMatrix () const
 The Camera's view matrix.
void setInverseViewMatrix (const mat4 &mat)
 The camera's reference frame matrix, the inverse of the view matrix.
const mat4inverseViewMatrix () const
 The camera's reference frame matrix, the inverse of the view matrix.
void setProjectionMatrix (const mat4 &mat)
 The camera's projection matrix.
const mat4projectionMatrix () const
 The camera's projection matrix.
void setProjectionAsPerspective ()
 Builds a perspective projection matrix for the Camera based on the Camera's and Viewport's settings.
void setProjectionAsPerspective (Real fov, Real near, Real far)
 Builds a perspective projection matrix for the Camera based on the Camera's and Viewport's settings.
void setProjectionAsFrustum (Real left, Real right, Real bottom, Real top, Real near, Real far)
 Builds a perspective projection matrix for the Camera based on the give frustum.
void setProjectionAsOrtho (Real offset=-0.5f)
 Builds an orthographic projection matrix for the Camera based on the Camera's near/far planes and its Viewport's settings.
void setProjectionAsOrtho2D (Real offset=-0.5f)
 Builds an orthographic projection matrix for the Camera based on its Viewport's settings.
void setViewMatrixAsLookAt (const vec3 &eye, const vec3 &center, const vec3 &up)
 Setup the modelview transform of the camera based on look-at parameters.
void getViewMatrixAsLookAt (vec3 &eye, vec3 &look, vec3 &up, vec3 &right) const
 Returns the look-at parameters of the modelview transform.
bool project (const vec4 &in_world, vec4 &out_viewp) const
 Projects a vector from world coordinates to viewport coordinates.
bool unproject (const vec3 &in_viewp, vec4 &out_world) const
 Unprojects a vector from viewport coordinates to world coordinates.
bool unproject (std::vector< vec3 > &points) const
 Unprojects a set of points.
Ray computeRay (int viewp_x, int viewp_y)
 Computes the ray passing through the point <viewp_x,viewp_y>.
Frustum computeRayFrustum (int viewp_x, int viewp_y)
 Computes a 1 pixel wide frustum suitable to cull objects during ray intersection detection.
void adjustView (const AABB &aabb, const vec3 &dir, const vec3 &up, Real bias=1.0f)
 Adjusts the camera position so that the given aabb can be properly viewed.

Protected Attributes

mat4 mViewMatrix
mat4 mInverseViewMatrix
mat4 mProjectionMatrix
ref< ViewportmViewport
Frustum mFrustum
ref< TransformmFollowTransform
Real mFOV
Real mNearPlane
Real mFarPlane

Detailed Description

Represents a virtual camera defining, among other things, the point of view from which scenes can be rendered.

Definition at line 50 of file Camera.hpp.


Constructor & Destructor Documentation

Camera::Camera (  )

Constructs a perspective projecting camera with FOV = 60.0, Near Plane = 0.05, Far Plane = 10000.0.

Definition at line 46 of file Camera.cpp.

References farPlane(), fov(), vl::Matrix4< GLfloat >::getPerspective(), mFarPlane, mFOV, mFrustum, mNearPlane, mProjectionMatrix, mViewport, nearPlane(), vl::Frustum::planes(), and VL_DEBUG_SET_OBJECT_NAME.


Member Function Documentation

virtual const char* vl::Camera::className (  ) [inline, virtual]

Returns the name of the class.

Reimplemented from vl::Object.

Definition at line 53 of file Camera.hpp.

void Camera::computeNearFarOptimizedProjMatrix ( const Sphere scene_bounding_sphere )

The near and far clipping planes are adjusted to fit the provided scene_bounding_sphere.

Optimizing the near and far clipping planes results in an optimized usage of the z-buffer with the consequence of minimizing possible z-fighting artifacts, thus enhancing the rendering quality.

Note:
Optimizing the near and far clipping planes might slightly impact the rendering performances if the scene contains a huge number of objects.
At the moment the near and far clipping planes optimization is available only when using a perspective projection matrix set up by setProjectionAsPerspective().

Definition at line 98 of file Camera.cpp.

References vl::Sphere::center(), vl::Sphere::isNull(), vl::max(), mFarPlane, mNearPlane, vl::Sphere::radius(), setProjectionAsPerspective(), vl::Sphere::transformed(), viewMatrix(), and vl::Vector3< T_Scalar >::z().

Referenced by vl::Rendering::render().

void Camera::computeFrustumPlanes (  )

Computes the Camera's frustum planes in world space.

If nearFarClippingPlanesOptimized() == true the near and far culling planes distances are respectively set to nearPlane() and farPlane().

Definition at line 155 of file Camera.cpp.

References vl::extractPlanes(), mFrustum, vl::Frustum::planes(), projectionMatrix(), and viewMatrix().

Referenced by vl::Rendering::render().

void Camera::applyModelViewMatrix ( const mat4 model_matrix ) const

Loads the GL_MODELVIEW matrix with the Camera's view matrix multiplied by the specified model matrix.

Definition at line 58 of file Camera.cpp.

References vl::Matrix4< T_Scalar >::e(), vl::Matrix4< T_Scalar >::ptr(), viewMatrix(), VL_glLoadMatrix, and VL_glMultMatrix.

Referenced by vl::Light::apply().

void Camera::applyViewMatrix (  ) const

Loads the GL_MODELVIEW matrix with the Camera's view matrix.

Definition at line 86 of file Camera.cpp.

References vl::Matrix4< T_Scalar >::e(), vl::Matrix4< T_Scalar >::ptr(), viewMatrix(), and VL_glLoadMatrix.

Referenced by vl::EdgeRenderer::renderLines(), and vl::EdgeRenderer::renderSolids().

void Camera::applyProjMatrix (  ) const

Loads the GL_PROJECTION matrix with the Camera's projection matrix.

Definition at line 79 of file Camera.cpp.

References projectionMatrix(), and VL_glLoadMatrix.

Referenced by vl::EdgeRenderer::render().

Real vl::Camera::aspectRatio (  ) const [inline]

Returns the aspect ratio computed as viewport()->width()/viewport()->height().

If viewport() == NULL the function returns 0.

Definition at line 83 of file Camera.hpp.

Referenced by setProjectionAsPerspective().

void vl::Camera::setFOV ( Real  fov ) [inline]

The field of view of the camera.

Note:
This setting will not take effect until setProjectionAsPerspective() is called.

Definition at line 93 of file Camera.hpp.

Referenced by setProjectionAsFrustum(), and setProjectionAsPerspective().

Real vl::Camera::fov (  ) const [inline]

The field of view of the camera.

Definition at line 96 of file Camera.hpp.

Referenced by Camera(), and setProjectionAsPerspective().

void vl::Camera::setNearPlane ( Real  nearplane ) [inline]

The near clipping plane.

Note:
This setting will not take effect until setProjectionAsPerspective() or setProjectionAsOrtho() is called.

Definition at line 100 of file Camera.hpp.

Referenced by setProjectionAsFrustum(), and setProjectionAsPerspective().

Real vl::Camera::nearPlane (  ) const [inline]

The near clipping plane.

Definition at line 103 of file Camera.hpp.

Referenced by Camera(), setProjectionAsOrtho(), and setProjectionAsPerspective().

void vl::Camera::setFarPlane ( Real  farplane ) [inline]

The far clipping plane.

Note:
This setting will not take effect until setProjectionAsPerspective() or setProjectionAsOrtho() is called.

Definition at line 107 of file Camera.hpp.

Referenced by setProjectionAsFrustum(), and setProjectionAsPerspective().

Real vl::Camera::farPlane (  ) const [inline]

The far clipping plane.

Definition at line 110 of file Camera.hpp.

Referenced by Camera(), setProjectionAsOrtho(), and setProjectionAsPerspective().

void vl::Camera::setFrustum ( const Frustum frustum ) [inline]

The view frustum of the camera used to perform frustum culling.

Definition at line 113 of file Camera.hpp.

const Frustum& vl::Camera::frustum (  ) const [inline]

The view frustum of the camera used to perform frustum culling.

Definition at line 116 of file Camera.hpp.

Referenced by adjustView(), computeRayFrustum(), vl::SceneManagerPortals::extractVisibleActors(), vl::ActorTreeAbstract::extractVisibleActors(), and vl::Rendering::render().

Frustum& vl::Camera::frustum (  ) [inline]

The view frustum of the camera used to perform frustum culling.

Definition at line 119 of file Camera.hpp.

void vl::Camera::setViewport ( Viewport viewport ) [inline]

The viewport bound to a camera.

Definition at line 122 of file Camera.hpp.

Viewport* vl::Camera::viewport (  ) [inline]
const Viewport* vl::Camera::viewport (  ) const [inline]

The viewport bound to a camera.

Definition at line 128 of file Camera.hpp.

void vl::Camera::followTransform ( Transform transform ) [inline]

Bind the camera to a Transform.

Definition at line 131 of file Camera.hpp.

const Transform* vl::Camera::followedTransform (  ) const [inline]

Returns the Transform bound to a camera.

Definition at line 134 of file Camera.hpp.

Transform* vl::Camera::followedTransform (  ) [inline]

Returns the Transform bound to a camera.

Definition at line 137 of file Camera.hpp.

void vl::Camera::setViewMatrix ( const mat4 mat ) [inline]

Sets the Camera's view matrix.

Definition at line 140 of file Camera.hpp.

References vl::Matrix4< T_Scalar >::invert().

const mat4& vl::Camera::viewMatrix (  ) const [inline]
void vl::Camera::setInverseViewMatrix ( const mat4 mat ) [inline]

The camera's reference frame matrix, the inverse of the view matrix.

Definition at line 146 of file Camera.hpp.

References vl::Matrix4< T_Scalar >::invert().

Referenced by adjustView(), vl::TrackballManipulator::mouseMoveEvent(), vl::Rendering::render(), setViewMatrixAsLookAt(), and vl::GhostCameraManipulator::updateEvent().

const mat4& vl::Camera::inverseViewMatrix (  ) const [inline]
void vl::Camera::setProjectionMatrix ( const mat4 mat ) [inline]

The camera's projection matrix.

Note:
This function sets the fov, near clipping plane and far clipping plane to -1.

Definition at line 155 of file Camera.hpp.

Referenced by setProjectionAsOrtho(), and setProjectionAsOrtho2D().

const mat4& vl::Camera::projectionMatrix (  ) const [inline]
void Camera::setProjectionAsPerspective (  )

Builds a perspective projection matrix for the Camera based on the Camera's and Viewport's settings.

See also http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml for more information.

Definition at line 180 of file Camera.cpp.

References aspectRatio(), farPlane(), fov(), vl::Matrix4< GLfloat >::getPerspective(), mProjectionMatrix, and nearPlane().

Referenced by computeNearFarOptimizedProjMatrix(), and vl::Applet::resizeEvent().

void Camera::setProjectionAsPerspective ( Real  fov,
Real  near,
Real  far 
)

Builds a perspective projection matrix for the Camera based on the Camera's and Viewport's settings.

See also http://www.opengl.org/sdk/docs/man/xhtml/gluPerspective.xml for more information.

Definition at line 172 of file Camera.cpp.

References aspectRatio(), vl::Matrix4< GLfloat >::getPerspective(), mProjectionMatrix, setFarPlane(), setFOV(), and setNearPlane().

void Camera::setProjectionAsFrustum ( Real  left,
Real  right,
Real  bottom,
Real  top,
Real  near,
Real  far 
)

Builds a perspective projection matrix for the Camera based on the give frustum.

See also http://www.opengl.org/sdk/docs/man/xhtml/glFrustum.xml for more information.

Note:
This method sets fov() to -1.

Definition at line 164 of file Camera.cpp.

References vl::Matrix4< GLfloat >::getFrustum(), mProjectionMatrix, setFarPlane(), setFOV(), and setNearPlane().

void Camera::setProjectionAsOrtho ( Real  offset = -0.5f )

Builds an orthographic projection matrix for the Camera based on the Camera's near/far planes and its Viewport's settings.

See also http://www.opengl.org/sdk/docs/man/xhtml/glOrtho.xml for more information. Equivalent to:

    setProjectionMatrix( 
      mat4::getOrtho(
        offset, (Real)mViewport->width()  + offset,
        offset, (Real)mViewport->height() + offset,
        nearPlane(), farPlane())
    );

Definition at line 185 of file Camera.cpp.

References farPlane(), vl::Matrix4< GLfloat >::getOrtho(), mViewport, nearPlane(), and setProjectionMatrix().

void Camera::setProjectionAsOrtho2D ( Real  offset = -0.5f )

Builds an orthographic projection matrix for the Camera based on its Viewport's settings.

See also http://www.opengl.org/sdk/docs/man/xhtml/glOrtho2D.xml for more information. Equivalent to:

    setProjectionMatrix( 
      mat4::getOrtho(
        offset, viewport()->width()  + offset,
        offset, viewport()->height() + offset,
        -1, +1)
    );

Definition at line 195 of file Camera.cpp.

References vl::Matrix4< GLfloat >::getOrtho(), mViewport, and setProjectionMatrix().

void Camera::setViewMatrixAsLookAt ( const vec3 eye,
const vec3 center,
const vec3 up 
)

Setup the modelview transform of the camera based on look-at parameters.

Parameters:
eyeThe position of the camera.
centerThe point the camera is looking at.
upThe vector defining the up direction.

Definition at line 205 of file Camera.cpp.

References vl::Matrix4< GLfloat >::getLookAt(), and setInverseViewMatrix().

void Camera::getViewMatrixAsLookAt ( vec3 eye,
vec3 look,
vec3 up,
vec3 right 
) const

Returns the look-at parameters of the modelview transform.

Parameters:
eyeThe position of the camera.
lookThe direction the camera is looking at.
upThe vector defining the Y positive direction of the camera.
rightThe vector defining the X positive direction of the camera.

Definition at line 212 of file Camera.cpp.

References vl::Matrix4< T_Scalar >::getAsLookAt(), and mInverseViewMatrix.

bool Camera::project ( const vec4 in_world,
vec4 out_viewp 
) const
bool Camera::unproject ( const vec3 in_viewp,
vec4 out_world 
) const

Unprojects a vector from viewport coordinates to world coordinates.

Parameters:
in_viewpThe viewport coordinates point to be projected. Note that, in accordance to OpenGL conventions, the viewport coordinates have the Y axis pointing upwards and origin at the bottom left corner of the viewport.
out_worldThe world coordinates of the projected point.
Note:
The z coordinate of in_viewp determines the position of the projected point along the ray passing through in_viewp. If z equals 0 then out_world will lay on the near clipping plane, if z equals 1 then out_world will lay on the far clipping plane.

Definition at line 239 of file Camera.cpp.

References mProjectionMatrix, mViewMatrix, mViewport, vl::Vector4< T_Scalar >::w(), vl::Vector3< T_Scalar >::x(), vl::Vector4< T_Scalar >::x(), vl::Vector3< T_Scalar >::y(), vl::Vector4< T_Scalar >::y(), vl::Vector3< T_Scalar >::z(), and vl::Vector4< T_Scalar >::z().

Referenced by computeRay(), and computeRayFrustum().

bool Camera::unproject ( std::vector< vec3 > &  points ) const

Unprojects a set of points.

See unproject(const vec3& in_viewp, vec4& out_world) for more information.

Definition at line 269 of file Camera.cpp.

References mProjectionMatrix, mViewMatrix, mViewport, vl::Vector4< T_Scalar >::w(), vl::Vector4< T_Scalar >::x(), vl::Vector4< T_Scalar >::y(), and vl::Vector4< T_Scalar >::z().

Ray Camera::computeRay ( int  viewp_x,
int  viewp_y 
)

Computes the ray passing through the point <viewp_x,viewp_y>.

Parameters:
viewp_xThe x position of the point defining the ray
viewp_yThe y position of the point defining the ray
Note:
<viewp_x,viewp_y> is considered to be a point in viewport coordinate. In accordance to the OpenGL conventions the viewport coordinates have origin on the bottom left corner of the viewport, with the Y axis pointing upwards. Most GUI libraries define the the orgin at the top left corner of the window and Y axis pointing downwards. You can convert such coordinates to OpenGL ones using the folloviewp_g simple formula: "opengl_y = window_height - coord_y".

Definition at line 304 of file Camera.cpp.

References vl::Matrix4< T_Scalar >::getT(), inverseViewMatrix(), vl::normalize(), vl::Ray::setDirection(), vl::Ray::setOrigin(), unproject(), and vl::Vector4< T_Scalar >::xyz().

Frustum Camera::computeRayFrustum ( int  viewp_x,
int  viewp_y 
)

Computes a 1 pixel wide frustum suitable to cull objects during ray intersection detection.

Definition at line 318 of file Camera.cpp.

References vl::cross(), frustum(), vl::Frustum::planes(), unproject(), and vl::Vector4< T_Scalar >::xyz().

void Camera::adjustView ( const AABB aabb,
const vec3 dir,
const vec3 up,
Real  bias = 1.0f 
)

Adjusts the camera position so that the given aabb can be properly viewed.

Parameters:
aabbThe AABB (in world coords) that should be visible from the newly computed camera position.
dirThe direction (in world coords) along which the camera should be displaced to view the given AABB.
upThe vector that defines the up direction (in world coords). Used to properly compute the new camera matrix.
biasA bias factor used to adjust the computed camera distance from the given AABB. Values between 0 and 1 make the camera closer to the AABB center, values greater than 1 position the camera further away.

Definition at line 122 of file Camera.cpp.

References vl::Log::bug(), vl::AABB::center(), vl::dot(), vl::extractPlanes(), frustum(), vl::Matrix4< GLfloat >::getLookAt(), vl::Matrix4< T_Scalar >::getT(), vl::Matrix4< T_Scalar >::getZ(), inverseViewMatrix(), vl::Plane::normal(), vl::Plane::origin(), vl::Frustum::plane(), vl::Frustum::planes(), projectionMatrix(), vl::Sphere::radius(), setInverseViewMatrix(), viewMatrix(), and VL_CHECK.

Referenced by vl::TrackballManipulator::adjustView().


Member Data Documentation

Definition at line 244 of file Camera.hpp.

Referenced by project(), and unproject().

Definition at line 245 of file Camera.hpp.

Referenced by getViewMatrixAsLookAt().

Definition at line 247 of file Camera.hpp.

Referenced by Camera(), project(), setProjectionAsOrtho(), setProjectionAsOrtho2D(), and unproject().

Definition at line 248 of file Camera.hpp.

Referenced by Camera(), and computeFrustumPlanes().

Definition at line 249 of file Camera.hpp.

Real vl::Camera::mFOV [protected]

Definition at line 250 of file Camera.hpp.

Referenced by Camera().

Definition at line 251 of file Camera.hpp.

Referenced by Camera(), and computeNearFarOptimizedProjMatrix().

Definition at line 252 of file Camera.hpp.

Referenced by Camera(), and computeNearFarOptimizedProjMatrix().


The documentation for this class was generated from the following files:

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:37.
Permission is granted to use this page to write and publish articles regarding Visualization Library.