OMKOgreVis.cpp

Go to the documentation of this file.
00001 /************************************************************************/
00002 /* This file is part of openMask(c) INRIA, CNRS, Universite de Rennes 1 */
00003 /* 1993-2002, thereinafter the Software                                 */
00004 /*                                                                      */
00005 /* The Software has been developped within the Siames Project.          */
00006 /* INRIA, the University of Rennes 1 and CNRS jointly hold intellectual */
00007 /* property rights                                                      */
00008 /*                                                                      */
00009 /* The Software has been registered with the Agence pour la Protection  */
00010 /* des Programmes (APP) under registration number                       */
00011 /* IDDN.FR.001.510008.00.S.P.2001.000.41200                             */
00012 /*                                                                      */
00013 /* This file may be distributed under the terms of the Q Public License */
00014 /* version 1.0 as defined by Trolltech AS of Norway and appearing in    */
00015 /* the file LICENSE.QPL included in the packaging of this file.         */
00016 /*                                                                      */
00017 /* Licensees holding valid specific licenses issued by INRIA, CNRS or   */
00018 /* Universite Rennes 1 for the software may use this file in            */
00019 /* acordance with that specific license                                 */
00020 /************************************************************************/
00021 #include "OMKOgreVis.h"
00022 #include "OMKUniqueConfigurationParameter.h"
00023 #include "dotSceneInterface.h"
00024 #include "OgreWindowEventUtilities.h"
00025 #include "OBTSingleton.h"
00026 
00027 using namespace OBT ;
00028 using namespace OMK ;
00029 using namespace OMK::Vis ;
00030 using namespace Ogre ;
00031 
00034 class LogOutput : public TracerOutput
00035 {
00036 public:
00037         LogOutput() {}
00038         virtual ~LogOutput() {}
00040         virtual void trace( const std::string& msg ) 
00041         { 
00042                 Ogre::LogManager::getSingleton().logMessage( msg ) ; \
00043         }
00044 } ;
00045 
00046 
00047 REGISTER_OBJECT_FACTORY( OgreVis, "OgreVis" ) ;
00048 
00049 //-------------------------------------------------------------------------
00050 // constructor
00051 //-------------------------------------------------------------------------
00052 OgreVis::OgreVis( OMK::Controller& ctrl, const ObjectDescriptor& objectDescriptor )
00053 :
00054 VisBase( ctrl, objectDescriptor ),
00055 _root( NULL )
00056 {
00057 }
00058 
00059 //-------------------------------------------------------------------------
00060 // destructor
00061 //-------------------------------------------------------------------------
00062 OgreVis::~OgreVis( void )
00063 {
00064 #if !defined NDEBUG 
00065         // reset the trace output to the console
00066         OBT::Singleton<OBT::Tracer>::getInstance().setOutput( 0, false ) ;
00067 #endif
00068 
00069         // destroy camera, scene manager and root
00070         if( _root ) 
00071         {
00072                 Ogre::SceneManager* sceneManager( _root->getSceneManager( "sceneManager" ) ) ;
00073                 if( sceneManager ) 
00074                 {
00075                         _root->destroySceneManager( sceneManager ) ;
00076                 }
00077         }
00078         delete _root ;
00079 }
00080 
00081 //-------------------------------------------------------------------------
00082 // loadConfigurationParameters
00083 //-------------------------------------------------------------------------
00084 bool 
00085 OgreVis::loadParameters( const ConfigurationParameterDescriptor * node )
00086 {
00087         bool ok( VisBase::loadParameters( node ) );
00088 
00089         // Retrieve the plugins for Ogre
00090         std::string pluginsFile ;
00091         if( ParametersAccessor::get( node, "OgreHome", pluginsFile ) )
00092         {
00093 #if defined _MSC_VER
00094                 // Windows
00095 #  if defined NDEBUG 
00096                 pluginsFile += "\\bin\\release\\Plugins.cfg" ;
00097 #  else // !defined NDEBUG 
00098                 pluginsFile += "\\bin\\debug\\Plugins.cfg" ;
00099 #  endif
00100 #else
00101     // Linux 
00102     pluginsFile += "/lib/OGRE/Plugins.cfg" ;
00103 #endif
00104         }
00105         else if( !ParametersAccessor::get( node, "PluginsCfg", pluginsFile ) )
00106         {
00107                 OMERROR( "Error for " << debugMsg( this ) << std::endl
00108                         << ">>> :-( Unable to determine which configuration must be used for Ogre." << std::endl
00109                         << "One of these two fields, 'OgreHome' or 'PluginsCfg', must be set." << std::endl
00110                         << "The path to the Ogre folder is defined in 'OgreHome' like this : OgreHome \"${OGRE_HOME}\"" << std::endl
00111                         << "Or the configuration file path is defined in 'OgrePluginsCfg' like this : PluginsCfg \"${OGRE_HOME}/lib/OGRE/plugins.cfg\"" << std::endl
00112                         << "See documentation" ) ;
00113                 ok = false ;
00114         }
00115 
00116         if ( ok )
00117         {
00118                 // Retrieve the configuration file
00119                 std::string configFile( "ogre.cfg" ) ;
00120                 if( !ParametersAccessor::get( node, "ConfigFile", configFile ) )
00121                 {
00122                         OMTRACEID( OMK_DEBUG_VIS, "Warning for " << debugMsg( this ) << std::endl
00123                                 << ">>> :-| no configuration file provided, will use \"ogre.cfg\" by default" ) ; 
00124                 }
00125 
00126                 // Retrieve the resource configuration
00127                 std::string resourcesFile( "resources.cfg" ) ;
00128                 if( !ParametersAccessor::get( node, "ResourcesFile", resourcesFile ) )
00129                 {
00130                         OMTRACEID( OMK_DEBUG_VIS, "Warning for " << debugMsg( this ) << std::endl
00131                                 << ">>> :-| no resources file provided, will use \"resources.cfg\" by default" ) ; 
00132                 }
00133 
00134                 // Retrieve the resource group name
00135                 std::string resourceGroupName( "General" ) ; // default value
00136                 if( !ParametersAccessor::get( node, "ResourceGroup", resourceGroupName ) )
00137                 {
00138                         OMTRACEID( OMK_DEBUG_VIS, "Warning for " << debugMsg( this ) << std::endl
00139                                 << ">>> :-| no resource group name provided, will use \"General\" by default" ) ; 
00140                 }
00141 
00142                 // Retrieve scene file name
00143                 std::string sceneFileName ;
00144                 if( !ParametersAccessor::get( node, "SceneFile", sceneFileName ) )
00145                 {
00146                         OMTRACEID( OMK_DEBUG_VIS, "Warning for " << debugMsg( this ) << std::endl
00147                                 << ">>> :-| no scene file name provided" ) ; 
00148                 }
00149 
00150                 // Retrieve camera name
00151                 std::string cameraName ;
00152                 if( !ParametersAccessor::get( node, "Camera", cameraName ) )
00153                 {
00154                         OMTRACEID( OMK_DEBUG_VIS, "Warning for " << debugMsg( this ) << std::endl
00155                                 << ">>> :-| no camera name provided" ) ; 
00156                 }
00157 
00158                 // Retrieve sceneManagerType
00159                 std::string sceneManagerType( "DefaultSceneManager" ) ;
00160                 if( !ParametersAccessor::get( node, "SceneManagerType", sceneManagerType ) )
00161                 {
00162                         OMTRACEID( OMK_DEBUG_VIS, "Warning for " << debugMsg( this ) << std::endl
00163                                 << ">>> :-| no SceneManagerType provided" ) ; 
00164                 }
00165 
00166                 // Retrieve windowName
00167                 std::string windowName( "Ogre Window" ) ;
00168                 if( !ParametersAccessor::get( node, "WindowName", windowName ) )
00169                 {
00170                         OMTRACEID( OMK_DEBUG_VIS, "Warning for " << debugMsg( this ) << std::endl
00171                                 << ">>> :-| no WindowName provided" ) ; 
00172                 }
00173 
00174    // createLog 
00175 
00176                 // OGRE initialisation
00177                 ok = ogreInitialisation( pluginsFile, configFile, resourcesFile, sceneManagerType, windowName ) ;
00178                 if ( ok )
00179                 {
00180                         // load the dotScene file of configuration parameters "SceneFile" 
00181                         // and resource group "ResourceGroup"
00182                         loadSceneFile( resourceGroupName, sceneFileName ) ;
00183 
00184                         if ( _root->getAutoCreatedWindow()->getNumViewports() == 0 )
00185                         {
00186                                 // try to initialise the camera named "Camera"
00187                                 // if cameraName was not specified a default camera is created
00188                                 cameraName = initCamera( cameraName ) ;
00189 
00190 
00191                                 // creates a default viewport of colour (0.5,0.5,0.5)
00192                                 createViewport( cameraName ) ;
00193                         }
00194                 }
00195         }
00196 
00197         return ok ;
00198 }
00199 
00200 //-------------------------------------------------------------------------
00201 // compute
00202 //-------------------------------------------------------------------------
00203 void 
00204 OgreVis::computeParameters()
00205 {
00206         VisBase::computeParameters() ;
00207 
00208         //Allow platform to pump/create/etc messages/events once per frame 
00209         Ogre::WindowEventUtilities::messagePump( ) ; 
00210         _root->renderOneFrame() ;
00211 }
00212 
00213 //-------------------------------------------------------------------------
00214 // initialise
00215 //-------------------------------------------------------------------------
00216 bool 
00217 OgreVis::ogreInitialisation(    const std::string& pluginsFile, 
00218                                                         const std::string& configurationFile, 
00219                                                         const std::string& resourcesFile,
00220                                                         const std::string& sceneManagerType,
00221                                                         const std::string& windowName )
00222 {
00223         bool initialised( false ) ;
00224 
00225         // creation of Ogre Root
00226         _root = new Ogre::Root( pluginsFile, configurationFile, "" ) ;
00227 
00228         // add resource locations
00229         addResourceLocations( resourcesFile );
00230 
00231         // Show the configuration dialog and initialise the system
00232         // You can skip this and use root.restoreConfig() to load configuration
00233         // settings if you were sure there are valid ones saved in ogre.cfg
00234         if( ( _root->restoreConfig() == true ) || ( _root->showConfigDialog() == true ) )
00235         {
00236                 // If returned true, user clicked OK so initialise
00237                 // Here we choose to let the system create a default rendering window by passing 'true'
00238                 _root->initialise( true, windowName ) ;
00239 
00240                 // creates a sceneManager named "sceneManager" of type ST_GENERIC
00241                 createSceneManager( sceneManagerType ) ;
00242 
00243                 // Set default mipmap level (NB some APIs ignore this)
00244                 TextureManager::getSingleton().setDefaultNumMipmaps( 5 ) ;
00245 
00246                 // Create any resource listeners (for loading screens)
00247                 createResourceListener() ;
00248 
00249                 // Initialise resources
00250                 initResources();
00251 
00252 #if !defined NDEBUG 
00253                 // Initialise the trace output to the console
00254                 OBT::Singleton<OBT::Tracer>::getInstance().setOutput( new LogOutput, true ) ;
00255 #endif
00256 
00257                 initialised = true ;
00258         }
00259 
00260         return initialised ;
00261 }
00262 
00263 //-------------------------------------------------------------------------
00264 // createSceneManager
00265 //-------------------------------------------------------------------------
00266 void 
00267 OgreVis::createSceneManager( const std::string& sceneManagerType )
00268 {
00269         try
00270         {
00271                 // Create the SceneManager
00272                 _root->createSceneManager( sceneManagerType, "sceneManager" ) ;
00273         }
00274         catch ( Ogre::ItemIdentityException& e )
00275         {
00276                 OMFATALERROR( "Scene manager type : " << sceneManagerType << " does not exist" << std::endl 
00277                         << e.getFullDescription() ) ;
00278         }
00279 }
00280 
00281 //-------------------------------------------------------------------------
00282 // createResourceListener
00283 //-------------------------------------------------------------------------
00284 void 
00285 OgreVis::createResourceListener( void )
00286 {
00287 }
00288 
00289 //-------------------------------------------------------------------------
00290 // addResourceLocations
00291 //-------------------------------------------------------------------------
00292 void 
00293 OgreVis::addResourceLocations( const std::string& resourcesFile )
00294 {
00295         // Load resource paths from config file
00296         ConfigFile configFile ;
00297         configFile.load( resourcesFile ) ;
00298 
00299         // Go through all sections & settings in the file
00300         ConfigFile::SectionIterator sectionIterator( configFile.getSectionIterator() ) ;
00301 
00302         String sectionName ;
00303         String typeName ;
00304         String archName ;
00305         while ( sectionIterator.hasMoreElements() == true )
00306         {
00307                 sectionName = sectionIterator.peekNextKey();
00308                 ConfigFile::SettingsMultiMap* settings( sectionIterator.getNext() ) ;
00309                 for ( ConfigFile::SettingsMultiMap::iterator i( settings->begin() ) ; i != settings->end(); ++i )
00310                 {
00311                         typeName = i->first ;
00312                         archName = i->second ;
00313                         ResourceGroupManager::getSingleton().addResourceLocation( archName, typeName, sectionName ) ;
00314                 }
00315         }
00316 }
00317 
00318 //-------------------------------------------------------------------------
00319 // initResources
00320 //-------------------------------------------------------------------------
00321 void 
00322 OgreVis::initResources( void )
00323 {
00324         ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
00325 }
00326 
00327 //-------------------------------------------------------------------------
00328 // loadSceneFile
00329 //-------------------------------------------------------------------------
00330 void
00331 OgreVis::loadSceneFile( const std::string& resourceGroupName, const std::string& sceneFileName )
00332 {
00333         if ( sceneFileName.empty() == false )
00334         {
00335                 // load the scene associated to the simulated object OgreVis
00336                 OBT::Singleton<dsi::dotSceneLoader>::getInstance().load
00337                         (       sceneFileName, 
00338                                 resourceGroupName, 
00339                                 _root->getSceneManager( "sceneManager" ), 
00340                                 _root->getAutoCreatedWindow(), 
00341                                 NULL, 
00342                                 false, 
00343                                 false, 
00344                                 true ) ;
00345 
00346                 // check for load errors
00347                 dsi::dotSceneInfo::itLoadLog it( dsi::dotSceneInfo::getSceneLoadingLog() ) ;
00348                 String errs ;
00349                 while ( it.hasMoreElements() )
00350                 {
00351                         errs += it.getNext() + "\n";
00352                 }
00353                 if ( errs != StringUtil::BLANK )
00354                 {
00355                         OMTRACEID( OMK_DEBUG_VIS, "Warning for " << debugMsg( this ) << std::endl
00356                                 << ">>> :-| errors occured during scene loading\n" << errs ) ;
00357                 }
00358         }
00359 }
00360 
00361 //-------------------------------------------------------------------------
00362 // initCamera
00363 //-------------------------------------------------------------------------
00364 const std::string&
00365 OgreVis::initCamera( const std::string& cameraName )
00366 {
00367         Ogre::SceneManager* sceneManager( _root->getSceneManager( "sceneManager" ) ) ;
00368         // creates a default camera if cameraName does not exist        
00369         if ( sceneManager->hasCamera( cameraName ) == false )
00370         {
00371 
00372                 // Create the camera
00373                 Camera* camera( sceneManager->createCamera( "camera0" ) ) ;
00374                 // Position it at 0, 0, 0
00375                 camera->setPosition( Vector3( 0, 0, 0 ) ) ;
00376                 // Look back along -Z
00377                 camera->lookAt( Vector3( 0, 0, -1 ) ) ;
00378                 camera->setNearClipDistance( 1 ) ;
00379                 camera->setFarClipDistance( 5000 ) ;
00380                 
00381                 //Put Camera on Node, useful for SoundListener 
00382                 SceneNode* cameraNode( sceneManager->getRootSceneNode()->createChildSceneNode( "CameraNode" ) ) ;
00383                 cameraNode->attachObject( camera );
00384                 return camera->getName() ;
00385         }
00386         return cameraName ;
00387 }
00388 
00389 //-------------------------------------------------------------------------
00390 // createViewport
00391 //-------------------------------------------------------------------------
00392 void 
00393 OgreVis::createViewport( const std::string& cameraName )
00394 {
00395         Ogre::Camera* camera( _root->getSceneManager( "sceneManager" )->getCamera( cameraName ) ) ;
00396 
00397         // Create one viewport, entire window
00398         Viewport* viewport( _root->getAutoCreatedWindow()->addViewport( camera ) ) ;
00399         viewport->setBackgroundColour( ColourValue( 0.5, 0.5, 0.5 ) ) ;
00400 
00401         // Alter the camera aspect ratio to match the viewport
00402         camera->setAspectRatio( Real( viewport->getActualWidth() ) / Real( viewport->getActualHeight() ) ) ;
00403 }

logo OpenMask

Documentation generated on Mon Jun 9 11:45:57 2008

Generated with doxygen by Dimitri van Heesch ,   1997-2007