00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef SOFA_GUI_SIMPLE_SIMPLEGUI_H
00028 #define SOFA_GUI_SIMPLE_SIMPLEGUI_H
00029
00030 #include <sofa/gui/SofaGUI.h>
00031
00032 #include <sofa/helper/system/config.h>
00033 #include <sofa/defaulttype/Vec.h>
00034 #include <sofa/defaulttype/Quat.h>
00035 #include <sofa/helper/gl/Transformation.h>
00036 #include <sofa/helper/gl/Trackball.h>
00037 #include <sofa/helper/gl/Texture.h>
00038 #include <sofa/helper/gl/Capture.h>
00039 #include <sofa/helper/system/thread/CTime.h>
00040 #include <sofa/component/collision/RayPickInteractor.h>
00041 #include <sofa/helper/system/gl.h>
00042 #include <sofa/helper/system/glu.h>
00043 #include <sofa/helper/system/glut.h>
00044
00045 #include <math.h>
00046 #include <stdlib.h>
00047 #include <stdio.h>
00048 #include <string.h>
00049 #include <fstream>
00050
00051 namespace sofa
00052 {
00053
00054 namespace gui
00055 {
00056
00057 namespace glut
00058 {
00059
00060 using namespace sofa::defaulttype;
00061 using namespace sofa::helper::gl;
00062 using namespace sofa::helper::system::thread;
00063 using namespace sofa::component::collision;
00064
00065
00066 class SimpleGUI : public sofa::gui::SofaGUI
00067 {
00068
00069 public:
00070
00073
00074 SimpleGUI();
00075
00076 int mainLoop();
00077 void redraw();
00078 int closeGUI();
00079
00080 sofa::simulation::tree::GNode* currentSimulation()
00081 {
00082 return getScene();
00083 }
00084
00086
00089
00090 static int InitGUI(const char* name, const std::vector<std::string>& options);
00091 static SofaGUI* CreateGUI(const char* name, const std::vector<std::string>& options, sofa::simulation::Node* groot = NULL, const char* filename = NULL);
00092
00094
00095 protected:
00097 ~SimpleGUI();
00098
00099 public:
00100
00101
00102
00103 static SimpleGUI* instance;
00104
00105 static void glut_display();
00106 static void glut_reshape(int w, int h);
00107 static void glut_keyboard(unsigned char k, int x, int y);
00108 static void glut_mouse(int button, int state, int x, int y);
00109 static void glut_motion(int x, int y);
00110 static void glut_special(int k, int x, int y);
00111 static void glut_idle();
00112
00113 private:
00114
00115 enum {
00116 TRACKBALL_MODE = 1,
00117 PAN_MODE = 2,
00118 ZOOM_MODE = 3,
00119
00120 BTLEFT_MODE = 101,
00121 BTRIGHT_MODE = 102,
00122 BTMIDDLE_MODE = 103,
00123 };
00124
00125 enum {
00126 XY_TRANSLATION = 1,
00127 Z_TRANSLATION = 2,
00128 };
00129
00130 enum { MINMOVE = 10 };
00131
00132
00133 sofa::simulation::tree::GNode* groot;
00134 std::string sceneFileName;
00135
00136 int _W, _H;
00137 int _clearBuffer;
00138 bool _lightModelTwoSides;
00139 float _lightPosition[4];
00140 int _navigationMode;
00141 Trackball _currentTrackball;
00142 Trackball _newTrackball;
00143
00144
00145 int _mouseX, _mouseY;
00146 int _savedMouseX, _savedMouseY;
00147 bool _spinning;
00148 bool _moving;
00149 bool _video;
00150 bool _animationOBJ; int _animationOBJcounter;
00151 bool _axis;
00152 int _background;
00153 bool _shadow;
00154 bool _glshadow;
00155 float _zoomSpeed;
00156 float _panSpeed;
00157 Transformation _sceneTransform;
00158 Vector3 _previousEyePos;
00159 GLUquadricObj* _arrow;
00160 GLUquadricObj* _tube;
00161 GLUquadricObj* _sphere;
00162 GLUquadricObj* _disk;
00163 GLuint _numOBJmodels;
00164 GLuint _materialMode;
00165 GLboolean _facetNormal;
00166 float _zoom;
00167 int _renderingMode;
00168 bool _waitForRender;
00169
00170 Texture *texLogo;
00171 ctime_t _beginTime;
00172 RayPickInteractor* interactor;
00173 double lastProjectionMatrix[16];
00174 double lastModelviewMatrix[16];
00175 GLint lastViewport[4];
00176 bool sceneBBoxIsValid;
00177 Vector3 sceneMinBBox;
00178 Vector3 sceneMaxBBox;
00179 bool initTexturesDone;
00180 Capture capture;
00181 public:
00182
00183 void step();
00184 void animate();
00185 void playpause();
00186 void resetScene();
00187 void resetView();
00188 void saveView();
00189 void showVisual(bool);
00190 void showBehavior(bool);
00191 void showCollision(bool);
00192 void showBoundingCollision(bool);
00193 void showMapping(bool);
00194 void showMechanicalMapping(bool);
00195 void showForceField(bool);
00196 void showInteractionForceField(bool);
00197 void showWireFrame(bool);
00198 void showNormals(bool);
00199 void screenshot(int compression_level = -1);
00200 void exportOBJ(bool exportMTL=true);
00201 void dumpState(bool);
00202 void displayComputationTime(bool);
00203 void setExportGnuplot(bool);
00204
00205 void initializeGL();
00206 void paintGL();
00207 void resizeGL( int w, int h );
00208
00209 void keyPressEvent ( int k );
00210 void keyReleaseEvent ( int k );
00211
00212 enum EventType
00213 {
00214 MouseButtonPress, MouseMove, MouseButtonRelease
00215 };
00216 void mouseEvent ( int type, int x, int y, int bt );
00217
00218 void eventNewStep();
00219
00220 protected:
00221
00222 void calcProjection();
00223 void ApplyShadowMap();
00224 void CreateRenderTexture(GLuint& textureID, int sizeX, int sizeY, int channels, int type);
00225 void StoreLightMatrices();
00226
00227 public:
00228 void setScene(sofa::simulation::Node* scene, const char* filename=NULL);
00229 sofa::simulation::tree::GNode* getScene()
00230 {
00231 return groot;
00232 }
00233 const std::string& getSceneFileName()
00234 {
00235 return sceneFileName;
00236 }
00237 void SwitchToPresetView();
00238 int GetWidth()
00239 {
00240 return _W;
00241 };
00242 int GetHeight()
00243 {
00244 return _H;
00245 };
00246
00247 void UpdateOBJ(void);
00248
00250
00252
00253 bool _mouseInteractorTranslationMode;
00254 bool _mouseInteractorRotationMode;
00255 bool _mouseInteractorMoving;
00256 int _mouseInteractorSavedPosX;
00257 int _mouseInteractorSavedPosY;
00258 int _translationMode;
00259 Quaternion _mouseInteractorCurrentQuat;
00260 Vector3 _mouseInteractorAbsolutePosition;
00261 Trackball _mouseInteractorTrackball;
00262 void ApplyMouseInteractorTransformation(int x, int y);
00263
00264 Quaternion _mouseInteractorNewQuat;
00265 Vector3 _mouseInteractorRelativePosition;
00266 Quaternion _newQuat;
00267 Quaternion _currentQuat;
00268 bool _mouseTrans;
00269 bool _mouseRotate;
00270
00271 private:
00272
00273 void InitGFX(void);
00274 void PrintString(void* font, char* string);
00275 void Display3DText(float x, float y, float z, char* string);
00276 void DrawAxis(double xpos, double ypos, double zpos, double arrowSize);
00277 void DrawBox(double* minBBox, double* maxBBox, double r=0.0);
00278 void DrawXYPlane(double zo, double xmin, double xmax, double ymin,
00279 double ymax, double step);
00280 void DrawYZPlane(double xo, double ymin, double ymax, double zmin,
00281 double zmax, double step);
00282 void DrawXZPlane(double yo, double xmin, double xmax, double zmin,
00283 double zmax, double step);
00284 void CreateOBJmodelDisplayList(int material_mode);
00285
00286
00287 void DrawLogo(void);
00288 void DisplayOBJs(bool shadowPass = false);
00289 void DisplayMenu(void);
00290 void DrawScene();
00291 void DrawAutomate();
00292 void ApplySceneTransformation(int x, int y);
00293
00294
00295 protected:
00296 bool isControlPressed() const;
00297 bool isShiftPressed() const;
00298 bool isAltPressed() const;
00299 bool m_isControlPressed;
00300 bool m_isShiftPressed;
00301 bool m_isAltPressed;
00302 void updateModifiers();
00303 bool m_dumpState;
00304 bool m_displayComputationTime;
00305 bool m_exportGnuplot;
00306 std::ofstream* m_dumpStateStream;
00307 };
00308
00309 }
00310
00311 }
00312
00313 }
00314
00315 #endif
00316
00317