OMKMaterial.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 "OMKParametersAccessor.inl"
00023 #include "OMKMaterial.h" 
00024 using namespace OBT ;
00025 using namespace OMK ;
00026 using namespace OMK::Vis ;
00027 using namespace Wm4 ;
00028 
00029 
00030 
00031 //=============================================================================
00032 // NodeMaterial
00033 //=============================================================================
00034 NodeMaterial::~NodeMaterial() 
00035 {
00036   for( EntityMaterialIterator it = getEntityMaterialIterator() ;
00037        it.hasMoreElements() ;
00038        it.moveNext() ) 
00039   {
00040     delete it.peekNextValue() ;
00041   }
00042 }
00043 //-----------------------------------------------------------------------------
00044 void NodeMaterial::doAction( const IMaterialAction& action ) 
00045 {
00046   if( action.nodeAction( this ) )
00047   {
00048     for( EntityMaterialIterator it = getEntityMaterialIterator() ;
00049          it.hasMoreElements() ;
00050          it.moveNext() ) 
00051     {
00052       it.peekNextValue()->doAction( action ) ;
00053     }
00054   }
00055 }
00056 //-----------------------------------------------------------------------------
00057 void NodeMaterial::activateMaterial() 
00058 {
00059   for( EntityMaterialIterator it = getEntityMaterialIterator() ;
00060        it.hasMoreElements() ;
00061        it.moveNext() )
00062   {
00063     it.peekNextValue()->activateMaterial() ;
00064   }
00065 }
00066 //-----------------------------------------------------------------------------
00067 void NodeMaterial::clearMaterial() 
00068 {
00069   for( EntityMaterialIterator it = getEntityMaterialIterator() ;
00070        it.hasMoreElements() ;
00071        it.moveNext() )
00072   {
00073     it.peekNextValue()->clearMaterial() ;
00074   }
00075 }
00076 //-----------------------------------------------------------------------------
00077 
00078 //=============================================================================
00079 // EntityMaterial
00080 //=============================================================================
00081 EntityMaterial::~EntityMaterial() 
00082 {
00083   for( SubEntityMaterialIterator it = getSubEntityMaterialIterator() ;
00084        it.hasMoreElements() ;
00085        it.moveNext() ) 
00086   {
00087     delete it.peekNext() ;
00088   }
00089 }
00090 //-----------------------------------------------------------------------------
00091 void EntityMaterial::doAction( const IMaterialAction& action ) 
00092 {
00093   if( action.entityAction( this ) )
00094   {
00095     for( SubEntityMaterialIterator it = getSubEntityMaterialIterator() ;
00096          it.hasMoreElements() ;
00097          it.moveNext() ) 
00098     {
00099       it.peekNext()->doAction( action ) ;
00100     }
00101   }
00102 }
00103 //-----------------------------------------------------------------------------
00104 void EntityMaterial::activateMaterial() 
00105 {
00106   for( SubEntityMaterialIterator it = getSubEntityMaterialIterator() ;
00107        it.hasMoreElements() ;
00108        it.moveNext() )
00109   {
00110     it.peekNext()->activateMaterial() ;
00111   }
00112 }
00113 //-----------------------------------------------------------------------------
00114 void EntityMaterial::clearMaterial() 
00115 {
00116   for( SubEntityMaterialIterator it = getSubEntityMaterialIterator() ;
00117        it.hasMoreElements() ;
00118        it.moveNext() )
00119   {
00120     it.peekNext()->clearMaterial() ;
00121   }
00122 }
00123 //-----------------------------------------------------------------------------
00124 
00125 //=============================================================================
00126 // SubEntityMaterial
00127 //=============================================================================
00128 void SubEntityMaterial::doAction( const IMaterialAction& action ) 
00129 {
00130   action.subEntityAction( this ) ;
00131 }
00132 //-----------------------------------------------------------------------------
00133 void SubEntityMaterial::activateMaterial() 
00134 {
00135   if( !_mat.isNull() )
00136   {
00137     OMTRACEID("OMK::Material", "set Material " << _mat->getName());
00138     _ogreSubEntity->setMaterialName( _mat->getName() ) ;
00139   }
00140 }
00141 //-----------------------------------------------------------------------------
00142 void SubEntityMaterial::activateMaterial( const Ogre::String& materialName ) 
00143 {
00144   if( _mat.isNull() || materialName != _mat->getName() )
00145   {
00146     OMTRACEID("OMK::Material", "set Material start " << materialName);
00147     clearMaterial() ;
00148     _mat = Ogre::MaterialManager::getSingleton().getByName( materialName ) ;
00149     _ogreSubEntity->setMaterialName( materialName ) ;
00150   }
00151 }
00152 //-----------------------------------------------------------------------------
00153 void SubEntityMaterial::clearMaterial() 
00154 {
00155   if( !_mat.isNull() )
00156   {
00157     Ogre::MaterialManager::getSingleton().remove( _mat->getName() ) ;
00158     _mat.setNull() ;
00159   }
00160 }
00161 //-----------------------------------------------------------------------------
00162 
00163 //=============================================================================
00164 // NodeMaterialOrig
00165 //=============================================================================
00166 NodeMaterialOrig::NodeMaterialOrig( Ogre::SceneNode* sceneNode ) 
00167 : NodeMaterial(),
00168   _sceneNode( sceneNode ) 
00169 {
00170   for( Ogre::SceneNode::ObjectIterator it = sceneNode->getAttachedObjectIterator() ;
00171        it.hasMoreElements() ;
00172        it.moveNext() )
00173   {
00174     Ogre::MovableObject * currentMO = it.peekNextValue() ;
00175     if ( currentMO->getMovableType() == Ogre::EntityFactory::FACTORY_TYPE_NAME ) 
00176     {
00177       OMTRACEID( "DEBUG", "Create entity " << currentMO->getName() ) ;
00178       _entities[ it.peekNextKey() ] = new EntityMaterialOrig( static_cast< Ogre::Entity *> ( currentMO ) ) ;
00179     }
00180   }
00181   for( Ogre::Node::ChildNodeIterator it = sceneNode->getChildIterator() ;
00182        it.hasMoreElements() ;
00183        it.moveNext() )
00184   {
00185     Ogre::SceneNode* childSceneNode = static_cast< Ogre::SceneNode* >( it.peekNextValue() ) ;
00186     OMTRACEID( "DEBUG", "Create ChildSceneNode " << childSceneNode->getName() ) ;
00187     NodeMaterialOrig* child = new NodeMaterialOrig( childSceneNode ) ;
00188     _childs.push_back( child ) ;
00189   }
00190 }
00191 //-----------------------------------------------------------------------------
00192 NodeMaterialOrig::~NodeMaterialOrig()
00193 {
00194   for( NodeMaterialOrigVector::iterator it = _childs.begin() ;
00195        it != _childs.end() ;
00196        it++ ) 
00197   {
00198     delete *it ;
00199   }
00200   for( ItemStack::iterator it = _stack.begin() ; 
00201        it != _stack.end() ;
00202        it++ )
00203   {
00204     delete it->nodeMaterial ;
00205   }
00206 }
00207 //-----------------------------------------------------------------------------
00208 void NodeMaterialOrig::doAction( const IMaterialAction& action ) 
00209 {
00210   if( action.nodeAction( this ) )
00211   {
00212     for( EntityMaterialIterator it = getEntityMaterialIterator() ;
00213          it.hasMoreElements() ;
00214          it.moveNext() ) 
00215     {
00216       it.peekNextValue()->doAction( action ) ;
00217     }
00218   }
00219   for( NodeMaterialOrigVector::iterator it = _childs.begin() ;
00220        it != _childs.end() ;
00221        it++ ) 
00222   {
00223     (*it)->doAction( action ) ;
00224   }
00225 }
00226 //-----------------------------------------------------------------------------
00227 NodeMaterialOrig* NodeMaterialOrig::find( Ogre::SceneNode* node ) 
00228 {
00229   NodeMaterialOrig* result = node != _sceneNode ? 0 : this ;
00230   for( NodeMaterialOrigVector::iterator it = _childs.begin() ;
00231        it != _childs.end() && !result ;
00232        it++ ) 
00233   {
00234     result = (*it)->find( node ) ;
00235   }
00236   return result ;
00237 }
00238 //-----------------------------------------------------------------------------
00239 void NodeMaterialOrig::updateCopyWith( IUpdateMaterial* update ) 
00240 {
00241   NodeMaterialCopy* newNode = new NodeMaterialCopy( _stack.empty() ? (NodeMaterial*)this : (NodeMaterial*)_stack.back().nodeMaterial, update ) ;
00242   _stack.push_back( Item( newNode, update ) ) ;
00243   OMTRACEID("DEBUG", "" << update->getMaterialName(".") 
00244         << " Stack size " << _stack.size() );
00245     for( EntityMaterialIterator it = getEntityMaterialIterator() ;
00246        it.hasMoreElements() ;
00247        it.moveNext() )
00248   {
00249    OMTRACEID("DEBUG", "Entities "  <<   it.peekNextKey()  );
00250      for( SubEntityMaterialIterator it2 = it.peekNextValue()->getSubEntityMaterialIterator() ;
00251        it2.hasMoreElements() ;
00252        it2.moveNext() )
00253   {
00254     if ( !  it2.peekNext()->getMaterial().isNull())
00255       OMTRACEID("DEBUG", "SubEntitity Mat "<<   it2.peekNext()->getMaterial()->getName()) ;
00256   }
00257 
00258   }
00259 
00260   update->update( newNode ) ;
00261 }
00262 //-----------------------------------------------------------------------------
00263 void NodeMaterialOrig::createCopyAndUpdateWith( IUpdateMaterial* update ) 
00264 {
00265   updateCopyWith( update ) ;
00266   for( NodeMaterialOrigVector::iterator it = _childs.begin() ;
00267        it != _childs.end() ;
00268        it++ ) 
00269   {
00270     OMTRACEID("DEBUG", "Child --------------- " );
00271       (*it)->createCopyAndUpdateWith( update ) ;
00272   }
00273 }
00274 //-----------------------------------------------------------------------------
00275 void NodeMaterialOrig::undoFor( IUpdateMaterial* update, bool deleteNode ) 
00276 {
00277   // Undo the current node level
00278   UpdateMaterialStack updateStack ;
00279   for( ; _stack.back().update != update ; _stack.pop_back() )
00280   {
00281     updateStack.push( _stack.back().update ) ;
00282     _stack.back().update->undo( _stack.back().nodeMaterial ) ;
00283     delete _stack.back().nodeMaterial ;
00284   }
00285   // delete the node which is undoing
00286   if( deleteNode )
00287   {
00288     delete _stack.back().nodeMaterial ;
00289     _stack.pop_back() ;
00290   }
00291   // activate the new current material
00292   ( _stack.empty() ? (NodeMaterial*)this : (NodeMaterial*)( _stack.back().nodeMaterial ) )->activateMaterial() ;
00293   // Do the same for children
00294   for( NodeMaterialOrigVector::iterator it = _childs.begin() ;
00295        it != _childs.end() ;
00296        it++ ) 
00297   {
00298     (*it)->undoFor( update, deleteNode ) ;
00299   }
00300   // Redo the action
00301   if( !deleteNode )
00302   {
00303     update->update( _stack.back().nodeMaterial ) ;
00304   }
00305   for( ; !updateStack.empty(); updateStack.pop() )
00306   {
00307     updateCopyWith( updateStack.top() ) ;
00308   }
00309 }
00310 //-----------------------------------------------------------------------------
00311 
00312 //=============================================================================
00313 // EntityMaterialOrig
00314 //=============================================================================
00315 EntityMaterialOrig::EntityMaterialOrig( Ogre::Entity *ogreEntity ) 
00316 : EntityMaterial( ogreEntity )
00317 {
00318   for( unsigned int i = 0; 
00319        i < ogreEntity->getNumSubEntities() ; 
00320        i++ ) 
00321   {
00322     OMTRACEID( "DEBUG", "Create SubEntityMaterial " << i  ) ;
00323     _subEntities.push_back( new SubEntityMaterialOrig( ogreEntity->getSubEntity( i ) ) ) ;
00324   }
00325 }
00326 //-----------------------------------------------------------------------------
00327 
00328 //=============================================================================
00329 // SubEntityMaterialOrig
00330 //=============================================================================
00331 SubEntityMaterialOrig::SubEntityMaterialOrig( Ogre::SubEntity *ogreSubEntity ) 
00332 : SubEntityMaterial( ogreSubEntity )
00333 {
00334   _mat = Ogre::MaterialManager::getSingleton().getByName( _ogreSubEntity->getMaterialName() ) ;
00335   OMTRACEID( "DEBUG", "get Material of " <<_ogreSubEntity->getMaterialName()  << (_mat.isNull() ? " NULL" : " OK" )) ;
00336 }
00337 //-----------------------------------------------------------------------------
00338 SubEntityMaterialOrig::~SubEntityMaterialOrig() 
00339 {
00340   _ogreSubEntity->setMaterialName( _mat->getName() ) ;
00341 }
00342 //-----------------------------------------------------------------------------
00343 
00344 //=============================================================================
00345 // NodeMaterialCopy
00346 //=============================================================================
00347 NodeMaterialCopy::NodeMaterialCopy( NodeMaterial* node, IUpdateMaterial* update )
00348 : NodeMaterial()
00349 {
00350   for( EntityMaterialIterator it = node->getEntityMaterialIterator() ;
00351        it.hasMoreElements() ;
00352        it.moveNext() )
00353   {
00354     _entities[ it.peekNextKey() ] = new EntityMaterialCopy( it.peekNextValue(), update ) ;
00355   }
00356 }
00357 //-----------------------------------------------------------------------------
00358 NodeMaterialCopy::~NodeMaterialCopy()
00359 {
00360 }
00361 //-----------------------------------------------------------------------------
00362 
00363 //=============================================================================
00364 // EntityMaterialCopy
00365 //=============================================================================
00366 EntityMaterialCopy::EntityMaterialCopy( EntityMaterial *e, IUpdateMaterial* update )
00367 : EntityMaterial( e->_ogreEntity )
00368 {
00369   for( SubEntityMaterialIterator it = e->getSubEntityMaterialIterator() ;
00370        it.hasMoreElements() ;
00371        it.moveNext() )
00372   {
00373     _subEntities.push_back( new SubEntityMaterialCopy( it.peekNext(), update ) ) ;
00374   }
00375 }
00376 //-----------------------------------------------------------------------------
00377 EntityMaterialCopy::~EntityMaterialCopy()
00378 {
00379 }
00380 //-----------------------------------------------------------------------------
00381 
00382 //=============================================================================
00383 // SubEntityMaterialCopy
00384 //=============================================================================
00385 SubEntityMaterialCopy::SubEntityMaterialCopy( SubEntityMaterial *se, IUpdateMaterial* update )
00386 : SubEntityMaterial( se->_ogreSubEntity )
00387 {
00388   if( !se->_mat.isNull() )
00389   {
00390     Ogre::MaterialPtr matSrc( se->_mat ) ;
00391     Ogre::String nameDest( update->getMaterialName( matSrc->getName() ) ) ;
00392     // Try to reuse existing material
00393     if( Ogre::MaterialManager::getSingleton().resourceExists( nameDest ) )
00394     { // ok => get the material
00395       _mat = Ogre::MaterialManager::getSingleton().getByName( nameDest ) ;
00396     }
00397     else
00398     { // none => create a copy
00399       _mat = matSrc->clone( nameDest ) ;
00400     }
00401     _ogreSubEntity->setMaterialName( nameDest ) ;
00402   }
00403 }
00404 //-----------------------------------------------------------------------------
00405 SubEntityMaterialCopy::~SubEntityMaterialCopy()
00406 {
00407 }
00408 //-----------------------------------------------------------------------------
00409 
00410 //=============================================================================
00411 // IMaterialAction
00412 //=============================================================================
00413 Ogre::Pass* IMaterialAction::getFirstPassOfFirstTechnique( SubEntityMaterial* subEntity )
00414 {  
00415   OMASSERTM( 0 < subEntity->getMaterial()->getNumTechniques(), "Don't get Any Tecnnique on Material" << subEntity->getMaterial()->getName() );
00416   Ogre::Technique * technique = subEntity->getMaterial()->getTechnique( 0 );
00417   OMASSERTM( 0 < technique->getNumPasses(), "Don't get Any PASS on Material " << subEntity->getMaterial()->getName() );
00418   return technique->getPass( 0 ) ;
00419 }
00420 //-----------------------------------------------------------------------------
00421 
00422 //=============================================================================
00423 // IUpdateMaterial
00424 //=============================================================================
00425 void IUpdateMaterial::undo( NodeMaterial* node ) 
00426 { 
00427   node->clearMaterial() ; 
00428 }
00429 //-----------------------------------------------------------------------------
00430 Ogre::String IUpdateMaterial::getUniqueMaterialName( const Ogre::String& name ) 
00431 {
00432   Ogre::String uniqueName( name ) ;
00433   for( ; 
00434     Ogre::MaterialManager::getSingleton().resourceExists( uniqueName ) ;
00435     uniqueName = name + Ogre::StringConverter::toString( Ogre::Math::UnitRandom() ) )
00436        ;
00437   return uniqueName ;
00438 }
00439 //-----------------------------------------------------------------------------
00440 

logo OpenMask

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

Generated with doxygen by Dimitri van Heesch ,   1997-2007