Moteur.cpp

Aller à la documentation de ce fichier.
00001 /*
00002  * Moteur.cpp : Animation d'une scene 3D
00003  * Copyright (C) 2007 Florence Zara, LIRIS
00004  *               florence.zara@liris.univ-lyon1.fr
00005  *               http://liris.cnrs.fr/florence.zara/
00006  *
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License as published
00010  * by the Free Software Foundation; either version 2.1 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  */
00022 
00023 
00032 #include <sstream>
00033 #include <string.h>
00034 #include <iostream>
00035 
00036 #ifdef WIN32
00037 #include <string.h>
00038 #else
00039 #include <strings.h>
00040 #endif
00041 
00042 
00043 
00045  /* Sous MAC */
00046 #include <GLUT/glut.h>  
00047 
00048 /* Sous Linux */
00049 // #include <GL/glut.h> 
00050 
00052 #include "trackball.h"
00053 
00055 #include "Navigation.h"
00056 
00058 #include "Noeuds.h"
00059 #include "Scene.h"
00060 #include "Mesh.h"
00061 #include "ObjetSimule.h"
00062 
00063 
00065 int trackballBeginX, trackballBeginY;
00066 
00067 GLuint trackballWidth, trackballHeight;
00068 GLint trackballButton = GLUT_LEFT_BUTTON;
00069 bool trackballTracking = false;
00070 
00071 GLuint trackballLastTime;
00072 float currentQuaternion[4];
00073 float newQuaternion[4];
00074 
00075 float scale_factor = 1.0;
00076 GLint xGL, yGL;
00077 
00078 eShading shadingMode = SHADING_SMOOTH;
00079 
00080 bool normalDisplaying = false;
00081 
00082 
00083 
00085 Scene *S; 
00086 
00088 int _NbObj;
00089 
00091 std::string _Affichage = "fil";
00092 
00094 int Tps = 0;    
00095 
00096 
00097 
00098 
00102 void display(void)              
00103 {
00104         /* clear the display */
00105         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00106 
00107         glPushMatrix();
00108         GLfloat m[4][4];
00109     build_rotmatrix(m, currentQuaternion);
00110     glMultMatrixf(&m[0][0]);
00111         glScalef(scale_factor, scale_factor, scale_factor);
00112 
00113         glDisable(GL_LIGHTING);
00114         // glDisable(GL_DEPTH_TEST);
00115         
00117     glLineWidth (2);
00118     glColor3f(1.0, 0.0, 0.0);
00119     glBegin(GL_LINES);
00120     glVertex3f(0.0,0.0,0.0);
00121     glVertex3f(1.0,0.0,0.0);
00122     glEnd();
00123         
00124     glLineWidth (2);
00125     glColor3f(0.0, 1.0, 0.0);
00126     glBegin(GL_LINES);
00127     glVertex3f(0.0,0.0,0.0);
00128     glVertex3f(0.0,1.0,0.0);
00129     glEnd();
00130         
00131     glLineWidth (2);
00132     glColor3f(0.0, 0.0, 1.0);
00133     glBegin(GL_LINES);
00134     glVertex3f(0.0,0.0,0.0);
00135     glVertex3f(0.0,0.0,1.0);
00136     glEnd();
00137         
00139 
00140         // Affiche toutes les faces en mode ligne
00141         if (_Affichage == "fil")
00142                 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
00143         
00144         // Affiche les faces arrieres remplies
00145         else if (_Affichage == "remplissage")
00146                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00147         
00148         // Plaquage de la texture 
00149         else if (_Affichage == "texture")
00150                 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 
00151         
00152         
00153         // Affichage des objets de la scene
00154         //      std::cout << "Affichage ...." << std::endl;
00155         S->affiche();
00156         
00157         glutSwapBuffers() ;
00158         glPopMatrix() ;
00159         glFlush() ;
00160                 
00161 }
00162 
00163 
00167 void idle(void) 
00168 {
00169         //std::cout << "Simulation continue ...." << std::endl;
00170         S->Simulation(Tps);
00171         Tps = Tps + 1;
00172         
00173         glutPostRedisplay();
00174 }
00175 
00176 
00180 int main(int argc, char *argv[])
00181 {
00182 
00188 
00189         std::string *Fichier_Param;
00190         
00192         if (argc >= 2)
00193     {
00194                 /* Nombre d objets a simuler */
00195                 _NbObj = atoi(argv[1]);
00196                 std::cout << "Nombre d objets dans la scene : " << _NbObj << std::endl;
00197                 
00198                 /* Tableau contenant les noms des fichiers de parametres */
00199                 // Element 0 correspond au fichier contenant les parametres generaux de l animation
00200                 // Element i correspond au fichier contenant les parametres de l objet i
00201                 Fichier_Param = new std::string[_NbObj+1];
00202                 
00203                 // Recupere les noms des fichiers de donnees : 
00204                 // arv[0] : executable, argv[1] : nb objet
00205                 // Fichier_Param[0] de la simulation correspond a argv[2]
00206                 for (int i=0; i<= _NbObj; i++)
00207                         Fichier_Param[i] = argv[i+2]; 
00208         }
00209         
00210         else 
00211     {
00212                 /* Usage de l execution du programme */
00213                 std::cout << "Usage : ObjetDeformable NbObj <Fichier_Param_Anim> <Fichier_Param_Obj1> <Fichier_Param_Obj2> ..." << std::endl; 
00214                 
00215                 /* Arret du programme */
00216                 exit(1);
00217                 
00218     }
00219         
00221         S = new Scene(Fichier_Param[0]);
00222         
00224         for (int i=1; i<= _NbObj; i++)
00225                 S->attache(new ObjetSimule(Fichier_Param[i]));    
00226 
00227                 
00229         // Initialisation pour l'animation
00230         S->init();
00231         
00232 
00234         std::cout << "Simulation Begin...." << std::endl;
00235         S->Simulation(Tps);
00236 
00237                 
00242         int win;
00243         
00244         /* initialize GLUT system */
00245         glutInit(&argc, argv);          
00246         
00247         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
00248         
00249         /* width=400pixels height=500pixels */
00250         glutInitWindowSize(400,500);                    
00251         
00252         /* create window */
00253         win = glutCreateWindow("Animation 3D"); 
00254         
00255         /* from this point on the current window is win */
00256         /* set background to black */
00257         glClearColor(0.0,0.0,0.0,0.0);  
00258         
00259         /* how object is mapped to window */
00260         gluOrtho2D(0,400,0,500);                
00261         
00262         /* set window's display callback */
00263         glutDisplayFunc(display);       
00264                 
00265         /* set window's key callback */
00266         glutKeyboardFunc(keyboard);
00267         glutReshapeFunc(reshape);
00268         glutMotionFunc(motion);
00269         glutMouseFunc(mouse);
00270         glutVisibilityFunc(visible);
00271         
00272         glutCreateMenu(menu);
00273         glutAddMenuEntry("Menu", 0);
00274         glutAddMenuEntry("", 0);
00275         glutAddMenuEntry("[f]   Toggle fullscreen on/off", 'f');
00276         glutAddMenuEntry("[z]   Zoom out", 'z');
00277         glutAddMenuEntry("[Z]   Zoom in", 'Z');
00278         glutAddMenuEntry("[s]   switch shading mode", 's');
00279         glutAddMenuEntry("[n]   normal displaying", 'n');
00280         glutAddMenuEntry("[Esc] Quit", 27);
00281         glutAttachMenu(GLUT_RIGHT_BUTTON);
00282         
00283         trackball(currentQuaternion, 0.0, 0.0, 0.0, 0.0);
00284         
00285     /* start processing events... */
00286         glutMainLoop();                 
00287         
00288         /* execution never reaches this point */
00289         return 0;
00290 }

Généré le Thu Jan 24 19:11:42 2008 pour Animation 3D par  doxygen 1.5.1