# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

particle_system_manager.cpp

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 - 2002 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 #include "std3d.h"
00027 
00028 #include "3d/particle_system_manager.h"
00029 #include "3d/particle_system_model.h"
00030 #include "3d/scene.h"
00031 
00032 namespace NL3D {
00033 
00035 CParticleSystemManager::CParticleSystemManager() : _NumModels(0)
00036 {
00037         _CurrListIterator = _ModelList.end();
00038 }
00039 
00041 void    CParticleSystemManager::refreshModels(const std::vector<NLMISC::CPlane> &worldFrustumPyramid,  const NLMISC::CVector &viewerPos)
00042 {       
00043         #ifdef NL_DEBUG
00044                 nlassert(_NumModels == _ModelList.size());
00045         #endif
00046 
00047         if (_NumModels == 0) return;
00048         const uint toProcess = std::min(_NumModels, (uint) NumProcessToRefresh);        
00049 
00050         for (uint k = 0; k < toProcess; ++k)
00051         {
00052                 if (_CurrListIterator == _ModelList.end())
00053                 {
00054                         _CurrListIterator = _ModelList.begin();
00055                 }
00056                 
00057                 if ((*_CurrListIterator)->refreshRscDeletion(worldFrustumPyramid, viewerPos)) // check if the resources of the model must be released (which also release them)
00058                 {                                               
00059                         _CurrListIterator = _ModelList.erase(_CurrListIterator);                        
00060                         -- _NumModels;
00061                 }
00062                 else
00063                 {
00064                         ++ _CurrListIterator;
00065                 }               
00066         }
00067 
00068         #ifdef NL_DEBUG
00069                 nlassert(_NumModels == _ModelList.size());
00070         #endif
00071 }
00072 
00074 CParticleSystemManager::TModelHandle CParticleSystemManager::addSystemModel(CParticleSystemModel *model)
00075 {
00076         #ifdef NL_DEBUG
00077                 nlassert(std::find(_ModelList.begin(), _ModelList.end(), model) == _ModelList.end()); 
00078         #endif
00079         _ModelList.push_front(model);
00080         ++_NumModels;
00081 
00082         #ifdef NL_DEBUG
00083                 nlassert(_NumModels == _ModelList.size());
00084         #endif
00085 
00086         TModelHandle handle;
00087         handle.Valid = true;
00088         handle.Iter = _ModelList.begin();
00089         return handle;
00090 }
00091 
00092 
00094 void            CParticleSystemManager::removeSystemModel(TModelHandle &handle)
00095 {       
00096         nlassert(handle.Valid);
00097         #ifdef NL_DEBUG
00098                 nlassert(_NumModels == _ModelList.size());
00099         #endif
00100         nlassert(_NumModels != 0);      
00101         if (handle.Iter == _CurrListIterator)
00102         {
00103                 ++_CurrListIterator;            
00104         }
00105 
00106         _ModelList.erase(handle.Iter);
00107         --_NumModels;
00108         #ifdef NL_DEBUG
00109                 nlassert(_NumModels == _ModelList.size());
00110         #endif
00111 }
00112 
00113 
00115 CParticleSystemManager::TModelHandle    CParticleSystemManager::addPermanentlyAnimatedSystem(CParticleSystemModel *ps)
00116 {
00117         #ifdef NL_DEBUG
00118                 nlassert(std::find(_PermanentlyAnimatedModelList.begin(), _PermanentlyAnimatedModelList.end(), ps) == _PermanentlyAnimatedModelList.end());
00119         #endif
00120         _PermanentlyAnimatedModelList.push_front(ps);
00121 
00122 
00123         TModelHandle handle;
00124         handle.Valid = true;
00125         handle.Iter = _PermanentlyAnimatedModelList.begin();
00126         return handle;  
00127 }
00128 
00130 void                    CParticleSystemManager::removePermanentlyAnimatedSystem(CParticleSystemManager::TModelHandle &handle)
00131 {
00132         nlassert(handle.Valid);
00133         _PermanentlyAnimatedModelList.erase(handle.Iter);       
00134 }
00135 
00137 void    CParticleSystemManager::processAnimate(TAnimationTime deltaT)
00138 {
00139         for (TModelList::iterator it = _PermanentlyAnimatedModelList.begin(); it != _PermanentlyAnimatedModelList.end(); ++it)
00140         {
00141                 CParticleSystemModel &psm = *(*it);
00142                 CParticleSystem          *ps  = psm.getPS();
00143                 if (ps)
00144                 {
00145                         if (psm.isAutoGetEllapsedTimeEnabled())
00146                         {
00147                                 psm.setEllapsedTime(ps->getScene()->getEllapsedTime() * psm.getEllapsedTimeRatio());
00148                         }
00149                         TAnimationTime delay = psm.getEllapsedTime();
00150                         // animate particles
00151                         ps->step(CParticleSystem::Anim, delay); 
00152 
00153                         if (!psm.getEditionMode())
00154                         {                       
00155                                 // test deletion condition (no more particle, no more particle and emitters)
00156                                 if (ps->getDestroyCondition() != CParticleSystem::none)
00157                                 {
00158                                         if (ps->getSystemDate() > ps->getDelayBeforeDeathConditionTest())
00159                                         {
00160                                                 switch (ps->getDestroyCondition())
00161                                                 {
00162                                                         case CParticleSystem::noMoreParticles:
00163                                                                 if (!ps->hasParticles())
00164                                                                 {                                                       
00165                                                                         psm.releaseRscAndInvalidate();
00166                                                                         return;
00167                                                                 }
00168                                                         break;
00169                                                         case CParticleSystem::noMoreParticlesAndEmitters:
00170                                                                 if (!ps->hasParticles() && !ps->hasEmitters())
00171                                                                 {
00172                                                                         psm.releaseRscAndInvalidate();
00173                                                                         return;
00174                                                                 }
00175                                                         break;
00176                                                         default: break;
00177                                                 }
00178                                         }
00179                                 }
00180                         }
00181                 }
00182         }
00183 }
00184 
00185 } // NL3D