00001
00002
00003
00004
00009 #include <stdlib.h>
00010 #include <iostream.h>
00011 #include <Inventor/SoDB.h>
00012 #include <Inventor/Xt/SoXt.h>
00013 #include <Inventor/Xt/viewers/SoXtExaminerViewer.h>
00014 #include <Inventor/nodes/SoSeparator.h>
00015 #include <Inventor/nodes/SoTransform.h>
00016 #include <Inventor/nodes/SoDrawStyle.h>
00017 #include <Inventor/nodes/SoEventCallback.h>
00018 #include <Inventor/nodes/SoCoordinate3.h>
00019 #include <Inventor/nodes/SoTextureCoordinate2.h>
00020 #include <Inventor/nodes/SoTexture2.h>
00021 #include <Inventor/nodes/SoQuadMesh.h>
00022 #include <Inventor/events/SoKeyboardEvent.h>
00023 #include <Inventor/events/SoMouseButtonEvent.h>
00024 #include <Inventor/events/SoLocation2Event.h>
00025 #include <Inventor/sensors/SoTimerSensor.h>
00026 #include "ivutils.h"
00027 #include "argumentParser.hpp"
00028 #include "dynamique/dynamique.hpp"
00029
00030 SoXtExaminerViewer *myViewer;
00031 SoSeparator* root;
00032 SoDrawStyle* drawStyle;
00033 SoTimerSensor* animClock;
00034
00035 float t=0;
00036 float dt = 0.01;
00037
00038 SbVec3f gravity(0,-10,0);
00039 int boule_fixe;
00040 SbVec3f pointAttache(0,0,0);
00041 SbVec3f pos_init(0,2,0);
00042 SbVec3f vit_init(1,0,0);
00043 double raideur_ressort = 1;
00044 double longueur_ressort = 0;
00045 double facteur_amortissement_ressort = 0.1;
00046
00047
00048
00049
00050 void creeBoules()
00051 {
00052 dynamique::init();
00053 dynamique::setGravity(gravity);
00054
00055 boule_fixe = dynamique::ajouteBoule( root, pointAttache, SbVec3f(0,0,0), 0.0, 0.1, SbVec3f(0,1,0) );
00056
00057 int boule = dynamique::ajouteBoule( root, pos_init, vit_init, 1.0, 1.0, SbVec3f(1,0,0) );
00058
00059 }
00060
00061
00062
00063 void
00064 update(void *, SoSensor *)
00065 {
00066 dynamique::anime(dt);
00067 t += dt;
00068 }
00069
00070
00071
00072 void
00073 myKeyPressCB(void *, SoEventCallback *eventCB)
00074 {
00075 const SoEvent *event = eventCB->getEvent();
00076
00077 if (SoKeyboardEvent::isKeyPressEvent (event,SoKeyboardEvent::W))
00078
00079 {
00080 static int mode = 0;
00081 if(mode==0){
00082 drawStyle->style = SoDrawStyleElement::LINES;
00083 mode=1;}
00084 else if(mode==1){
00085 drawStyle->style = SoDrawStyleElement::POINTS;
00086 mode=2;}
00087 else {
00088 drawStyle->style = SoDrawStyleElement::FILLED;
00089 mode=0;}
00090 eventCB->setHandled();
00091 }
00092 else if (SO_KEY_PRESS_EVENT(event, S))
00093
00094 {
00095 printf("Sauvegarde dans le fichier scene.iv\n");
00096 iv::writeFile(root,"scene.iv");
00097 eventCB->setHandled();
00098 }
00099 else if (SO_KEY_PRESS_EVENT(event, SPACE))
00100
00101 {
00102
00103 if( event->wasShiftDown() ) dt = dt<0 ? dt : -dt;
00104 else dt = dt>0 ? dt : -dt;
00105
00106 update(0,0);
00107 }
00108 else if (SO_KEY_PRESS_EVENT(event, R))
00109
00110 {
00111 t = 0;
00112 eventCB->setHandled();
00113 }
00114 }
00115
00116
00117
00118 bool button1=false;
00119 bool button2=false;
00120 bool button3=false;
00121
00122 void
00123 myMousePressCB(void *, SoEventCallback *eventCB)
00124 {
00125 const SoEvent *event = eventCB->getEvent();
00126 static bool running = false;
00127
00128 if ( SoMouseButtonEvent::isButtonPressEvent(event,SoMouseButtonEvent:: BUTTON1) ){
00129 button1 = true;
00130 running = !running;
00131 if( running ) animClock->schedule();
00132 else animClock->unschedule();
00133 eventCB->setHandled();
00134 }
00135 else if ( SoMouseButtonEvent::isButtonReleaseEvent(event,SoMouseButtonEvent:: BUTTON1) ){
00136 button1 = false;
00137 }
00138
00139 else if ( SoMouseButtonEvent::isButtonPressEvent(event,SoMouseButtonEvent:: BUTTON2) ){
00140 button2 = true;
00141 }
00142 else if ( SoMouseButtonEvent::isButtonReleaseEvent(event,SoMouseButtonEvent:: BUTTON2) ){
00143 button2 = false;
00144 }
00145
00146 else if ( SoMouseButtonEvent::isButtonPressEvent(event,SoMouseButtonEvent:: BUTTON3) ){
00147 button3 = true;
00148 }
00149 else if ( SoMouseButtonEvent::isButtonReleaseEvent(event,SoMouseButtonEvent:: BUTTON3) ){
00150 button3 = false;
00151 }
00152 }
00153
00154
00155
00156 void
00157 myMouseMoveCB(void *, SoEventCallback *eventCB)
00158 {
00159 const SoEvent *event = eventCB->getEvent();
00160
00161 if(button2){
00162 SbVec2f location = event->getNormalizedPosition( myViewer->getViewportRegion() );
00163 std::cout<<"souris en "<<location[0]<<", "<<location[1]<<endl;
00164 }
00165
00166 }
00167
00168
00169
00170
00171
00172 main(int argc, char **argv)
00173 {
00174
00175
00176
00177 animal::ArgumentParser("Ce programme implémente une simulation physique.\nLes commandes interactives sont les suivantes:\nbouton gauche: démarre/arrèter\ntouche W: mode d'affichage\nTouche S: sauvegarde de la scene dans le fichier scene.iv\nTouche Espace: avancer d'un pas de temps\nShift+Espace: reculer d'un pas de temps\nTouche R: revenir à la position de départ")
00178 .option(&dt,'t',"dt","pas de temps")
00179 .option(&pointAttache,'p',"point_attache","point d'attache de la boule")
00180 .option(&pos_init,'x',"position_initiale","positions initiale de la boule")
00181 .option(&raideur_ressort,'k',"raideur","raideur du(des) ressort(s)")
00182 .option(&longueur_ressort,'l',"longueur","longueur au repos du(des) ressort(s)")
00183 .option(&facteur_amortissement_ressort,'a',"amortissement_ressort","facteur d'amortissement du(des) ressort(s)")
00184 (argc,argv);
00185
00186
00187 Widget myWindow = SoXt::init(argv[0]);
00188 if (myWindow == NULL) exit(1);
00189
00190
00191
00192
00193 root = new SoSeparator;
00194 root->ref();
00195
00196
00197 SoSeparator* repere = iv::readFile("frame.iv");
00198 root->addChild( repere );
00199
00200
00201 drawStyle = new SoDrawStyle;
00202 root->addChild( drawStyle );
00203
00204
00205 creeBoules();
00206
00207
00208
00209
00210 SoEventCallback *myEventCB = new SoEventCallback;
00211 myEventCB->addEventCallback(
00212 SoKeyboardEvent::getClassTypeId(),
00213 myKeyPressCB, NULL);
00214 myEventCB->addEventCallback(
00215 SoMouseButtonEvent::getClassTypeId(),
00216 myMousePressCB, NULL);
00217 myEventCB->addEventCallback(
00218 SoLocation2Event::getClassTypeId(),
00219 myMouseMoveCB, NULL);
00220 root->addChild(myEventCB);
00221
00222
00223 animClock = new SoTimerSensor( update, NULL );
00224 animClock->setInterval( dt );
00225
00226
00227 myViewer = new SoXtExaminerViewer(myWindow);
00228 myViewer->setSceneGraph(root);
00229 myViewer->setTitle("Dynamique");
00230 myViewer->show();
00231 myViewer->viewAll();
00232
00233
00234 SoXt::show(myWindow);
00235 SoXt::mainLoop();
00236 }
00237
00238