Biomechanical Joint Model
 Author: Anderson Maciel

sample_marching_cubes.cpp

Go to the documentation of this file.
00001 /*####################################################################################################################
00002  *
00003  *    Test functionality of Marching_cubes library.
00004  *
00005  *####################################################################################################################*/
00006 
00007 
00008 #include <OpenGLMyLib/glmodel3D.h>
00009 #include <OpenGLMyLib/glModImplicit.h> 
00010 #include <stdlib.h>
00011 #include <GL/glut.h>
00012 
00013 
00014 using namespace MyOGL;
00015 using namespace VL;
00016 
00017 // GLUT callback functions 
00018 void OpenGLInit(int argc, char** argv);
00019 void MyDisplay(void);
00020 void MyMouse( int button, int state, int x, int y );
00021 void MyReshape(GLsizei w, GLsizei h);
00022 void MyMotion(int x, int y);
00023 void MyKeyb(unsigned char key, int x, int y);
00024 void MySpecKeyb(int key, int x, int y);
00025 
00026 // Perspective default params
00027 double Near = 5.0;        // near and far cliping planes
00028 double Far  = 50.0;       // far clipping plane
00029 double Distance = 25.0;   // distance of the camera to the object
00030 double Fovy = M_PI/2;     // perspective angle
00031 double Size_persp = 2*Distance*tan(Fovy/2); // size of the object to be visible by the above given perspective proj. params
00032 
00033 double CalculateAngle(double size, double distance){
00034     double radtheta, degtheta;
00035 
00036     radtheta = 2.0 * atan2 (size/2.0, distance);
00037     degtheta = (180.0 * radtheta) / M_PI;
00038     return (degtheta);
00039 }
00040 
00041 void InitLight(){
00042 // setting general light property(herein white light source)
00043   glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
00044   glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
00045   glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);  
00046   // light source position
00047   // 1) directional light source: it is in infinity and directed in (1,1,1) direction
00048   //    light is emited in all direction
00049 #if DIRECTONAL_LIGHT
00050   GLfloat light_position_directional[] = { 0.0, 0.0, 5*Size_persp, 0.0 };
00051   for(int i=0; i<4; i++) light_position[i]=light_position_directional[i];
00052 #else
00053   // 2) positional light source: light is at (1,1,1) position
00054   GLfloat light_position_positional[]  = { 0.0, 0.0, 5*Size_persp, 1.0 };
00055   for(int i=0; i<4; i++) light_position[i]=light_position_positional[i];
00056 #endif
00057   glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00058 #if GLOBAL_AMBIENT_LIGHT
00059   //1) Global ambient light 
00060   GLfloat lmodel_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
00061   glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
00062 #endif
00063   //2) controls the position of the viewpoint(false means that the view point is in infinity, while
00064   //   when it is true view point is placet at (0,0,0). This influence specularity. Better use true.
00065   glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
00066 
00067   //3) use one or both sides of the facets for light computattion
00068   glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE); 
00069 
00070 // setting general material property
00071   GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
00072   GLfloat mat_ambient[] = { 0.5, 0.5, 0.5, 1.0 };
00073   GLfloat mat_ambient_color[] = { 0.1, 0.1, 0.1, 1.0 };
00074   GLfloat mat_diffuse[] = { 0.5, 0.5, 0.5, 1.0 };
00075   GLfloat mat_specular[] = { 0.5, 0.5, 0.5, 0.5 };
00076   GLfloat no_shininess[] = { 0.0 };
00077   GLfloat low_shininess[] = { 20.0 };
00078   GLfloat high_shininess[] = { 100.0 };
00079   
00080   glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
00081   glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
00082   glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
00083   glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess);
00084   glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
00085 
00086 
00087   // background color
00088   glClearColor (0.0, 0.0, 0.0, 0.0);
00089 
00090   // shading model
00091   glShadeModel(GL_SMOOTH);
00092   
00093   glEnable(GL_LIGHTING);
00094   glEnable(GL_LIGHT0);
00095   glEnable(GL_COLOR_MATERIAL); 
00096 }
00097 
00098 void OpenGLInit(int argc, char **argv)
00099 { 
00100 
00101   Vector4d<double> LL, UR;
00102   double *CG, size;
00103 
00104   if(argc < 2 ) 
00105      PrintHelp(argv[0]);
00106 
00107     //Loads model
00108   if(ReadOptionString("-sph",sphere_fname)){ 
00109     Model = new glModImplicit(resP,sphere_fname,SPHERE,stiffnesPF);
00110     fprintf(stderr,"read spheres\n");
00111   }else if(ReadOptionString("-el",ellipse_fname)){
00112     Model = new glModImplicit(resP,sphere_fname,ELLIPSOID,stiffnesPF);
00113     fprintf(stderr,"read ellipsoids\n");
00114   }else{
00115     Model = new glmodel3D(model_filename);
00116     fprintf(stderr,"read mesh\n");
00117   }
00118   
00119   Model->SetAllEdgesVisibles();
00120   Model->GetTriangulationGCdist();
00121   // Set correct perspective in terms of object initial size and position
00122   LL=Model->GetBboxLL();
00123   UR=Model->GetBboxUR();
00124   CG=Model->GetCG();
00125   Size_persp=sqrt((UR[0]-LL[0])*(UR[0]-LL[0]) + (UR[1]-LL[1])*(UR[1]-LL[1]) + (UR[2]-LL[2])*(UR[2]-LL[2]))/2;
00126   Distance = 3*Size_persp;
00127   Fovy=CalculateAngle(2*Size_persp,Distance);
00128   Near=Distance/2;
00129   Far=2*Distance;
00130 #if 1
00131   fprintf(stderr,"CG = [%lf, %lf, %lf]\n",CG[0],CG[1],CG[2]);
00132   fprintf(stderr,"Bounding box: LL[%lf, %lf, %lf], UR[%lf, %lf, %lf]\n",LL[0],LL[1],LL[2],UR[0],UR[1],UR[2]);
00133   fprintf(stderr,"Size_persp = %lf, Distance = %lf, Fovy = %lf, Near = %lf, Far = %lf\n",Size_persp,Distance,Fovy,Near,Far);
00134 #endif
00135    
00136   // set the initial translation so that the mesh is centered at the coordinate frame
00137   origX=-(*CG++), origY=-(*CG++), origZ=-(*CG);
00138   origScaleX=scalex, origScaleY=scaley, origScaleZ=scalez;
00139 
00140   glEnable(GL_DEPTH_TEST);
00141   glClearColor(0.0, 0.0, 0.0, 0.0);
00142   sc_global=Size_persp/0.5;
00143   sc_local=sc_global/4.0;
00144   CoordFrames(sc_global,sc_local);
00145 }
00146 
00147 void CoordFrames(double sc_global, double sc_local){
00148   // global coordinate frame
00149  glNewList(GLOBAL_COORD_FRAME, GL_COMPILE);
00150    // x 
00151   glBegin(GL_LINES); 
00152      glColor3f(1.0, 0.0, 0.0);
00153      glVertex3f(0.0, 0.0, 0.0);
00154      glVertex3f(0.5, 0.0, 0.0);
00155      glVertex3f(0.5, 0.0, 0.0);
00156      glVertex3f(0.45, 0.01, 0.0);
00157      glVertex3f(0.5, 0.0, 0.0);
00158      glVertex3f(0.45, -0.01, 0.0);
00159      glVertex3f(0.45, 0.1, 0.0);
00160      glVertex3f(0.50, 0.05, 0.0);
00161      glVertex3f(0.50, 0.1, 0.0);
00162      glVertex3f(0.45, 0.05, 0.0);
00163   glEnd();
00164   glBegin(GL_LINES);
00165    // y 
00166     glColor3f(0.0, 1.0, 0.0);
00167     glVertex3f(0.0, 0.0, 0.0);
00168     glVertex3f(0.0, 0.5, 0.0);
00169     glVertex3f(0.0, 0.5, 0.0);
00170     glVertex3f(0.01, 0.45, 0.0);
00171     glVertex3f(0.0, 0.5, 0.0);
00172     glVertex3f(-0.01, 0.45, 0.0);
00173   glEnd();
00174   glBegin(GL_LINES);
00175    // z 
00176     glColor3f(0.0, 0.0, 1.0);
00177     glVertex3f(0.0, 0.0, 0.0);
00178     glVertex3f(0.0, 0.0, 0.5);
00179     glVertex3f(0.0, 0.0, 0.5);
00180     glVertex3f(0.0, 0.01, 0.45);
00181     glVertex3f(0.0, 0.0, 0.5);
00182     glVertex3f(0.0, -0.01, 0.45);
00183   glEnd();
00184   glColor3f(0.0, 0.0, 0.0);
00185   glEndList();
00186 
00187   // local coordinate frame
00188   glNewList(LOCAL_COORD_FRAME, GL_COMPILE);
00189   glBegin(GL_LINES);
00190   // x 
00191     glColor3f(1.0, 0.0, 0.0);
00192     glVertex3f(0.0,0.0,0.0);
00193     glVertex3f(0.5*sc_local, 0.0, 0.0);
00194     glVertex3f(0.5*sc_local, 0.0, 0.0);
00195     glVertex3f(0.45*sc_local, 0.01*sc_local, 0.0);
00196     glVertex3f(0.5*sc_local, 0.0, 0.0);
00197     glVertex3f(0.45*sc_local, -0.01*sc_local, 0.0);
00198     glVertex3f(0.45*sc_local, 0.1*sc_local, 0.0);
00199     glVertex3f(0.50*sc_local, 0.05*sc_local, 0.0);
00200     glVertex3f(0.50*sc_local, 0.1*sc_local, 0.0);
00201     glVertex3f(0.45*sc_local, 0.05*sc_local, 0.0);
00202   glEnd();
00203   glBegin(GL_LINES);
00204    // y 
00205     glColor3f(0.0, 1.0, 0.0);
00206     glVertex3f(0.0, 0.0, 0.0);
00207     glVertex3f(0.0, 0.5*sc_local, 0.0);
00208     glVertex3f(0.0, 0.5*sc_local, 0.0);
00209     glVertex3f(0.01*sc_local, 0.45*sc_local, 0.0);
00210     glVertex3f(0.0, 0.5*sc_local, 0.0);
00211     glVertex3f(-0.01*sc_local, 0.45*sc_local, 0.0);
00212   glEnd();
00213   glBegin(GL_LINES);
00214    // z 
00215     glColor3f(0.0, 0.0, 1.0);
00216     glVertex3f(0.0, 0.0, 0.0);
00217     glVertex3f(0.0, 0.0, 0.5*sc_local);
00218     glVertex3f(0.0, 0.0, 0.5*sc_local);
00219     glVertex3f(0.0, 0.01*sc_local, 0.45*sc_local);
00220     glVertex3f(0.0, 0.0, 0.5*sc_local);
00221     glVertex3f(0.0, -0.01*sc_local, 0.45*sc_local);
00222   glEnd();
00223   glColor3f(0.0, 0.0, 0.0);
00224   glEndList();
00225 
00226 
00227 }
00228 
00229 void MyDisplay(void)
00230 {    
00231   // clear the screen
00232   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
00233  
00234   // draw world referential
00235   glPushMatrix();
00236     glScaled(sc_global,sc_global,sc_global);
00237     glCallList(GLOBAL_COORD_FRAME);
00238   glPopMatrix();
00239 
00240   glPushMatrix();
00241     glTranslatef(xgl, ygl, zgl);
00242     glRotated(spinx, 1.0, 0.0, 0.0);
00243     glRotated(spiny, 0.0, 1.0, 0.0);
00244     glRotated(spinz, 0.0, 0.0, 1.0);
00245     glScaled(scalex, scaley, scalez);
00246     // translation and rotation of the objects
00247     glTranslatef(origX, origY, origZ);
00248   
00249   // draw wireframe model and data cloud(observations)
00250     glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
00251     if(drawShaded){
00252       glColor3f(0.9, 0.9, 0.9);
00253       Model->glDrawSolid(shading);
00254       glColor3f(0.5, 0.5, 0.5);
00255       Model->glDrawWireEdges();
00256     }
00257     else{
00258       glColor3f(0.5, 0.5, 0.5);
00259       Model->glDrawWire();
00260     }
00261   glPopMatrix();
00262   
00263   glPushMatrix();
00264   // translation and rotation of the local coord frame
00265     glTranslatef(xgl, ygl, zgl);
00266     glRotated(spinx, 1.0, 0.0, 0.0);
00267     glRotated(spiny, 0.0, 1.0, 0.0);
00268     glRotated(spinz, 0.0, 0.0, 1.0);
00269     glScaled(scalex, scaley, scalez);
00270 #if 0
00271     printf("xgl=%lf, ygl=%lf, zgl=%lf\n",xgl,ygl,zgl);
00272     printf("origX=%lf, origY=%lf, origZ=%lf\n",origX,origY,origZ);
00273 #endif
00274     glCallList(LOCAL_COORD_FRAME);
00275   glPopMatrix();
00276 
00277   glutSwapBuffers();
00278 }
00279 
00280 void MyReshape(GLsizei w, GLsizei h){
00281   glViewport(0, 0, w, h);
00282   Cam.setShape(Fovy, w/h, Near, Far);
00283   glLoadIdentity();
00284   gluLookAt(0, 0, Distance, 0, 0, 0, 0, 1, 0);
00285 }
00286 
00287 void MyMouse(int button, int state, int x, int y ){
00288 
00289   Facet  *SelectedFacet;
00290   Vertex *SelectedVertex;
00291   double Trans[3], Rot[3], Scale[3];
00292   Point  *Pt;
00293   int selId;
00294   
00295  
00296 //  x & y rotation
00297   if(button == GLUT_LEFT_BUTTON && glutGetModifiers() != GLUT_ACTIVE_SHIFT && \
00298      glutGetModifiers() != GLUT_ACTIVE_ALT && state == GLUT_DOWN){
00299     rotxy = GL_TRUE;
00300     startx = x;
00301     starty = y;
00302   }
00303 
00304 //  z rotation 
00305   if(button == GLUT_LEFT_BUTTON && glutGetModifiers() == GLUT_ACTIVE_SHIFT && state == GLUT_DOWN){
00306     rotz = GL_TRUE;
00307     starty = y;
00308   }
00309 
00310 //  x & y translation
00311   if(button == GLUT_RIGHT_BUTTON && glutGetModifiers() != GLUT_ACTIVE_SHIFT && \
00312      glutGetModifiers() != GLUT_ACTIVE_CTRL && state == GLUT_DOWN){       
00313     movexy = GL_TRUE;
00314     startx = x;
00315     starty = y;
00316   }
00317 
00318 //  z translation
00319   if(button == GLUT_RIGHT_BUTTON && glutGetModifiers() == GLUT_ACTIVE_SHIFT \
00320      && glutGetModifiers() != GLUT_ACTIVE_CTRL && state == GLUT_DOWN){
00321     movez = GL_TRUE;
00322     starty = y;
00323   }
00324 
00325   glutPostRedisplay();
00326   
00327 }
00328 
00329 void MyMotion(int x, int y){
00330         
00331   if(rotxy){
00332     spinx = spinx + (y - starty)/DIV_FACTOR_ROT;  
00333     spiny = spiny + (x - startx)/DIV_FACTOR_ROT;
00334     startx = x;
00335     starty = y;
00336   }
00337 
00338   if(rotz){
00339     spinz = spinz + (y - starty)/DIV_FACTOR_ROT;
00340     starty = y;
00341   }
00342 
00343   if(movexy){
00344     xgl = xgl + (x - startx)/DIV_FACTOR_TRANS;
00345     ygl = ygl - (y - starty)/DIV_FACTOR_TRANS;
00346     startx = x;
00347     starty = y;
00348   }
00349 
00350   if(movez){
00351     zgl = zgl + (y - starty)/DIV_FACTOR_TRANS;
00352     starty = y;
00353   }
00354 
00355   glutPostRedisplay();
00356  
00357 }
00358 
00359 
00360 void MyKeyb(unsigned char key, int x, int y){   
00361 
00362   switch(key){    
00363     case 'q' : 
00364     case 'Q' :
00365       exit(0);
00366     case 'z' : 
00367          scalex+=0.01;
00368          scaley+=0.01;
00369          scalez+=0.01;
00370          break;
00371     case 'Z' :
00372       if(scalex > 0.0){
00373          scalex-=0.01;
00374          scaley-=0.01;
00375          scalez-=0.01;
00376       }
00377       break;
00378     case 's':
00379     case 'S':  
00380       shading=SMOOTH_SHADING;
00381       drawShaded=GL_TRUE;
00382       break;
00383     case 'f':
00384     case 'F':  
00385       shading=FLAT_SHADING;
00386       drawShaded=GL_TRUE;
00387       break;
00388     case 'w':
00389     case 'W':
00390       drawShaded=GL_FALSE;
00391       break;
00392 
00393     case 'o':
00394     case 'O': // positioning mesh in the origin
00395       xgl=0.0, ygl=0.0, zgl=0.0;
00396       spinx=spiny=spinz=0.0;
00397       scalex=origScaleX, scaley=origScaleY, scalez=origScaleZ;
00398       glFlush();
00399       break;
00400   }
00401   glutPostRedisplay();
00402 }
00403 

Generated on Thu Dec 1 10:13:40 2005 for COME - Biomechanical Joint Model by  doxygen 1.4.5