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