QtOgreViewer.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 __sofaOgreWidget_H__
00028 #define __sofaOgreWidget_H__
00029 
00030 #include <stdlib.h>
00031 
00032 #include <viewer/qtogre/DotSceneLoader.h>
00033 
00034 #include <viewer/SofaViewer.h>
00035 #include <sofa/helper/gl/Capture.h>
00036 #include <sofa/helper/system/FileRepository.h>
00037 #include <sofa/helper/system/SetDirectory.h>
00038 #include <sofa/core/ObjectFactory.h>
00039 #include <sofa/simulation/tree/Simulation.h>
00040 
00041 
00042 #ifdef SOFA_QT4
00043 #include <QPaintEvent>
00044 #include <QWidget>
00045 #else
00046 #include <qlayout.h>
00047 #endif
00048 
00049 #include <Ogre.h>
00050 
00051 
00052 
00053 using namespace sofa::helper::system::thread;
00054 using namespace sofa::simulation::tree;
00055 
00056 #if defined(SOFA_GPU_CUDA)
00057 #include <sofa/gpu/cuda/mycuda.h>
00058 using namespace sofa::gpu::cuda;
00059 #endif
00060 
00061 
00062 #if OGRE_PLATFORM == OGRE_PLATFORM_APPLE
00063 std::string macBundlePath();
00064 #endif
00065 
00066 
00067 //*********************************************************************************//
00068 // Widget with Ogre embedded
00069 //*********************************************************************************//
00070 
00071 namespace sofa 
00072 {
00073 
00074   namespace gui
00075   {  
00076 
00077     namespace qt
00078     {
00079 
00080       namespace viewer
00081       {
00082 
00083     namespace qtogre
00084     {
00085 
00086       class QtOgreViewer : public QWidget, public sofa::gui::qt::viewer::SofaViewer
00087         {
00088           Q_OBJECT
00089         public:
00090 
00091           std::string sceneName;
00092 
00093 
00097           static int EnableViewer();
00098 
00102           static int DisableViewer();
00103 
00104           QtOgreViewer( QWidget *parent=0, const char *name=0 );
00105           ~QtOgreViewer()
00106         {
00107 
00108           if(mRoot != NULL){
00109             delete mRoot;
00110             mRoot = NULL;
00111           }
00112         };
00113 
00114           QWidget* getQWidget() { return this; }
00115 
00116           bool ready(){return _waitForRender;};
00117           void wait(){_waitForRender = true;};/*
00118                             virtual void update(void);*/
00119           void showEntireScene(void);
00120 
00121 
00122           void setScene(sofa::simulation::tree::GNode* scene, const char* filename, bool keepParams=false);
00123 
00124           void setup(void);
00125           void setupView(void);          //Creation of the first window of visualization
00126           QString helpString();
00127 
00128           void moveRayPickInteractor(int eventX, int eventY);
00129           
00130         private:
00131                 
00132           Ogre::String mResourcePath;
00133           //------------------------------------------
00134           //Setup
00135           Ogre::RenderWindow* mRenderWindow;
00136           Ogre::SceneManager* mSceneMgr;
00137           //  SofaListener* mFrameListener;
00138           Ogre::Camera* mCamera;
00139           Ogre::Viewport* mVp;
00140           Ogre::Root* mRoot;
00141           Ogre::ShadowTechnique shadow;
00143           bool _mouseInteractorMoving;
00144           int _mouseInteractorSavedPosX;
00145           int _mouseInteractorSavedPosY;
00146           unsigned int number_visualModels;
00147           //sofa::helper::gl::Capture capture;
00148 
00149           void setupResources(void);     //Ogre Initialization 
00150 
00151           void setupConfiguration(void); //Graphic configuration
00152 
00153 
00154           void loadResources(void)       //Launch the Resource Manager
00155           {
00156         // Initialise, parse scripts etc
00157         Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
00158           }
00159 
00160           void createScene(void);        
00161 
00162           //******************************Configuration Panel for Ogre***********************************
00163           virtual bool configure(void)
00164           {
00165         // Show the configuration dialog and initialise the system
00166         // You can skip this and use root.restoreConfig() to load configuration
00167         // settings if you were sure there are valid ones saved in ogre.cfg
00168         std::cerr<<"Show Dialog " << mRoot<<"\n";
00169         if(mRoot->showConfigDialog())
00170           {
00171             // Custom option - to use PlaneOptimalShadowCameraSetup we must have
00172             // double-precision. Thus, set the D3D floating point mode if present, 
00173             // no matter what was chosen
00174             Ogre::ConfigOptionMap& optMap = mRoot->getRenderSystem()->getConfigOptions();
00175             Ogre::ConfigOptionMap::iterator i = optMap.find("Floating-point mode");
00176             if (i != optMap.end())
00177               {
00178             if (i->second.currentValue != "Consistent")
00179               {
00180                 i->second.currentValue = "Consistent";
00181                 Ogre::LogManager::getSingleton().logMessage("ExampleApplication: overriding "
00182                                     "D3D floating point mode to 'Consistent' to ensure precision "
00183                                     "for numerical computations");
00184               }
00185               }
00186             // If returned true, user clicked OK so initialise
00187 
00188             mRoot->initialise(false, "SOFA - OGRE");
00189             return true;
00190           }
00191         else
00192           {
00193             return false;
00194           }
00195           }
00196 
00197           void update();
00198 
00199         protected:
00200 
00201           ctime_t _beginTime;
00202 
00203           bool m_mouseLeftPressed;
00204           bool m_mouseRightPressed;
00205           bool m_mouseMiddlePressed;
00206           QPoint m_mousePressPos;
00207           QPoint m_mousePos;
00208           bool m_background;
00209           bool pickDone;
00210 
00211           Ogre::Vector3 m_mTranslateVector;
00212           Ogre::Radian m_mRotX, m_mRotY;
00213           Ogre::Real m_mMoveSpeed;
00214           Ogre::Degree m_mRotateSpeed;
00215           Ogre::SceneNode* zeroNode;
00216           Ogre::SceneNode*  camNode;
00217           bool _waitForRender;
00218 
00219           //Initial Bounding Box
00220           Ogre::AxisAlignedBox world_BB;
00221 
00222           void resize()
00223           {
00224         if (mRenderWindow != NULL)
00225           {
00226             emit(resizeW(width())); 
00227             emit(resizeH(height())); 
00228             mRenderWindow->windowMovedOrResized();
00229             mRenderWindow->resize(width(), height());     
00230             mVp->setDimensions(0,0, 1.0, 1.0);
00231             mCamera->setAspectRatio(Ogre::Real(mVp->getActualWidth()) / Ogre::Real(mVp->getActualHeight()));
00232             update();
00233           }
00234           }
00235 
00236           bool updateInteractor( QMouseEvent * e ); 
00237                 
00238           virtual void paintEvent(QPaintEvent*);                
00239           virtual void resizeEvent(QResizeEvent*);          
00240           virtual void timerEvent(QTimerEvent * event){Q_UNUSED(event);update();}
00241 
00242           virtual void keyPressEvent ( QKeyEvent * e );
00243           virtual void mousePressEvent(QMouseEvent* evt);
00244           virtual void mouseReleaseEvent(QMouseEvent* evt);
00245           virtual void mouseMoveEvent(QMouseEvent* evt);
00246           virtual void wheelEvent(QWheelEvent* evt); 
00247             
00248           void moveCamera(void)
00249           {
00250 
00251         mCamera->yaw(m_mRotX);
00252         mCamera->pitch(m_mRotY);
00253 
00254         //Reset to zero
00255         m_mRotX = m_mRotY = Ogre::Degree(0);
00256         m_mTranslateVector = Ogre::Vector3::ZERO;
00257           }
00258 
00259 
00260           public slots:       
00261           virtual void resetView();
00262           virtual void saveView();  
00263           virtual void setSizeW(int); 
00264           virtual void setSizeH(int);
00265 
00266         signals:
00267           void redrawn(); 
00268           void resizeW( int ); 
00269           void resizeH( int ); 
00270 
00271 
00272         };
00273     } //qtogre
00274       } //viewer
00275     } //qt
00276   } //gui
00277 } //sofa
00278 
00279 
00280 #endif
00281 

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