Blender  V2.59
MyGlutMouseHandler.h
Go to the documentation of this file.
00001 
00029 #ifndef NAN_INCLUDED_MyGlutMouseHandler_h
00030 #define NAN_INCLUDED_MyGlutMouseHandler_h
00031 
00032 #include "../common/GlutMouseManager.h"
00033 #include <GL/glut.h>
00034 #include "IK_solver.h"
00035 
00036 class MyGlutMouseHandler : public GlutMouseHandler
00037 {
00038  
00039 public :
00040  
00041         static 
00042                 MyGlutMouseHandler *
00043         New(
00044         ) {
00045                 MEM_SmartPtr<MyGlutMouseHandler> output = new MyGlutMouseHandler();
00046                 if (output == NULL
00047                 ) {
00048                         return NULL;
00049                 }
00050                 return output.Release();
00051                 
00052         }
00053 
00054                 void
00055         SetChain(
00056                 IK_Chain_ExternPtr *chains, int num_chains
00057         ){
00058                 m_chains = chains;
00059                 m_num_chains = num_chains;
00060         }
00061 
00062                 void
00063         Mouse(
00064                 int button,
00065                 int state,
00066                 int x,
00067                 int y
00068         ){
00069                 if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
00070                         m_moving = true;
00071                         m_begin_x = x;
00072                         m_begin_y = y;  
00073                 }
00074                 if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
00075                         m_moving = false;
00076                 }
00077 
00078                 if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
00079                         m_tracking = true;
00080                 }
00081                 if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP) {
00082                         m_tracking = false;
00083                 }
00084 
00085                 if (button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) {
00086                         m_cg_on = true;
00087                 }
00088                 if (button == GLUT_MIDDLE_BUTTON && state == GLUT_UP) {
00089                         m_cg_on = false;
00090                 }
00091 
00092         }
00093 
00094 
00095                 void
00096         Motion(
00097                 int x,
00098                 int y
00099         ){
00100                 if (m_moving) {
00101                         m_angle_x = m_angle_x + (x - m_begin_x);
00102                         m_begin_x = x;
00103 
00104                         m_angle_y = m_angle_y + (y - m_begin_y);
00105                         m_begin_y = y;
00106 
00107                         glutPostRedisplay();
00108                 }
00109                 if (m_tracking) {
00110 
00111                         int w_h = glutGet((GLenum)GLUT_WINDOW_HEIGHT);
00112 
00113                         y = w_h - y;
00114 
00115                         double mvmatrix[16];
00116                         double projmatrix[16];
00117                         GLint viewport[4];
00118 
00119                         double px, py, pz,sz;
00120 
00121                         /* Get the matrices needed for gluUnProject */
00122                         glGetIntegerv(GL_VIEWPORT, viewport);
00123                         glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
00124                         glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
00125 
00126                         // work out the position of the end effector in screen space
00127 
00128                         GLdouble ex,ey,ez;
00129                         ex = m_pos.x();
00130                         ey = m_pos.y();
00131                         ez = m_pos.z();
00132 
00133                         gluProject(ex, ey, ez, mvmatrix, projmatrix, viewport, &px, &py, &sz);
00134                         gluUnProject((GLdouble) x, (GLdouble) y, sz, mvmatrix, projmatrix, viewport, &px, &py, &pz);
00135 
00136                         m_pos = MT_Vector3(px,py,pz);
00137 
00138                 }
00139                 if (m_tracking || m_cg_on) {
00140                         float temp[3];
00141                         m_pos.getValue(temp);
00142 
00143                         IK_SolveChain(m_chains[0],temp,0.01,200,0.1,m_chains[1]->segments);
00144                         IK_LoadChain(m_chains[0],m_chains[0]->segments,m_chains[0]->num_segments);
00145         
00146                         glutPostRedisplay();
00147                 }                       
00148 
00149 
00150         }
00151 
00152         const 
00153                 float
00154         AngleX(
00155         ) const {
00156                 return m_angle_x;
00157         }
00158 
00159         const 
00160                 float
00161         AngleY(
00162         ) const {
00163                 return m_angle_y;
00164         }
00165 
00166         const
00167                 MT_Vector3      
00168         Position(
00169         ) const {
00170                 return m_pos;
00171         }
00172 
00173 
00174 private :
00175 
00176         MyGlutMouseHandler (
00177         ) :  
00178                 m_angle_x(0),
00179                 m_angle_y(0),
00180                 m_begin_x(0),
00181                 m_begin_y(0),
00182                 m_moving (false),
00183                 m_tracking (false),
00184                 m_pos(0,0,0),
00185                 m_cg_on (false),
00186                 m_chains(NULL),
00187                 m_num_chains(0)
00188         {
00189         };
00190                 
00191         float m_angle_x;
00192         float m_angle_y;
00193         float m_begin_x;
00194         float m_begin_y;
00195 
00196         bool m_moving;
00197         bool m_tracking;
00198         bool m_cg_on;
00199         MT_Vector3 m_pos;
00200         
00201         IK_Chain_ExternPtr *m_chains;
00202         int m_num_chains;
00203 
00204 };
00205 
00206 #endif
00207