2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2002-06-05 20:44:55 +08:00
|
|
|
//osgParticle - Copyright (C) 2002 Marco Jez
|
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
#ifndef OSGPARTICLE_PARTICLEPROCESSOR
|
|
|
|
#define OSGPARTICLE_PARTICLEPROCESSOR 1
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
#include <osgParticle/Export>
|
|
|
|
#include <osgParticle/ParticleSystem>
|
|
|
|
|
|
|
|
#include <osg/ref_ptr>
|
|
|
|
#include <osg/Object>
|
2003-08-14 08:05:34 +08:00
|
|
|
#include <osg/Transform>
|
2002-06-05 20:44:55 +08:00
|
|
|
#include <osg/NodeVisitor>
|
|
|
|
#include <osg/CopyOp>
|
|
|
|
#include <osg/Vec3>
|
|
|
|
#include <osg/Matrix>
|
|
|
|
|
|
|
|
namespace osgParticle
|
|
|
|
{
|
|
|
|
|
2005-05-21 05:01:57 +08:00
|
|
|
/** A common base interface for those classes which need to do something on particles. Such classes
|
|
|
|
* are, for example, Emitter (particle generation) and Program (particle animation).
|
|
|
|
* This class holds some properties, like a <I>reference frame</I> and a reference to a ParticleSystem;
|
|
|
|
* descendant classes should process the particles taking into account the reference frame, computing the right
|
|
|
|
* transformations when needed.
|
2002-06-05 20:44:55 +08:00
|
|
|
*/
|
|
|
|
class OSGPARTICLE_EXPORT ParticleProcessor: public osg::Node {
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum ReferenceFrame {
|
2004-10-25 04:04:00 +08:00
|
|
|
RELATIVE_RF,
|
|
|
|
ABSOLUTE_RF
|
2002-06-05 20:44:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ParticleProcessor();
|
2005-04-29 17:47:57 +08:00
|
|
|
ParticleProcessor(const ParticleProcessor& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
|
2002-06-05 20:44:55 +08:00
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
virtual const char* libraryName() const { return "osgParticle"; }
|
|
|
|
virtual const char* className() const { return "ParticleProcessor"; }
|
|
|
|
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const ParticleProcessor*>(obj) != 0; }
|
2002-06-05 20:44:55 +08:00
|
|
|
virtual void accept(osg::NodeVisitor& nv) { if (nv.validNodeMask(*this)) { nv.pushOntoNodePath(this); nv.apply(*this); nv.popFromNodePath(); } }
|
|
|
|
|
|
|
|
/// Get the reference frame.
|
|
|
|
inline ReferenceFrame getReferenceFrame() const;
|
|
|
|
|
|
|
|
/// Set the reference frame.
|
|
|
|
inline void setReferenceFrame(ReferenceFrame rf);
|
|
|
|
|
|
|
|
/// Get whether this processor is enabled or not.
|
2010-01-28 01:09:05 +08:00
|
|
|
bool getEnabled() const { return _enabled; }
|
2002-06-05 20:44:55 +08:00
|
|
|
inline bool isEnabled() const;
|
|
|
|
|
|
|
|
/// Set whether this processor is enabled or not.
|
|
|
|
inline void setEnabled(bool v);
|
|
|
|
|
|
|
|
/// Get a pointer to the destination particle system.
|
2005-04-29 17:47:57 +08:00
|
|
|
inline ParticleSystem* getParticleSystem();
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
/// Get a const pointer to the destination particle system.
|
2005-04-29 17:47:57 +08:00
|
|
|
inline const ParticleSystem* getParticleSystem() const;
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
/// Set the destination particle system.
|
2005-04-29 17:47:57 +08:00
|
|
|
inline void setParticleSystem(ParticleSystem* ps);
|
2002-06-05 20:44:55 +08:00
|
|
|
|
2003-09-03 04:39:41 +08:00
|
|
|
/// Set the endless flag of this processor.
|
|
|
|
inline void setEndless(bool type);
|
|
|
|
|
|
|
|
/// Check whether this processor is endless.
|
2010-01-28 01:09:05 +08:00
|
|
|
bool getEndless() const { return _endless; }
|
2003-09-03 04:39:41 +08:00
|
|
|
inline bool isEndless() const;
|
|
|
|
|
|
|
|
/// Set the lifetime of this processor.
|
|
|
|
inline void setLifeTime(double t);
|
|
|
|
|
|
|
|
/// Get the lifetime of this processor.
|
|
|
|
inline double getLifeTime() const;
|
|
|
|
|
|
|
|
/// Set the start time of this processor.
|
|
|
|
inline void setStartTime(double t);
|
|
|
|
|
|
|
|
/// Get the start time of this processor.
|
|
|
|
inline double getStartTime() const;
|
|
|
|
|
|
|
|
/// Set the current time of this processor.
|
|
|
|
inline void setCurrentTime(double t);
|
|
|
|
|
|
|
|
/// Get the current time of this processor.
|
|
|
|
inline double getCurrentTime() const;
|
|
|
|
|
|
|
|
/// Set the reset time of this processor. A value of 0 disables reset.
|
|
|
|
inline void setResetTime(double t);
|
|
|
|
|
|
|
|
/// Get the reset time of this processor.
|
|
|
|
inline double getResetTime() const;
|
|
|
|
|
2004-03-18 04:31:55 +08:00
|
|
|
/**
|
|
|
|
Check whether the processor is alive with respect to start time and
|
|
|
|
life duration. Note that this method may return true even if the
|
|
|
|
processor has been disabled by calling setEnabled(false). To test
|
|
|
|
whether the processor is actually processing particles or not, you
|
|
|
|
should evaluate (isEnabled() && isAlive()).
|
|
|
|
*/
|
|
|
|
inline bool isAlive() const;
|
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
void traverse(osg::NodeVisitor& nv);
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
/// Get the current local-to-world transformation matrix (valid only during cull traversal).
|
2005-04-29 17:47:57 +08:00
|
|
|
inline const osg::Matrix& getLocalToWorldMatrix();
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
/// Get the current world-to-local transformation matrix (valid only during cull traversal).
|
2005-04-29 17:47:57 +08:00
|
|
|
inline const osg::Matrix& getWorldToLocalMatrix();
|
2002-06-05 20:44:55 +08:00
|
|
|
|
2005-08-25 22:12:08 +08:00
|
|
|
/// Get the previous local-to-world transformation matrix (valid only during cull traversal).
|
|
|
|
inline const osg::Matrix& getPreviousLocalToWorldMatrix();
|
|
|
|
|
|
|
|
/// Get the previous world-to-local transformation matrix (valid only during cull traversal).
|
|
|
|
inline const osg::Matrix& getPreviousWorldToLocalMatrix();
|
|
|
|
|
|
|
|
|
2002-06-05 20:44:55 +08:00
|
|
|
/// Transform a point from local to world coordinates (valid only during cull traversal).
|
2005-04-29 17:47:57 +08:00
|
|
|
inline osg::Vec3 transformLocalToWorld(const osg::Vec3& P);
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
/// Transform a vector from local to world coordinates, discarding translation (valid only during cull traversal).
|
2005-04-29 17:47:57 +08:00
|
|
|
inline osg::Vec3 rotateLocalToWorld(const osg::Vec3& P);
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
/// Transform a point from world to local coordinates (valid only during cull traversal).
|
2005-04-29 17:47:57 +08:00
|
|
|
inline osg::Vec3 transformWorldToLocal(const osg::Vec3& P);
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
/// Transform a vector from world to local coordinates, discarding translation (valid only during cull traversal).
|
2005-04-29 17:47:57 +08:00
|
|
|
inline osg::Vec3 rotateWorldToLocal(const osg::Vec3& P);
|
2002-06-05 20:44:55 +08:00
|
|
|
|
2005-05-12 22:03:22 +08:00
|
|
|
virtual osg::BoundingSphere computeBound() const;
|
|
|
|
|
2002-06-05 20:44:55 +08:00
|
|
|
protected:
|
|
|
|
virtual ~ParticleProcessor() {}
|
2005-04-29 17:47:57 +08:00
|
|
|
ParticleProcessor& operator=(const ParticleProcessor&) { return *this; }
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
virtual void process(double dt) = 0;
|
|
|
|
|
|
|
|
private:
|
2005-04-29 17:47:57 +08:00
|
|
|
ReferenceFrame _rf;
|
|
|
|
bool _enabled;
|
|
|
|
double _t0;
|
|
|
|
osg::ref_ptr<ParticleSystem> _ps;
|
2005-08-25 22:12:08 +08:00
|
|
|
bool _first_ltw_compute;
|
2005-04-29 17:47:57 +08:00
|
|
|
bool _need_ltw_matrix;
|
2005-08-25 22:12:08 +08:00
|
|
|
bool _first_wtl_compute;
|
2005-04-29 17:47:57 +08:00
|
|
|
bool _need_wtl_matrix;
|
|
|
|
osg::Matrix _ltw_matrix;
|
|
|
|
osg::Matrix _wtl_matrix;
|
2005-08-25 22:12:08 +08:00
|
|
|
osg::Matrix _previous_ltw_matrix;
|
|
|
|
osg::Matrix _previous_wtl_matrix;
|
2005-04-29 17:47:57 +08:00
|
|
|
osg::NodeVisitor* _current_nodevisitor;
|
|
|
|
|
|
|
|
bool _endless;
|
|
|
|
|
|
|
|
double _lifeTime;
|
|
|
|
double _startTime;
|
|
|
|
double _currentTime;
|
|
|
|
double _resetTime;
|
2006-02-05 05:12:45 +08:00
|
|
|
|
|
|
|
//added- 1/17/06- bgandere@nps.edu
|
|
|
|
//a var to keep from doing multiple updates
|
|
|
|
int _frameNumber;
|
2002-06-05 20:44:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// INLINE FUNCTIONS
|
|
|
|
|
|
|
|
inline ParticleProcessor::ReferenceFrame ParticleProcessor::getReferenceFrame() const
|
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
return _rf;
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void ParticleProcessor::setReferenceFrame(ReferenceFrame rf)
|
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
_rf = rf;
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ParticleProcessor::isEnabled() const
|
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
return _enabled;
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void ParticleProcessor::setEnabled(bool v)
|
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
_enabled = v;
|
|
|
|
if (_enabled)
|
|
|
|
{
|
|
|
|
_t0 = -1;
|
|
|
|
_currentTime = 0;
|
|
|
|
}
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
inline ParticleSystem* ParticleProcessor::getParticleSystem()
|
2002-06-05 20:44:55 +08:00
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
return _ps.get();
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
inline const ParticleSystem* ParticleProcessor::getParticleSystem() const
|
2002-06-05 20:44:55 +08:00
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
return _ps.get();
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
inline void ParticleProcessor::setParticleSystem(ParticleSystem* ps)
|
2002-06-05 20:44:55 +08:00
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
_ps = ps;
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
2003-09-03 04:39:41 +08:00
|
|
|
|
|
|
|
inline void ParticleProcessor::setEndless(bool type)
|
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
_endless = type;
|
2003-09-03 04:39:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool ParticleProcessor::isEndless() const
|
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
return _endless;
|
2003-09-03 04:39:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void ParticleProcessor::setLifeTime(double t)
|
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
_lifeTime = t;
|
|
|
|
}
|
2003-09-03 04:39:41 +08:00
|
|
|
|
|
|
|
inline double ParticleProcessor::getLifeTime() const
|
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
return _lifeTime;
|
|
|
|
}
|
|
|
|
|
2003-09-03 04:39:41 +08:00
|
|
|
inline void ParticleProcessor::setStartTime(double t)
|
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
_startTime = t;
|
|
|
|
}
|
2003-09-03 04:39:41 +08:00
|
|
|
|
|
|
|
inline double ParticleProcessor::getStartTime() const
|
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
return _startTime;
|
|
|
|
}
|
2003-09-03 04:39:41 +08:00
|
|
|
inline void ParticleProcessor::setCurrentTime(double t)
|
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
_currentTime = t;
|
|
|
|
}
|
2003-09-03 04:39:41 +08:00
|
|
|
|
|
|
|
inline double ParticleProcessor::getCurrentTime() const
|
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
return _currentTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void ParticleProcessor::setResetTime(double t)
|
|
|
|
{
|
|
|
|
_resetTime = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline double ParticleProcessor::getResetTime() const
|
|
|
|
{
|
|
|
|
return _resetTime;
|
|
|
|
}
|
2002-06-05 20:44:55 +08:00
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
inline const osg::Matrix& ParticleProcessor::getLocalToWorldMatrix()
|
2002-06-05 20:44:55 +08:00
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
if (_need_ltw_matrix) {
|
2005-08-25 22:12:08 +08:00
|
|
|
_previous_ltw_matrix = _ltw_matrix;
|
2005-04-29 17:47:57 +08:00
|
|
|
_ltw_matrix = osg::computeLocalToWorld(_current_nodevisitor->getNodePath());
|
2005-08-25 22:12:08 +08:00
|
|
|
if (_first_ltw_compute)
|
|
|
|
{
|
|
|
|
_previous_ltw_matrix = _ltw_matrix;
|
|
|
|
_first_ltw_compute = false;
|
|
|
|
}
|
2005-04-29 17:47:57 +08:00
|
|
|
_need_ltw_matrix = false;
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
2005-04-29 17:47:57 +08:00
|
|
|
return _ltw_matrix;
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
inline const osg::Matrix& ParticleProcessor::getWorldToLocalMatrix()
|
2002-06-05 20:44:55 +08:00
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
if (_need_wtl_matrix) {
|
2005-08-25 22:12:08 +08:00
|
|
|
_previous_wtl_matrix = _wtl_matrix;
|
2005-04-29 17:47:57 +08:00
|
|
|
_wtl_matrix = osg::computeWorldToLocal(_current_nodevisitor->getNodePath());
|
2005-08-25 22:12:08 +08:00
|
|
|
if (_first_wtl_compute)
|
|
|
|
{
|
|
|
|
_previous_wtl_matrix = _wtl_matrix;
|
|
|
|
_first_wtl_compute = false;
|
|
|
|
}
|
2005-04-29 17:47:57 +08:00
|
|
|
_need_wtl_matrix = false;
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
2005-04-29 17:47:57 +08:00
|
|
|
return _wtl_matrix;
|
2002-06-05 20:44:55 +08:00
|
|
|
}
|
|
|
|
|
2005-08-25 22:12:08 +08:00
|
|
|
inline const osg::Matrix& ParticleProcessor::getPreviousLocalToWorldMatrix()
|
|
|
|
{
|
|
|
|
if (_need_ltw_matrix) getLocalToWorldMatrix();
|
|
|
|
return _previous_ltw_matrix;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const osg::Matrix& ParticleProcessor::getPreviousWorldToLocalMatrix()
|
|
|
|
{
|
|
|
|
if (_need_wtl_matrix) getWorldToLocalMatrix();
|
|
|
|
return _previous_wtl_matrix;
|
|
|
|
}
|
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
inline osg::Vec3 ParticleProcessor::transformLocalToWorld(const osg::Vec3& P)
|
2002-06-05 20:44:55 +08:00
|
|
|
{
|
|
|
|
return getLocalToWorldMatrix().preMult(P);
|
|
|
|
}
|
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
inline osg::Vec3 ParticleProcessor::transformWorldToLocal(const osg::Vec3& P)
|
2002-06-05 20:44:55 +08:00
|
|
|
{
|
|
|
|
return getWorldToLocalMatrix().preMult(P);
|
|
|
|
}
|
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
inline osg::Vec3 ParticleProcessor::rotateLocalToWorld(const osg::Vec3& P)
|
2002-06-05 20:44:55 +08:00
|
|
|
{
|
|
|
|
return getLocalToWorldMatrix().preMult(P) -
|
|
|
|
getLocalToWorldMatrix().preMult(osg::Vec3(0, 0, 0));
|
|
|
|
}
|
|
|
|
|
2005-04-29 17:47:57 +08:00
|
|
|
inline osg::Vec3 ParticleProcessor::rotateWorldToLocal(const osg::Vec3& P)
|
2002-06-05 20:44:55 +08:00
|
|
|
{
|
|
|
|
return getWorldToLocalMatrix().preMult(P) -
|
|
|
|
getWorldToLocalMatrix().preMult(osg::Vec3(0, 0, 0));
|
|
|
|
}
|
2004-03-18 04:31:55 +08:00
|
|
|
|
|
|
|
inline bool ParticleProcessor::isAlive() const
|
|
|
|
{
|
2005-04-29 17:47:57 +08:00
|
|
|
return _currentTime < (_lifeTime + _startTime);
|
2004-03-18 04:31:55 +08:00
|
|
|
}
|
2002-06-05 20:44:55 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|