QtViewer.h

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This program is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU General Public License as published by the Free  *
00007 * Software Foundation; either version 2 of the License, or (at your option)   *
00008 * any later version.                                                          *
00009 *                                                                             *
00010 * This program is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    *
00013 * more details.                                                               *
00014 *                                                                             *
00015 * You should have received a copy of the GNU General Public License along     *
00016 * with this program; if not, write to the Free Software Foundation, Inc., 51  *
00017 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.                   *
00018 *******************************************************************************
00019 *                            SOFA :: Applications                             *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
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       //using namespace sofa::defaulttype;
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           // Interaction
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           //    Quaternion      _currentQuat;
00102           //    Quaternion      _newQuat;
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           //GLuint          _logoTexture;
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           //void            reshape(int width, int height);
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           // Interaction //
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           //int     loadBMP(char *filename, TextureImage *texture);
00235           //void    LoadGLTexture(char *Filename);
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           //int     handle(int event);  // required by FLTK
00244 
00245         protected:
00246           //virtual bool event ( QEvent * e );
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     } // namespace qt
00258   
00259       } // namespace viewer
00260 
00261     } //namespace qt  
00262 
00263   } // namespace gui
00264 
00265 } // namespace sofa
00266 
00267 #endif
00268 
00269 

Generated on Sun Nov 23 02:28:37 2008 for Sofa by  doxygen 1.5.7.1