particle_system_model.h

Go to the documentation of this file.
00001 
00005 /* Copyright, 2001 Nevrax Ltd.
00006  *
00007  * This file is part of NEVRAX NEL.
00008  * NEVRAX NEL is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2, or (at your option)
00011  * any later version.
00012 
00013  * NEVRAX NEL is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * General Public License for more details.
00017 
00018  * You should have received a copy of the GNU General Public License
00019  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00020  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00021  * MA 02111-1307, USA.
00022  */
00023 
00024 #ifndef NL_PARTICLE_SYSTEM_MODEL_H
00025 #define NL_PARTICLE_SYSTEM_MODEL_H
00026 
00027 #include "nel/misc/types_nl.h"
00028 #include "nel/misc/plane.h"
00029 #include "nel/misc/contiguous_block_allocator.h"
00030 #include "nel/3d/transform_shape.h"
00031 #include "nel/3d/particle_system.h"
00032 #include "nel/3d/particle_system_manager.h"
00033 #include "nel/3d/clip_trav.h"
00034 #include "nel/3d/anim_detail_trav.h"
00035 #include "nel/3d/load_balancing_trav.h"
00036 #include "nel/3d/scene.h"
00037 
00038 // tmp
00039 #include "nel/misc/hierarchical_timer.h"
00040 
00041 #include <vector>
00042 
00043 namespace NL3D {
00044 
00046 // THE FOLLOWING CLASS IS FOR INSERTION OF A PARTICLE  //
00047 // SYSTEM IN A MOT MODEL                               //
00048 // SEE also CParticleSystemshape, CParticleSystem      //
00050 
00051 class CParticleSystem;
00052 class CScene;
00053 class CParticleSystemShape;
00054 
00055 
00061 class CParticleSystemModel : public CTransformShape
00062 {
00063 public:
00065 
00066 
00067         CParticleSystemModel();
00069         ~CParticleSystemModel();
00070 
00072         static  void                registerBasic();
00074 
00076 
00077 
00082         CParticleSystem             *getPS(void)
00083         {
00084             return _ParticleSystem;
00085         }
00086 
00090         const CParticleSystem       *getPS(void) const
00091         {
00092             return _ParticleSystem;
00093         }
00094 
00098         void                        setParticleSystem(CParticleSystem *ps)
00099         {
00100             nlassert(!_ParticleSystem);
00101             _ParticleSystem = ps;
00102             updateOpacityInfos();
00103         }
00105 
00107 
00108 
00112         bool isInvalid(void) const { return _Invalidated; }
00113 
00115         struct IPSModelObserver
00116         {
00117             virtual ~IPSModelObserver() {}
00119             virtual void invalidPS(CParticleSystemModel *psm) = 0;
00120         };
00121 
00123         void registerPSModelObserver(IPSModelObserver *obs);
00124 
00128         void removePSModelObserver(IPSModelObserver *obs);
00130         bool isPSModelObserver(IPSModelObserver *obs);
00132 
00133 
00134     //\name Time managment
00136 
00140         void                        enableAutoGetEllapsedTime(bool enable = true)
00141         {
00142             _AutoGetEllapsedTime = enable;
00143         }
00148         void                        setEllapsedTimeRatio(float value)
00149         {
00150             nlassert(value >= 0);
00151             _EllapsedTimeRatio = value;
00152         }
00153         //
00154         float                       getEllapsedTimeRatio() const { return _EllapsedTimeRatio; }
00156         bool                        isAutoGetEllapsedTimeEnabled(void) const
00157         {
00158             return _AutoGetEllapsedTime;
00159         }
00161         void                        setEllapsedTime(TAnimationTime ellapsedTime)
00162         {
00163             _EllapsedTime = ellapsedTime;
00164         }
00166         TAnimationTime              getEllapsedTime(void) const
00167         {
00168             return _EllapsedTime;
00169         }
00171 
00173 
00174 
00175         void                        enableDisplayTools(bool enable = true);
00176 
00177         // check whether the display of tools is enabled
00178         bool                        isToolDisplayEnabled(void) const
00179         {
00180             return _ToolDisplayEnabled;
00181         }
00188         void                        setEditionMode(bool enable = true) ;
00190         bool                        getEditionMode(void) const
00191         {
00192             return _EditionMode;
00193         }
00195         void                        touchTransparencyState(void)
00196         {
00197             _TransparencyStateTouched = true;
00198         }
00200         void                        touchLightableState(void)
00201         {
00202             _LightableStateTouched = true;
00203         }
00205 
00207 
00208 
00209         enum    TAnimValues
00210         {
00211             OwnerBit= CTransformShape::AnimValueLast,
00212             PSParam0,
00213             PSParam1,
00214             PSParam2,
00215             PSParam3,
00216             PSTrigger, // trigger the instanciation of the system
00217             AnimValueLast,
00218         };
00219         virtual IAnimatedValue      *getValue (uint valueId);
00220         virtual const char          *getValueName (uint valueId) const;
00221         static const char           *getPSParamName (uint valueId);
00222         virtual ITrack              *getDefaultTrack (uint valueId);
00223         virtual void                registerToChannelMixer(CChannelMixer *chanMixer,
00224                                                            const std::string &prefix = "");
00225         // Bypass a global user param.
00226         void  bypassGlobalUserParamValue(uint userParamIndex, bool byPass = true);
00227         bool  isGlobalUserParamValueBypassed(uint userParamIndex) const;
00229 
00233     void                        updateOpacityInfos(void);
00234     // Update the lighted/not lighted flag of the system
00235     void                        updateLightingInfos(void);
00236 
00237     virtual void                getAABBox(NLMISC::CAABBox &bbox) const;
00239     virtual float               getNumTriangles (float distance);
00241     static CTransform               *creator()
00242     {
00243         return new CParticleSystemModel;
00244     }
00245 
00246 
00248     // @{
00251     virtual void    traverseClip();
00252     // no-op clip() because all done in special traverse()
00253     virtual bool    clip();
00259     virtual void    traverseAnimDetail();
00260     virtual void    traverseRender();
00261     // @}
00262 
00263     // activate / deactivate all emitters
00264     void    activateEmitters(bool active);
00265     // test if there are active emitters in the system
00266     bool    hasActiveEmitters() const;
00267 
00268     // user color
00269     void            setUserColor(NLMISC::CRGBA userColor);
00270     NLMISC::CRGBA   getUserColor() const { return _UserColor; }
00271 
00280     void setUserMatrix(const NLMISC::CMatrix &userMatrix) { _UserMatrix = userMatrix; }
00281     // Set the user matrix, with instant update of the CParticleSystem pointed by that model (if instanciated)
00282     void forceSetUserMatrix(const NLMISC::CMatrix &userMatrix);
00283     const NLMISC::CMatrix &getUserMatrix() const { return _UserMatrix; }
00284 
00285     void forceInstanciate();
00286 
00287     // Set z-bias. Value is in world coordinates. Value remains even if ps isn't present (rsc not allocated)
00288     void setZBias(float value);
00289 
00290     // sound on / off
00291     void stopSound();
00292     void reactivateSound();
00293 
00294     // from CTransform
00295     virtual void update();
00296 
00299 private:
00300     friend class CParticleSystemShape;
00301     friend class CParticleSystemManager;
00302 
00303 
00305     void reallocRsc();
00307     void refreshRscDeletion(const std::vector<CPlane>   &worldFrustumPyramid,  const NLMISC::CVector &viewerPos);
00308     // Release the resources (attached system) of this model, but doesn't make it invalid.
00309     void releaseRsc();
00310     // Mark this system model as invalid, delete the attached system, and calls his observers
00311     void releaseRscAndInvalidate();
00313     bool checkAgainstPyramid(const std::vector<CPlane>  &worldFrustumPyramid) const;
00314     // Release PS and backup system params
00315     void releasePSPointer();
00316 
00317     void invalidateAutoAnimatedHandle();
00318 
00319 
00320     // insert the model in the Clip/AnimDetail/LoadBalacing visible list.
00321     void                insertInVisibleList()
00322     {
00323         // if not already not inserted
00324         if (!_InsertedInVisibleList)
00325         {
00326             _Visible = true;
00327             _InsertedInVisibleList = true;
00328             // add to clip/load Trav.
00329             getOwnerScene()->getClipTrav().addVisibleModel(this);
00330             getOwnerScene()->getLoadBalancingTrav().addVisibleModel(this);
00331 
00332             // Add only if no ancestor skeleton model
00333             if( _AncestorSkeletonModel==NULL )
00334             {
00335                 // need to test isLightable(), because most of PS are not lightable
00336                 // NB: don't insert if has an _AncestorSkeletonModel, because in this case,
00337                 // result is driven by the _LightContribution of the _AncestorSkeletonModel.
00338                 if( isLightable() )
00339                     getOwnerScene()->getLightTrav().addLightedModel(this);
00340                 // no need to test isAnimDetailable()... for PS, always add them
00341                 // NB: don't insert if has an _AncestorSkeletonModel, because in this case, this ancestor will
00342                 // animDetail through the hierarchy...
00343                 getOwnerScene()->getAnimDetailTrav().addVisibleModel(this);
00344             }
00345         }
00346     }
00347     bool checkDestroyCondition(CParticleSystem *ps);
00348     // perform actual animation of the particles
00349     void    doAnimate();
00350 
00351 private:
00352     CParticleSystemManager::TModelHandle    _ModelHandle; 
00355     CParticleSystemManager::TAlwaysAnimatedModelHandle    _AnimatedModelHandle; // handle for permanenlty animated models
00356     NLMISC::CSmartPtr<CParticleSystem>      _ParticleSystem;
00357     CScene                                 *_Scene;
00358     TAnimationTime                          _EllapsedTime;
00359     float                                   _EllapsedTimeRatio;
00360 
00361     // old animation mode for the system
00362     CParticleSystem::TAnimType              _AnimType;
00363 
00364     bool                                    _AutoGetEllapsedTime        : 1;
00365     bool                                    _ToolDisplayEnabled         : 1;
00366     bool                                    _TransparencyStateTouched   : 1;
00367     bool                                    _LightableStateTouched      : 1;
00368     bool                                    _EditionMode                : 1;
00369     bool                                    _Invalidated                : 1;  
00370     bool                                    _InsertedInVisibleList      : 1;
00371     bool                                    _InClusterAndVisible        : 1;
00372     bool                                    _EmitterActive              : 1;
00373     bool                                    _SoundActive                : 1;
00374 
00375     std::vector<IPSModelObserver *>         _Observers;
00376     CAnimatedValueBool                      _TriggerAnimatedValue;
00378     CAnimatedValueFloat                     _UserParam[MaxPSUserParam];
00379     uint8                                   _BypassGlobalUserParam;  // mask to bypass a global user param. This state is not serialized
00380     NLMISC::CRGBA                           _UserColor;
00381     NLMISC::CMatrix                         _UserMatrix;
00382     float                                   _ZBias;
00383     CHrcTrav::TVisibility                   _LastVisibility;
00384 
00385     #ifdef PS_FAST_ALLOC
00386         // for fast allocation of ps resources
00387         NLMISC::CContiguousBlockAllocator       _Allocator;
00388     #endif
00389 };
00390 
00391 
00392 // tmp
00393 class CMiniTimer
00394 {
00395 public:
00396     NLMISC::CSimpleClock SC;
00397     uint64 &Target;
00398     uint64 StartDate;
00399     CMiniTimer(uint64 &target) : Target(target)
00400     {
00401         SC.start();
00402     }
00403     ~CMiniTimer()
00404     {
00405         SC.stop();
00406         Target += SC.getNumTicks();
00407     }
00408 };
00409 
00410 #define MINI_TIMER(name)
00411 
00412 /*
00413 #define MINI_TIMER(name) CMiniTimer mt(name);
00414 
00415 
00416 extern uint64 PSStatsRegisterPSModelObserver;
00417 extern uint64 PSStatsRemovePSModelObserver;
00418 extern uint64 PSStatsUpdateOpacityInfos;
00419 extern uint64 PSStatsUpdateLightingInfos;
00420 extern uint64 PSStatsGetAABBox;
00421 extern uint64 PSStatsReallocRsc;
00422 extern uint64 PSStatsReleasePSPointer;
00423 extern uint64 PSStatsRefreshRscDeletion;
00424 extern uint64 PSStatsReleaseRsc;
00425 extern uint64 PSStatsReleaseRscAndInvalidate;
00426 extern uint64 PSStatsGetNumTriangles;
00427 extern uint64 PSStatsCheckAgainstPyramid;
00428 extern uint64 PSStatsTraverseAnimDetail;
00429 extern uint64 PSStatsTraverseAnimDetailPart1;
00430 extern uint64 PSStatsTraverseAnimDetailPart2;
00431 extern uint64 PSStatsTraverseAnimDetailPart3;
00432 extern uint64 PSStatsTraverseAnimDetailPart4;
00433 extern uint64 PSStatsDoAnimate;
00434 extern uint64 PSStatsDoAnimatePart1;
00435 extern uint64 PSStatsDoAnimatePart2;
00436 extern uint64 PSStatsDoAnimatePart3;
00437 extern uint64 PSStatsTraverseRender;
00438 extern uint64 PSStatsTraverseClip;
00439 extern uint64 PSStatsClipSystemInstanciated;
00440 extern uint64 PSStatsClipSystemNotInstanciated;
00441 extern uint64 PSStatsClipSystemCheckAgainstPyramid;
00442 extern uint64 PSStatsInsertInVisibleList;
00443 extern uint64 PSStatsCheckDestroyCondition;
00444 extern uint64 PSStatsForceInstanciate;
00445 extern uint64 PSAnim1;
00446 extern uint64 PSAnim2;
00447 extern uint64 PSAnim3;
00448 extern uint64 PSAnim4;
00449 extern uint64 PSAnim5;
00450 extern uint64 PSAnim6;
00451 extern uint64 PSAnim7;
00452 extern uint64 PSAnim8;
00453 extern uint64 PSAnim9;
00454 extern uint64 PSAnim10;
00455 extern uint64 PSAnim11;
00456 extern uint PSStatsNumDoAnimateCalls;;
00457 extern float PSMaxET;
00458 extern uint PSMaxNBPass;
00459 extern uint64 PSStatsZonePlane;
00460 extern uint64 PSStatsZoneSphere;
00461 extern uint64 PSStatsZoneDisc;
00462 extern uint64 PSStatsZoneRectangle;
00463 extern uint64 PSStatsZoneCylinder;
00464 extern uint64 PSMotion1;
00465 extern uint64 PSMotion2;
00466 extern uint64 PSMotion3;
00467 extern uint64 PSMotion4;
00468 extern uint64 PSStatCollision;
00469 extern uint64 PSStatEmit;
00470 extern uint64 PSStatRender;
00471 
00472 */
00473 
00474 
00475 
00476 
00477 
00478 } // NL3D
00479 
00480 
00481 
00482 
00483 
00484 
00485 #endif // NL_PARTICLE_SYSTEM_MODEL_H
00486 
00487 /* End of particle_system_model.h */
00488 
00489 
00490 
00491 
00492 
00493 
00494 
00495 
00496 
00497 
00498 
00499 
00500 
00501 
00502 
00503 
00504 
00505 
00506 
00507 
00508 

Generated on Thu Jan 7 08:26:19 2010 for NeL by  doxygen 1.6.1