Blender V2.61 - r43446

ChainDrawer.h

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