Go to the documentation of this file.00001 #ifndef OPENTISSUE_UTILITY_GL_TRACKBALL_H
00002 #define OPENTISSUE_UTILITY_GL_TRACKBALL_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/utility/gl/gl.h>
00013 #include <OpenTissue/utility/trackball/trackball.h>
00014
00015 namespace OpenTissue
00016 {
00017
00018 namespace gl
00019 {
00020
00024 template<typename math_types>
00025 class TrackBall
00026 {
00027 public:
00028
00029 typedef typename math_types::real_type real_type;
00030
00031 typedef OpenTissue::utility::trackball::Trackball<real_type> trackball_type;
00032
00033 typedef typename math_types::vector3_type vector3_type;
00034 typedef typename math_types::matrix3x3_type matrix3x3_type;
00035 typedef typename math_types::quaternion_type quaternion_type;
00036 typedef typename trackball_type::gl_transform_type gl_transform_type;
00037
00038 protected:
00039
00040 real_type m_radius;
00041 trackball_type m_trackball;
00042 gl_transform_type m_trackball_matrix;
00043 public:
00044
00045 matrix3x3_type const & get_current_rotation()
00046 {
00047 return m_trackball.get_current_rotation();
00048 }
00049
00050 gl_transform_type const & get_gl_current_transform()
00051 {
00052 return m_trackball.get_gl_current_transform();
00053 }
00054
00055 matrix3x3_type const & get_incremental_rotation() const
00056 {
00057 return m_trackball.get_incremental_rotation();
00058 }
00059
00060 protected:
00061
00062 void normalize( real_type const & sx, real_type const & sy, real_type & nx, real_type & ny )
00063 {
00064 GLfloat viewport[4];
00065 glGetFloatv(GL_VIEWPORT,viewport);
00066 nx = 2.0 * sx / viewport[2] - 1.0;
00067 if ( nx < -1.0 )
00068 nx = -1.0;
00069 if ( nx > 1.0 )
00070 nx = 1.0;
00071
00072 ny = -( 2.0 * sy / viewport[3] - 1.0 );
00073 if ( ny < -1.0 )
00074 ny = -1.0;
00075 if ( ny > 1.0 )
00076 ny = 1.0;
00077 };
00078
00079 public:
00080
00081 TrackBall()
00082 : m_radius(1.0)
00083 , m_trackball(m_radius)
00084 {
00085 };
00086
00087 void mouse_down(real_type const & sx,real_type const & sy)
00088 {
00089 real_type nx,ny;
00090 normalize(sx,sy,nx,ny);
00091 m_trackball.begin_drag( nx, ny );
00092 };
00093
00094 void mouse_up(real_type const & sx,real_type const & sy)
00095 {
00096 real_type nx,ny;
00097 normalize(sx,sy,nx,ny);
00098 m_trackball.end_drag( nx, ny );
00099 };
00100
00101 void mouse_move(real_type const & sx,real_type const & sy)
00102 {
00103 real_type nx,ny;
00104 normalize(sx,sy,nx,ny);
00105 m_trackball.drag( nx, ny );
00106 };
00107
00108 };
00109
00113 template<typename types>
00114 inline void MultTrackballMatrix( TrackBall<types> const & trackball )
00115 {
00116 glMultMatrixd( const_cast<TrackBall<types>*>(&trackball)->get_absolute_rotation() );
00117 }
00118
00122 template<typename types>
00123 inline void LoadTrackballMatrix( TrackBall<types> const & trackball )
00124 {
00125 glLoadMatrixd( const_cast<TrackBall<types>*>(&trackball)->get_absolute_rotation() );
00126 }
00127
00128 }
00129
00130 }
00131
00132
00133 #endif