QtViewer.h
Go to the documentation of this file.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_QT_QTVIEWER_H
00028 #define SOFA_GUI_QT_QTVIEWER_H
00029
00030 #include <sofa/helper/system/config.h>
00031 #include <sofa/helper/system/gl.h>
00032 #include <qgl.h>
00033 #include <qtimer.h>
00034 #include <math.h>
00035 #include <stdlib.h>
00036 #include <stdio.h>
00037 #include <string.h>
00038 #include <fstream>
00039
00040
00041 #include <viewer/SofaViewer.h>
00042 #include <sofa/defaulttype/Vec.h>
00043 #include <sofa/defaulttype/Quat.h>
00044 #include <sofa/helper/gl/Transformation.h>
00045 #include <sofa/helper/gl/Trackball.h>
00046 #include <sofa/helper/gl/Texture.h>
00047 #include <sofa/helper/system/thread/CTime.h>
00048 #include <sofa/simulation/tree/xml/Element.h>
00049
00050 namespace sofa
00051 {
00052
00053 namespace gui
00054 {
00055
00056 namespace qt
00057 {
00058
00059 namespace viewer
00060 {
00061
00062 namespace qt
00063 {
00064
00065
00066 using sofa::defaulttype::Vector3;
00067 using sofa::defaulttype::Quaternion;
00068 using namespace sofa::helper::gl;
00069 using namespace sofa::helper::system::thread;
00070 using namespace sofa::component::collision;
00071
00072
00073 class QtViewer :public QGLWidget, public sofa::gui::qt::viewer::SofaViewer
00074 {
00075 Q_OBJECT
00076
00077 private:
00078
00079 enum {
00080 TRACKBALL_MODE = 1,
00081 PAN_MODE = 2,
00082 ZOOM_MODE = 3,
00083 };
00084
00085 enum {
00086 XY_TRANSLATION = 1,
00087 Z_TRANSLATION = 2,
00088 };
00089
00090 enum { MINMOVE = 10 };
00091
00092
00093 QTimer* timerAnimate;
00094 int _W, _H;
00095 int _clearBuffer;
00096 bool _lightModelTwoSides;
00097 float _lightPosition[4];
00098
00099 Trackball _currentTrackball;
00100 Trackball _newTrackball;
00101
00102
00103 int _mouseX, _mouseY;
00104 int _savedMouseX, _savedMouseY;
00105 bool _spinning;
00106 bool _moving;
00107
00108 float _zoomSpeed;
00109 float _panSpeed;
00110 Transformation _sceneTransform;
00111 Vector3 _previousEyePos;
00112 GLUquadricObj* _arrow;
00113 GLUquadricObj* _tube;
00114 GLUquadricObj* _sphere;
00115 GLUquadricObj* _disk;
00116 GLuint _numOBJmodels;
00117 GLuint _materialMode;
00118 GLboolean _facetNormal;
00119 float _zoom;
00120 int _renderingMode;
00121
00122 bool _waitForRender;
00123
00124
00125
00126 Texture *texLogo;
00127
00128
00129 ctime_t _beginTime;
00130
00131 double lastProjectionMatrix[16];
00132 double lastModelviewMatrix[16];
00133 GLint lastViewport[4];
00134 Vector3 sceneMinBBox;
00135 Vector3 sceneMaxBBox;
00136 public:
00137
00141 static int EnableViewer();
00142
00146 static int DisableViewer();
00147
00148 QtViewer( QWidget* parent, const char* name="" );
00149 ~QtViewer();
00150
00151 QWidget* getQWidget() { return this; }
00152
00153 bool ready(){return _waitForRender;};
00154 void wait(){_waitForRender = true;};
00155
00156 public slots:
00157 void resetView();
00158 virtual void saveView();
00159 virtual void setSizeW(int);
00160 virtual void setSizeH(int);
00161
00162 signals:
00163 void redrawn();
00164 void resizeW( int );
00165 void resizeH( int );
00166
00167
00168
00169 protected:
00170
00171 void calcProjection();
00172 void initializeGL();
00173 void paintGL();
00174 void resizeGL( int w, int h );
00175 void ApplyShadowMap();
00176 void CreateRenderTexture(GLuint& textureID, int sizeX, int sizeY, int channels, int type);
00177 void StoreLightMatrices();
00178
00179 public:
00180 void setScene(sofa::simulation::tree::GNode* scene, const char* filename=NULL, bool keepParams=false);
00181 sofa::simulation::tree::GNode* getScene()
00182 {
00183 return groot;
00184 }
00185
00186
00187
00188 int GetWidth()
00189 {
00190 return _W;
00191 };
00192 int GetHeight()
00193 {
00194 return _H;
00195 };
00196
00197 void UpdateOBJ(void);
00198 void moveRayPickInteractor(int eventX, int eventY);
00200
00202
00203 bool _mouseInteractorTranslationMode;
00204 bool _mouseInteractorRotationMode;
00205 int _translationMode;
00206 Quaternion _mouseInteractorCurrentQuat;
00207 Vector3 _mouseInteractorAbsolutePosition;
00208 Trackball _mouseInteractorTrackball;
00209 void ApplyMouseInteractorTransformation(int x, int y);
00210
00211 static Quaternion _mouseInteractorNewQuat;
00212 static Vector3 _mouseInteractorRelativePosition;
00213 static Quaternion _newQuat;
00214 static Quaternion _currentQuat;
00215 static bool _mouseTrans;
00216 static bool _mouseRotate;
00217
00218
00219 QString helpString();
00220 private:
00221
00222 void InitGFX(void);
00223 void PrintString(void* font, char* string);
00224 void Display3DText(float x, float y, float z, char* string);
00225 void DrawAxis(double xpos, double ypos, double zpos, double arrowSize);
00226 void DrawBox(SReal* minBBox, SReal* maxBBox, SReal r=0.0);
00227 void DrawXYPlane(double zo, double xmin, double xmax, double ymin,
00228 double ymax, double step);
00229 void DrawYZPlane(double xo, double ymin, double ymax, double zmin,
00230 double zmax, double step);
00231 void DrawXZPlane(double yo, double xmin, double xmax, double zmin,
00232 double zmax, double step);
00233 void CreateOBJmodelDisplayList(int material_mode);
00234
00235
00236 void DrawLogo(void);
00237 void DisplayOBJs(bool shadowPass = false);
00238 void DisplayMenu(void);
00239 void DrawScene();
00240
00241
00242 void ApplySceneTransformation(int x, int y);
00243
00244
00245 protected:
00246
00247
00248 virtual void keyPressEvent ( QKeyEvent * e );
00249 virtual void keyReleaseEvent ( QKeyEvent * e );
00250 virtual void mousePressEvent ( QMouseEvent * e );
00251 virtual void mouseReleaseEvent ( QMouseEvent * e );
00252 virtual void mouseMoveEvent ( QMouseEvent * e );
00253 virtual void wheelEvent ( QWheelEvent* e);
00254 virtual void mouseEvent ( QMouseEvent * e );
00255 };
00256
00257 }
00258
00259 }
00260
00261 }
00262
00263 }
00264
00265 }
00266
00267 #endif
00268
00269