Particle
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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.
*/
//osgParticle - Copyright (C) 2002 Marco Jez
#ifndef OSGPARTICLE_PARTICLE
#define OSGPARTICLE_PARTICLE 1
#include <osgParticle/Export>
#include <osgParticle/Interpolator>
#include <osgParticle/range>
#include <osg/ref_ptr>
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Matrix>
namespace osgParticle
{
// forward declare so we can reference it
class ParticleSystem;
class OSGPARTICLE_EXPORT Particle {
friend class ParticleSystem;
public:
enum
{
INVALID_INDEX = -1
};
enum Shape {
POINT, // uses GL_POINTS as primitive
QUAD, // uses GL_QUADS as primitive
QUAD_TRIANGLESTRIP, // no longer supported, falls back to QUAD
HEXAGON, // no longer supported, falls back to QUAD
LINE, // uses GL_LINES to draw line segments that point to the direction of motion
USER // no longer supported, falls back to QUAD
};
Particle();
inline Shape getShape() const;
inline void setShape(Shape s);
inline bool isAlive() const;
inline double getLifeTime() const;
inline double getAge() const;
inline const rangef& getSizeRange() const;
inline const rangef& getAlphaRange() const;
inline const rangev4& getColorRange() const;
inline const Interpolator* getSizeInterpolator() const;
inline const Interpolator* getAlphaInterpolator() const;
inline const Interpolator* getColorInterpolator() const;
inline float getRadius() const;
inline float getMass() const;
inline float getMassInv() const;
inline const osg::Vec3& getPosition() const;
inline const osg::Vec3& getVelocity() const;
inline const osg::Vec3& getPreviousPosition() const;
inline const osg::Vec3& getAngle() const;
inline const osg::Vec3& getAngularVelocity() const;
inline const osg::Vec3& getPreviousAngle() const;
inline const osg::Vec4& getCurrentColor() const { return _current_color; }
inline float getCurrentAlpha() const { return _current_alpha; }
inline float getSTexCoord() const { return _s_coord; }
inline float getTTexCoord() const { return _t_coord; }
inline float getSTexTile() const { return _s_tile; }
inline float getTTexTile() const { return _t_tile; }
inline int getTileS() const;
inline int getTileT() const;
inline int getNumTiles() const { return _end_tile - _start_tile + 1; }
inline void kill();
inline void setLifeTime(double t);
inline void setSizeRange(const rangef& r);
inline void setAlphaRange(const rangef& r);
inline void setColorRange(const rangev4& r);
inline void setSizeInterpolator(Interpolator* ri);
template<class T> void setSizeInterpolator(const osg::ref_ptr<T>& ri) { setSizeInterpolator(ri.get()); }
inline void setAlphaInterpolator(Interpolator* ai);
template<class T> void setAlphaInterpolator(const osg::ref_ptr<T>& ri) { setAlphaInterpolator(ri.get()); }
inline void setColorInterpolator(Interpolator* ci);
template<class T> void setColorInterpolator(const osg::ref_ptr<T>& ri) { setColorInterpolator(ri.get()); }
inline void setRadius(float r);
inline void setMass(float m);
inline void setPosition(const osg::Vec3& p);
inline void setVelocity(const osg::Vec3& v);
inline void addVelocity(const osg::Vec3& dv);
inline void transformPositionVelocity(const osg::Matrix& xform);
void transformPositionVelocity(const osg::Matrix& xform1, const osg::Matrix& xform2, float r);
inline void setAngle(const osg::Vec3& a);
inline void setAngularVelocity(const osg::Vec3& v);
inline void addAngularVelocity(const osg::Vec3& dv);
inline void transformAngleVelocity(const osg::Matrix& xform);
bool update(double dt, bool onlyTimeStamp);
inline float getCurrentSize() const;
inline void setTextureTileRange(int sTile, int tTile, int startTile, int endTile);
inline void setTextureTile(int sTile, int tTile, int end = -1);
inline int getStartTile() const;
inline int getEndTile() const;
inline void setPreviousParticle(int previous) { _previousParticle = previous; }
inline int getPreviousParticle() const { return _previousParticle; }
inline void setNextParticle(int next) { _nextParticle = next; }
inline int getNextParticle() const { return _nextParticle; }
inline void setDepth(double d) { _depth = d; }
inline double getDepth() const { return _depth; }
bool operator<(const Particle &P) const { return _depth < P._depth; }
void setUpTexCoordsAsPartOfConnectedParticleSystem(ParticleSystem* ps);
protected:
Shape _shape;
rangef _sr;
rangef _ar;
rangev4 _cr;
osg::ref_ptr<Interpolator> _si;
osg::ref_ptr<Interpolator> _ai;
osg::ref_ptr<Interpolator> _ci;
bool _mustdie;
double _lifeTime;
float _radius;
float _mass;
float _massinv;
osg::Vec3 _prev_pos;
osg::Vec3 _position;
osg::Vec3 _velocity;
osg::Vec3 _prev_angle;
osg::Vec3 _angle;
osg::Vec3 _angul_arvel;
double _t0;
float _alive;
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;
float _t_tile;
int _start_tile;
int _end_tile;
int _cur_tile;
float _s_coord;
float _t_coord;
// 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;
};
// INLINE FUNCTIONS
inline Particle::Shape Particle::getShape() const
{
return _shape;
}
inline void Particle::setShape(Shape s)
{
_shape = s;
}
inline bool Particle::isAlive() const
{
return _alive>0.0f;
}
inline double Particle::getLifeTime() const
{
return _lifeTime;
}
inline double Particle::getAge() const
{
return _t0;
}
inline float Particle::getRadius() const
{
return _radius;
}
inline void Particle::setRadius(float r)
{
_radius = r;
}
inline const rangef& Particle::getSizeRange() const
{
return _sr;
}
inline const rangef& Particle::getAlphaRange() const
{
return _ar;
}
inline const rangev4& Particle::getColorRange() const
{
return _cr;
}
inline const Interpolator* Particle::getSizeInterpolator() const
{
return _si.get();
}
inline const Interpolator* Particle::getAlphaInterpolator() const
{
return _ai.get();
}
inline const Interpolator* Particle::getColorInterpolator() const
{
return _ci.get();
}
inline const osg::Vec3& Particle::getPosition() const
{
return _position;
}
inline const osg::Vec3& Particle::getVelocity() const
{
return _velocity;
}
inline const osg::Vec3& Particle::getPreviousPosition() const
{
return _prev_pos;
}
inline const osg::Vec3& Particle::getAngle() const
{
return _angle;
}
inline const osg::Vec3& Particle::getAngularVelocity() const
{
return _angul_arvel;
}
inline const osg::Vec3& Particle::getPreviousAngle() const
{
return _prev_angle;
}
inline int Particle::getTileS() const
{
return (_s_tile>0.0f) ? static_cast<int>(1.0f / _s_tile) : 1;
}
inline int Particle::getTileT() const
{
return (_t_tile>0.0f) ? static_cast<int>(1.0f / _t_tile) : 1;
}
inline void Particle::kill()
{
_mustdie = true;
}
inline void Particle::setLifeTime(double t)
{
_lifeTime = t;
}
inline void Particle::setSizeRange(const rangef& r)
{
_sr = r;
}
inline void Particle::setAlphaRange(const rangef& r)
{
_ar = r;
}
inline void Particle::setColorRange(const rangev4& r)
{
_cr = r;
}
inline void Particle::setSizeInterpolator(Interpolator* ri)
{
_si = ri;
}
inline void Particle::setAlphaInterpolator(Interpolator* ai)
{
_ai = ai;
}
inline void Particle::setColorInterpolator(Interpolator* ci)
{
_ci = ci;
}
inline void Particle::setPosition(const osg::Vec3& p)
{
_position = p;
}
inline void Particle::setVelocity(const osg::Vec3& v)
{
_velocity = v;
}
inline void Particle::addVelocity(const osg::Vec3& dv)
{
_velocity += dv;
}
inline void Particle::transformPositionVelocity(const osg::Matrix& xform)
{
_position = xform.preMult(_position);
_velocity = osg::Matrix::transform3x3(_velocity, xform);
}
inline void Particle::transformPositionVelocity(const osg::Matrix& xform1, const osg::Matrix& xform2, float r)
{
osg::Vec3 position1 = xform1.preMult(_position);
osg::Vec3 velocity1 = osg::Matrix::transform3x3(_velocity, xform1);
osg::Vec3 position2 = xform2.preMult(_position);
osg::Vec3 velocity2 = osg::Matrix::transform3x3(_velocity, xform2);
float one_minus_r = 1.0f-r;
_position = position1*r + position2*one_minus_r;
_velocity = velocity1*r + velocity2*one_minus_r;
}
inline void Particle::setAngle(const osg::Vec3& a)
{
_angle = a;
}
inline void Particle::setAngularVelocity(const osg::Vec3& v)
{
_angul_arvel = v;
}
inline void Particle::addAngularVelocity(const osg::Vec3& dv)
{
_angul_arvel += dv;
}
inline void Particle::transformAngleVelocity(const osg::Matrix& xform)
{
// this should be optimized!
osg::Vec3 a1 = _angle + _angul_arvel;
_angle = xform.preMult(_angle);
a1 = xform.preMult(a1);
_angul_arvel = a1 - _angle;
}
inline float Particle::getMass() const
{
return _mass;
}
inline float Particle::getMassInv() const
{
return _massinv;
}
inline void Particle::setMass(float m)
{
_mass = m;
_massinv = 1 / m;
}
inline float Particle::getCurrentSize() const
{
return _current_size;
}
inline void Particle::setTextureTile(int sTile, int tTile, int end)
{
setTextureTileRange(sTile, tTile, -1, end);
}
inline void Particle::setTextureTileRange(int sTile, int tTile, int startTile, int endTile)
{
_s_tile = (sTile>0) ? 1.0f / static_cast<float>(sTile) : 1.0f;
_t_tile = (tTile>0) ? 1.0f / static_cast<float>(tTile) : 1.0f;
if(startTile == -1)
{
_start_tile = 0;
}
else
{
_start_tile = startTile;
}
if(endTile == -1)
{
_end_tile = sTile * tTile;
}
else
{
_end_tile = endTile;
}
}
inline int Particle::getStartTile() const
{
return _start_tile;
}
inline int Particle::getEndTile() const
{
return _end_tile;
}
}
#endif
Matrix
Vec3
Vec4
osgParticle::Interpolator
An abstract base class for implementing interpolators.
Definition osgParticle/Interpolator:30
osgParticle::Particle::getTTexCoord
float getTTexCoord() const
Get the t texture coordinate of the bottom left of the particle.
Definition Particle:148
osgParticle::Particle::transformAngleVelocity
void transformAngleVelocity(const osg::Matrix &xform)
Transform angle and angularVelocity vectors by a matrix.
Definition Particle:530
osgParticle::Particle::update
bool update(double dt, bool onlyTimeStamp)
osgParticle::Particle::_s_tile
float _s_tile
Definition Particle:320
osgParticle::Particle::_shape
Shape _shape
Definition Particle:288
osgParticle::Particle::getTileT
int getTileT() const
Get height of texture tile.
Definition Particle:438
osgParticle::Particle::_cr
rangev4 _cr
Definition Particle:292
osgParticle::Particle::_si
osg::ref_ptr< Interpolator > _si
Definition Particle:294
osgParticle::Particle::_current_size
float _current_size
Definition Particle:315
osgParticle::Particle::_prev_pos
osg::Vec3 _prev_pos
Definition Particle:304
osgParticle::Particle::setPreviousParticle
void setPreviousParticle(int previous)
Set the previous particle.
Definition Particle:263
osgParticle::Particle::getSTexTile
float getSTexTile() const
Get the texture coordinate width of the particle.
Definition Particle:151
osgParticle::Particle::setDepth
void setDepth(double d)
Set the depth of the particle.
Definition Particle:275
osgParticle::Particle::_angle
osg::Vec3 _angle
Definition Particle:309
osgParticle::Particle::setSizeRange
void setSizeRange(const rangef &r)
Set the minimum and maximum values for polygon size.
Definition Particle:453
osgParticle::Particle::getDepth
double getDepth() const
Get the depth of the particle.
Definition Particle:278
osgParticle::Particle::setShape
void setShape(Shape s)
Set the shape of the particle.
Definition Particle:343
osgParticle::Particle::_radius
float _radius
Definition Particle:301
osgParticle::Particle::getAlphaRange
const rangef & getAlphaRange() const
Get the minimum and maximum values for alpha.
Definition Particle:378
osgParticle::Particle::ParticleSystem
friend class ParticleSystem
Definition Particle:48
osgParticle::Particle::addAngularVelocity
void addAngularVelocity(const osg::Vec3 &dv)
Add a vector to the angular velocity vector.
Definition Particle:525
osgParticle::Particle::setTextureTile
void setTextureTile(int sTile, int tTile, int end=-1)
Same as above, range starts at 0 and ends at end.
Definition Particle:564
osgParticle::Particle::getTileS
int getTileS() const
Get width of texture tile.
Definition Particle:433
osgParticle::Particle::_t_tile
float _t_tile
Definition Particle:321
osgParticle::Particle::_velocity
osg::Vec3 _velocity
Definition Particle:306
osgParticle::Particle::_start_tile
int _start_tile
Definition Particle:322
osgParticle::Particle::getLifeTime
double getLifeTime() const
Get the life time of the particle (in seconds).
Definition Particle:353
osgParticle::Particle::_prev_angle
osg::Vec3 _prev_angle
Definition Particle:308
osgParticle::Particle::_ci
osg::ref_ptr< Interpolator > _ci
Definition Particle:296
osgParticle::Particle::getColorInterpolator
const Interpolator * getColorInterpolator() const
Get the interpolator for computing color values.
Definition Particle:398
osgParticle::Particle::_current_color
osg::Vec4 _current_color
Definition Particle:318
osgParticle::Particle::addVelocity
void addVelocity(const osg::Vec3 &dv)
Add a vector to the velocity vector.
Definition Particle:493
osgParticle::Particle::setAlphaInterpolator
void setAlphaInterpolator(Interpolator *ai)
Set the interpolator for computing alpha values.
Definition Particle:473
osgParticle::Particle::getMass
float getMass() const
Definition Particle:542
osgParticle::Particle::_massinv
float _massinv
Definition Particle:303
osgParticle::Particle::setColorRange
void setColorRange(const rangev4 &r)
Set the minimum and maximum values for color.
Definition Particle:463
osgParticle::Particle::setTextureTileRange
void setTextureTileRange(int sTile, int tTile, int startTile, int endTile)
Definition Particle:569
osgParticle::Particle::_position
osg::Vec3 _position
Definition Particle:305
osgParticle::Particle::setAngle
void setAngle(const osg::Vec3 &a)
Set the angle vector.
Definition Particle:515
osgParticle::Particle::getMassInv
float getMassInv() const
Get 1 / getMass().
Definition Particle:547
osgParticle::Particle::getPreviousAngle
const osg::Vec3 & getPreviousAngle() const
Get the previous angle vector.
Definition Particle:428
osgParticle::Particle::_end_tile
int _end_tile
Definition Particle:323
osgParticle::Particle::getCurrentColor
const osg::Vec4 & getCurrentColor() const
Get the current color.
Definition Particle:139
osgParticle::Particle::_t0
double _t0
Definition Particle:312
osgParticle::Particle::setPosition
void setPosition(const osg::Vec3 &p)
Set the position vector.
Definition Particle:483
osgParticle::Particle::_depth
double _depth
Definition Particle:333
osgParticle::Particle::getCurrentSize
float getCurrentSize() const
Get the current (interpolated) polygon size. Valid only after the first call to update().
Definition Particle:558
osgParticle::Particle::_angul_arvel
osg::Vec3 _angul_arvel
Definition Particle:310
osgParticle::Particle::_ai
osg::ref_ptr< Interpolator > _ai
Definition Particle:295
osgParticle::Particle::setAlphaRange
void setAlphaRange(const rangef &r)
Set the minimum and maximum values for alpha.
Definition Particle:458
osgParticle::Particle::getAlphaInterpolator
const Interpolator * getAlphaInterpolator() const
Get the interpolator for computing alpha values.
Definition Particle:393
osgParticle::Particle::_lifeTime
double _lifeTime
Definition Particle:299
osgParticle::Particle::getNumTiles
int getNumTiles() const
Get number of texture tiles.
Definition Particle:164
osgParticle::Particle::_sr
rangef _sr
Definition Particle:290
osgParticle::Particle::setSizeInterpolator
void setSizeInterpolator(Interpolator *ri)
Set the interpolator for computing size values.
Definition Particle:468
osgParticle::Particle::_t_coord
float _t_coord
Definition Particle:326
osgParticle::Particle::getSTexCoord
float getSTexCoord() const
Get the s texture coordinate of the bottom left of the particle.
Definition Particle:145
osgParticle::Particle::getEndTile
int getEndTile() const
Definition Particle:598
osgParticle::Particle::_mustdie
bool _mustdie
Definition Particle:298
osgParticle::Particle::getColorRange
const rangev4 & getColorRange() const
Get the minimum and maximum values for color.
Definition Particle:383
osgParticle::Particle::kill
void kill()
Definition Particle:443
osgParticle::Particle::isAlive
bool isAlive() const
Get whether the particle is still alive.
Definition Particle:348
osgParticle::Particle::_mass
float _mass
Definition Particle:302
osgParticle::Particle::getPreviousPosition
const osg::Vec3 & getPreviousPosition() const
Get the previous position (the position before last update).
Definition Particle:413
osgParticle::Particle::getTTexTile
float getTTexTile() const
Get the texture coordinate height of the particle.
Definition Particle:154
osgParticle::Particle::setNextParticle
void setNextParticle(int next)
Set the next particle.
Definition Particle:269
osgParticle::Particle::setSizeInterpolator
void setSizeInterpolator(const osg::ref_ptr< T > &ri)
Definition Particle:186
osgParticle::Particle::setAlphaInterpolator
void setAlphaInterpolator(const osg::ref_ptr< T > &ri)
Definition Particle:190
osgParticle::Particle::Shape
Shape
Definition Particle:60
osgParticle::Particle::USER
@ USER
Definition Particle:66
osgParticle::Particle::POINT
@ POINT
Definition Particle:61
osgParticle::Particle::LINE
@ LINE
Definition Particle:65
osgParticle::Particle::QUAD_TRIANGLESTRIP
@ QUAD_TRIANGLESTRIP
Definition Particle:63
osgParticle::Particle::HEXAGON
@ HEXAGON
Definition Particle:64
osgParticle::Particle::QUAD
@ QUAD
Definition Particle:62
osgParticle::Particle::getStartTile
int getStartTile() const
Definition Particle:593
osgParticle::Particle::_ar
rangef _ar
Definition Particle:291
osgParticle::Particle::setMass
void setMass(float m)
Definition Particle:552
osgParticle::Particle::setColorInterpolator
void setColorInterpolator(Interpolator *ci)
Set the interpolator for computing color values.
Definition Particle:478
osgParticle::Particle::_cur_tile
int _cur_tile
Definition Particle:324
osgParticle::Particle::getShape
Shape getShape() const
Get the shape of the particle.
Definition Particle:338
osgParticle::Particle::getSizeRange
const rangef & getSizeRange() const
Get the minimum and maximum values for polygon size.
Definition Particle:373
osgParticle::Particle::getAge
double getAge() const
Get the age of the particle (in seconds).
Definition Particle:358
osgParticle::Particle::_base_prop
osg::Vec3 _base_prop
Definition Particle:317
osgParticle::Particle::operator<
bool operator<(const Particle &P) const
Sorting operator.
Definition Particle:281
osgParticle::Particle::getPosition
const osg::Vec3 & getPosition() const
Get the position vector.
Definition Particle:403
osgParticle::Particle::_s_coord
float _s_coord
Definition Particle:325
osgParticle::Particle::INVALID_INDEX
@ INVALID_INDEX
Definition Particle:53
osgParticle::Particle::setLifeTime
void setLifeTime(double t)
Set the life time of the particle.
Definition Particle:448
osgParticle::Particle::getPreviousParticle
int getPreviousParticle() const
Get the previous particle.
Definition Particle:266
osgParticle::Particle::_previousParticle
int _previousParticle
Definition Particle:329
osgParticle::Particle::_alive
float _alive
Definition Particle:314
osgParticle::Particle::setUpTexCoordsAsPartOfConnectedParticleSystem
void setUpTexCoordsAsPartOfConnectedParticleSystem(ParticleSystem *ps)
Method for initializing a particles texture coords as part of a connected particle system.
osgParticle::Particle::_nextParticle
int _nextParticle
Definition Particle:330
osgParticle::Particle::getVelocity
const osg::Vec3 & getVelocity() const
Definition Particle:408
osgParticle::Particle::getCurrentAlpha
float getCurrentAlpha() const
Get the current alpha.
Definition Particle:142
osgParticle::Particle::transformPositionVelocity
void transformPositionVelocity(const osg::Matrix &xform)
Transform position and velocity vectors by a matrix.
Definition Particle:498
osgParticle::Particle::getRadius
float getRadius() const
Definition Particle:363
osgParticle::Particle::_current_alpha
float _current_alpha
Definition Particle:316
osgParticle::Particle::setVelocity
void setVelocity(const osg::Vec3 &v)
Definition Particle:488
osgParticle::Particle::setColorInterpolator
void setColorInterpolator(const osg::ref_ptr< T > &ri)
Definition Particle:194
osgParticle::Particle::getNextParticle
int getNextParticle() const
Get the const next particle.
Definition Particle:272
osgParticle::Particle::setRadius
void setRadius(float r)
Definition Particle:368
osgParticle::Particle::getSizeInterpolator
const Interpolator * getSizeInterpolator() const
Get the interpolator for computing the size of polygons.
Definition Particle:388
osgParticle::Particle::getAngularVelocity
const osg::Vec3 & getAngularVelocity() const
Get the rotational velocity vector.
Definition Particle:423
osgParticle::Particle::Particle
Particle()
osgParticle::Particle::getAngle
const osg::Vec3 & getAngle() const
Get the angle vector.
Definition Particle:418
osgParticle::Particle::setAngularVelocity
void setAngularVelocity(const osg::Vec3 &v)
Definition Particle:520
osg::Matrixd::transform3x3
static Vec3f transform3x3(const Vec3f &v, const Matrixd &m)
Definition Matrixd:665
osg::Matrixd::preMult
Vec3f preMult(const Vec3f &v) const
Definition Matrixd:618
osg::ref_ptr
Definition ref_ptr:32
osg::ref_ptr::get
T * get() const
Definition ref_ptr:117
osgParticle
Definition AccelOperator:27
osgParticle::rangev4
range< osg::Vec4 > rangev4
Range of osg::Vec4s.
Definition range:85
osgParticle::rangef
range< float > rangef
Range of floats.
Definition range:76
osg::Vec3
Vec3f Vec3
Definition Vec3:21
osg::Matrix
Matrixd Matrix
Definition Matrix:27
osg::Vec4
Vec4f Vec4
Definition Vec4:21
Export
OSGPARTICLE_EXPORT
#define OSGPARTICLE_EXPORT
Definition osgParticle/Export:40
Interpolator
range
ref_ptr