GEL
2
GEL is a library for Geometry and Linear Algebra
|
00001 #ifndef __GLGRAPHICS_TrackBall_H__ 00002 #define __GLGRAPHICS_TrackBall_H__ 00003 00004 #include "CGLA/Vec2i.h" 00005 #include "CGLA/Vec2f.h" 00006 #include "CGLA/Vec3f.h" 00007 #include "CGLA/Vec3Hf.h" 00008 #include "CGLA/Quatf.h" 00009 00010 namespace GLGraphics 00011 { 00012 00013 enum TrackBallAction 00014 { 00015 NO_ACTION = 0, 00016 ROTATE_ACTION, 00017 PAN_ACTION, 00018 ZOOM_ACTION 00019 }; 00020 00026 class QuatTrackBall 00027 { 00028 CGLA::Vec3f centre; 00029 CGLA::Vec2i screen_centre; 00030 00031 unsigned width, height; 00032 CGLA::Quatf qrot; 00033 CGLA::Quatf qinc; 00034 CGLA::Vec2f trans; 00035 float scale; 00036 float ballsize; 00037 float eye_dist; 00038 CGLA::Vec2f last_pos; 00039 TrackBallAction current_action; 00040 00041 void rotate(const CGLA::Vec2f&); 00042 void pan(const CGLA::Vec2f&); 00043 void zoom(const CGLA::Vec2f&); 00044 00045 void calcRotation(const CGLA::Vec2f&); 00046 float projectToSphere(const CGLA::Vec2f&); 00047 CGLA::Vec2f scalePoint(const CGLA::Vec2i&) const; 00048 00049 void set_position(const CGLA::Vec2f&); 00050 00051 public: 00052 00057 QuatTrackBall(const CGLA::Vec3f&, float, unsigned, unsigned); 00058 00060 void set_screen_window(unsigned _width, unsigned _height) 00061 { 00062 width = _width; 00063 height = _height; 00064 screen_centre[0] = static_cast<int>(width/2.0f); 00065 screen_centre[1] = static_cast<int>(height/2.0f); 00066 } 00067 00069 void set_centre(const CGLA::Vec3f& _centre) 00070 { 00071 centre = _centre; 00072 } 00073 00074 void set_screen_centre(const CGLA::Vec2i& _screen_centre) 00075 { 00076 screen_centre[0] = _screen_centre[0]; 00077 screen_centre[1] = height - _screen_centre[1]; 00078 } 00079 00080 const CGLA::Quatf& get_rotation() const 00081 { 00082 return qrot; 00083 } 00084 00085 void set_rotation(const CGLA::Quatf& _qrot) 00086 { 00087 qrot = _qrot; 00088 } 00089 00090 void set_eye_dist(float _eye_dist) 00091 { 00092 eye_dist = _eye_dist; 00093 } 00094 00095 float get_eye_dist() const 00096 { 00097 return eye_dist; 00098 } 00099 00101 void set_gl_modelview() const; 00102 00105 void do_spin(); 00106 00107 bool is_spinning() const; 00108 00110 void stop_spin(); 00111 00113 void grab_ball(TrackBallAction,const CGLA::Vec2i&); 00114 00116 void roll_ball(const CGLA::Vec2i&); 00117 00119 void release_ball() 00120 { 00121 current_action = NO_ACTION; 00122 } 00123 00125 bool is_grabbed() const 00126 { 00127 if(current_action == NO_ACTION) 00128 return false; 00129 return true; 00130 } 00131 00132 void get_view_param(CGLA::Vec3f& eye, 00133 CGLA::Vec3f& _centre, CGLA::Vec3f& up) const; 00134 00135 TrackBallAction get_current_action() 00136 { 00137 return current_action; 00138 } 00139 00140 }; 00141 00142 } 00143 00144 #endif