OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Particle.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version. The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * OpenSceneGraph Public License for more details.
12 */
13 //osgParticle - Copyright (C) 2002 Marco Jez
14 
15 #ifndef OSGPARTICLE_PARTICLE
16 #define OSGPARTICLE_PARTICLE 1
17 
18 #include <osgParticle/Export>
19 #include <osgParticle/Interpolator>
20 #include <osgParticle/range>
21 
22 #include <osg/ref_ptr>
23 #include <osg/Vec3>
24 #include <osg/Vec4>
25 #include <osg/Matrix>
26 #include <osg/Drawable>
27 #include <osg/GL>
28 #include <osg/GLBeginEndAdapter>
29 
30 namespace osgParticle
31 {
32 
33  // forward declare so we can reference it
34  class ParticleSystem;
35 
51  friend class ParticleSystem;
52  public:
53 
54  enum
55  {
56  INVALID_INDEX = -1
57  };
58 
63  enum Shape {
64  POINT, // uses GL_POINTS as primitive
65  QUAD, // uses GL_QUADS as primitive
66  QUAD_TRIANGLESTRIP, // uses GL_TRI_angleSTRIP as primitive, but each particle needs a glBegin/glEnd pair
67  HEXAGON, // may save some filling time, but uses more triangles
68  LINE, // uses GL_LINES to draw line segments that point to the direction of motion
69  USER // uses a user-defined drawable as primitive
70  };
71 
72  Particle();
73 
75  inline Shape getShape() const;
76 
78  inline void setShape(Shape s);
79 
81  inline bool isAlive() const;
82 
84  inline double getLifeTime() const;
85 
87  inline double getAge() const;
88 
90  inline const rangef& getSizeRange() const;
91 
93  inline const rangef& getAlphaRange() const;
94 
96  inline const rangev4& getColorRange() const;
97 
99  inline const Interpolator* getSizeInterpolator() const;
100 
102  inline const Interpolator* getAlphaInterpolator() const;
103 
105  inline const Interpolator* getColorInterpolator() const;
106 
110  inline float getRadius() const;
111 
115  inline float getMass() const;
116 
118  inline float getMassInv() const;
119 
121  inline const osg::Vec3& getPosition() const;
122 
127  inline const osg::Vec3& getVelocity() const;
128 
130  inline const osg::Vec3& getPreviousPosition() const;
131 
133  inline const osg::Vec3& getAngle() const;
134 
136  inline const osg::Vec3& getAngularVelocity() const;
137 
139  inline const osg::Vec3& getPreviousAngle() const;
140 
142  inline const osg::Vec4& getCurrentColor() const { return _current_color; }
143 
145  inline float getCurrentAlpha() const { return _current_alpha; }
146 
148  inline float getSTexCoord() const { return _s_coord; }
149 
151  inline float getTTexCoord() const { return _t_coord; }
152 
154  inline int getTileS() const;
155 
157  inline int getTileT() const;
158 
160  inline int getNumTiles() const { return _end_tile - _start_tile + 1; }
161 
166  inline void kill();
167 
169  inline void setLifeTime(double t);
170 
172  inline void setSizeRange(const rangef& r);
173 
175  inline void setAlphaRange(const rangef& r);
176 
178  inline void setColorRange(const rangev4& r);
179 
181  inline void setSizeInterpolator(Interpolator* ri);
182 
184  inline void setAlphaInterpolator(Interpolator* ai);
185 
187  inline void setColorInterpolator(Interpolator* ci);
188 
192  inline void setRadius(float r);
193 
197  inline void setMass(float m);
198 
200  inline void setPosition(const osg::Vec3& p);
201 
206  inline void setVelocity(const osg::Vec3& v);
207 
209  inline void addVelocity(const osg::Vec3& dv);
210 
212  inline void transformPositionVelocity(const osg::Matrix& xform);
213 
215  void transformPositionVelocity(const osg::Matrix& xform1, const osg::Matrix& xform2, float r);
216 
218  inline void setAngle(const osg::Vec3& a);
219 
224  inline void setAngularVelocity(const osg::Vec3& v);
225 
227  inline void addAngularVelocity(const osg::Vec3& dv);
228 
230  inline void transformAngleVelocity(const osg::Matrix& xform);
231 
238  bool update(double dt, bool onlyTimeStamp);
239 
241  inline void beginRender(osg::GLBeginEndAdapter* gl) const;
242 
244  void render(osg::GLBeginEndAdapter* gl, const osg::Vec3& xpos, const osg::Vec3& px, const osg::Vec3& py, float scale = 1.0f) const;
245 
247  void render(osg::RenderInfo& renderInfo, const osg::Vec3& xpos, const osg::Vec3& xrot) const;
248 
250  inline void endRender(osg::GLBeginEndAdapter* gl) const;
251 
253  inline float getCurrentSize() const;
254 
258  inline void setTextureTileRange(int sTile, int tTile, int startTile, int endTile);
259 
261  inline void setTextureTile(int sTile, int tTile, int end = -1);
262 
263  inline int getStartTile() const;
264 
265  inline int getEndTile() const;
266 
268  inline void setPreviousParticle(int previous) { _previousParticle = previous; }
269 
271  inline int getPreviousParticle() const { return _previousParticle; }
272 
274  inline void setNextParticle(int next) { _nextParticle = next; }
275 
277  inline int getNextParticle() const { return _nextParticle; }
278 
280  inline void setDepth(double d) { _depth = d; }
281 
283  inline double getDepth() const { return _depth; }
284 
286  inline void setDrawable(osg::Drawable* d) { _drawable = d; }
287 
289  inline osg::Drawable* getDrawable() const { return _drawable.get(); }
290 
292  bool operator<(const Particle &P) const { return _depth < P._depth; }
293 
295  void setUpTexCoordsAsPartOfConnectedParticleSystem(ParticleSystem* ps);
296 
297  protected:
298 
300 
304 
308 
309  bool _mustdie;
310  double _lifeTime;
311 
312  float _radius;
313  float _mass;
314  float _massinv;
318 
322 
323  double _t0;
324 
325  float _alive;
328  osg::Vec3 _base_prop; // [0] _alive [1] _current_size [2] _current_alpha
330 
331  float _s_tile;
332  float _t_tile;
336  float _s_coord;
337  float _t_coord;
338 
339  // previous and next Particles are only used in ConnectedParticleSystems
342 
343  // the depth of the particle is used only when sorting is enabled
344  double _depth;
345 
346  // the particle drawable is used only when USER shape is enabled
348  };
349 
350  // INLINE FUNCTIONS
351 
353  {
354  return _shape;
355  }
356 
357  inline void Particle::setShape(Shape s)
358  {
359  _shape = s;
360  }
361 
362  inline bool Particle::isAlive() const
363  {
364  return _alive>0.0f;
365  }
366 
367  inline double Particle::getLifeTime() const
368  {
369  return _lifeTime;
370  }
371 
372  inline double Particle::getAge() const
373  {
374  return _t0;
375  }
376 
377  inline float Particle::getRadius() const
378  {
379  return _radius;
380  }
381 
382  inline void Particle::setRadius(float r)
383  {
384  _radius = r;
385  }
386 
387  inline const rangef& Particle::getSizeRange() const
388  {
389  return _sr;
390  }
391 
392  inline const rangef& Particle::getAlphaRange() const
393  {
394  return _ar;
395  }
396 
397  inline const rangev4& Particle::getColorRange() const
398  {
399  return _cr;
400  }
401 
403  {
404  return _si.get();
405  }
406 
408  {
409  return _ai.get();
410  }
411 
413  {
414  return _ci.get();
415  }
416 
417  inline const osg::Vec3& Particle::getPosition() const
418  {
419  return _position;
420  }
421 
422  inline const osg::Vec3& Particle::getVelocity() const
423  {
424  return _velocity;
425  }
426 
428  {
429  return _prev_pos;
430  }
431 
432  inline const osg::Vec3& Particle::getAngle() const
433  {
434  return _angle;
435  }
436 
438  {
439  return _angul_arvel;
440  }
441 
442  inline const osg::Vec3& Particle::getPreviousAngle() const
443  {
444  return _prev_angle;
445  }
446 
447  inline int Particle::getTileS() const
448  {
449  return (_s_tile>0.0f) ? static_cast<int>(1.0f / _s_tile) : 1;
450  }
451 
452  inline int Particle::getTileT() const
453  {
454  return (_t_tile>0.0f) ? static_cast<int>(1.0f / _t_tile) : 1;
455  }
456 
457  inline void Particle::kill()
458  {
459  _mustdie = true;
460  }
461 
462  inline void Particle::setLifeTime(double t)
463  {
464  _lifeTime = t;
465  }
466 
467  inline void Particle::setSizeRange(const rangef& r)
468  {
469  _sr = r;
470  }
471 
472  inline void Particle::setAlphaRange(const rangef& r)
473  {
474  _ar = r;
475  }
476 
477  inline void Particle::setColorRange(const rangev4& r)
478  {
479  _cr = r;
480  }
481 
483  {
484  _si = ri;
485  }
486 
488  {
489  _ai = ai;
490  }
491 
493  {
494  _ci = ci;
495  }
496 
497  inline void Particle::setPosition(const osg::Vec3& p)
498  {
499  _position = p;
500  }
501 
502  inline void Particle::setVelocity(const osg::Vec3& v)
503  {
504  _velocity = v;
505  }
506 
507  inline void Particle::addVelocity(const osg::Vec3& dv)
508  {
509  _velocity += dv;
510  }
511 
513  {
514  _position = xform.preMult(_position);
516  }
517 
518  inline void Particle::transformPositionVelocity(const osg::Matrix& xform1, const osg::Matrix& xform2, float r)
519  {
520  osg::Vec3 position1 = xform1.preMult(_position);
521  osg::Vec3 velocity1 = osg::Matrix::transform3x3(_velocity, xform1);
522  osg::Vec3 position2 = xform2.preMult(_position);
523  osg::Vec3 velocity2 = osg::Matrix::transform3x3(_velocity, xform2);
524  float one_minus_r = 1.0f-r;
525  _position = position1*r + position2*one_minus_r;
526  _velocity = velocity1*r + velocity2*one_minus_r;
527  }
528 
529  inline void Particle::setAngle(const osg::Vec3& a)
530  {
531  _angle = a;
532  }
533 
535  {
536  _angul_arvel = v;
537  }
538 
540  {
541  _angul_arvel += dv;
542  }
543 
545  {
546  // this should be optimized!
547 
549 
550  _angle = xform.preMult(_angle);
551  a1 = xform.preMult(a1);
552 
553  _angul_arvel = a1 - _angle;
554  }
555 
556  inline float Particle::getMass() const
557  {
558  return _mass;
559  }
560 
561  inline float Particle::getMassInv() const
562  {
563  return _massinv;
564  }
565 
566  inline void Particle::setMass(float m)
567  {
568  _mass = m;
569  _massinv = 1 / m;
570  }
571 
573  {
574  switch (_shape)
575  {
576  case POINT:
577  gl->Begin(GL_POINTS);
578  break;
579  case QUAD:
580  gl->Begin(GL_QUADS);
581  break;
582  case LINE:
583  gl->Begin(GL_LINES);
584  break;
585  default: ;
586  }
587  }
588 
590  {
591  switch (_shape)
592  {
593  case POINT:
594  case QUAD:
595  case LINE:
596  gl->End();
597  break;
598  default: ;
599  }
600  }
601 
602  inline float Particle::getCurrentSize() const
603  {
604  return _current_size;
605  }
606 
607 
608  inline void Particle::setTextureTile(int sTile, int tTile, int end)
609  {
610  setTextureTileRange(sTile, tTile, -1, end);
611  }
612 
613  inline void Particle::setTextureTileRange(int sTile, int tTile, int startTile, int endTile)
614  {
615  _s_tile = (sTile>0) ? 1.0f / static_cast<float>(sTile) : 1.0f;
616  _t_tile = (tTile>0) ? 1.0f / static_cast<float>(tTile) : 1.0f;
617 
618  if(startTile == -1)
619  {
620  _start_tile = 0;
621  }
622  else
623  {
624  _start_tile = startTile;
625  }
626 
627  if(endTile == -1)
628  {
629  _end_tile = sTile * tTile;
630  }
631  else
632  {
633  _end_tile = endTile;
634  }
635  }
636 
637  inline int Particle::getStartTile() const
638  {
639  return _start_tile;
640  }
641 
642  inline int Particle::getEndTile() const
643  {
644  return _end_tile;
645  }
646 }
647 
648 #endif
649 
void beginRender(osg::GLBeginEndAdapter *gl) const
Perform some pre-rendering tasks. Called automatically by particle systems.
Definition: Particle.h:572
An abstract base class for implementing interpolators.
Definition: Interpolator.h:30
osg::Vec3 _prev_angle
Definition: Particle.h:319
void setColorRange(const rangev4 &r)
Set the minimum and maximum values for color.
Definition: Particle.h:477
osg::ref_ptr< osg::Drawable > _drawable
Definition: Particle.h:347
void Begin(GLenum mode)
void setNextParticle(int next)
Set the next particle.
Definition: Particle.h:274
int getNumTiles() const
Get number of texture tiles.
Definition: Particle.h:160
osg::Drawable * getDrawable() const
Get the user-defined particle drawable.
Definition: Particle.h:289
void setAlphaRange(const rangef &r)
Set the minimum and maximum values for alpha.
Definition: Particle.h:472
double getLifeTime() const
Get the life time of the particle (in seconds).
Definition: Particle.h:367
void setRadius(float r)
Definition: Particle.h:382
osg::Vec3 _angul_arvel
Definition: Particle.h:321
const osg::Vec4 & getCurrentColor() const
Get the current color.
Definition: Particle.h:142
float getMassInv() const
Get 1 / getMass().
Definition: Particle.h:561
osg::Vec4 _current_color
Definition: Particle.h:329
Shape getShape() const
Get the shape of the particle.
Definition: Particle.h:352
void transformAngleVelocity(const osg::Matrix &xform)
Transform angle and angularVelocity vectors by a matrix.
Definition: Particle.h:544
int getNextParticle() const
Get the const next particle.
Definition: Particle.h:277
void setMass(float m)
Definition: Particle.h:566
osg::ref_ptr< Interpolator > _ai
Definition: Particle.h:306
float getCurrentSize() const
Get the current (interpolated) polygon size. Valid only after the first call to update().
Definition: Particle.h:602
void setLifeTime(double t)
Set the life time of the particle.
Definition: Particle.h:462
const osg::Vec3 & getAngle() const
Get the angle vector.
Definition: Particle.h:432
osg::ref_ptr< Interpolator > _si
Definition: Particle.h:305
void setPosition(const osg::Vec3 &p)
Set the position vector.
Definition: Particle.h:497
const rangef & getAlphaRange() const
Get the minimum and maximum values for alpha.
Definition: Particle.h:392
const osg::Vec3 & getPreviousAngle() const
Get the previous angle vector.
Definition: Particle.h:442
static Vec3f transform3x3(const Vec3f &v, const Matrixd &m)
Definition: Matrixd.h:659
const osg::Vec3 & getAngularVelocity() const
Get the rotational velocity vector.
Definition: Particle.h:437
int getEndTile() const
Definition: Particle.h:642
void setDrawable(osg::Drawable *d)
Set the user-defined particle drawable.
Definition: Particle.h:286
void setAngularVelocity(const osg::Vec3 &v)
Definition: Particle.h:534
void setColorInterpolator(Interpolator *ci)
Set the interpolator for computing color values.
Definition: Particle.h:492
const osg::Vec3 & getPreviousPosition() const
Get the previous position (the position before last update).
Definition: Particle.h:427
void setPreviousParticle(int previous)
Set the previous particle.
Definition: Particle.h:268
void setVelocity(const osg::Vec3 &v)
Definition: Particle.h:502
double getAge() const
Get the age of the particle (in seconds).
Definition: Particle.h:372
bool isAlive() const
Get whether the particle is still alive.
Definition: Particle.h:362
void setShape(Shape s)
Set the shape of the particle.
Definition: Particle.h:357
#define OSGPARTICLE_EXPORT
Definition: Export.h:40
int getTileS() const
Get width of texture tile.
Definition: Particle.h:447
const osg::Vec3 & getPosition() const
Get the position vector.
Definition: Particle.h:417
float getMass() const
Definition: Particle.h:556
osg::Vec3 _prev_pos
Definition: Particle.h:315
osg::Vec3 _velocity
Definition: Particle.h:317
osg::Vec3 _position
Definition: Particle.h:316
Vec3f preMult(const Vec3f &v) const
Definition: Matrixd.h:612
const Interpolator * getColorInterpolator() const
Get the interpolator for computing color values.
Definition: Particle.h:412
bool operator<(const Particle &P) const
Sorting operator.
Definition: Particle.h:292
float getTTexCoord() const
Get the t texture coordinate of the bottom left of the particle.
Definition: Particle.h:151
void setSizeRange(const rangef &r)
Set the minimum and maximum values for polygon size.
Definition: Particle.h:467
osg::Vec3 _base_prop
Definition: Particle.h:328
void setSizeInterpolator(Interpolator *ri)
Set the interpolator for computing size values.
Definition: Particle.h:482
float getCurrentAlpha() const
Get the current alpha.
Definition: Particle.h:145
const Interpolator * getSizeInterpolator() const
Get the interpolator for computing the size of polygons.
Definition: Particle.h:402
void setAlphaInterpolator(Interpolator *ai)
Set the interpolator for computing alpha values.
Definition: Particle.h:487
void setDepth(double d)
Set the depth of the particle.
Definition: Particle.h:280
double getDepth() const
Get the depth of the particle.
Definition: Particle.h:283
const Interpolator * getAlphaInterpolator() const
Get the interpolator for computing alpha values.
Definition: Particle.h:407
float getSTexCoord() const
Get the s texture coordinate of the bottom left of the particle.
Definition: Particle.h:148
int getPreviousParticle() const
Get the previous particle.
Definition: Particle.h:271
void setTextureTileRange(int sTile, int tTile, int startTile, int endTile)
Definition: Particle.h:613
const rangef & getSizeRange() const
Get the minimum and maximum values for polygon size.
Definition: Particle.h:387
osg::ref_ptr< Interpolator > _ci
Definition: Particle.h:307
float getRadius() const
Definition: Particle.h:377
void addAngularVelocity(const osg::Vec3 &dv)
Add a vector to the angular velocity vector.
Definition: Particle.h:539
const osg::Vec3 & getVelocity() const
Definition: Particle.h:422
void setTextureTile(int sTile, int tTile, int end=-1)
Same as above, range starts at 0 and ends at end.
Definition: Particle.h:608
void addVelocity(const osg::Vec3 &dv)
Add a vector to the velocity vector.
Definition: Particle.h:507
int getStartTile() const
Definition: Particle.h:637
void endRender(osg::GLBeginEndAdapter *gl) const
Perform some post-rendering tasks. Called automatically by particle systems.
Definition: Particle.h:589
void transformPositionVelocity(const osg::Matrix &xform)
Transform position and velocity vectors by a matrix.
Definition: Particle.h:512
int getTileT() const
Get height of texture tile.
Definition: Particle.h:452
const rangev4 & getColorRange() const
Get the minimum and maximum values for color.
Definition: Particle.h:397
void setAngle(const osg::Vec3 &a)
Set the angle vector.
Definition: Particle.h:529