Blender V2.61 - r43446

main.cpp

Go to the documentation of this file.
00001 
00028 #include "MEM_SmartPtr.h"
00029 
00030 #ifdef USE_QUATERNIONS
00031 #include "IK_Qsolver.h"
00032 #else 
00033 #include "IK_solver.h"
00034 #endif
00035 
00036 #include <GL/glut.h>
00037 #include "MT_Vector3.h"
00038 #include "MT_Quaternion.h"
00039 #include "MT_Matrix3x3.h"
00040 #include "MyGlutMouseHandler.h" 
00041 #include "MyGlutKeyHandler.h"
00042 #include "ChainDrawer.h"
00043  
00044 void
00045 init(MT_Vector3 min,MT_Vector3 max)
00046 {
00047  
00048     GLfloat light_diffuse0[] = {1.0, 0.0, 0.0, 1.0};  /* Red diffuse light. */
00049     GLfloat light_position0[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */
00050 
00051     GLfloat light_diffuse1[] = {1.0, 1.0, 1.0, 1.0};  /* Red diffuse light. */
00052     GLfloat light_position1[] = {1.0, 0, 0, 0.0};  /* Infinite light location. */
00053 
00054   /* Enable a single OpenGL light. */
00055   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse0);
00056   glLightfv(GL_LIGHT0, GL_POSITION, light_position0);
00057 
00058   glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse1);
00059   glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
00060 
00061   glEnable(GL_LIGHT0);
00062   glEnable(GL_LIGHT1);
00063   glEnable(GL_LIGHTING);
00064 
00065   /* Use depth buffering for hidden surface elimination. */
00066   glEnable(GL_DEPTH_TEST);
00067 
00068   /* Setup the view of the cube. */
00069   glMatrixMode(GL_PROJECTION);
00070 
00071     // center of the box + 3* depth of box
00072 
00073   MT_Vector3 center = (min + max) * 0.5;
00074   MT_Vector3 diag = max - min;
00075 
00076     float depth = diag.length();
00077     float distance = 2;
00078 
00079   gluPerspective( 
00080     /* field of view in degree */ 40.0,
00081     /* aspect ratio */ 1.0,
00082     /* Z near */ 1.0, 
00083     /* Z far */ distance * depth * 2
00084   );
00085   glMatrixMode(GL_MODELVIEW);   
00086 
00087 
00088   gluLookAt(
00089     center.x(), center.y(), center.z() + distance*depth,  /* eye is at (0,0,5) */
00090     center.x(), center.y(), center.z(),      /* center is at (0,0,0) */
00091     0.0, 1.0, 0.);      /* up is in positive Y direction */
00092 
00093   glPushMatrix();   
00094 
00095  
00096 }
00097 int
00098 main(int argc, char **argv)
00099 {
00100 
00101 
00102     const int seg_num = 5;
00103     const MT_Scalar seg_length = 15;
00104 
00105     const float seg_startA[3] = {0,0,0};
00106     const float seg_startB[3] = {0,-20,0};
00107 
00108     // create some segments to solve with
00109 
00110     // First chain
00112 
00113 
00114     IK_Segment_ExternPtr const segmentsA = new IK_Segment_Extern[seg_num];  
00115     IK_Segment_ExternPtr const segmentsB = new IK_Segment_Extern[seg_num];  
00116 
00117     IK_Segment_ExternPtr seg_it = segmentsA;    
00118     IK_Segment_ExternPtr seg_itB = segmentsB;   
00119 
00120     
00121     {
00122 
00123 //      MT_Quaternion qmat(MT_Vector3(0,0,1),-3.141/2);
00124         MT_Quaternion qmat(MT_Vector3(0,0,1),0);
00125         MT_Matrix3x3 mat(qmat);
00126 
00127         seg_it->seg_start[0] = seg_startA[0];
00128         seg_it->seg_start[1] = seg_startA[1];
00129         seg_it->seg_start[2] = seg_startA[2];
00130 
00131         float temp[12];
00132         mat.getValue(temp);
00133  
00134         seg_it->basis[0] = temp[0];
00135         seg_it->basis[1] = temp[1];
00136         seg_it->basis[2] = temp[2];
00137 
00138         seg_it->basis[3] = temp[4];
00139         seg_it->basis[4] = temp[5];
00140         seg_it->basis[5] = temp[6];
00141 
00142         seg_it->basis[6] = temp[8];
00143         seg_it->basis[7] = temp[9];
00144         seg_it->basis[8] = temp[10];
00145  
00146         seg_it->length = seg_length;
00147 
00148         MT_Quaternion q;
00149         q.setEuler(0,0,0);
00150 
00151             
00152         MT_Matrix3x3 qrot(q);
00153 
00154         seg_it->basis_change[0] = 1;
00155         seg_it->basis_change[1] = 0;
00156         seg_it->basis_change[2] = 0;
00157         seg_it->basis_change[3] = 0;
00158         seg_it->basis_change[4] = 1;
00159         seg_it->basis_change[5] = 0;
00160         seg_it->basis_change[6] = 0;
00161         seg_it->basis_change[7] = 0;
00162         seg_it->basis_change[8] = 1;
00163 
00164 
00165         seg_it ++;          
00166 
00167         seg_itB->seg_start[0] = seg_startA[0];
00168         seg_itB->seg_start[1] = seg_startA[1];
00169         seg_itB->seg_start[2] = seg_startA[2];
00170  
00171         seg_itB->basis[0] = temp[0];
00172         seg_itB->basis[1] = temp[1];
00173         seg_itB->basis[2] = temp[2];
00174 
00175         seg_itB->basis[3] = temp[4];
00176         seg_itB->basis[4] = temp[5];
00177         seg_itB->basis[5] = temp[6];
00178 
00179         seg_itB->basis[6] = temp[8];
00180         seg_itB->basis[7] = temp[9];
00181         seg_itB->basis[8] = temp[10];
00182  
00183         seg_itB->length = seg_length;
00184 
00185         seg_itB->basis_change[0] = 1;
00186         seg_itB->basis_change[1] = 0;
00187         seg_itB->basis_change[2] = 0;
00188         seg_itB->basis_change[3] = 0;
00189         seg_itB->basis_change[4] = 1;
00190         seg_itB->basis_change[5] = 0;
00191         seg_itB->basis_change[6] = 0;
00192         seg_itB->basis_change[7] = 0;
00193         seg_itB->basis_change[8] = 1;
00194 
00195 
00196         seg_itB ++;         
00197 
00198 
00199     }
00200 
00201 
00202     int i;
00203     for (i=1; i < seg_num; ++i, ++seg_it,++seg_itB) {
00204 
00205         MT_Quaternion qmat(MT_Vector3(0,0,1),0.3);
00206         MT_Matrix3x3 mat(qmat);
00207 
00208         seg_it->seg_start[0] = 0;
00209         seg_it->seg_start[1] = 0;
00210         seg_it->seg_start[2] = 0;
00211 
00212         float temp[12];
00213         mat.getValue(temp);
00214  
00215         seg_it->basis[0] = temp[0];
00216         seg_it->basis[1] = temp[1];
00217         seg_it->basis[2] = temp[2];
00218 
00219         seg_it->basis[3] = temp[4];
00220         seg_it->basis[4] = temp[5];
00221         seg_it->basis[5] = temp[6];
00222 
00223         seg_it->basis[6] = temp[8];
00224         seg_it->basis[7] = temp[9];
00225         seg_it->basis[8] = temp[10];
00226  
00227         seg_it->length = seg_length;
00228 
00229         MT_Quaternion q;
00230         q.setEuler(0,0,0);
00231 
00232             
00233         MT_Matrix3x3 qrot(q);
00234 
00235         seg_it->basis_change[0] = 1;
00236         seg_it->basis_change[1] = 0;
00237         seg_it->basis_change[2] = 0;
00238         seg_it->basis_change[3] = 0;
00239         seg_it->basis_change[4] = 1;
00240         seg_it->basis_change[5] = 0;
00241         seg_it->basis_change[6] = 0;
00242         seg_it->basis_change[7] = 0;
00243         seg_it->basis_change[8] = 1;
00244 
00245 
00247 
00248         seg_itB->seg_start[0] = 0;
00249         seg_itB->seg_start[1] = 0;
00250         seg_itB->seg_start[2] = 0;
00251  
00252         seg_itB->basis[0] = temp[0];
00253         seg_itB->basis[1] = temp[1];
00254         seg_itB->basis[2] = temp[2];
00255 
00256         seg_itB->basis[3] = temp[4];
00257         seg_itB->basis[4] = temp[5];
00258         seg_itB->basis[5] = temp[6];
00259 
00260         seg_itB->basis[6] = temp[8];
00261         seg_itB->basis[7] = temp[9];
00262         seg_itB->basis[8] = temp[10];
00263  
00264         seg_itB->length = seg_length;
00265 
00266         seg_itB->basis_change[0] = 1;
00267         seg_itB->basis_change[1] = 0;
00268         seg_itB->basis_change[2] = 0;
00269         seg_itB->basis_change[3] = 0;
00270         seg_itB->basis_change[4] = 1;
00271         seg_itB->basis_change[5] = 0;
00272         seg_itB->basis_change[6] = 0;
00273         seg_itB->basis_change[7] = 0;
00274         seg_itB->basis_change[8] = 1;
00275 
00276 
00277 
00278     }
00279 
00280     // create the chains
00281 
00282     const int num_chains = 2;
00283 
00284     IK_Chain_ExternPtr chains[num_chains];
00285 
00286     chains[0] = IK_CreateChain();
00287     chains[1] = IK_CreateChain();
00288 
00289     // load segments into chain
00290 
00291     IK_LoadChain(chains[0],segmentsA,seg_num);
00292     IK_LoadChain(chains[1],segmentsB,seg_num);
00293 
00294     // make and install a mouse handler
00295 
00296     MEM_SmartPtr<MyGlutMouseHandler> mouse_handler (MyGlutMouseHandler::New());
00297     GlutMouseManager::Instance()->InstallHandler(mouse_handler);
00298 
00299     mouse_handler->SetChain(chains,num_chains);
00300 
00301     // make and install a keyhandler
00302     MEM_SmartPtr<MyGlutKeyHandler> key_handler (MyGlutKeyHandler::New());
00303     GlutKeyboardManager::Instance()->InstallHandler(key_handler);
00304 
00305     // instantiate the drawing class    
00306 
00307     MEM_SmartPtr<ChainDrawer> drawer (ChainDrawer::New());
00308     GlutDrawManager::Instance()->InstallDrawer(drawer);
00309 
00310     drawer->SetMouseHandler(mouse_handler);
00311     drawer->SetChain(chains,num_chains);
00312     drawer->SetKeyHandler(key_handler);
00313 
00314     glutInit(&argc, argv);
00315     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
00316     glutCreateWindow("ik");
00317     glutDisplayFunc(GlutDrawManager::Draw);
00318     glutMouseFunc(GlutMouseManager::Mouse);
00319     glutMotionFunc(GlutMouseManager::Motion);
00320     glutKeyboardFunc(GlutKeyboardManager::HandleKeyboard);
00321 
00322     init(MT_Vector3(-50,-50,-50),MT_Vector3(50,50,50));
00323     glutMainLoop();
00324     return 0;             /* ANSI C requires main to return int. */
00325 }