Form Wang Rui, "An initial GLSL shader support of rendering particles. Only the POINT
type is supported at present. The attached osgparticleshader.cpp will show how it works. It can also be placed in the examples folder. But I just wonder how this example co-exists with another two (osgparticle and osgparticleeffect)? Member variables in Particle, including _alive, _current_size and _current_alpha, are now merged into one Vec3 variable. Then we can make use of the set...Pointer() methods to treat them as vertex attribtues in GLSL. User interfaces are not changed. Additional methods of ParticleSystem are introduced, including setDefaultAttributesUsingShaders(), setSortMode() and setVisibilityDistance(). You can see how they work in osgparticleshader.cpp. Additional user-defined particle type is introduced. Set the particle type to USER and attach a drawable to the template. Be careful because of possible huge memory consumption. It is highly suggested to use display lists here. The ParticleSystemUpdater can accepts ParticleSystem objects as child drawables now. I myself think it is a little simpler in structure, than creating a new geode for each particle system. Of course, the latter is still compatible, and can be used to transform entire particles in the world. New particle operators: bounce, sink, damping, orbit and explosion. The bounce and sink opeartors both use a concept of domains, and can simulate a very basic collision of particles and objects. New composite placer. It contains a set of placers and emit particles from them randomly. The added virtual method size() of each placer will help determine the probability of generating. New virtual method operateParticles() for the Operator class. It actually calls operate() for each particle, but can be overrode to use speedup techniques like SSE, or even shaders in the future. Partly fix a floating error of 'delta time' in emitter, program and updaters. Previously they keep the _t0 variable seperately and compute different copies of dt by themseleves, which makes some operators, especially the BounceOperator, work incorrectly (because the dt in operators and updaters are slightly different). Now a getDeltaTime() method is maintained in ParticleSystem, and will return the unique dt value (passing by reference) for use. This makes thing better, but still very few unexpected behavours at present... All dotosg and serialzier wrappers for functionalities above are provided. ... According to some simple tests, the new shader support is slightly efficient than ordinary glBegin()/end(). That means, I haven't got a big improvement at present. I think the bottlenack here seems to be the cull traversal time. Because operators go through the particle list again and again (for example, the fountain in the shader example requires 4 operators working all the time). A really ideal solution here is to implement the particle operators in shaders, too, and copy the results back to particle attributes. The concept of GPGPU is good for implementing this. But in my opinion, the Camera class seems to be too heavy for realizing such functionality in a particle system. Myabe a light-weight ComputeDrawable class is enough for receiving data as textures and outputting the results to the FBO render buffer. What do you think then? The floating error of emitters (http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/2009-May/028435.html) is not solved this time. But what I think is worth testing is that we could directly compute the node path from the emitter to the particle system rather than multiplying the worldToLocal and LocalToWorld matrices. I'll try this idea later. "
This commit is contained in:
94
include/osgParticle/AngularDampingOperator
Normal file
94
include/osgParticle/AngularDampingOperator
Normal file
@@ -0,0 +1,94 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
// Written by Wang Rui, (C) 2010
|
||||
|
||||
#ifndef OSGPARTICLE_ANGULARDAMPINGOPERATOR
|
||||
#define OSGPARTICLE_ANGULARDAMPINGOPERATOR
|
||||
|
||||
#include <osgParticle/Operator>
|
||||
#include <osgParticle/Particle>
|
||||
|
||||
namespace osgParticle
|
||||
{
|
||||
|
||||
|
||||
/** A angular damping operator applies damping constant to particle's angular velocity.
|
||||
Refer to David McAllister's Particle System API (http://www.particlesystems.org)
|
||||
*/
|
||||
class AngularDampingOperator : public Operator
|
||||
{
|
||||
public:
|
||||
AngularDampingOperator() : Operator(), _cutoffLow(0.0f), _cutoffHigh(FLT_MAX)
|
||||
{}
|
||||
|
||||
AngularDampingOperator( const AngularDampingOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
|
||||
: Operator(copy, copyop), _damping(copy._damping),
|
||||
_cutoffLow(copy._cutoffLow), _cutoffHigh(copy._cutoffHigh)
|
||||
{}
|
||||
|
||||
META_Object( osgParticle, AngularDampingOperator );
|
||||
|
||||
/// Set the damping factors
|
||||
void setDamping( float x, float y, float z ) { _damping.set(x, y, z); }
|
||||
void setDamping( const osg::Vec3& damping ) { _damping = damping; }
|
||||
|
||||
/// Set the damping factors to one value
|
||||
void setDamping( float x ) { _damping.set(x, x, x); }
|
||||
|
||||
/// Get the damping factors
|
||||
void getDamping( float& x, float& y, float& z ) const
|
||||
{ x = _damping.x(); y = _damping.y(); z = _damping.z(); }
|
||||
|
||||
const osg::Vec3& getDamping() const { return _damping; }
|
||||
|
||||
/// Set the velocity cutoff factors
|
||||
void setCutoff( float low, float high ) { _cutoffLow = low; _cutoffHigh = high; }
|
||||
void setCutoffLow( float low ) { _cutoffLow = low; }
|
||||
void setCutoffHigh( float low ) { _cutoffHigh = low; }
|
||||
|
||||
/// Get the velocity cutoff factors
|
||||
void getCutoff( float& low, float& high ) const { low = _cutoffLow; high = _cutoffHigh; }
|
||||
float getCutoffLow() const { return _cutoffLow; }
|
||||
float getCutoffHigh() const { return _cutoffHigh; }
|
||||
|
||||
/// Apply the acceleration to a particle. Do not call this method manually.
|
||||
inline void operate( Particle* P, double dt );
|
||||
|
||||
protected:
|
||||
virtual ~AngularDampingOperator() {}
|
||||
AngularDampingOperator& operator=( const AngularDampingOperator& ) { return *this; }
|
||||
|
||||
osg::Vec3 _damping;
|
||||
float _cutoffLow;
|
||||
float _cutoffHigh;
|
||||
};
|
||||
|
||||
// INLINE METHODS
|
||||
|
||||
inline void AngularDampingOperator::operate( Particle* P, double dt )
|
||||
{
|
||||
const osg::Vec3& vel = P->getAngularVelocity();
|
||||
float length2 = vel.length2();
|
||||
if ( length2>=_cutoffLow && length2<=_cutoffHigh )
|
||||
{
|
||||
osg::Vec3 newvel( vel.x() * (1.0f - (1.0f - _damping.x()) * dt),
|
||||
vel.y() * (1.0f - (1.0f - _damping.y()) * dt),
|
||||
vel.z() * (1.0f - (1.0f - _damping.z()) * dt) );
|
||||
P->setAngularVelocity( newvel );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
78
include/osgParticle/BounceOperator
Normal file
78
include/osgParticle/BounceOperator
Normal file
@@ -0,0 +1,78 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
// Written by Wang Rui, (C) 2010
|
||||
|
||||
#ifndef OSGPARTICLE_BOUNCEOPERATOR
|
||||
#define OSGPARTICLE_BOUNCEOPERATOR
|
||||
|
||||
#include <osgParticle/Particle>
|
||||
#include <osgParticle/DomainOperator>
|
||||
|
||||
namespace osgParticle
|
||||
{
|
||||
|
||||
|
||||
/** A bounce operator can affect the particle's velocity to make it rebound.
|
||||
Refer to David McAllister's Particle System API (http://www.particlesystems.org)
|
||||
*/
|
||||
class OSGPARTICLE_EXPORT BounceOperator : public DomainOperator
|
||||
{
|
||||
public:
|
||||
BounceOperator()
|
||||
: DomainOperator(), _friction(1.0f), _resilience(0.0f), _cutoff(0.0f)
|
||||
{}
|
||||
|
||||
BounceOperator( const BounceOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
|
||||
: DomainOperator(copy, copyop),
|
||||
_friction(copy._friction), _resilience(copy._resilience), _cutoff(copy._cutoff)
|
||||
{}
|
||||
|
||||
META_Object( osgParticle, BounceOperator );
|
||||
|
||||
/// Set the friction
|
||||
void setFriction( float f ) { _friction = f; }
|
||||
|
||||
/// Get the friction
|
||||
float getFriction() const { return _friction; }
|
||||
|
||||
/// Set the resilience
|
||||
void setResilience( float r ) { _resilience = r; }
|
||||
|
||||
/// Get the velocity cutoff factor
|
||||
float getResilience() const { return _resilience; }
|
||||
|
||||
/// Set the velocity cutoff factor
|
||||
void setCutoff( float v ) { _cutoff = v; }
|
||||
|
||||
/// Get the velocity cutoff factor
|
||||
float getCutoff() const { return _cutoff; }
|
||||
|
||||
protected:
|
||||
virtual ~BounceOperator() {}
|
||||
BounceOperator& operator=( const BounceOperator& ) { return *this; }
|
||||
|
||||
virtual void handleTriangle( const Domain& domain, Particle* P, double dt );
|
||||
virtual void handleRectangle( const Domain& domain, Particle* P, double dt );
|
||||
virtual void handlePlane( const Domain& domain, Particle* P, double dt );
|
||||
virtual void handleSphere( const Domain& domain, Particle* P, double dt );
|
||||
virtual void handleDisk( const Domain& domain, Particle* P, double dt );
|
||||
|
||||
float _friction;
|
||||
float _resilience;
|
||||
float _cutoff;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -69,6 +69,9 @@ namespace osgParticle
|
||||
|
||||
/// Place a particle. Do not call it manually.
|
||||
inline void place(Particle* P) const;
|
||||
|
||||
/// return the volume of the box
|
||||
inline float size() const;
|
||||
|
||||
/// return the control position
|
||||
inline osg::Vec3 getControlPosition() const;
|
||||
@@ -153,6 +156,13 @@ namespace osgParticle
|
||||
|
||||
P->setPosition(pos);
|
||||
}
|
||||
|
||||
inline float BoxPlacer::size() const
|
||||
{
|
||||
return (_x_range.maximum - _x_range.minimum) *
|
||||
(_y_range.maximum - _y_range.minimum) *
|
||||
(_z_range.maximum - _z_range.minimum);
|
||||
}
|
||||
|
||||
inline osg::Vec3 BoxPlacer::getControlPosition() const
|
||||
{
|
||||
|
||||
104
include/osgParticle/CompositePlacer
Normal file
104
include/osgParticle/CompositePlacer
Normal file
@@ -0,0 +1,104 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
// Written by Wang Rui, (C) 2010
|
||||
|
||||
#ifndef OSGPARTICLE_COMPOSITEPLACER
|
||||
#define OSGPARTICLE_COMPOSITEPLACER
|
||||
|
||||
#include <osgParticle/Placer>
|
||||
#include <osgParticle/Particle>
|
||||
|
||||
namespace osgParticle
|
||||
{
|
||||
|
||||
|
||||
/** A composite particle placer which allows particles to be generated from a union of placers. */
|
||||
class CompositePlacer : public Placer
|
||||
{
|
||||
public:
|
||||
CompositePlacer() : Placer() {}
|
||||
|
||||
CompositePlacer( const CompositePlacer& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
|
||||
: Placer(copy, copyop), _placers(copy._placers) {}
|
||||
|
||||
META_Object( osgParticle, CompositePlacer );
|
||||
|
||||
// Set a child placer at specific index
|
||||
void setPlacer( unsigned int i, Placer* p )
|
||||
{
|
||||
if (i<_placers.size()) _placers[i] = p;
|
||||
else addPlacer(p);
|
||||
}
|
||||
|
||||
/// Add a child placer
|
||||
void addPlacer( Placer* p ) { _placers.push_back(p); }
|
||||
|
||||
/// Remove a child placer
|
||||
void removePlacer( unsigned int i )
|
||||
{ if (i<_placers.size()) _placers.erase(_placers.begin()+i); }
|
||||
|
||||
/// Get a child placer
|
||||
Placer* getPlacer( unsigned int i ) { return _placers.at(i); }
|
||||
const Placer* getPlacer( unsigned int i ) const { return _placers.at(i); }
|
||||
|
||||
/// Get number of placers
|
||||
unsigned int getNumPlacers() const { return _placers.size(); }
|
||||
|
||||
/// Place a particle. Do not call it manually.
|
||||
inline void place( Particle* P ) const;
|
||||
|
||||
/// return the volume of the box
|
||||
inline float size() const;
|
||||
|
||||
/// return the control position
|
||||
inline osg::Vec3 getControlPosition() const;
|
||||
|
||||
protected:
|
||||
virtual ~CompositePlacer() {}
|
||||
CompositePlacer& operator=( const CompositePlacer& ) { return *this; }
|
||||
|
||||
typedef std::vector< osg::ref_ptr<Placer> > PlacerList;
|
||||
PlacerList _placers;
|
||||
};
|
||||
|
||||
// INLINE METHODS
|
||||
|
||||
inline void CompositePlacer::place( Particle* P ) const
|
||||
{
|
||||
rangef sizeRange( 0.0f, size() );
|
||||
float current = 0.0f, selected = sizeRange.get_random();
|
||||
for ( PlacerList::const_iterator itr=_placers.begin(); itr!=_placers.end(); ++itr )
|
||||
{
|
||||
current += (*itr)->size();
|
||||
if ( selected<=current ) (*itr)->place( P );
|
||||
}
|
||||
}
|
||||
|
||||
inline float CompositePlacer::size() const
|
||||
{
|
||||
float total_size = 0.0f;
|
||||
for ( PlacerList::const_iterator itr=_placers.begin(); itr!=_placers.end(); ++itr )
|
||||
total_size += (*itr)->size();
|
||||
return total_size;
|
||||
}
|
||||
|
||||
inline osg::Vec3 CompositePlacer::getControlPosition() const
|
||||
{
|
||||
if ( !_placers.size() ) return osg::Vec3();
|
||||
return _placers.front()->getControlPosition();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
94
include/osgParticle/DampingOperator
Normal file
94
include/osgParticle/DampingOperator
Normal file
@@ -0,0 +1,94 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
// Written by Wang Rui, (C) 2010
|
||||
|
||||
#ifndef OSGPARTICLE_DAMPINGOPERATOR
|
||||
#define OSGPARTICLE_DAMPINGOPERATOR
|
||||
|
||||
#include <osgParticle/Operator>
|
||||
#include <osgParticle/Particle>
|
||||
|
||||
namespace osgParticle
|
||||
{
|
||||
|
||||
|
||||
/** A damping operator applies damping constant to particle's velocity.
|
||||
Refer to David McAllister's Particle System API (http://www.particlesystems.org)
|
||||
*/
|
||||
class DampingOperator : public Operator
|
||||
{
|
||||
public:
|
||||
DampingOperator() : Operator(), _cutoffLow(0.0f), _cutoffHigh(FLT_MAX)
|
||||
{ _damping.set(1.0f, 1.0f, 1.0f); }
|
||||
|
||||
DampingOperator( const DampingOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
|
||||
: Operator(copy, copyop), _damping(copy._damping),
|
||||
_cutoffLow(copy._cutoffLow), _cutoffHigh(copy._cutoffHigh)
|
||||
{}
|
||||
|
||||
META_Object( osgParticle, DampingOperator );
|
||||
|
||||
/// Set the damping factors
|
||||
void setDamping( float x, float y, float z ) { _damping.set(x, y, z); }
|
||||
void setDamping( const osg::Vec3& damping ) { _damping = damping; }
|
||||
|
||||
/// Set the damping factors to one value
|
||||
void setDamping( float x ) { _damping.set(x, x, x); }
|
||||
|
||||
/// Get the damping factors
|
||||
void getDamping( float& x, float& y, float& z ) const
|
||||
{ x = _damping.x(); y = _damping.y(); z = _damping.z(); }
|
||||
|
||||
const osg::Vec3& getDamping() const { return _damping; }
|
||||
|
||||
/// Set the velocity cutoff factors
|
||||
void setCutoff( float low, float high ) { _cutoffLow = low; _cutoffHigh = high; }
|
||||
void setCutoffLow( float low ) { _cutoffLow = low; }
|
||||
void setCutoffHigh( float low ) { _cutoffHigh = low; }
|
||||
|
||||
/// Get the velocity cutoff factors
|
||||
void getCutoff( float& low, float& high ) const { low = _cutoffLow; high = _cutoffHigh; }
|
||||
float getCutoffLow() const { return _cutoffLow; }
|
||||
float getCutoffHigh() const { return _cutoffHigh; }
|
||||
|
||||
/// Apply the acceleration to a particle. Do not call this method manually.
|
||||
inline void operate( Particle* P, double dt );
|
||||
|
||||
protected:
|
||||
virtual ~DampingOperator() {}
|
||||
DampingOperator& operator=( const DampingOperator& ) { return *this; }
|
||||
|
||||
osg::Vec3 _damping;
|
||||
float _cutoffLow;
|
||||
float _cutoffHigh;
|
||||
};
|
||||
|
||||
// INLINE METHODS
|
||||
|
||||
inline void DampingOperator::operate( Particle* P, double dt )
|
||||
{
|
||||
const osg::Vec3& vel = P->getVelocity();
|
||||
float length2 = vel.length2();
|
||||
if ( length2>=_cutoffLow && length2<=_cutoffHigh )
|
||||
{
|
||||
osg::Vec3 newvel( vel.x() * (1.0f - (1.0f - _damping.x()) * dt),
|
||||
vel.y() * (1.0f - (1.0f - _damping.y()) * dt),
|
||||
vel.z() * (1.0f - (1.0f - _damping.z()) * dt) );
|
||||
P->setVelocity( newvel );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
235
include/osgParticle/DomainOperator
Normal file
235
include/osgParticle/DomainOperator
Normal file
@@ -0,0 +1,235 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
// Written by Wang Rui, (C) 2010
|
||||
|
||||
#ifndef OSGPARTICLE_DOMAINOPERATOR
|
||||
#define OSGPARTICLE_DOMAINOPERATOR
|
||||
|
||||
#include <osg/Plane>
|
||||
#include <osgParticle/Operator>
|
||||
#include <osgParticle/Particle>
|
||||
|
||||
namespace osgParticle
|
||||
{
|
||||
|
||||
|
||||
/** A domain operator which accepts different domain shapes as the parameter.
|
||||
It can be derived to implement operators that require particles interacting with domains.
|
||||
Refer to David McAllister's Particle System API (http://www.particlesystems.org)
|
||||
*/
|
||||
class OSGPARTICLE_EXPORT DomainOperator : public Operator
|
||||
{
|
||||
public:
|
||||
struct Domain
|
||||
{
|
||||
enum Type
|
||||
{
|
||||
UNDEFINED_DOMAIN = 0,
|
||||
POINT_DOMAIN,
|
||||
LINE_DOMAIN,
|
||||
TRI_DOMAIN,
|
||||
RECT_DOMAIN,
|
||||
PLANE_DOMAIN,
|
||||
SPHERE_DOMAIN,
|
||||
BOX_DOMAIN,
|
||||
DISK_DOMAIN
|
||||
};
|
||||
|
||||
Domain( Type t ) : r1(0.0f), r2(0.0f), type(t) {}
|
||||
osg::Plane plane;
|
||||
osg::Vec3 v1;
|
||||
osg::Vec3 v2;
|
||||
osg::Vec3 v3;
|
||||
osg::Vec3 s1;
|
||||
osg::Vec3 s2;
|
||||
float r1;
|
||||
float r2;
|
||||
Type type;
|
||||
};
|
||||
|
||||
DomainOperator()
|
||||
: Operator()
|
||||
{}
|
||||
|
||||
DomainOperator( const DomainOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
|
||||
: Operator(copy, copyop), _domains(copy._domains), _backupDomains(copy._backupDomains)
|
||||
{}
|
||||
|
||||
META_Object( osgParticle, DomainOperator );
|
||||
|
||||
/// Add a point domain
|
||||
inline void addPointDomain( const osg::Vec3& p );
|
||||
|
||||
/// Add a line segment domain
|
||||
inline void addLineSegmentDomain( const osg::Vec3& v1, const osg::Vec3& v2 );
|
||||
|
||||
/// Add a triangle domain
|
||||
inline void addTriangleDomain( const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3 );
|
||||
|
||||
/// Add a rectangle domain
|
||||
inline void addRectangleDomain( const osg::Vec3& corner, const osg::Vec3& w, const osg::Vec3& h );
|
||||
|
||||
/// Add a plane domain
|
||||
inline void addPlaneDomain( const osg::Plane& plane );
|
||||
|
||||
/// Add a sphere domain
|
||||
inline void addSphereDomain( const osg::Vec3& c, float r );
|
||||
|
||||
/// Add a box domain
|
||||
inline void addBoxDomain( const osg::Vec3& min, const osg::Vec3& max );
|
||||
|
||||
/// Add a disk domain
|
||||
inline void addDiskDomain( const osg::Vec3& c, const osg::Vec3& n, float r1, float r2=0.0f );
|
||||
|
||||
/// Add a domain object directly, used by the .osg wrappers and serializers.
|
||||
void addDomain( const Domain& domain ) { _domains.push_back(domain); }
|
||||
|
||||
/// Get a domain object directly, used by the .osg wrappers and serializers.
|
||||
const Domain& getDomain( unsigned int i ) const { return _domains[i]; }
|
||||
|
||||
/// Remove a domain at specific index
|
||||
void removeDomain( unsigned int i )
|
||||
{ if (i<_domains.size()) _domains.erase(_domains.begin() + i); }
|
||||
|
||||
/// Remove all existing domains
|
||||
void removeAllDomains() { _domains.clear(); }
|
||||
|
||||
/// Get number of domains
|
||||
unsigned int getNumDomains() const { return _domains.size(); }
|
||||
|
||||
/// Apply the acceleration to a particle. Do not call this method manually.
|
||||
void operate( Particle* P, double dt );
|
||||
|
||||
/// Perform some initializations. Do not call this method manually.
|
||||
void beginOperate( Program* prg );
|
||||
|
||||
/// Perform some post-operations. Do not call this method manually.
|
||||
void endOperate();
|
||||
|
||||
protected:
|
||||
virtual ~DomainOperator() {}
|
||||
DomainOperator& operator=( const DomainOperator& ) { return *this; }
|
||||
|
||||
virtual void handlePoint( const Domain& domain, Particle* P, double dt ) { ignore("Point"); }
|
||||
virtual void handleLineSegment( const Domain& domain, Particle* P, double dt ) { ignore("LineSegment"); }
|
||||
virtual void handleTriangle( const Domain& domain, Particle* P, double dt ) { ignore("Triangle"); }
|
||||
virtual void handleRectangle( const Domain& domain, Particle* P, double dt ) { ignore("Rectangle"); }
|
||||
virtual void handlePlane( const Domain& domain, Particle* P, double dt ) { ignore("Plane"); }
|
||||
virtual void handleSphere( const Domain& domain, Particle* P, double dt ) { ignore("Sphere"); }
|
||||
virtual void handleBox( const Domain& domain, Particle* P, double dt ) { ignore("Box"); }
|
||||
virtual void handleDisk( const Domain& domain, Particle* P, double dt ) { ignore("Disk"); }
|
||||
|
||||
inline void computeNewBasis( const osg::Vec3&, const osg::Vec3&, osg::Vec3&, osg::Vec3& );
|
||||
inline void ignore( const std::string& func );
|
||||
|
||||
std::vector<Domain> _domains;
|
||||
std::vector<Domain> _backupDomains;
|
||||
};
|
||||
|
||||
// INLINE METHODS
|
||||
|
||||
inline void DomainOperator::addPointDomain( const osg::Vec3& p )
|
||||
{
|
||||
Domain domain( Domain::POINT_DOMAIN );
|
||||
domain.v1 = p;
|
||||
_domains.push_back( domain );
|
||||
}
|
||||
|
||||
inline void DomainOperator::addLineSegmentDomain( const osg::Vec3& v1, const osg::Vec3& v2 )
|
||||
{
|
||||
Domain domain( Domain::LINE_DOMAIN );
|
||||
domain.v1 = v1;
|
||||
domain.v2 = v2;
|
||||
domain.r1 = (v2 - v1).length();
|
||||
_domains.push_back( domain );
|
||||
}
|
||||
|
||||
inline void DomainOperator::addTriangleDomain( const osg::Vec3& v1, const osg::Vec3& v2, const osg::Vec3& v3 )
|
||||
{
|
||||
Domain domain( Domain::TRI_DOMAIN );
|
||||
domain.v1 = v1;
|
||||
domain.v2 = v2;
|
||||
domain.v3 = v3;
|
||||
domain.plane.set(v1, v2, v3);
|
||||
computeNewBasis( v2-v1, v3-v1, domain.s1, domain.s2 );
|
||||
_domains.push_back( domain );
|
||||
}
|
||||
|
||||
inline void DomainOperator::addRectangleDomain( const osg::Vec3& corner, const osg::Vec3& w, const osg::Vec3& h )
|
||||
{
|
||||
Domain domain( Domain::RECT_DOMAIN );
|
||||
domain.v1 = corner;
|
||||
domain.v2 = w;
|
||||
domain.v3 = h;
|
||||
domain.plane.set(corner, corner+w, corner+h);
|
||||
computeNewBasis( w, h, domain.s1, domain.s2 );
|
||||
_domains.push_back( domain );
|
||||
}
|
||||
|
||||
inline void DomainOperator::addPlaneDomain( const osg::Plane& plane )
|
||||
{
|
||||
Domain domain( Domain::PLANE_DOMAIN );
|
||||
domain.plane.set(plane);
|
||||
_domains.push_back( domain );
|
||||
}
|
||||
|
||||
inline void DomainOperator::addSphereDomain( const osg::Vec3& c, float r )
|
||||
{
|
||||
Domain domain( Domain::SPHERE_DOMAIN );
|
||||
domain.v1 = c;
|
||||
domain.r1 = r;
|
||||
_domains.push_back( domain );
|
||||
}
|
||||
|
||||
inline void DomainOperator::addBoxDomain( const osg::Vec3& min, const osg::Vec3& max )
|
||||
{
|
||||
Domain domain( Domain::BOX_DOMAIN );
|
||||
domain.v1 = min;
|
||||
domain.v2 = max;
|
||||
_domains.push_back( domain );
|
||||
}
|
||||
|
||||
inline void DomainOperator::addDiskDomain( const osg::Vec3& c, const osg::Vec3& n, float r1, float r2 )
|
||||
{
|
||||
Domain domain( Domain::DISK_DOMAIN );
|
||||
domain.v1 = c;
|
||||
domain.v2 = n;
|
||||
domain.r1 = r1;
|
||||
domain.r2 = r2;
|
||||
domain.plane.set(n, c);
|
||||
_domains.push_back( domain );
|
||||
}
|
||||
|
||||
inline void DomainOperator::computeNewBasis( const osg::Vec3& u, const osg::Vec3& v, osg::Vec3& s1, osg::Vec3& s2 )
|
||||
{
|
||||
// Copied from David McAllister's Particle System API (http://www.particlesystems.org), pDomain.h
|
||||
osg::Vec3 w = u ^ v;
|
||||
float det = w.z()*u.x()*v.y() - w.z()*u.y()*v.x() - u.z()*w.x()*v.y() -
|
||||
u.x()*v.z()*w.y() + v.z()*w.x()*u.y() + u.z()*v.x()*w.y();
|
||||
det = 1.0f / det;
|
||||
|
||||
s1.set( v.y()*w.z() - v.z()*w.y(), v.z()*w.x() - v.x()*w.z(), v.x()*w.y() - v.y()*w.x() );
|
||||
s1 = s1 * det;
|
||||
s2.set( u.y()*w.z() - u.z()*w.y(), u.z()*w.x() - u.x()*w.z(), u.x()*w.y() - u.y()*w.x() );
|
||||
s2 = s2 * (-det);
|
||||
}
|
||||
|
||||
inline void DomainOperator::ignore( const std::string& func )
|
||||
{
|
||||
OSG_NOTICE << className() << ": " << func << " domain not yet implemented. " << std::endl;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
124
include/osgParticle/ExplosionOperator
Normal file
124
include/osgParticle/ExplosionOperator
Normal file
@@ -0,0 +1,124 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
// Written by Wang Rui, (C) 2010
|
||||
|
||||
#ifndef OSGPARTICLE_EXPLOSIONOPERATOR
|
||||
#define OSGPARTICLE_EXPLOSIONOPERATOR
|
||||
|
||||
#include <osgParticle/ModularProgram>
|
||||
#include <osgParticle/Operator>
|
||||
#include <osgParticle/Particle>
|
||||
|
||||
namespace osgParticle
|
||||
{
|
||||
|
||||
|
||||
/** An explosion operator exerts force on each particle away from the explosion center.
|
||||
Refer to David McAllister's Particle System API (http://www.particlesystems.org)
|
||||
*/
|
||||
class ExplosionOperator : public Operator
|
||||
{
|
||||
public:
|
||||
ExplosionOperator()
|
||||
: Operator(), _radius(1.0f), _magnitude(1.0f), _epsilon(1e-3), _sigma(1.0f)
|
||||
{}
|
||||
|
||||
ExplosionOperator( const ExplosionOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
|
||||
: Operator(copy, copyop), _center(copy._center), _radius(copy._radius),
|
||||
_magnitude(copy._magnitude), _epsilon(copy._epsilon), _sigma(copy._sigma)
|
||||
{}
|
||||
|
||||
META_Object( osgParticle, ExplosionOperator );
|
||||
|
||||
/// Set the center of shock wave
|
||||
void setCenter( const osg::Vec3& c ) { _center = c; }
|
||||
|
||||
/// Get the center of shock wave
|
||||
const osg::Vec3& getCenter() const { return _center; }
|
||||
|
||||
/// Set the radius of wave peak
|
||||
void setRadius( float r ) { _radius = r; }
|
||||
|
||||
/// Get the radius of wave peak
|
||||
float getRadius() const { return _radius; }
|
||||
|
||||
/// Set the acceleration scale
|
||||
void setMagnitude( float mag ) { _magnitude = mag; }
|
||||
|
||||
/// Get the acceleration scale
|
||||
float getMagnitude() const { return _magnitude; }
|
||||
|
||||
/// Set the acceleration epsilon
|
||||
void setEpsilon( float eps ) { _epsilon = eps; }
|
||||
|
||||
/// Get the acceleration epsilon
|
||||
float getEpsilon() const { return _epsilon; }
|
||||
|
||||
/// Set broadness of the strength of the wave
|
||||
void setSigma( float s ) { _sigma = s; }
|
||||
|
||||
/// Get broadness of the strength of the wave
|
||||
float getSigma() const { return _sigma; }
|
||||
|
||||
/// Apply the acceleration to a particle. Do not call this method manually.
|
||||
inline void operate( Particle* P, double dt );
|
||||
|
||||
/// Perform some initializations. Do not call this method manually.
|
||||
inline void beginOperate( Program* prg );
|
||||
|
||||
protected:
|
||||
virtual ~ExplosionOperator() {}
|
||||
ExplosionOperator& operator=( const ExplosionOperator& ) { return *this; }
|
||||
|
||||
osg::Vec3 _center;
|
||||
osg::Vec3 _xf_center;
|
||||
float _radius;
|
||||
float _magnitude;
|
||||
float _epsilon;
|
||||
float _sigma;
|
||||
float _inexp;
|
||||
float _outexp;
|
||||
};
|
||||
|
||||
// INLINE METHODS
|
||||
|
||||
inline void ExplosionOperator::operate( Particle* P, double dt )
|
||||
{
|
||||
osg::Vec3 dir = P->getPosition() - _xf_center;
|
||||
float length = dir.length();
|
||||
float distanceFromWave2 = (_radius - length) * (_radius - length);
|
||||
float Gd = exp(distanceFromWave2 * _inexp) * _outexp;
|
||||
float factor = (_magnitude * dt) / (length * (_epsilon+length*length));
|
||||
P->addVelocity( dir * (Gd * factor) );
|
||||
}
|
||||
|
||||
inline void ExplosionOperator::beginOperate( Program* prg )
|
||||
{
|
||||
if ( prg->getReferenceFrame()==ModularProgram::RELATIVE_RF )
|
||||
{
|
||||
_xf_center = prg->transformLocalToWorld(_center);
|
||||
}
|
||||
else
|
||||
{
|
||||
_xf_center = _center;
|
||||
}
|
||||
|
||||
float oneOverSigma = (_sigma!=0.0f ? (1.0f / _sigma) : 1.0f);
|
||||
_inexp = -0.5f * oneOverSigma * oneOverSigma;
|
||||
_outexp = oneOverSigma / sqrt(osg::PI * 2.0f);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -64,6 +64,9 @@ namespace osgParticle {
|
||||
/// Place a partice. Called automatically by <CODE>ModularEmitter</CODE>, do not call this method manually.
|
||||
void place(Particle* P) const;
|
||||
|
||||
/// return the length of the multi-segment
|
||||
inline float size() const;
|
||||
|
||||
/// return the control position
|
||||
inline osg::Vec3 getControlPosition() const;
|
||||
|
||||
@@ -127,6 +130,11 @@ namespace osgParticle {
|
||||
recompute_length();
|
||||
}
|
||||
|
||||
inline float MultiSegmentPlacer::size() const
|
||||
{
|
||||
return _total_length;
|
||||
}
|
||||
|
||||
inline osg::Vec3 MultiSegmentPlacer::getControlPosition() const
|
||||
{
|
||||
return _vx.empty() ? osg::Vec3(0.0f,0.0f,0.0f) : _vx[0].first;
|
||||
|
||||
@@ -47,9 +47,23 @@ namespace osgParticle
|
||||
/// Enable or disable this operator.
|
||||
inline void setEnabled(bool v);
|
||||
|
||||
/** Do something on a particle.
|
||||
/** Do something on all emitted particles.
|
||||
This method is called by <CODE>ModularProgram</CODE> objects to perform some operations
|
||||
on the particles. You must override it in descendant classes. Common operations
|
||||
on the particles. By default, it will call the <CODE>operate()</CODE> method for each particle.
|
||||
You must override it in descendant classes.
|
||||
*/
|
||||
virtual void operateParticles(ParticleSystem* ps, double dt)
|
||||
{
|
||||
int n = ps->numParticles();
|
||||
for (int i=0; i<n; ++i)
|
||||
{
|
||||
Particle* P = ps->getParticle(i);
|
||||
if (P->isAlive() && isEnabled()) operate(P, dt);
|
||||
}
|
||||
}
|
||||
|
||||
/** Do something on a particle.
|
||||
You must override it in descendant classes. Common operations
|
||||
consist of modifying the particle's velocity vector. The <CODE>dt</CODE> parameter is
|
||||
the time elapsed from last operation.
|
||||
*/
|
||||
|
||||
112
include/osgParticle/OrbitOperator
Normal file
112
include/osgParticle/OrbitOperator
Normal file
@@ -0,0 +1,112 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
// Written by Wang Rui, (C) 2010
|
||||
|
||||
#ifndef OSGPARTICLE_ORBITOPERATOR
|
||||
#define OSGPARTICLE_ORBITOPERATOR
|
||||
|
||||
#include <osgParticle/ModularProgram>
|
||||
#include <osgParticle/Operator>
|
||||
#include <osgParticle/Particle>
|
||||
|
||||
namespace osgParticle
|
||||
{
|
||||
|
||||
|
||||
/** An orbit operator forces particles in the orbit around a point.
|
||||
Refer to David McAllister's Particle System API (http://www.particlesystems.org)
|
||||
*/
|
||||
class OrbitOperator : public Operator
|
||||
{
|
||||
public:
|
||||
OrbitOperator()
|
||||
: Operator(), _magnitude(1.0f), _epsilon(1e-3), _maxRadius(FLT_MAX)
|
||||
{}
|
||||
|
||||
OrbitOperator( const OrbitOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
|
||||
: Operator(copy, copyop), _center(copy._center), _magnitude(copy._magnitude),
|
||||
_epsilon(copy._epsilon), _maxRadius(copy._maxRadius)
|
||||
{}
|
||||
|
||||
META_Object( osgParticle, OrbitOperator );
|
||||
|
||||
/// Set the center of orbit
|
||||
void setCenter( const osg::Vec3& c ) { _center = c; }
|
||||
|
||||
/// Get the center of orbit
|
||||
const osg::Vec3& getCenter() const { return _center; }
|
||||
|
||||
/// Set the acceleration scale
|
||||
void setMagnitude( float mag ) { _magnitude = mag; }
|
||||
|
||||
/// Get the acceleration scale
|
||||
float getMagnitude() const { return _magnitude; }
|
||||
|
||||
/// Set the acceleration epsilon
|
||||
void setEpsilon( float eps ) { _epsilon = eps; }
|
||||
|
||||
/// Get the acceleration epsilon
|
||||
float getEpsilon() const { return _epsilon; }
|
||||
|
||||
/// Set max radius between the center and the particle
|
||||
void setMaxRadius( float max ) { _maxRadius = max; }
|
||||
|
||||
/// Get max radius between the center and the particle
|
||||
float getMaxRadius() const { return _maxRadius; }
|
||||
|
||||
/// Apply the acceleration to a particle. Do not call this method manually.
|
||||
inline void operate( Particle* P, double dt );
|
||||
|
||||
/// Perform some initializations. Do not call this method manually.
|
||||
inline void beginOperate( Program* prg );
|
||||
|
||||
protected:
|
||||
virtual ~OrbitOperator() {}
|
||||
OrbitOperator& operator=( const OrbitOperator& ) { return *this; }
|
||||
|
||||
osg::Vec3 _center;
|
||||
osg::Vec3 _xf_center;
|
||||
float _magnitude;
|
||||
float _epsilon;
|
||||
float _maxRadius;
|
||||
};
|
||||
|
||||
// INLINE METHODS
|
||||
|
||||
inline void OrbitOperator::operate( Particle* P, double dt )
|
||||
{
|
||||
osg::Vec3 dir = _xf_center - P->getPosition();
|
||||
float length = dir.length();
|
||||
if ( length<_maxRadius )
|
||||
{
|
||||
P->addVelocity( dir * ((_magnitude * dt) /
|
||||
(length * (_epsilon+length*length))) );
|
||||
}
|
||||
}
|
||||
|
||||
inline void OrbitOperator::beginOperate( Program* prg )
|
||||
{
|
||||
if ( prg->getReferenceFrame()==ModularProgram::RELATIVE_RF )
|
||||
{
|
||||
_xf_center = prg->transformLocalToWorld(_center);
|
||||
}
|
||||
else
|
||||
{
|
||||
_xf_center = _center;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <osg/Vec3>
|
||||
#include <osg/Vec4>
|
||||
#include <osg/Matrix>
|
||||
#include <osg/Drawable>
|
||||
#include <osg/GL>
|
||||
#include <osg/GLBeginEndAdapter>
|
||||
|
||||
@@ -47,6 +48,7 @@ namespace osgParticle
|
||||
minimum values are used.
|
||||
*/
|
||||
class OSGPARTICLE_EXPORT Particle {
|
||||
friend class ParticleSystem;
|
||||
public:
|
||||
|
||||
enum
|
||||
@@ -63,7 +65,8 @@ namespace osgParticle
|
||||
QUAD, // uses GL_QUADS as primitive
|
||||
QUAD_TRIANGLESTRIP, // uses GL_TRI_angleSTRIP as primitive, but each particle needs a glBegin/glEnd pair
|
||||
HEXAGON, // may save some filling time, but uses more triangles
|
||||
LINE // uses GL_LINES to draw line segments that point to the direction of motion
|
||||
LINE, // uses GL_LINES to draw line segments that point to the direction of motion
|
||||
USER // uses a user-defined drawable as primitive
|
||||
};
|
||||
|
||||
Particle();
|
||||
@@ -139,7 +142,7 @@ namespace osgParticle
|
||||
inline const osg::Vec4& getCurrentColor() const { return _current_color; }
|
||||
|
||||
/// Get the current alpha
|
||||
inline float getCurrentAlpha() const { return _current_alpha; }
|
||||
inline float getCurrentAlpha() const { return _base_prop.z(); }
|
||||
|
||||
/// Get the s texture coordinate of the bottom left of the particle
|
||||
inline float getSTexCoord() const { return _s_coord; }
|
||||
@@ -226,13 +229,13 @@ namespace osgParticle
|
||||
/// Transform angle and angularVelocity vectors by a matrix.
|
||||
inline void transformAngleVelocity(const osg::Matrix& xform);
|
||||
|
||||
/** Update the particle (don't call this method manually).
|
||||
/** Update the particle (don't call this method manually).
|
||||
This method is called automatically by <CODE>ParticleSystem::update()</CODE>; it
|
||||
updates the graphical properties of the particle for the current time,
|
||||
checks whether the particle is still alive, and then updates its position
|
||||
by computing <I>P = P + V * dt</I> (where <I>P</I> is the position and <I>V</I> is the velocity).
|
||||
*/
|
||||
bool update(double dt);
|
||||
bool update(double dt, bool onlyTimeStamp);
|
||||
|
||||
/// Perform some pre-rendering tasks. Called automatically by particle systems.
|
||||
inline void beginRender(osg::GLBeginEndAdapter* gl) const;
|
||||
@@ -240,6 +243,9 @@ namespace osgParticle
|
||||
/// Render the particle. Called automatically by particle systems.
|
||||
void render(osg::GLBeginEndAdapter* gl, const osg::Vec3& xpos, const osg::Vec3& px, const osg::Vec3& py, float scale = 1.0f) const;
|
||||
|
||||
/// Render the particle with user-defined drawable
|
||||
void render(osg::RenderInfo& renderInfo, const osg::Vec3& xpos, const osg::Vec3& xrot) const;
|
||||
|
||||
/// Perform some post-rendering tasks. Called automatically by particle systems.
|
||||
inline void endRender(osg::GLBeginEndAdapter* gl) const;
|
||||
|
||||
@@ -265,6 +271,21 @@ namespace osgParticle
|
||||
|
||||
/// Get the const next particle
|
||||
inline int getNextParticle() const { return _nextParticle; }
|
||||
|
||||
/// Set the depth of the particle
|
||||
inline void setDepth(double d) { _depth = d; }
|
||||
|
||||
/// Get the depth of the particle
|
||||
inline double getDepth() const { return _depth; }
|
||||
|
||||
/// Set the user-defined particle drawable
|
||||
inline void setDrawable(osg::Drawable* d) { _drawable = d; }
|
||||
|
||||
/// Get the user-defined particle drawable
|
||||
inline osg::Drawable* getDrawable() const { return _drawable.get(); }
|
||||
|
||||
/// Sorting operator
|
||||
bool operator<(const Particle &P) const { return _depth < P._depth; }
|
||||
|
||||
/// Method for initializing a particles texture coords as part of a connected particle system.
|
||||
void setUpTexCoordsAsPartOfConnectedParticleSystem(ParticleSystem* ps);
|
||||
@@ -281,7 +302,7 @@ namespace osgParticle
|
||||
osg::ref_ptr<Interpolator> _ai;
|
||||
osg::ref_ptr<Interpolator> _ci;
|
||||
|
||||
bool _alive;
|
||||
//bool _alive;
|
||||
bool _mustdie;
|
||||
double _lifeTime;
|
||||
|
||||
@@ -298,8 +319,9 @@ namespace osgParticle
|
||||
|
||||
double _t0;
|
||||
|
||||
float _current_size;
|
||||
float _current_alpha;
|
||||
//float _current_size;
|
||||
//float _current_alpha;
|
||||
osg::Vec3 _base_prop; // [0] _alive [1] _current_size [2] _current_alpha
|
||||
osg::Vec4 _current_color;
|
||||
|
||||
float _s_tile;
|
||||
@@ -313,6 +335,12 @@ namespace osgParticle
|
||||
// previous and next Particles are only used in ConnectedParticleSystems
|
||||
int _previousParticle;
|
||||
int _nextParticle;
|
||||
|
||||
// the depth of the particle is used only when sorting is enabled
|
||||
double _depth;
|
||||
|
||||
// the particle drawable is used only when USER shape is enabled
|
||||
osg::ref_ptr<osg::Drawable> _drawable;
|
||||
};
|
||||
|
||||
// INLINE FUNCTIONS
|
||||
@@ -329,7 +357,7 @@ namespace osgParticle
|
||||
|
||||
inline bool Particle::isAlive() const
|
||||
{
|
||||
return _alive;
|
||||
return _base_prop.x()>0.0;
|
||||
}
|
||||
|
||||
inline double Particle::getLifeTime() const
|
||||
@@ -569,7 +597,7 @@ namespace osgParticle
|
||||
|
||||
inline float Particle::getCurrentSize() const
|
||||
{
|
||||
return _current_size;
|
||||
return _base_prop.y();
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -151,7 +151,6 @@ namespace osgParticle
|
||||
private:
|
||||
ReferenceFrame _rf;
|
||||
bool _enabled;
|
||||
double _t0;
|
||||
osg::ref_ptr<ParticleSystem> _ps;
|
||||
bool _first_ltw_compute;
|
||||
bool _need_ltw_matrix;
|
||||
@@ -197,7 +196,6 @@ namespace osgParticle
|
||||
_enabled = v;
|
||||
if (_enabled)
|
||||
{
|
||||
_t0 = -1;
|
||||
_currentTime = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,6 +110,26 @@ namespace osgParticle
|
||||
*/
|
||||
inline void setDefaultBoundingBox(const osg::BoundingBox& bbox);
|
||||
|
||||
/// Return true if we use vertex arrays for rendering particles.
|
||||
bool getUseVertexArray() const { return _useVertexArray; }
|
||||
|
||||
/** Set to use vertex arrays for rendering particles.
|
||||
Lots of variables will be omitted: particles' shape, alive or not, visibility distance, and so on,
|
||||
so the rendering result is not as good as we wish (although it's fast than using glBegin/glEnd).
|
||||
We had better use this for GLSL shaders, in which particle parameters will be kept as uniforms.
|
||||
This method is called automatically by <CODE>setDefaultAttributesUsingShaders()</CODE>.
|
||||
*/
|
||||
void setUseVertexArray(bool v) { _useVertexArray = v; }
|
||||
|
||||
/// Return true if shaders are required.
|
||||
bool getUseShaders() const { return _useShaders; }
|
||||
|
||||
/** Set to use GLSL shaders for rendering particles.
|
||||
Particles' parameters will be used as shader attribute arrays, and necessary variables, including
|
||||
the visibility distance, texture, etc, will be used and updated as uniforms.
|
||||
*/
|
||||
void setUseShaders(bool v) { _useShaders = v; _dirty_uniforms = true; }
|
||||
|
||||
/// Get the double pass rendering flag.
|
||||
inline bool getDoublePassRendering() const;
|
||||
|
||||
@@ -157,6 +177,9 @@ namespace osgParticle
|
||||
|
||||
/// Get the last frame number.
|
||||
inline int getLastFrameNumber() const;
|
||||
|
||||
/// Get the unique delta time for emitters and updaters to use
|
||||
inline double& getDeltaTime( double currentTime );
|
||||
|
||||
/// Get a reference to the default particle template.
|
||||
inline Particle& getDefaultParticleTemplate();
|
||||
@@ -178,6 +201,12 @@ namespace osgParticle
|
||||
*/
|
||||
void setDefaultAttributes(const std::string& texturefile = "", bool emissive_particles = true, bool lighting = false, int texture_unit = 0);
|
||||
|
||||
/** A useful method to set the most common <CODE>StateAttribute</CODE> and use GLSL shaders to draw particles.
|
||||
At present, when enabling shaders in the particle system, user-defined shapes will not be usable.
|
||||
If <CODE>texturefile</CODE> is empty, then texturing is turned off.
|
||||
*/
|
||||
void setDefaultAttributesUsingShaders(const std::string& texturefile = "", bool emissive_particles = true, int texture_unit = 0);
|
||||
|
||||
/// (<B>EXPERIMENTAL</B>) Get the level of detail.
|
||||
inline int getLevelOfDetail() const;
|
||||
|
||||
@@ -185,9 +214,32 @@ namespace osgParticle
|
||||
get the actual number of particles to be drawn. This value must be greater than zero.
|
||||
*/
|
||||
inline void setLevelOfDetail(int v);
|
||||
|
||||
enum SortMode
|
||||
{
|
||||
NO_SORT,
|
||||
SORT_FRONT_TO_BACK,
|
||||
SORT_BACK_TO_FRONT
|
||||
};
|
||||
|
||||
/// Get the sort mode.
|
||||
inline SortMode getSortMode() const;
|
||||
|
||||
/** Set the sort mode. It will force resorting the particle list by the Z direction of the view coordinates.
|
||||
This can be used for the purpose of transparent rendering or <CODE>setVisibilityDistance()</CODE>.
|
||||
*/
|
||||
inline void setSortMode(SortMode mode);
|
||||
|
||||
/// Get the visibility distance.
|
||||
inline double getVisibilityDistance() const;
|
||||
|
||||
/** Set the visibility distance which allows the particles to be rendered only when depth is inside the distance.
|
||||
When using shaders, it can work well directly; otherwise the sort mode should also be set to pre-compute depth.
|
||||
*/
|
||||
inline void setVisibilityDistance(double distance);
|
||||
|
||||
/// Update the particles. Don't call this directly, use a <CODE>ParticleSystemUpdater</CODE> instead.
|
||||
virtual void update(double dt);
|
||||
virtual void update(double dt, osg::NodeVisitor& nv);
|
||||
|
||||
virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
|
||||
|
||||
@@ -212,7 +264,8 @@ namespace osgParticle
|
||||
ParticleSystem& operator=(const ParticleSystem&) { return *this; }
|
||||
|
||||
inline void update_bounds(const osg::Vec3& p, float r);
|
||||
void single_pass_render(osg::State& state, const osg::Matrix& modelview) const;
|
||||
void single_pass_render(osg::RenderInfo& renderInfo, const osg::Matrix& modelview) const;
|
||||
void render_vertex_array(osg::RenderInfo& renderInfo) const;
|
||||
|
||||
typedef std::vector<Particle> Particle_vector;
|
||||
typedef std::stack<Particle*> Death_stack;
|
||||
@@ -227,6 +280,10 @@ namespace osgParticle
|
||||
osg::Vec3 _align_Y_axis;
|
||||
ParticleScaleReferenceFrame _particleScaleReferenceFrame;
|
||||
|
||||
bool _useVertexArray;
|
||||
bool _useShaders;
|
||||
bool _dirty_uniforms;
|
||||
|
||||
bool _doublepass;
|
||||
bool _frozen;
|
||||
|
||||
@@ -238,9 +295,16 @@ namespace osgParticle
|
||||
|
||||
Particle _def_ptemp;
|
||||
mutable int _last_frame;
|
||||
mutable bool _dirty_dt;
|
||||
bool _freeze_on_cull;
|
||||
|
||||
double _t0;
|
||||
double _dt;
|
||||
|
||||
int _detail;
|
||||
SortMode _sortMode;
|
||||
double _visibilityDistance;
|
||||
|
||||
mutable int _draw_count;
|
||||
|
||||
mutable ReadWriterMutex _readWriteMutex;
|
||||
@@ -343,6 +407,19 @@ namespace osgParticle
|
||||
{
|
||||
return _last_frame;
|
||||
}
|
||||
|
||||
inline double& ParticleSystem::getDeltaTime( double currentTime )
|
||||
{
|
||||
if ( _dirty_dt )
|
||||
{
|
||||
_dt = currentTime - _t0;
|
||||
if ( _dt<0.0 ) _dt = 0.0;
|
||||
|
||||
_t0 = currentTime;
|
||||
_dirty_dt = false;
|
||||
}
|
||||
return _dt;
|
||||
}
|
||||
|
||||
inline void ParticleSystem::update_bounds(const osg::Vec3& p, float r)
|
||||
{
|
||||
@@ -398,6 +475,27 @@ namespace osgParticle
|
||||
_detail = v;
|
||||
}
|
||||
|
||||
inline ParticleSystem::SortMode ParticleSystem::getSortMode() const
|
||||
{
|
||||
return _sortMode;
|
||||
}
|
||||
|
||||
inline void ParticleSystem::setSortMode(SortMode mode)
|
||||
{
|
||||
_sortMode = mode;
|
||||
}
|
||||
|
||||
inline double ParticleSystem::getVisibilityDistance() const
|
||||
{
|
||||
return _visibilityDistance;
|
||||
}
|
||||
|
||||
inline void ParticleSystem::setVisibilityDistance(double distance)
|
||||
{
|
||||
_visibilityDistance = distance;
|
||||
if (_useShaders) _dirty_uniforms = true;
|
||||
}
|
||||
|
||||
// I'm not sure this function should be inlined...
|
||||
|
||||
inline Particle* ParticleSystem::createParticle(const Particle* ptemplate)
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
#include <osg/ref_ptr>
|
||||
#include <osg/CopyOp>
|
||||
#include <osg/Object>
|
||||
#include <osg/Node>
|
||||
#include <osg/Geode>
|
||||
#include <osg/NodeVisitor>
|
||||
|
||||
#include <osgUtil/CullVisitor>
|
||||
@@ -36,13 +36,19 @@ namespace osgParticle
|
||||
update() method on the specified particle systems. You should place this updater
|
||||
AFTER other nodes like emitters and programs.
|
||||
*/
|
||||
class OSGPARTICLE_EXPORT ParticleSystemUpdater: public osg::Node {
|
||||
class OSGPARTICLE_EXPORT ParticleSystemUpdater: public osg::Geode {
|
||||
public:
|
||||
ParticleSystemUpdater();
|
||||
ParticleSystemUpdater(const ParticleSystemUpdater& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
|
||||
|
||||
META_Node(osgParticle,ParticleSystemUpdater);
|
||||
|
||||
/// Add a Drawable and call addParticleSystem() if it is a particle system
|
||||
virtual bool addDrawable(osg::Drawable* drawable);
|
||||
|
||||
/// Remove a Drawable and call removeParticleSystem() if it is a particle system
|
||||
virtual bool removeDrawable(osg::Drawable* drawable);
|
||||
|
||||
/// Add a particle system to the list.
|
||||
virtual bool addParticleSystem(ParticleSystem* ps);
|
||||
|
||||
@@ -86,7 +92,6 @@ namespace osgParticle
|
||||
typedef std::vector<osg::ref_ptr<ParticleSystem> > ParticleSystem_Vector;
|
||||
|
||||
ParticleSystem_Vector _psv;
|
||||
double _t0;
|
||||
|
||||
//added 1/17/06- bgandere@nps.edu
|
||||
//a var to keep from doing multiple updates per frame
|
||||
|
||||
@@ -39,6 +39,9 @@ namespace osgParticle
|
||||
|
||||
/// Place a particle. Must be implemented in descendant classes.
|
||||
virtual void place(Particle* P) const = 0;
|
||||
|
||||
/// Size of the placer. Can be implemented in descendant classes.
|
||||
virtual float size() const { return 1.0f; }
|
||||
|
||||
/// Return the control position of particles that placer will generate. Must be implemented in descendant classes.
|
||||
virtual osg::Vec3 getControlPosition() const = 0;
|
||||
|
||||
@@ -61,6 +61,9 @@ namespace osgParticle
|
||||
/// Place a particle. Do not call it manually.
|
||||
inline void place(Particle* P) const;
|
||||
|
||||
/// return the area of the sector
|
||||
inline float size() const;
|
||||
|
||||
/// return the control position
|
||||
inline osg::Vec3 getControlPosition() const;
|
||||
|
||||
@@ -129,6 +132,12 @@ namespace osgParticle
|
||||
|
||||
P->setPosition(pos);
|
||||
}
|
||||
|
||||
inline float SectorPlacer::size() const
|
||||
{
|
||||
return 0.5f * (_phi_range.maximum - _phi_range.minimum) *
|
||||
(_rad_range.maximum*_rad_range.maximum - _rad_range.minimum*_rad_range.minimum);
|
||||
}
|
||||
|
||||
inline osg::Vec3 SectorPlacer::getControlPosition() const
|
||||
{
|
||||
|
||||
@@ -60,6 +60,9 @@ namespace osgParticle {
|
||||
/// Place a particle. This method is called by <CODE>ModularEmitter</CODE>, do not call it manually.
|
||||
inline void place(Particle* P) const;
|
||||
|
||||
/// return the length of the segment
|
||||
inline float size() const;
|
||||
|
||||
/// return the control position
|
||||
inline osg::Vec3 getControlPosition() const;
|
||||
|
||||
@@ -105,6 +108,11 @@ namespace osgParticle {
|
||||
P->setPosition(rangev3(_vertexA, _vertexB).get_random());
|
||||
}
|
||||
|
||||
inline float SegmentPlacer::size() const
|
||||
{
|
||||
return (_vertexB - _vertexA).length();
|
||||
}
|
||||
|
||||
inline void SegmentPlacer::setVertexA(const osg::Vec3& v)
|
||||
{
|
||||
_vertexA = v;
|
||||
|
||||
100
include/osgParticle/SinkOperator
Normal file
100
include/osgParticle/SinkOperator
Normal file
@@ -0,0 +1,100 @@
|
||||
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
// Written by Wang Rui, (C) 2010
|
||||
|
||||
#ifndef OSGPARTICLE_SINKOPERATOR
|
||||
#define OSGPARTICLE_SINKOPERATOR
|
||||
|
||||
#include <osgParticle/Particle>
|
||||
#include <osgParticle/DomainOperator>
|
||||
|
||||
namespace osgParticle
|
||||
{
|
||||
|
||||
|
||||
/** A sink operator kills particles if positions or velocities inside/outside the specified domain.
|
||||
Refer to David McAllister's Particle System API (http://www.particlesystems.org)
|
||||
*/
|
||||
class OSGPARTICLE_EXPORT SinkOperator : public DomainOperator
|
||||
{
|
||||
public:
|
||||
enum SinkTarget { SINK_POSITION, SINK_VELOCITY, SINK_ANGULAR_VELOCITY };
|
||||
enum SinkStrategy { SINK_INSIDE, SINK_OUTSIDE };
|
||||
|
||||
SinkOperator()
|
||||
: DomainOperator(), _sinkTarget(SINK_POSITION), _sinkStrategy(SINK_INSIDE)
|
||||
{}
|
||||
|
||||
SinkOperator( const SinkOperator& copy, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY )
|
||||
: DomainOperator(copy, copyop), _sinkTarget(copy._sinkTarget), _sinkStrategy(copy._sinkStrategy)
|
||||
{}
|
||||
|
||||
META_Object( osgParticle, SinkOperator );
|
||||
|
||||
/// Set the sink strategy
|
||||
void setSinkTarget( SinkTarget so ) { _sinkTarget = so; }
|
||||
|
||||
/// Get the sink strategy
|
||||
SinkTarget getSinkTarget() const { return _sinkTarget; }
|
||||
|
||||
/// Set the sink strategy
|
||||
void setSinkStrategy( SinkStrategy ss ) { _sinkStrategy = ss; }
|
||||
|
||||
/// Get the sink strategy
|
||||
SinkStrategy getSinkStrategy() const { return _sinkStrategy; }
|
||||
|
||||
/// Perform some initializations. Do not call this method manually.
|
||||
void beginOperate( Program* prg );
|
||||
|
||||
protected:
|
||||
virtual ~SinkOperator() {}
|
||||
SinkOperator& operator=( const SinkOperator& ) { return *this; }
|
||||
|
||||
virtual void handlePoint( const Domain& domain, Particle* P, double dt );
|
||||
virtual void handleLineSegment( const Domain& domain, Particle* P, double dt );
|
||||
virtual void handleTriangle( const Domain& domain, Particle* P, double dt );
|
||||
virtual void handleRectangle( const Domain& domain, Particle* P, double dt );
|
||||
virtual void handlePlane( const Domain& domain, Particle* P, double dt );
|
||||
virtual void handleSphere( const Domain& domain, Particle* P, double dt );
|
||||
virtual void handleBox( const Domain& domain, Particle* P, double dt );
|
||||
virtual void handleDisk( const Domain& domain, Particle* P, double dt );
|
||||
|
||||
inline const osg::Vec3& getValue( Particle* P );
|
||||
inline void kill( Particle* P, bool insideDomain );
|
||||
|
||||
SinkTarget _sinkTarget;
|
||||
SinkStrategy _sinkStrategy;
|
||||
};
|
||||
|
||||
// INLINE METHODS
|
||||
|
||||
inline const osg::Vec3& SinkOperator::getValue( Particle* P )
|
||||
{
|
||||
switch ( _sinkTarget )
|
||||
{
|
||||
case SINK_VELOCITY: return P->getVelocity();
|
||||
case SINK_ANGULAR_VELOCITY: return P->getAngularVelocity();
|
||||
case SINK_POSITION: default: return P->getPosition();
|
||||
}
|
||||
}
|
||||
|
||||
inline void SinkOperator::kill( Particle* P, bool insideDomain )
|
||||
{
|
||||
if ( !((_sinkStrategy==SINK_INSIDE) ^ insideDomain) )
|
||||
P->kill();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user