main.cpp

Go to the documentation of this file.
00001 
00002 
00003 #include <GL/glut.h>
00004 #include <iostream>
00005 
00006 
00007 
00008 #include <MC_particle_engine.hpp>
00009 #include <MC_grid_3d_scalar.hpp>
00010 #include <MC_grid_3d_scalar_marching_cube.hpp>
00011 #include <MC_potential_sphere.hpp>
00012 
00013 #include <mesh_converter.hpp>
00014 #include <vector>
00015 #include <algorithm>
00016 
00017 
00018 using namespace mesh_conv;
00019 using namespace std;
00020 
00021 //glut functions
00022 static void display_callback();
00023 static void reshape_callback (int width, int height);
00024 static void keyboard_callback(unsigned char key,int x,int y);
00025 static void idle_callback(int value);
00026 static void motion_callback (int x, int y);
00027 static void mouse_callback (int button, int action, int x, int y);
00028 
00029 
00030 //main functions
00031 void draw_orientation();
00032 void draw_pointer();
00033 void draw_fps();
00034 void draw_extra();
00035 void draw_sphere(const MC_v3d& center,const double& radius);
00036 
00037 int setup_opengl(int argc,char **argv);
00038 int draw_scene(); //draw the model
00039 void init_parameters();
00040 double get_rand();//just a rand
00041 int evolve();
00042 
00043 
00044 
00045 // GLOBAL VARIABLES
00046 // ************************************ //
00047 bool is_shift_on=false;
00048 bool is_draw_grid=false;
00049 bool is_draw_sphere=true;
00050 bool is_run=false;
00051 bool is_draw_force=false;
00052 bool is_draw_isosurface=false;
00053 bool is_plane=false;
00054 bool is_plane_cleared;
00055 bool is_throw_ball=false;
00056 bool is_collision_detection=true;
00057 bool is_potential=false;
00058 
00059 unsigned int counter_global=0;
00060 
00061 GLuint id_draw_sphere=0;
00062 unsigned int counter=0;
00063 mesh_conv::MC_navigator_tool navigator;
00064 
00065 unsigned int thrower_timer=0;
00066 
00067 mesh_conv::MC_particle_engine engine;
00068 mesh_conv::MC_v3d_vector COLOR_TABLE;
00069 mesh_conv::MC_grid_3d_scalar potential;
00070 mesh_conv::MC_mesh_index_vector_draw mesh_iso;
00071 double isovalue=2;
00072 unsigned int N_smooth=2;
00073 unsigned int thrower_time=90;
00074 double sphere_radius=0.03;
00075 
00076 unsigned int dim_grid_3d=30;
00077 
00078 
00079 double plane_Lz=0.0;
00080 double plane_L=0.5;
00081 mesh_conv::MC_mesh_index_vector_draw plane_mesh;
00082 
00083 
00084 void update_potential();
00085 void update_marching_cube();
00086 void draw_plane();
00087 void plane_clear();
00088 void update_plane(const unsigned int& N);
00089 void update_plane_density();
00090 void export_scene(unsigned int number);
00091 
00092 void engine_run();
00093 unsigned int engine_timer=0;
00094 void set_light();
00095 
00096 int main(int argc, char *argv[])
00097 {
00098   cout<<"**hello**\n"<<endl;
00099   std::cout<<"run "<<argv[0]<<" with "<<argc-1<<" parameters ... \n"<<std::endl;
00100 
00101 
00102   //load camera & light
00103   std::string file_cam("saved_position/saved_camera.dat");
00104   navigator.read_cam1_file(file_cam);
00105 
00106 
00107   //*************//
00108   //set parameters
00109   //*************//
00110 
00111   //size of the 3D grid of the potential
00112   dim_grid_3d=20;
00113   //frequency of the creation of new sphere
00114   thrower_time=90;
00115   //radius of the spheres
00116   sphere_radius=0.03;
00117   //isovalue
00118   isovalue=2;
00119   //smooth the isosurface N times
00120   N_smooth=2;
00121   //does the sphere collide
00122   is_collision_detection=true;
00123   //does the potential interaction force computed
00124   is_potential=false;
00125 
00126 
00127 
00128   //init two spheres
00129 //  engine.add(MC_sphere_physic(MC_v3d(0.5,0.2,0.5),sphere_radius,MC_v3d(0,0.1,1)));
00130 //  engine.add(MC_sphere_physic(MC_v3d(0.5,0.8,0.5),sphere_radius,MC_v3d(0,-0.1,1)));
00131   engine.add(MC_sphere_physic(MC_v3d(0.5,0.2,0.5),sphere_radius,MC_v3d(0,1,1)));
00132   engine.add(MC_sphere_physic(MC_v3d(0.5,0.8,0.5),sphere_radius,MC_v3d(0,-1,1)));
00133 
00134 
00135   //*************//
00136   //start OpenGL
00137   //*************//
00138   navigator.x_screen_size_1=800;
00139   navigator.y_screen_size_1=800;
00140   setup_opengl(argc,argv);
00141 
00142 
00143 
00144   //*************//
00145   //load  models
00146   //*************//
00147   init_parameters();
00148 
00149   //*************//
00150   //run
00151   //*************//
00152   std::cout<<"**RUN**\n"<<std::endl;
00153   std::cout<<"Navigation with keys:\n"<<std::endl;
00154   std::cout<<"=========================="<<std::endl;
00155   std::cout<<"Space Displacement"<<std::endl;
00156   std::cout<<"=========================="<<std::endl;
00157   std::cout<<"  z    r"<<std::endl;
00158   std::cout<<"q s d  f\n"<<std::endl;
00159   std::cout<<"left mouse: rotate "<<std::endl;
00160   std::cout<<"right mouse: scale "<<std::endl;
00161   std::cout<<"shift+left mouse: translate in-plane "<<std::endl;
00162   std::cout<<"shift+right mouse: translate out-of-plane "<<std::endl<<std::endl;
00163   std::cout<<"=========================="<<std::endl;
00164   std::cout<<"View:"<<std::endl;
00165   std::cout<<"=========================="<<std::endl;
00166   std::cout<<"W: wireframe mode"<<std::endl;
00167   std::cout<<"F: filled mode"<<std::endl;
00168   std::cout<<"G: Grid mode"<<std::endl;
00169   std::cout<<"X: Print cursor coordinates"<<std::endl;
00170   std::cout<<"S: Draw spheres"<<std::endl;
00171   std::cout<<"V: Draw forces"<<std::endl;
00172   std::cout<<"Q: exit"<<std::endl<<std::endl;
00173   std::cout<<"=========================="<<std::endl;
00174   std::cout<<"Run:"<<std::endl;
00175   std::cout<<"=========================="<<std::endl;
00176   std::cout<<"R: Start/Stop simulation"<<std::endl;
00177   std::cout<<"T: Start/Stop add spheres"<<std::endl;
00178   std::cout<<"I: Compute isosurface"<<std::endl;
00179   std::cout<<"U: Create plane (+shift mouse left/shift mouse right)"<<std::endl;
00180   std::cout<<"==========================\n"<<std::endl;
00181   glutMainLoop ();
00182 
00183 
00184   return 0;
00185 }
00186 
00187 
00188 
00189 //update the 3D grid
00190 void update_potential()
00191 {
00192     //initial value of the grid initialized at -1
00193     potential.fill(-1.0);
00194 
00195     //get every sphere
00196     std::map<unsigned int,MC_sphere_physic> particle=engine.particle_system();
00197     for(std::map<unsigned int,MC_sphere_physic>::const_iterator it=particle.begin(),it_end=particle.end();it!=it_end;++it)
00198     {
00199         const MC_sphere_physic& s=it->second;
00200         implicit::MC_potential_sphere pot(s);
00201 
00202         //add the potential on the sphere
00203         potential.add_potential(pot);
00204     }
00205 }
00206 
00207 //update the marching cube
00208 void update_marching_cube()
00209 {
00210     //run the marching cube algorithm
00211     mesh_iso=MC_grid_3d_scalar_marching_cube::marching_cube(potential,isovalue);
00212 
00213     //smooth the mesh
00214     //mesh_iso.laplacian_smoothing(0.3,N_smooth,false);
00215     mesh_iso.normal_vertex_update();
00216 }
00217 
00218 
00219 
00220 
00221 //evole through time
00222 void engine_run()
00223 {
00224 
00225     //run particle system
00226     engine.evolve(is_collision_detection,is_potential);
00227 
00228     //update isosurface
00229     if(is_draw_isosurface)
00230     {
00231         update_potential();
00232         update_marching_cube();
00233     }
00234 
00235     //export_scene(counter_global++);
00236 
00237 }
00238 
00239 
00240 //particles evolve through time and add new spheres
00241 int evolve()
00242 {
00243     //set-up timer
00244     int ut=glutGet(GLUT_ELAPSED_TIME);
00245 
00246 
00247     if(ut-engine_timer>25)
00248     {
00249         if(is_run)
00250         {
00251             //add new balls
00252             if(is_throw_ball)
00253             {
00254                 if(ut-thrower_timer>thrower_time)
00255                 {
00256                     engine.add(MC_sphere_physic(MC_v3d(0.5,0.5,0.5),sphere_radius,MC_v3d(1,1,2)*1+MC_v3d(1+0.1*cos(navigator.current_time*34232),1+0.2*sin(5245.34*navigator.current_time*34232),1)));
00257                     thrower_timer=ut;
00258                 }
00259             }
00260 
00261             //evolve particles
00262             engine_run();
00263             engine_timer=ut;
00264         }
00265     }
00266 
00267     return 0;
00268 }
00269 
00270 
00271 //init some parameters
00272 void init_parameters()
00273 {
00274     COLOR_TABLE=MC_v3d_vector(MC_v3d(1,0,0))<<MC_v3d(0,1,0)<<MC_v3d(0,0,1)<<MC_v3d(0,1,1)<<MC_v3d(1,0,1)<<MC_v3d(1,1,0);
00275     id_draw_sphere=MC_opengl_drawer::send_gpu(MC_mesh_fast_draw(MC_mesh_index_vector::build_sphere(2)));
00276     potential=MC_grid_3d_scalar(MC_int_vector(dim_grid_3d,dim_grid_3d,dim_grid_3d),MC_v3d(1,1,1),MC_v3d(0,0,0));
00277 
00278 }
00279 
00280 
00281 
00282 
00283 
00284 
00285 
00286 
00287 void draw_extra()
00288 {
00289     glEnable(GL_LIGHTING);
00290     glColor3d(0.8,0.8,0.8);
00291 
00292     //draw what you want
00293 
00294 }
00295 
00296 
00297 
00298 
00299 
00300 
00301 
00302 
00303 
00304 
00305 
00306 //****************************************************************************//
00307 //****************************************************************************//
00308 //****************************************************************************//
00309 //****************************************************************************//
00310 
00311 
00312 
00313 
00314 
00315 
00316 
00317 
00318 
00319 //*******************//
00320 //GLUT STUFF
00321 //*******************//
00322 
00323 
00324 //glut functions
00325 int setup_opengl(int argc,char **argv)
00326 {
00327 
00328     //init window
00329     glutInit (&argc, argv);
00330     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
00331     glutInitWindowPosition(0,0);
00332     glutInitWindowSize(navigator.x_screen_size_1,navigator.y_screen_size_1);
00333     glutCreateWindow("win_1");
00334 
00335 
00336     //OpenGL set
00337     glClearColor(1.0f,1.0f,1.0f,1.0f);
00338     glPointSize(5);
00339     glLineWidth(2);
00340 
00341     glShadeModel(GL_SMOOTH);
00342     glEnable(GL_COLOR_MATERIAL);
00343     //glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
00344     glEnable(GL_DEPTH_TEST);
00345     glDepthMask(GL_TRUE);
00346 
00347 
00348     glFrontFace(GL_CCW);//clockwise CW or CCW
00349     glDisable(GL_CULL_FACE);
00350     //glEnable(GL_CULL_FACE);
00351 
00352     glDisable(GL_BLEND);
00353     glDisable(GL_TEXTURE_2D);
00354     glDisable(GL_FOG);
00355 
00356     glEnable(GL_LIGHTING);
00357     glEnable(GL_LIGHT0);
00358     glFrontFace( GL_CW ) ;
00359     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
00360 
00361     set_light();
00362 
00363 
00364 
00365 
00366     glEnable(GL_NORMALIZE);
00367 
00368 
00369 
00370 
00371 
00372     glEnable(GL_LINE_SMOOTH);
00373     glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
00374 
00375     //callback
00376     glutDisplayFunc (display_callback);
00377     glutReshapeFunc (reshape_callback);
00378     glutKeyboardFunc(keyboard_callback);
00379     glutTimerFunc(33,idle_callback,1);
00380     glutMotionFunc (motion_callback);
00381     glutMouseFunc (mouse_callback);
00382 
00383     return 0;
00384 }
00385 
00386 
00387 
00388 //display
00389 static void display_callback()
00390 {
00391 
00392 
00393     //vieport (all screen)
00394     glViewport (0, 0, navigator.x_screen_size_1,navigator.y_screen_size_1);
00395     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00396 
00397     //Projection
00398     glMatrixMode(GL_PROJECTION);
00399     glLoadIdentity ();
00400     mesh_conv::MC_double_vector proj_param=navigator.projection_param();
00401     glFrustum(proj_param[0], -proj_param[0], proj_param[1], -proj_param[1], proj_param[2], proj_param[3]);
00402 
00403 
00404     //Modelview
00405     glMatrixMode(GL_MODELVIEW);
00406     glLoadIdentity();
00407 
00408     glTranslated(0,0,static_cast<GLdouble>(navigator.dist_cam1)); //zoom
00409     MC_matrix temp_rotation = navigator.trackball_cam1.quaternion().conjugated().matrix().to_matrix4();
00410     glMultMatrixd(temp_rotation.pointer()); //rotation
00411     glTranslated(static_cast<GLdouble>(navigator.tr_cam1[0]),static_cast<GLdouble>(navigator.tr_cam1[1]),static_cast<GLdouble>(navigator.tr_cam1[2])); //translation
00412 
00413 
00414     //draw
00415     draw_scene();
00416 
00417     //draw_pointer
00418     draw_pointer();
00419 
00420     //draw orientation (clear DEPTH-BUFFER)
00421     draw_orientation();
00422 
00423     //update fps
00424     navigator.increase_frame_number();
00425     navigator.current_time=glutGet(GLUT_ELAPSED_TIME);
00426     if(navigator.delta_time()>1000)
00427     {navigator.update_fps();}
00428 
00429     //draw fps
00430     draw_fps();
00431 
00432     //error OpenGL_drawer_2
00433     GLenum errCode;
00434     const GLubyte *errString;
00435     if((errCode=glGetError()) != GL_NO_ERROR)
00436     {
00437         errString = gluErrorString(errCode);
00438         std::cout<<"OPENGL ERROR : "<<errString<<std::endl;
00439         exit(-2);
00440     }
00441 
00442     counter++;
00443 
00444 
00445   //redraw
00446   glFlush ();
00447   glutSwapBuffers ();
00448 
00449 }
00450 
00451 
00452 static void reshape_callback (int width, int height)
00453 {
00454     navigator.x_screen_size_1=width;navigator.y_screen_size_1=height;
00455     glViewport(0,0, width, height);
00456 }
00457 
00458 
00459 //keyboard
00460 static void keyboard_callback(unsigned char key,int x,int y)
00461 {
00462 
00463     x=0;y=0;
00464     double dL=0.001*(1+100*abs(navigator.dist_cam1));
00465 
00466 #define GLUT_SPACE 32
00467 
00468     int mod = glutGetModifiers();
00469     if((mod&GLUT_ACTIVE_CTRL)!=0)
00470     {
00471         std::cout<<"ctrl on"<<std::endl;
00472 
00473     }
00474 
00475 
00476     switch(key)
00477     {
00478     case 'Q'://quit
00479         {
00480             std::cout<<"\n[EXIT OK]\n\n"<<std::endl;
00481 
00482             std::string file_cam("saved_position/saved_camera.dat");
00483             navigator.write_cam1_file(file_cam);
00484 
00485             exit(0);
00486         }
00487         break;
00488 
00489 
00490     case 'z':
00491         navigator.go_forward_trackball_cam1(dL);
00492         break;
00493     case 's':
00494         navigator.go_forward_trackball_cam1(-dL);
00495         break;
00496     case 'q':
00497         navigator.go_right_trackball_cam1(-dL);
00498         break;
00499     case 'd':
00500         navigator.go_right_trackball_cam1(dL);
00501         break;
00502     case 'r':
00503         navigator.go_up_trackball_cam1(dL);
00504         break;
00505     case 'f':
00506         navigator.go_up_trackball_cam1(-dL);
00507         break;
00508 
00509     case 'G':
00510         is_draw_grid==1?is_draw_grid=0:is_draw_grid=1;
00511         break;
00512 
00513     case 'W'://wireframe
00514         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00515         break;
00516     case 'F'://Filled
00517         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00518         break;
00519 
00520     case 'X'://cursor position
00521         std::cout<<"cursor position: "<<-navigator.tr_cam1<<std::endl;
00522         break;
00523 
00524     case 'R'://run
00525         is_run=!is_run;
00526         std::cout<<"run: "<<is_run<<std::endl;
00527         break;
00528 
00529     case 'V': //draw forces
00530         is_draw_force=!is_draw_force;
00531         break;
00532 
00533     case 'S': //draw sphere
00534         is_draw_sphere=!is_draw_sphere;
00535         break;
00536 
00537     case 'I': //isosurface
00538         is_draw_isosurface=!is_draw_isosurface;
00539         if(is_draw_isosurface==true)
00540         {
00541             update_potential();
00542             update_marching_cube();
00543         }
00544         break;
00545 
00546     case 'U':
00547         {
00548             is_plane==true?is_plane=false:is_plane=true;
00549             is_plane_cleared=false;
00550             update_plane(2);
00551             if(is_plane==false)
00552             {
00553                 update_plane(180);
00554                 update_plane_density();
00555             }
00556         }
00557         break;
00558 
00559             case 'T': //throw ball
00560                 {
00561                     is_throw_ball=!is_throw_ball;
00562 
00563                 }
00564                 break;
00565 
00566                 case 'E': //save
00567                 export_scene(counter_global++);
00568             }
00569 
00570 
00571 
00572     glutPostRedisplay ();
00573 }
00574 
00575 
00576 static void idle_callback(int value)
00577 {
00578   value=1;//useless
00579 
00580   glutPostRedisplay ();
00581   glutTimerFunc( 10, idle_callback, 1);
00582 
00583 
00584   evolve();
00585 }
00586 
00587 
00588 
00589 static void motion_callback (int x, int y)
00590 {
00591     double x_old=navigator.x_previous,y_old=navigator.y_previous;
00592     double w=navigator.x_screen_size_1,h=navigator.y_screen_size_1;
00593 
00594 
00595 
00596     if ( is_shift_on==false && (navigator.left_button) )
00597     {
00598         //trackball
00599         double epsilon=1e-6;
00600         double x0=(2.0*x_old-w)/w,y0=(2.0*y_old-h)/h,x1=(2*x-w)/w,y1=(2*y-h)/h;
00601         if(std::sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1))>epsilon)
00602         {
00603             navigator.trackball_cam1.set_2d_coords((2.0*x_old-w)/w,(2.0*y_old-h)/h,(2*x-w)/w,(2*y-h)/h);
00604             navigator.trackball_cam1.apply_rotation();
00605         }
00606         navigator.x_previous=int(x);navigator.y_previous=int(y);
00607 
00608     }
00609     else if ( is_shift_on==false && (navigator.right_button))
00610     {
00611         double u = y-y_old;
00612         double fu = u/500.0; //factor of magnification
00613 
00614         navigator.dist_cam1 += (fabs(navigator.dist_cam1)+1)*fu; navigator.dist_cam1 = min(navigator.dist_cam1,0.0);
00615         navigator.y_previous=int(y);
00616 
00617     }
00618     if(is_plane==false && is_shift_on==true && (navigator.left_button) )
00619     {
00620         //PAN
00621         double dL=0.0001*(1+100*abs(navigator.dist_cam1));//move camera
00622 
00623         MC_v3d tr=MC_v3d(x-x_old,y-y_old,0.0);
00624         navigator.tr_cam1 += dL*navigator.trackball_cam1.quaternion().conjugated()*tr;
00625         navigator.x_previous=int(x);navigator.y_previous=int(y);
00626     }
00627     else if( is_plane==false && is_shift_on && (navigator.right_button) )
00628     {
00629 
00630         //PAN
00631         double dL=0.0002*(1+100*abs(navigator.dist_cam1));//move camera
00632 
00633         MC_v3d tr=MC_v3d(0,0,y-y_old);
00634         navigator.tr_cam1 += dL*navigator.trackball_cam1.quaternion().conjugated()*tr;
00635         navigator.x_previous=int(x);navigator.y_previous=int(y);
00636     }
00637 
00638 
00639     if(is_plane==true && is_shift_on && navigator.left_button )
00640     {
00641         plane_Lz += 0.01*static_cast<double>(y-y_old);
00642         navigator.x_previous=int(x);navigator.y_previous=int(y);
00643         update_plane(2);
00644     }
00645     else if(is_plane==true && is_shift_on && navigator.right_button )
00646     {
00647         double d1=x-x_old;
00648         double d2=y-y_old;
00649         double d;
00650 
00651         plane_L += 0.01*(d1+d2);
00652         navigator.x_previous=int(x);navigator.y_previous=int(y);
00653         update_plane(2);
00654     }
00655 
00656     glutPostRedisplay ();
00657 }
00658 static void mouse_callback (int button, int action, int x, int y)
00659 {
00660     navigator.x_previous=x;
00661     navigator.y_previous=y;
00662 
00663     int mod = glutGetModifiers();
00664     if((mod&GLUT_ACTIVE_SHIFT)!=0)
00665     {
00666         is_shift_on=true;
00667     }
00668     else
00669         is_shift_on=false;
00670 
00671   if (button == GLUT_LEFT_BUTTON && action == GLUT_DOWN) {
00672     navigator.left_button=true;
00673   }
00674   if (button == GLUT_LEFT_BUTTON && action == GLUT_UP)
00675     navigator.left_button=false;
00676 
00677   if (button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN) {
00678     navigator.right_button=true;
00679   }
00680   if (button == GLUT_RIGHT_BUTTON && action == GLUT_UP)
00681     navigator.right_button=false;
00682 
00683   glutPostRedisplay ();
00684 }
00685 
00686 //just a rand number \in [0,1]
00687 double get_rand()
00688 {
00689   int precision=50000;
00690   return double(rand()%precision)/double(precision);
00691 }
00692 
00693 
00694 void draw_orientation()
00695 {
00696     glDisable(GL_LIGHTING);
00697     //rotation
00698     glClear(GL_DEPTH_BUFFER_BIT);
00699     glMatrixMode(GL_PROJECTION);
00700     glPushMatrix();
00701     glLoadIdentity ();
00702     glOrtho(-1,1,1,-1,0.1,10.0);
00703     glMatrixMode(GL_MODELVIEW);
00704     glPushMatrix();
00705     glLoadIdentity();
00706     glTranslated(0.78,0.78,-1);
00707     // get pointer
00708     MC_matrix M=navigator.trackball_cam1.quaternion().conjugated().matrix().to_matrix4();
00709     // rotation
00710     glMultMatrixd(M.pointer());
00711 
00712     double L=0.2;
00713     glBegin(GL_LINES);
00714     glColor3d(1,0,0);
00715     glVertex3d(0,0,0);
00716     glVertex3d(L,0,0);
00717     glColor3d(0,1,0);
00718     glVertex3d(0,0,0);
00719     glVertex3d(0,L,0);
00720     glColor3d(0,0,1);
00721     glVertex3d(0,0,0);
00722     glVertex3d(0,0,L);
00723     glEnd();
00724     //pop matrices
00725     glPopMatrix();
00726     glMatrixMode(GL_PROJECTION);
00727     glPopMatrix();
00728     glMatrixMode(GL_MODELVIEW);
00729 
00730 }
00731 
00732 
00733 void draw_pointer()
00734 {
00735 
00736     glPushMatrix();
00737     glLoadIdentity();
00738 
00739     glTranslatef(0,0,GLfloat(navigator.dist_cam1)); //zoom
00740     MC_matrix temp_m=navigator.trackball_cam1.quaternion().conjugated().matrix().to_matrix4();
00741     glMultMatrixd(temp_m.pointer()); //rotation
00742 
00743     //local basis
00744     double L=0.01;
00745 
00746     glBegin(GL_LINES);
00747     glColor3d(1,0,0);
00748     glVertex3d(-L,0,0);
00749     glVertex3d(L,0,0);
00750     glColor3d(0,1,0);
00751     glVertex3d(0,-L,0);
00752     glVertex3d(0,L,0);
00753     glColor3d(0,0,1);
00754     glVertex3d(0,0,-L);
00755     glVertex3d(0,0,L);
00756     glEnd();
00757 
00758     glPopMatrix();
00759 
00760 }
00761 
00762 
00763 void draw_fps()
00764 {
00765     glDisable(GL_DEPTH_TEST);
00766 
00767     int int_fps=int(navigator.current_fps);
00768     int deci_fps=int(10*(navigator.current_fps-int_fps));
00769     double truncated_fps=int_fps+double(deci_fps)/10.0;
00770 
00771     std::string fps=(mesh_conv::MC_string_converter::to_string(truncated_fps)+" fps");
00772 
00773     glPushMatrix();
00774     glLoadIdentity();
00775     glRasterPos3f(-0.5f,-0.5f,-1.4f);
00776     for (unsigned int k=0,N=fps.length();k<N;++k)
00777         glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,fps[k]);
00778     glPopMatrix();
00779 
00780     std::string particles=(mesh_conv::MC_string_converter::to_string(engine.particle_system().size())+" particles");
00781     glPushMatrix();
00782     glLoadIdentity();
00783     glRasterPos3f(-0.5f,+0.5f,-1.4f);
00784     for (unsigned int k=0,N=particles.length();k<N;++k)
00785         glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,particles[k]);
00786     glPopMatrix();
00787 
00788     glEnable(GL_DEPTH_TEST);
00789 }
00790 
00791 
00792 
00793 //Draw the scene
00794 int draw_scene()
00795 {
00796 
00797     set_light();
00798 
00799 
00800     //draw spheres
00801     glColor3d(1,0,0);
00802     if(is_draw_sphere)
00803     {
00804         const std::map<unsigned int,MC_sphere_physic>& particles=engine.particle_system();
00805         for(std::map<unsigned int,MC_sphere_physic>::const_iterator it=particles.begin(),it_end=particles.end();it!=it_end;++it)
00806         {
00807             const unsigned int& id=it->first;
00808             glColor3dv(COLOR_TABLE[id%COLOR_TABLE.size()].pointer());
00809 
00810 
00811             const MC_sphere_physic& current_particle=it->second;
00812 
00813             const MC_v3d& c=current_particle.center();
00814             const double& r=current_particle.radius();
00815             glPushMatrix();
00816             glTranslated(c[0],c[1],c[2]);
00817             glScaled(r,r,r);
00818             glCallList(id_draw_sphere);
00819             glPopMatrix();
00820 
00821             //draw forces
00822             glColor3d(0,0,1);
00823             if(is_draw_force)
00824             {
00825                 glDisable(GL_LIGHTING);
00826                 MC_segment f(c,c+0.01*current_particle.force());
00827                 MC_opengl_drawer::draw(f);
00828             }
00829 
00830         }
00831     }
00832 
00833     //draw bounding cube
00834     glColor3d(0,0,0);
00835     glDisable(GL_LIGHTING);
00836     MC_opengl_drawer::draw_grid(MC_mesh_index_vector::build_cube());
00837 
00838 
00839 
00840     //draw isosurface
00841     glEnable(GL_LIGHTING);
00842     if(is_draw_isosurface)
00843     {
00844         glEnable(GL_POLYGON_OFFSET_FILL);
00845         glPolygonOffset(2,2);
00846         glColor3d(0.5,0.5,0.8);
00847         glEnable(GL_LIGHTING);
00848         MC_opengl_drawer::draw(mesh_iso);
00849 
00850         glDisable(GL_POLYGON_OFFSET_FILL);
00851 
00852         if(is_draw_grid)
00853         {
00854             glColor3d(0.7,0.1,0.1);
00855             glDisable(GL_LIGHTING);
00856             MC_opengl_drawer::draw_grid(mesh_iso);
00857         }
00858 
00859 
00860     }
00861 
00862 
00863     //draw plane
00864     {
00865 
00866         if(is_plane==true || is_plane_cleared==false)
00867         {
00868             glDisable(GL_LIGHTING);
00869             glColor3d(0.8,0.8,0.8);
00870             glEnable(GL_POLYGON_OFFSET_FILL);
00871             glPolygonOffset(2,2);
00872             MC_opengl_drawer::draw(plane_mesh);
00873             glDisable(GL_POLYGON_OFFSET_FILL);
00874 
00875             glDisable(GL_LIGHTING);
00876             glColor3d(0.2,0.2,1);
00877             if(is_draw_grid)
00878                 MC_opengl_drawer::draw_grid(plane_mesh);
00879         }
00880     }
00881 
00882 
00883 
00884     draw_extra();
00885 
00886   return 0;
00887 }
00888 
00889 void set_light()
00890 {
00891     MC_v3d_vector axis_cam=navigator.axis_cam1();
00892 
00893     MC_v3d Light_pos=MC_v3d(navigator.current_cam1().to_matrix3().first*(axis_cam[0]))+axis_cam[1]*10;//+eye_n*(+15.0)+eye_up*10.0;
00894     //MC_v3d Light_pos=axis_cam[0]+axis_cam[1]*0.5+axis_cam[2]*0.5;
00895 
00896     //light
00897     glEnable(GL_LIGHTING);
00898     glEnable(GL_LIGHT0);
00899 
00900     float light_ambient[4]={0.4f,0.4f,0.4f,1.0f};
00901     float light_diffuse[4]={0.4f,0.4f,0.4f,1.0f};
00902     float light_specular[4]={1.6f,1.6f,1.6f,1.0f};
00903     float light_position[4]={Light_pos[0],Light_pos[1],Light_pos[2],1.0};
00904 
00905     float material_ambient [4] = {1.0f,1.0f,1.0f,1.0f};
00906     float material_diffuse [4] = {1.0f,1.0f,1.0f,1.0f};
00907     float material_specular [4] = {1.0f,1.0f,1.0f,1.0f};
00908     float material_exponent [1] = {100.0f};
00909 
00910 
00911 
00912     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
00913     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00914     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
00915     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
00916     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
00917 
00918     glEnable(GL_COLOR_MATERIAL);
00919     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);
00920     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
00921     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
00922     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, material_exponent);
00923 }
00924 
00925 
00926 
00927 
00928 
00929 
00930 void update_plane(const unsigned int& N)
00931 {
00932     //int N=20;
00933     plane_mesh=MC_mesh_index_vector::build_square(N,N);
00934     plane_mesh+=MC_v3d(-0.5,-0.5,0);
00935 
00936     MC_v3d_vector axes=navigator.axis_cam1();
00937     MC_v3d normal=axes[1];
00938     MC_v3d right=axes[2];
00939     MC_v3d up=axes[3];
00940     MC_v3d center=-navigator.tr_cam1;
00941 
00942     MC_matrix R1=MC_matrix::rotation_axis_to_axis(MC_v3d(0,0,1),normal);
00943     MC_matrix R2=MC_matrix::rotation_axis_to_axis(R1*MC_v3d(0,1,0),up);
00944 
00945     plane_mesh=R2*(R1*plane_L)*plane_mesh+center+plane_Lz*normal;
00946 }
00947 void plane_clear()
00948 {
00949     plane_L=0.5;
00950     plane_Lz=0.0;
00951     is_plane_cleared=true;
00952 }
00953 
00954 void update_plane_density()
00955 {
00956 
00957     plane_mesh.color().resize(plane_mesh.point_set().size());
00958     for(int k=0,N=plane_mesh.vertex_number();k<N;++k)
00959     {
00960 
00961         MC_v3d x=plane_mesh.point_set()(k)-MC_v3d(0.0,0.0,0);
00962 
00963         double pot=potential(x);
00964 
00965         double b=0.0;
00966         double r=0.0;
00967         if(std::fabs(pot-isovalue)/isovalue<0.2)
00968         {
00969             r=0.0;
00970             b=1.0;
00971         }
00972         else
00973         {
00974             double u=std::fabs(pot)/(isovalue);
00975             if(u>1)
00976                 u=-log(u)+1;
00977             r=pow(sin(M_PI/2.0*u),2.0);
00978         }
00979 
00980         plane_mesh.color()(k)=MC_v3d(r,0,b);
00981     }
00982 }
00983 
00984 void export_scene(unsigned int number)
00985 {
00986     std::string dir="exported_data/";
00987     std::string number_str=MC_string_converter::zero_padding(MC_string_converter::to_string(number),5);
00988 
00989     //export mesh
00990     MC_io_obj::write_obj_file(dir+"mesh_"+number_str+".obj",mesh_iso);
00991 
00992     //export sphere
00993     std::string sphere_file=dir+"sphere_"+number_str+".sph";
00994     std::ofstream file(sphere_file.c_str());
00995     const std::map<unsigned int,MC_sphere_physic>& particles=engine.particle_system();
00996     for(std::map<unsigned int,MC_sphere_physic>::const_iterator it=particles.begin(),it_end=particles.end();it!=it_end;++it)
00997         file<<it->first<<" "<<it->second.center()[0]<<" "<<it->second.center()[1]<<" "<<it->second.center()[2]<<" "<<it->second.radius()<<std::endl;
00998     file.close();
00999 }

Generated on Sun Apr 18 20:24:47 2010 by  doxygen 1.6.1