![]() |
TAPs 0.7.7.3
|
00001 /****************************************************************************** 00002 TAPsOpenGLTrackball.hpp 00003 00004 An object using OpenGL for Trackball Rotation. 00005 00006 SUKITTI PUNAK (10/06/2004) 00007 UPDATE (08/10/2010) 00008 -------------------------------------------------------------------------------- 00009 Example of Usage: 00010 ----------------- 00011 For providing a trackball rotation on OpenGL 00012 First create a trackball 00013 OpenGLTrackball *world_trackball_ptr = new OpenGLTrackball(); 00014 00015 Put this in a mouse function for starting of motion 00016 world_trackball_ptr->StartMotion(x, y, w_pix, h_pix); 00017 And put this in a mouse motion function for moving object by 00018 calculating m in trackball 00019 world_trackball_ptr->MoveObject(x, y, w_pix, h_pix); 00020 00021 Have it multiply in a display function for rotation 00022 glMultMatrixd(world_trackball_ptr->m); 00023 and make sure that it will be updated somewhere by 00024 glutPostRedisplay(); 00025 00026 Reset the trackball rotation with 00027 world_trackball_ptr->Reset(); 00028 00029 Where w_pix and h_pix should be integer global variables 00030 for width and height sizes in pixel units respectively. 00031 ******************************************************************************/ 00032 #include <iostream> 00033 #include <cmath> 00034 #include "../Core/TAPsLib.hpp" 00035 00036 #ifndef TAPs_OPENGL_TRACKBALL_HPP 00037 #define TAPs_OPENGL_TRACKBALL_HPP 00038 00039 BEGIN_NAMESPACE_TAPs 00040 //============================================================================= 00041 class OpenGLTrackball { 00042 public: 00043 OpenGLTrackball(); 00044 ~OpenGLTrackball(); 00045 00046 // The mouse position is x and y. 00047 // The size of viewport is w_pix (width) and h_pix (height). 00048 void MoveObject(int x, int y, int w_pix, int h_pix); 00049 void StartMotion(int x, int y, int w_pix, int h_pix); 00050 // purpose: reset the trackball back to the original position. 00051 // (by set the trackball matrix to an identity matrix.) 00052 void Reset(); 00053 // These should be in private section, but it is easier to keep them here. 00054 double Om[16]; // old rotation matrix 00055 double m[16]; // new rotation matrix 00056 const double * const RetRotationMatrixDouble4by4 () 00057 { return m; } 00058 //--------------------------------------------------------------- 00059 private: 00060 double M[16]; // temp matrix 00061 int curx, cury; // current position (x and y) 00062 int startX, startY; // start position (x and y) 00063 double lastPos[3]; // last position 00064 double rotationAxis[3]; // axis 00065 double rotationAngle; // rotation angle 00066 00067 // HELPER FUNCTION 00068 // The mouse position is x and y. 00069 // The size of viewport is w_pix (width) and h_pix (height). 00070 // v[3] is a position. 00071 void Trackball_ptov(int x, int y, int width, int height, double v[3]); 00072 }; 00073 //============================================================================= 00074 END_NAMESPACE_TAPs 00075 //----------------------------------------------------------------------------- 00076 // Include definition if TAPs_USE_EXPORT is not defined 00077 #if !defined( TAPs_USE_EXPORT ) 00078 #include "TAPsOpenGLTrackball.cpp" 00079 #endif 00080 //----------------------------------------------------------------------------- 00081 #endif 00082 //345678901234567890123456789012345678901234567890123456789012345678901234567890 00083 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8