Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
Tucano::Trackball Class Reference

Trackball class for manipulating a camera. More...

#include <trackball.hpp>

Inheritance diagram for Tucano::Trackball:
Tucano::Camera Tucano::DirectionalTrackball Tucano::Manipulator Tucano::RotationManipulator Tucano::TranslationManipulator

Public Member Functions

 Trackball (string shader_dir="")
 Default constructor. More...
 
void reset (void)
 Resets trackball to initial position and orientation. More...
 
bool isRotating (void)
 Returns wether the trackball is being rotated or not. More...
 
bool isTranslating (void)
 Returns wether the trackball is being translated or not. More...
 
Eigen::Vector3f getDefaultTranslation (void)
 Returns the default translation for placing the camera outside the trackball sphere. More...
 
void setDefaultTranslation (Eigen::Vector3f t)
 Sets the default translation vector. More...
 
Eigen::Quaternion< float > getDefaultRotation (void)
 Returns the default rotation quaternion. More...
 
virtual Eigen::Quaternion< float > getRotation (void)
 Returns the rotation (without the default part) as a quaternion. More...
 
void setDefaultRotation (Eigen::Matrix3f rot)
 Sets the default rotation quaternion. More...
 
virtual void setRadius (float r)
 Sets the radius. More...
 
float getZoom (void)
 Retuns the zoom factor (the radius of the trackball). More...
 
void setTrackballProjectionMatrix (const Eigen::Matrix4f &mat)
 Sets the projection matrix used for the trackball rendering. Note that this is usually different than the projection matrix for the scene. More...
 
virtual void endRotation (void)
 Indicates that a rotation has ended. Disables the rotating flag, indicating that the mouse callback functions will stop reading the mouse coordinates in order to stop the trackball's rotation. More...
 
void endTranslation (void)
 Indicates that a translation has ended. Disable the translating flag, indicating that the mouse callback functions will stop reading the mouse coordinates in order to stop the trackball's translation. More...
 
void setRenderFlag (bool flag)
 Returns wether the trackball representation should be drawn or not. More...
 
void loadShader (void)
 Load Trackball Shader file. More...
 
void initOpenGLMatrices (void)
 Initializes the view and projection matrices. They are all initialized as Identity matrices, but view is then translated by the default_translation vector. More...
 
Eigen::Matrix4f setTrackballPerspectiveMatrix (float fy, float aspect_ratio, float near_plane, float far_plane)
 Sets the trackball projection matrix as perspective. More...
 
Eigen::Matrix4f setTrackballOrthographicMatrix (float left, float right, float bottom, float top, float near_plane, float far_plane)
 Sets the trackball projection matrix as orthographic. More...
 
virtual Eigen::Quaternion< float > computeRotationAngle (float sensibility=1.0)
 Computes the trackball's rotation, using stored initial and final position vectors. More...
 
virtual Eigen::Vector3f computeTranslationVector (void)
 Compute the trackball's translation, using stored initial and final position vectors. More...
 
Eigen::Vector2f normalizePosition (const Eigen::Vector2f &pos)
 Nomalizes a screen position to range [-1,1]. More...
 
virtual Eigen::Quaternion< float > rotateCamera (const Eigen::Vector2f &pos)
 Computes and applies the rotation transformations to the trackball given new position. More...
 
virtual Eigen::Vector3f translateCamera (const Eigen::Vector2f &pos)
 Computes and applies the translation transformations to the trackball given new position. More...
 
void increaseZoom (float scale)
 Increases the zoom on the scene by appling a scale to the View Matrix. The current scale used in View Matrix is multiplied by the given scale factor. More...
 
void decreaseZoom (float scale)
 Decreases the zoom on the scene by appling a scale to the View Matrix. The current scale used in View Matrix is divided by the given scale factor. More...
 
void applyScaleToViewMatrix (float scale)
 Applies a scale factor to the viewMatrix. The current scale used in view matrix will be substituted by the given scale factor. More...
 
void translateViewMatrix (const Eigen::Vector3f &translation)
 Translates the view matrix by a given vector trans. This directly modifies the translation vector of the trackball. Note that the view matrix is always recomputed using the translation, quaternion and zoom. More...
 
void rotateViewMatrix (const Eigen::Affine3f &rot)
 Rotates the view matrix by a given rotation matrix. This directly modifies the quaternion of the trackball. Note that the view matrix is always recomputed using the translation, quaternion and zoom. More...
 
virtual void updateViewMatrix (void)
 Applies all trackball's transformations to the view matrix. More...
 
void createTrackballRepresentation (void)
 Creates the circle representating rotation around Z axis. The other circles will be created by simply rotating this one around the desired axis. More...
 
virtual void render (Eigen::Affine3f ext_view_matrix=Eigen::Affine3f::Identity(), Eigen::Matrix4f ext_proj_matrix=Eigen::Matrix4f::Identity())
 Renders the trackball representation. More...
 
- Public Member Functions inherited from Tucano::Camera
void resetViewMatrix (void)
 Reset view matrix. More...
 
void resetProjectionMatrix (void)
 Reset projection matrix. More...
 
void reset (void)
 Resets trackball to initial position and orientation. More...
 
Eigen::Vector3f getCenter (void) const
 Returns the center of the camera in world space. More...
 
void getViewMatrix (GLdouble *matrix)
 Return the modelview matrix as a GLdouble array. More...
 
void getProjectionMatrix (GLdouble *matrix)
 Return the projection matrix as a GLdouble array. More...
 
Eigen::Vector3f projectPoint (const Eigen::Vector4f &pt, Eigen::Vector4f &viewport)
 Returns screen space coordinates of a 3D point. More...
 
Eigen::Affine3f getViewMatrix (void) const
 Returns the view matrix as an Affine 3x3 matrix. More...
 
Eigen::Affine3f * viewMatrix (void)
 Returns a pointer to the view matrix as an Affine 3x3 matrix. More...
 
Eigen::Matrix4f getProjectionMatrix (void) const
 Returns the view matrix as an 4x4 matrix. More...
 
Eigen::Matrix4f * projectionMatrix (void)
 Returns a pointer to the projection matrix as an 4x4 matrix. More...
 
Eigen::Matrix3f getRotationMatrix (void) const
 Returns a 3x3 matrix containing only the rotation of the view matrix. More...
 
Eigen::Vector3f getTranslationMatrix (void) const
 Returns the translation part of the view matrix as a vector. More...
 
float getPerspectiveScale (void) const
 Returns the perspective scale. More...
 
Eigen::Vector4f getViewport (void) const
 Returns the viewport coordinates. More...
 
Eigen::Vector2i getViewportSize (void) const
 Returns the dimensions of the viewport. More...
 
float getViewportAspectRatio (void) const
 Returns the viewport aspect ratio. More...
 
void setViewport (const Eigen::Vector4f &vp)
 Sets the viewport coordinates. More...
 
void setViewport (const Eigen::Vector2i &vp)
 
void setViewport (const Eigen::Vector2f &vp)
 Sets the viewport coordinates considering that the minimum coordinates are zero. More...
 
void setProjectionMatrix (const Eigen::Matrix4f &mat)
 Sets the projection matrix from a given 4x4 matrix. More...
 
void setViewMatrix (const Eigen::Affine3f &mat)
 Sets the view matrix from a given an affine 3x3 matrix. More...
 
float getNearPlane (void) const
 Returns near plane value. More...
 
float getFarPlane (void) const
 Returns far plane value. More...
 
 ~Camera ()
 Default destructor. More...
 
float getFovy (void) const
 Returns current field of view angle in y axis. More...
 
 Camera (void)
 Default constructor. More...
 
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. More...
 
void changeFovy (float new_fovy)
 Changes the fovy and computes new perspective projection matrix. More...
 
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. More...
 
void incrementFov (float inc)
 Increases the fov of the perspective matrix by a given increment. More...
 
void translate (const Eigen::Vector3f &translation)
 Translates the view matrix by a given vector. More...
 
void rotate (const Eigen::Quaternion< float > &rotation)
 Rotate the view matrix by a given quaternion. More...
 
void scale (const Eigen::Vector3f &scale_factors)
 Scales the view matrix by given factors. More...
 
void scale (float scale_factor)
 Scales the view matrix by given factor in all axis. More...
 

Protected Attributes

float zoom = 1.0
 The current scale being applied to the View Matrix. More...
 
bool rotating = false
 Flag that indicates wether the trackball is being rotated. More...
 
bool translating = false
 Flag that indicates wether the trackball is being translated. More...
 
bool drawTrackball = true
 Flag that indicates wether the trackball's representation should be drawn. More...
 
Eigen::Matrix4f trackballProjectionMatrix
 Projection matrix used for the trackball's drawing. By default it is defined as an orthogonal projection matrix. More...
 
Eigen::Vector3f initialPosition
 Initial position vector used to calculate trackball's rotation. More...
 
Eigen::Vector3f finalPosition
 Final position vector used to calculate trackball's rotation. More...
 
Eigen::Vector2f initialTranslationPosition
 Initial position vector used to calculate trackball's translation. More...
 
Eigen::Vector2f finalTranslationPosition
 Final position vector used to calculate trackball's translation. More...
 
Eigen::Quaternion< float > quaternion
 Trackball's quaternion. More...
 
Eigen::Quaternion< float > default_quaternion
 Trackball's default rotation. More...
 
Eigen::Vector3f translationVector
 Trackball's translation vector. More...
 
Eigen::Vector3f default_translation = Eigen::Vector3f (0.0, 0.0, -4.0)
 Default translation to move camera away from center. More...
 
Tucano::Shader trackball_shader
 Trackball Shader, used just for rendering the trackball's representation. More...
 
Tucano::Mesh mesh
 Trackball visual representation. More...
 
float radius = 0.8
 The trackball radius. It's defined as 0.8 times the smallest viewport dimension. More...
 
bool use_default_shaders
 Flag to use default shaders set as const strings in class, or pass a directory with custom shaders. More...
 
- Protected Attributes inherited from Tucano::Camera
Eigen::Matrix4f projection_matrix
 Projection, or intrinsic, matrix. More...
 
Eigen::Affine3f view_matrix
 View, or extrinsic, matrix. More...
 
Eigen::Vector4f viewport
 Viewport dimensions [minX, minY, width, height]. More...
 
Eigen::Affine3f default_view
 
float near_plane
 Near plane used for projection matrix. More...
 
float far_plane
 Far plane used for projection matrix. More...
 
float frustum_left
 Frustum left dimension. More...
 
float frustum_right
 Frustum right dimension. More...
 
float frustum_top
 Frustum top dimension. More...
 
float frustum_bottom
 Frustum bottom dimension. More...
 
float aspect_ratio
 Aspect ratio for projection matrix. More...
 
float focal_length
 Camera's Focal Length. More...
 
float fovy
 Field of view angle in y axis. More...
 
bool use_perspective
 Flag to indicate if using a perspective or othograpic projection. More...
 

Private Member Functions

virtual Eigen::Vector3f computeSpherePosition (const Eigen::Vector2f &pos)
 Computes 3d coordinates on sphere from 2d position. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Tucano::Camera
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. More...
 
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. More...
 

Detailed Description

Trackball class for manipulating a camera.

This class simulates a standard trackball manipulation. The visual representation is a sphere with radius equal to 0.8 centered in the middle of the window (represented by the equation: x^2 + y^2 + z^2 = r^2), nested in an hyperbolic sheet opening (represented by the equation: z = (r^2/2)/sqrt(x^2+y^2). It is usually used for camera and light manipulation. It's important to notice that the x and y mouse coordinates passed to the trackball must be ranging from (-1,-1), being the lower left corner of screen, to (1,1) being the higher right corner. This means the coordinate system is centered in screen and normalized. Also, the view matrix is by default translated by the vector [0,0,-2], in order to remove the camera from the center of the world.

Constructor & Destructor Documentation

Tucano::Trackball::Trackball ( string  shader_dir = "")
inline

Default constructor.

Parameters
shader_dirGiven directory containing trackball shaders, optional, otherwise uses default shaders.

Member Function Documentation

void Tucano::Trackball::applyScaleToViewMatrix ( float  scale)
inline

Applies a scale factor to the viewMatrix. The current scale used in view matrix will be substituted by the given scale factor.

Parameters
scaleThe new scale factor to be applied to View Matrix.
virtual Eigen::Quaternion<float> Tucano::Trackball::computeRotationAngle ( float  sensibility = 1.0)
inlinevirtual

Computes the trackball's rotation, using stored initial and final position vectors.

Parameters
sensibilityScales the rotation, works as a sensibility factor
Returns
Returns the increment rotation

Reimplemented in Tucano::RotationManipulator, and Tucano::DirectionalTrackball.

virtual Eigen::Vector3f Tucano::Trackball::computeSpherePosition ( const Eigen::Vector2f &  pos)
inlineprivatevirtual

Computes 3d coordinates on sphere from 2d position.

Parameters
posGiven 2d screen position.
virtual Eigen::Vector3f Tucano::Trackball::computeTranslationVector ( void  )
inlinevirtual

Compute the trackball's translation, using stored initial and final position vectors.

Reimplemented in Tucano::RotationManipulator, and Tucano::TranslationManipulator.

void Tucano::Trackball::createTrackballRepresentation ( void  )
inline

Creates the circle representating rotation around Z axis. The other circles will be created by simply rotating this one around the desired axis.

void Tucano::Trackball::decreaseZoom ( float  scale)
inline

Decreases the zoom on the scene by appling a scale to the View Matrix. The current scale used in View Matrix is divided by the given scale factor.

Parameters
scaleThe given scale factor, to which the current scale factor will be divided by.
virtual void Tucano::Trackball::endRotation ( void  )
inlinevirtual

Indicates that a rotation has ended. Disables the rotating flag, indicating that the mouse callback functions will stop reading the mouse coordinates in order to stop the trackball's rotation.

Reimplemented in Tucano::RotationManipulator.

void Tucano::Trackball::endTranslation ( void  )
inline

Indicates that a translation has ended. Disable the translating flag, indicating that the mouse callback functions will stop reading the mouse coordinates in order to stop the trackball's translation.

Eigen::Quaternion<float> Tucano::Trackball::getDefaultRotation ( void  )
inline

Returns the default rotation quaternion.

Returns
Default rotation quaternion
Eigen::Vector3f Tucano::Trackball::getDefaultTranslation ( void  )
inline

Returns the default translation for placing the camera outside the trackball sphere.

Returns
The default translation vector for the view matrix
virtual Eigen::Quaternion<float> Tucano::Trackball::getRotation ( void  )
inlinevirtual

Returns the rotation (without the default part) as a quaternion.

Returns
Rotation as quaternion
float Tucano::Trackball::getZoom ( void  )
inline

Retuns the zoom factor (the radius of the trackball).

Returns
Zoom factor.
void Tucano::Trackball::increaseZoom ( float  scale)
inline

Increases the zoom on the scene by appling a scale to the View Matrix. The current scale used in View Matrix is multiplied by the given scale factor.

Parameters
scaleThe given scale to be multiplied by current scale factor.
void Tucano::Trackball::initOpenGLMatrices ( void  )
inline

Initializes the view and projection matrices. They are all initialized as Identity matrices, but view is then translated by the default_translation vector.

bool Tucano::Trackball::isRotating ( void  )
inline

Returns wether the trackball is being rotated or not.

Returns
True if rotating, false otherwise.
bool Tucano::Trackball::isTranslating ( void  )
inline

Returns wether the trackball is being translated or not.

Returns
True if translating, false otherwise.
void Tucano::Trackball::loadShader ( void  )
inline

Load Trackball Shader file.

Eigen::Vector2f Tucano::Trackball::normalizePosition ( const Eigen::Vector2f &  pos)
inline

Nomalizes a screen position to range [-1,1].

Parameters
posScreen position
Returns
Returns position in normalized coordinates.
virtual void Tucano::Trackball::render ( Eigen::Affine3f  ext_view_matrix = Eigen::Affine3f::Identity(),
Eigen::Matrix4f  ext_proj_matrix = Eigen::Matrix4f::Identity() 
)
inlinevirtual

Renders the trackball representation.

Todo:
setTrackballOrthographicMatrix should be set during viewport resize The external matrices are useful when the trackball should follow a specif object for example, so the main camera matrices should be passed here
Parameters
ext_view_matrixExternal view matrix from other global camera
ext_proj_matrixExternal projection matrix from other global camera
void Tucano::Trackball::reset ( void  )
inline

Resets trackball to initial position and orientation.

virtual Eigen::Quaternion<float> Tucano::Trackball::rotateCamera ( const Eigen::Vector2f &  pos)
inlinevirtual

Computes and applies the rotation transformations to the trackball given new position.

Parameters
posNew mouse position in normalized trackball system

Reimplemented in Tucano::RotationManipulator.

void Tucano::Trackball::rotateViewMatrix ( const Eigen::Affine3f &  rot)
inline

Rotates the view matrix by a given rotation matrix. This directly modifies the quaternion of the trackball. Note that the view matrix is always recomputed using the translation, quaternion and zoom.

Parameters
rotGiven rotation matrix.
void Tucano::Trackball::setDefaultRotation ( Eigen::Matrix3f  rot)
inline

Sets the default rotation quaternion.

Parameters
rotGiven default rotation as matrix3f
void Tucano::Trackball::setDefaultTranslation ( Eigen::Vector3f  t)
inline

Sets the default translation vector.

Parameters
tGiven new default translation vector.
virtual void Tucano::Trackball::setRadius ( float  r)
inlinevirtual

Sets the radius.

Reimplemented in Tucano::RotationManipulator.

void Tucano::Trackball::setRenderFlag ( bool  flag)
inline

Returns wether the trackball representation should be drawn or not.

Returns
True if trackball representation is being rendered, false otherwise.
Eigen::Matrix4f Tucano::Trackball::setTrackballOrthographicMatrix ( float  left,
float  right,
float  bottom,
float  top,
float  near_plane,
float  far_plane 
)
inline

Sets the trackball projection matrix as orthographic.

Parameters
leftLeft plane for orthographic view.
rightRight plane for orthographic view.
bottomBottom plane for orthographic view.
topTop lane for orthographic view.
near_planeNear plane for orthographic view.
far_planeFar plane for orthographic view.
Returns
Return the created orthographic matrix.
Eigen::Matrix4f Tucano::Trackball::setTrackballPerspectiveMatrix ( float  fy,
float  aspect_ratio,
float  near_plane,
float  far_plane 
)
inline

Sets the trackball projection matrix as perspective.

Parameters
fyVertical field of view angle.
aspect_ratioRatio of width to the height of the viewport.
near_planeNear plane.
far_planeFar plane.
Returns
Return the created perspective matrix.
void Tucano::Trackball::setTrackballProjectionMatrix ( const Eigen::Matrix4f &  mat)
inline

Sets the projection matrix used for the trackball rendering. Note that this is usually different than the projection matrix for the scene.

Parameters
matGiven trackball projection matrix.
virtual Eigen::Vector3f Tucano::Trackball::translateCamera ( const Eigen::Vector2f &  pos)
inlinevirtual

Computes and applies the translation transformations to the trackball given new position.

Parameters
posNew mouse position in normalized trackball system
Returns
returns the translation increment

Reimplemented in Tucano::TranslationManipulator.

void Tucano::Trackball::translateViewMatrix ( const Eigen::Vector3f &  translation)
inline

Translates the view matrix by a given vector trans. This directly modifies the translation vector of the trackball. Note that the view matrix is always recomputed using the translation, quaternion and zoom.

Parameters
translationGiven translation vector.
virtual void Tucano::Trackball::updateViewMatrix ( void  )
inlinevirtual

Applies all trackball's transformations to the view matrix.

Reimplemented in Tucano::RotationManipulator, Tucano::TranslationManipulator, and Tucano::DirectionalTrackball.

Member Data Documentation

Eigen::Quaternion<float> Tucano::Trackball::default_quaternion
protected

Trackball's default rotation.

Eigen::Vector3f Tucano::Trackball::default_translation = Eigen::Vector3f (0.0, 0.0, -4.0)
protected

Default translation to move camera away from center.

bool Tucano::Trackball::drawTrackball = true
protected

Flag that indicates wether the trackball's representation should be drawn.

Eigen::Vector3f Tucano::Trackball::finalPosition
protected

Final position vector used to calculate trackball's rotation.

Eigen::Vector2f Tucano::Trackball::finalTranslationPosition
protected

Final position vector used to calculate trackball's translation.

Eigen::Vector3f Tucano::Trackball::initialPosition
protected

Initial position vector used to calculate trackball's rotation.

Eigen::Vector2f Tucano::Trackball::initialTranslationPosition
protected

Initial position vector used to calculate trackball's translation.

Tucano::Mesh Tucano::Trackball::mesh
protected

Trackball visual representation.

Eigen::Quaternion<float> Tucano::Trackball::quaternion
protected

Trackball's quaternion.

float Tucano::Trackball::radius = 0.8
protected

The trackball radius. It's defined as 0.8 times the smallest viewport dimension.

bool Tucano::Trackball::rotating = false
protected

Flag that indicates wether the trackball is being rotated.

Tucano::Shader Tucano::Trackball::trackball_shader
protected

Trackball Shader, used just for rendering the trackball's representation.

Eigen::Matrix4f Tucano::Trackball::trackballProjectionMatrix
protected

Projection matrix used for the trackball's drawing. By default it is defined as an orthogonal projection matrix.

bool Tucano::Trackball::translating = false
protected

Flag that indicates wether the trackball is being translated.

Eigen::Vector3f Tucano::Trackball::translationVector
protected

Trackball's translation vector.

bool Tucano::Trackball::use_default_shaders
protected

Flag to use default shaders set as const strings in class, or pass a directory with custom shaders.

float Tucano::Trackball::zoom = 1.0
protected

The current scale being applied to the View Matrix.


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