26 #include <Eigen/Dense> 98 projection_matrix = Eigen::Matrix4f::Identity();
117 return view_matrix.linear().inverse() * (-view_matrix.translation());
128 Eigen::Matrix4f mv = view_matrix.matrix();
129 for (
int i = 0; i < 16; ++i)
144 for (
int i = 0; i < 16; ++i)
157 inline Eigen::Vector3f
projectPoint (
const Eigen::Vector4f& pt, Eigen::Vector4f& viewport)
159 Eigen::Vector3f screen_pt = Eigen::Vector3f::Zero();
160 Eigen::Vector4f proj = projection_matrix * view_matrix * pt;
166 screen_pt[0] = viewport[0] + viewport[2] * (proj[0] * 0.5 + 0.5);
167 screen_pt[1] = viewport[1] + viewport[3] * (proj[1] * 0.5 + 0.5);
168 screen_pt[2] = proj[2]*0.5 + 0.5;
216 return view_matrix.rotation();
226 return view_matrix.translation();
237 return (
float)1.0f/tan((fovy/2.0f)*(M_PI/180.0f));
258 return Eigen::Vector2i(viewport[2], viewport[3]);
267 return (viewport[2] / viewport[3]);
281 viewport = Eigen::Vector4f(0.0, 0.0, (
float)vp[0], (
float)vp[1]);
293 viewport = Eigen::Vector4f(0.0, 0.0, vp[0], vp[1]);
302 projection_matrix = mat;
358 frustum_bottom = -1.0;
364 default_view = Eigen::Affine3f::Identity();
379 Eigen::Matrix4f out = Eigen::Matrix4f::Zero();
382 y_scale = (float)1.0/tan((fy/2.0)*(M_PI/180.0)),
383 x_scale = y_scale / in_aspect_ratio,
384 frustum_length = in_far_plane - in_near_plane;
388 out(2,2) = -((in_far_plane + in_near_plane) / frustum_length);
390 out(2,3) = -((2 * in_near_plane * in_far_plane) / frustum_length);
408 aspect_ratio = in_aspect_ratio;
409 near_plane = in_near_plane;
410 far_plane = in_far_plane;
414 use_perspective =
true;
438 static Eigen::Matrix4f
createOrthographicMatrix (
float left,
float right,
float bottom,
float top,
float near_plane,
float far_plane)
440 Eigen::Matrix4f out = Eigen::Matrix4f::Zero();
442 out(0,0) = 2.0/(right-left);
443 out(1,1) = 2.0/(top-bottom);
446 out(0,3) = -(right+left)/(right-left);
447 out(1,3) = -(top+bottom)/(top-bottom);
448 out(2,3) = -(far_plane+
near_plane)/(far_plane-near_plane);
465 Eigen::Matrix4f
setOrthographicMatrix (
float left,
float right,
float bottom,
float top,
float near_plane,
float far_plane)
469 use_perspective =
false;
491 view_matrix.translate(translation);
498 void rotate (
const Eigen::Quaternion<float>& rotation)
500 view_matrix.rotate(rotation);
507 void scale (
const Eigen::Vector3f& scale_factors)
509 view_matrix.scale(scale_factors);
518 view_matrix.scale(scale_factor);
Eigen::Affine3f getViewMatrix(void) const
Returns the view matrix as an Affine 3x3 matrix.
Definition: camera.hpp:177
float getNearPlane(void) const
Returns near plane value.
Definition: camera.hpp:319
float frustum_right
Frustum right dimension.
Definition: camera.hpp:63
bool use_perspective
Flag to indicate if using a perspective or othograpic projection.
Definition: camera.hpp:81
void setViewport(const Eigen::Vector2i &vp)
Definition: camera.hpp:279
void getViewMatrix(GLdouble *matrix)
Return the modelview matrix as a GLdouble array.
Definition: camera.hpp:126
float aspect_ratio
Aspect ratio for projection matrix.
Definition: camera.hpp:72
static Eigen::Matrix4f createPerspectiveMatrix(float fy, float in_aspect_ratio, float in_near_plane, float in_far_plane)
Returns a perspective projection matrix with the given parameters.
Definition: camera.hpp:377
void getProjectionMatrix(GLdouble *matrix)
Return the projection matrix as a GLdouble array.
Definition: camera.hpp:142
Definition: bufferobject.hpp:34
float getFarPlane(void) const
Returns far plane value.
Definition: camera.hpp:328
void setViewport(const Eigen::Vector4f &vp)
Sets the viewport coordinates.
Definition: camera.hpp:274
void scale(float scale_factor)
Scales the view matrix by given factor in all axis.
Definition: camera.hpp:516
float far_plane
Far plane used for projection matrix.
Definition: camera.hpp:57
Eigen::Matrix4f getProjectionMatrix(void) const
Returns the view matrix as an 4x4 matrix.
Definition: camera.hpp:195
void incrementFov(float inc)
Increases the fov of the perspective matrix by a given increment.
Definition: camera.hpp:477
float fovy
Field of view angle in y axis.
Definition: camera.hpp:78
float frustum_bottom
Frustum bottom dimension.
Definition: camera.hpp:69
float near_plane
Near plane used for projection matrix.
Definition: camera.hpp:54
~Camera()
Default destructor.
Definition: camera.hpp:336
Eigen::Matrix3f getRotationMatrix(void) const
Returns a 3x3 matrix containing only the rotation of the view matrix.
Definition: camera.hpp:214
void setViewMatrix(const Eigen::Affine3f &mat)
Sets the view matrix from a given an affine 3x3 matrix.
Definition: camera.hpp:309
void translate(const Eigen::Vector3f &translation)
Translates the view matrix by a given vector.
Definition: camera.hpp:489
void setProjectionMatrix(const Eigen::Matrix4f &mat)
Sets the projection matrix from a given 4x4 matrix.
Definition: camera.hpp:300
void resetProjectionMatrix(void)
Reset projection matrix.
Definition: camera.hpp:96
void rotate(const Eigen::Quaternion< float > &rotation)
Rotate the view matrix by a given quaternion.
Definition: camera.hpp:498
void changeFovy(float new_fovy)
Changes the fovy and computes new perspective projection matrix.
Definition: camera.hpp:422
void scale(const Eigen::Vector3f &scale_factors)
Scales the view matrix by given factors.
Definition: camera.hpp:507
Eigen::Matrix4f setPerspectiveMatrix(float fy, float in_aspect_ratio, float in_near_plane, float in_far_plane)
Sets the projection matrix as a perspective matrix.
Definition: camera.hpp:405
Eigen::Vector3f projectPoint(const Eigen::Vector4f &pt, Eigen::Vector4f &viewport)
Returns screen space coordinates of a 3D point.
Definition: camera.hpp:157
Eigen::Vector4f viewport
Viewport dimensions [minX, minY, width, height].
Definition: camera.hpp:48
Eigen::Vector3f getCenter(void) const
Returns the center of the camera in world space.
Definition: camera.hpp:115
float focal_length
Camera's Focal Length.
Definition: camera.hpp:75
Eigen::Affine3f default_view
Definition: camera.hpp:51
float getFovy(void) const
Returns current field of view angle in y axis.
Definition: camera.hpp:344
Eigen::Vector3f getTranslationMatrix(void) const
Returns the translation part of the view matrix as a vector.
Definition: camera.hpp:224
void setViewport(const Eigen::Vector2f &vp)
Sets the viewport coordinates considering that the minimum coordinates are zero.
Definition: camera.hpp:291
Camera(void)
Default constructor.
Definition: camera.hpp:354
Eigen::Affine3f * viewMatrix(void)
Returns a pointer to the view matrix as an Affine 3x3 matrix.
Definition: camera.hpp:186
float frustum_top
Frustum top dimension.
Definition: camera.hpp:66
float getPerspectiveScale(void) const
Returns the perspective scale.
Definition: camera.hpp:235
float frustum_left
Frustum left dimension.
Definition: camera.hpp:60
void reset(void)
Resets trackball to initial position and orientation.
Definition: camera.hpp:104
static Eigen::Matrix4f createOrthographicMatrix(float left, float right, float bottom, float top, float near_plane, float far_plane)
Returns an orthographic projection matrix with the given parameters.
Definition: camera.hpp:438
Defines an abstract camera with a projection and view matrices.
Definition: camera.hpp:37
void resetViewMatrix(void)
Reset view matrix.
Definition: camera.hpp:88
Eigen::Vector4f getViewport(void) const
Returns the viewport coordinates.
Definition: camera.hpp:246
Eigen::Vector2i getViewportSize(void) const
Returns the dimensions of the viewport.
Definition: camera.hpp:256
Eigen::Matrix4f projection_matrix
Projection, or intrinsic, matrix.
Definition: camera.hpp:42
Eigen::Matrix4f setOrthographicMatrix(float left, float right, float bottom, float top, float near_plane, float far_plane)
Sets the projection matrix as a orthographic matrix.
Definition: camera.hpp:465
Eigen::Affine3f view_matrix
View, or extrinsic, matrix.
Definition: camera.hpp:45
Eigen::Matrix4f * projectionMatrix(void)
Returns a pointer to the projection matrix as an 4x4 matrix.
Definition: camera.hpp:204
float getViewportAspectRatio(void) const
Returns the viewport aspect ratio.
Definition: camera.hpp:265