Blender  V2.59
ChainDrawer.h
Go to the documentation of this file.
00001 
00029 #ifndef NAN_INCLUDED_ChainDrawer_h
00030 #define NAN_INCLUDED_ChainDrawer_h
00031 
00032 #include "../common/GlutDrawer.h"
00033 #include "MyGlutMouseHandler.h"
00034 #include "MyGlutKeyHandler.h"
00035 #include "MT_Transform.h"
00036 #       include "IK_Qsolver.h"
00037 #       include "../intern/IK_QChain.h"
00038 #       include "../intern/IK_QSolver_Class.h"
00039 #include <GL/glut.h>
00040 
00041 class ChainDrawer : public GlutDrawer
00042 {
00043 public :
00044         static
00045                 ChainDrawer *
00046         New(
00047         ) {
00048                 return new ChainDrawer();
00049         }
00050         
00051                 void
00052         SetMouseHandler(
00053                 MyGlutMouseHandler *mouse_handler
00054         ) {
00055                 m_mouse_handler = mouse_handler;
00056         }
00057 
00058                 void
00059         SetKeyHandler (
00060                 MyGlutKeyHandler *key_handler
00061         ) {
00062                 m_key_handler = key_handler;
00063         }
00064 
00065                 void
00066         SetChain(
00067                 IK_Chain_ExternPtr *chains,int chain_num
00068         ) {
00069                 m_chain_num = chain_num;
00070                 m_chains = chains;
00071         }
00072 
00073 
00074         // inherited from GlutDrawer
00075                 void
00076         Draw(
00077         ) {
00078           glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00079                   glPopMatrix();
00080                   glPushMatrix();
00081                   glRotatef(m_mouse_handler->AngleX(), 0.0, 1.0, 0.0);
00082                   glRotatef(m_mouse_handler->AngleY(), 1.0, 0.0, 0.0);
00083                         
00084           DrawScene();
00085           glutSwapBuffers();
00086 
00087         }
00088 
00089         ~ChainDrawer(
00090         ){
00091                 // nothing to do
00092         };              
00093         
00094 private :
00095 
00096                 void
00097         DrawScene(
00098         ){
00099 
00100                 // draw a little cross at the position of the key handler
00101                 // coordinates
00102 
00103                 MT_Vector3 line_x(4,0,0);
00104                 MT_Vector3 line_y(0.0,4,0);
00105                 MT_Vector3 line_z(0.0,0.0,4);
00106 
00107                 MT_Vector3 cross_origin = m_mouse_handler->Position();
00108                 MT_Vector3 temp;
00109                 
00110                 glDisable(GL_LIGHTING);
00111 
00112 
00113                 glBegin(GL_LINES);
00114 
00115                 glColor3f (1.0f,1.0f,1.0f);
00116 
00117                 temp = cross_origin - line_x;
00118                 glVertex3f(temp[0],temp[1],temp[2]);
00119                 temp = cross_origin + line_x;
00120                 glVertex3f(temp[0],temp[1],temp[2]);
00121 
00122                 temp = cross_origin - line_y;
00123                 glVertex3f(temp[0],temp[1],temp[2]);
00124                 temp = cross_origin + line_y;
00125                 glVertex3f(temp[0],temp[1],temp[2]);
00126 
00127                 temp = cross_origin - line_z;
00128                 glVertex3f(temp[0],temp[1],temp[2]);
00129                 temp = cross_origin + line_z;
00130                 glVertex3f(temp[0],temp[1],temp[2]);
00131 
00132                 glEnd();
00133                 glEnable(GL_LIGHTING);
00134 
00135 
00136                 IK_Chain_ExternPtr chain;
00137 
00138                 int chain_num;
00139                 for (chain_num = 0; chain_num < m_chain_num; chain_num++) {
00140                         chain = m_chains[chain_num];
00141 
00142 
00143                         IK_Segment_ExternPtr segs = chain->segments;
00144                         IK_Segment_ExternPtr seg_start = segs;
00145                         const IK_Segment_ExternPtr seg_end = segs + chain->num_segments;
00146                         float ogl_matrix[16];
00147 
00148                         glColor3f (0.0f,1.0f,0.0f);
00149 
00150                         MT_Vector3 previous_origin(0,0,0);
00151 
00152                         MT_Transform global_transform;
00153                         global_transform.setIdentity();
00154                         
00155                         for (; seg_start != seg_end; ++seg_start) {
00156                                 
00157                                 glPushMatrix();
00158 
00159                                 // fill ogl_matrix with zeros
00160 
00161                                 std::fill(ogl_matrix,ogl_matrix + 16,float(0));
00162 
00163                                 // we have to do a bit of work here to compute the chain's
00164                                 // bone values
00165                                         
00166                                 // first compute all the matrices we need
00167                                 
00168                                 MT_Transform translation;
00169                                 translation.setIdentity();
00170                                 translation.translate(MT_Vector3(0,seg_start->length,0));
00171 
00172                                 MT_Matrix3x3 seg_rot(
00173                                         seg_start->basis_change[0],seg_start->basis_change[1],seg_start->basis_change[2],
00174                                         seg_start->basis_change[3],seg_start->basis_change[4],seg_start->basis_change[5],
00175                                         seg_start->basis_change[6],seg_start->basis_change[7],seg_start->basis_change[8]
00176                                 );
00177 
00178                                 seg_rot.transpose();
00179 
00180                                 MT_Matrix3x3 seg_pre_rot(
00181                                         seg_start->basis[0],seg_start->basis[1],seg_start->basis[2],
00182                                         seg_start->basis[3],seg_start->basis[4],seg_start->basis[5],
00183                                         seg_start->basis[6],seg_start->basis[7],seg_start->basis[8]
00184                                 );
00185 
00186                 
00187                                 MT_Transform seg_t_pre_rot(
00188                                         MT_Point3(
00189                                                 seg_start->seg_start[0],
00190                                                 seg_start->seg_start[1],
00191                                                 seg_start->seg_start[2]
00192                                         ),
00193                                         seg_pre_rot
00194                                 );
00195                                 // start of the bone is just the current global transform
00196                                 // multiplied by the seg_start vector
00197 
00198                                 
00199 
00200                                 MT_Transform seg_t_rot(MT_Point3(0,0,0),seg_rot);
00201                                 MT_Transform seg_local = seg_t_pre_rot * seg_t_rot * translation;
00202 
00203                                 MT_Vector3 bone_start = global_transform *      
00204                                         MT_Point3(
00205                                                 seg_start->seg_start[0],
00206                                                 seg_start->seg_start[1],
00207                                                 seg_start->seg_start[2]
00208                                         );
00209 
00210 
00211                                 global_transform = global_transform * seg_local;
00212 
00213                                 global_transform.getValue(ogl_matrix);
00214                                 MT_Vector3 bone_end = global_transform.getOrigin();
00215 
00216                                 glMultMatrixf(ogl_matrix);
00217 //                              glutSolidSphere(0.5,5,5);
00218 
00219                                 glPopMatrix();
00220                 
00221                                 glDisable(GL_LIGHTING);
00222 
00223                                 glBegin(GL_LINES);
00224 
00225                                 // draw lines of the principle axis of the local transform
00226 
00227                                 MT_Vector3 x_axis(1,0,0);
00228                                 MT_Vector3 y_axis(0,1,0);
00229                                 MT_Vector3 z_axis(0,0,1);
00230 
00231                                 x_axis = global_transform.getBasis() * x_axis * 5;
00232                                 y_axis = global_transform.getBasis() * y_axis * 5;
00233                                 z_axis = global_transform.getBasis() * z_axis * 5;
00234 
00235 
00236                                 x_axis = x_axis + bone_start;
00237                                 y_axis = y_axis + bone_start;
00238                                 z_axis = z_axis + bone_start;
00239 
00240                                 glColor3f(1,0,0);
00241 
00242                                 glVertex3f(x_axis.x(),x_axis.y(),x_axis.z());
00243                                 glVertex3f(
00244                                                 bone_start.x(),
00245                                                 bone_start.y(),
00246                                                 bone_start.z()
00247                                 );
00248 
00249                                 glColor3f(0,1,0);
00250 
00251                                 glVertex3f(y_axis.x(),y_axis.y(),y_axis.z());
00252                                 glVertex3f(
00253                                                 bone_start.x(),
00254                                                 bone_start.y(),
00255                                                 bone_start.z()
00256                                 );
00257 
00258                                 glColor3f(0,1,1);
00259 
00260                                 glVertex3f(z_axis.x(),z_axis.y(),z_axis.z());
00261                                 glVertex3f(
00262                                                 bone_start.x(),
00263                                                 bone_start.y(),
00264                                                 bone_start.z()
00265                                 );
00266 
00267                                 glColor3f(0,0,1);
00268 
00269                                 glVertex3f(
00270                                                 bone_start.x(),
00271                                                 bone_start.y(),
00272                                                 bone_start.z()
00273                                 );
00274                                 glVertex3f(bone_end[0],bone_end[1],bone_end[2]);
00275 
00276                                 glEnd();
00277                                 glEnable(GL_LIGHTING);
00278                         }
00279 #if 0
00280                         // draw jacobian column vectors
00281 
00282                         // hack access to internals
00283 
00284                         IK_Solver_Class * internals = static_cast<IK_Solver_Class *>(chain->intern);
00285 
00286                         glDisable(GL_LIGHTING);
00287 
00288                         glBegin(GL_LINES);
00289 
00290                         const TNT::Matrix<MT_Scalar> & jac = internals->Chain().TransposedJacobian();
00291 
00292                         int i = 0;
00293                         for (i=0; i < jac.num_rows(); i++) {
00294                                 glColor3f(1,1,1);
00295 
00296                                 previous_origin = internals->Chain().Segments()[i/3].GlobalSegmentStart();
00297 
00298                                 glVertex3f(previous_origin[0],previous_origin[1],previous_origin[2]);
00299                                 glVertex3f(jac[i][0] + previous_origin[0],jac[i][1] + previous_origin[1],jac[i][2] + previous_origin[2]);
00300                         
00301                                 
00302                         }
00303                         glEnd();
00304                         glEnable(GL_LIGHTING);
00305 #endif
00306                         
00307                 }
00308 
00309                 glColor3f(1.0,1.0,1.0);
00310 
00311                 glDisable(GL_LIGHTING);
00312                 glBegin(GL_LINES);
00313 
00314                 MT_Scalar cube_size = 50;
00315                 glVertex3f(cube_size,cube_size,cube_size);
00316                 glVertex3f(-cube_size,cube_size,cube_size);
00317 
00318                 glVertex3f(cube_size,-cube_size,cube_size);
00319                 glVertex3f(-cube_size,-cube_size,cube_size);
00320                 
00321                 glVertex3f(cube_size,cube_size,-cube_size);
00322                 glVertex3f(-cube_size,cube_size,-cube_size);
00323 
00324                 glVertex3f(cube_size,-cube_size,-cube_size);
00325                 glVertex3f(-cube_size,-cube_size,-cube_size);
00326 
00327 
00328                 glVertex3f(-cube_size,cube_size,cube_size);
00329                 glVertex3f(-cube_size,-cube_size,cube_size);
00330 
00331                 glVertex3f(cube_size,cube_size,-cube_size);
00332                 glVertex3f(cube_size,-cube_size,-cube_size);
00333 
00334                 glVertex3f(cube_size,cube_size,cube_size);
00335                 glVertex3f(cube_size,-cube_size,cube_size);
00336 
00337                 glVertex3f(-cube_size,cube_size,-cube_size);
00338                 glVertex3f(-cube_size,-cube_size,-cube_size);
00339 
00340 
00341                 glVertex3f(cube_size,cube_size,cube_size);
00342                 glVertex3f(cube_size,cube_size,-cube_size);
00343 
00344                 glVertex3f(cube_size,-cube_size,cube_size);
00345                 glVertex3f(cube_size,-cube_size,-cube_size);
00346 
00347                 glVertex3f(-cube_size,cube_size,cube_size);
00348                 glVertex3f(-cube_size,cube_size,-cube_size);
00349 
00350                 glVertex3f(-cube_size,-cube_size,cube_size);
00351                 glVertex3f(-cube_size,-cube_size,-cube_size);
00352                 glEnd();
00353                 glEnable(GL_LIGHTING);
00354 
00355         };
00356 
00357 
00358 
00359 private :
00360 
00361         MyGlutMouseHandler * m_mouse_handler;
00362         MyGlutKeyHandler *m_key_handler;
00363         IK_Chain_ExternPtr *m_chains;
00364 
00365         int m_chain_num;
00366         ChainDrawer (
00367         ) : m_chains (NULL),
00368                 m_mouse_handler (NULL),
00369                 m_chain_num (0)
00370         {
00371         };
00372         
00373 };
00374 
00375 #endif
00376