OpenSceneGraph 3.6.5
Particle
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>
20#include <osgParticle/range>
21
22#include <osg/ref_ptr>
23#include <osg/Vec3>
24#include <osg/Vec4>
25#include <osg/Matrix>
26
27namespace osgParticle
28{
29
30 // forward declare so we can reference it
31 class ParticleSystem;
32
48 friend class ParticleSystem;
49 public:
50
51 enum
52 {
54 };
55
60 enum Shape {
61 POINT, // uses GL_POINTS as primitive
62 QUAD, // uses GL_QUADS as primitive
63 QUAD_TRIANGLESTRIP, // no longer supported, falls back to QUAD
64 HEXAGON, // no longer supported, falls back to QUAD
65 LINE, // uses GL_LINES to draw line segments that point to the direction of motion
66 USER // no longer supported, falls back to QUAD
67 };
68
70
72 inline Shape getShape() const;
73
75 inline void setShape(Shape s);
76
78 inline bool isAlive() const;
79
81 inline double getLifeTime() const;
82
84 inline double getAge() const;
85
87 inline const rangef& getSizeRange() const;
88
90 inline const rangef& getAlphaRange() const;
91
93 inline const rangev4& getColorRange() const;
94
96 inline const Interpolator* getSizeInterpolator() const;
97
99 inline const Interpolator* getAlphaInterpolator() const;
100
102 inline const Interpolator* getColorInterpolator() const;
103
107 inline float getRadius() const;
108
112 inline float getMass() const;
113
115 inline float getMassInv() const;
116
118 inline const osg::Vec3& getPosition() const;
119
124 inline const osg::Vec3& getVelocity() const;
125
127 inline const osg::Vec3& getPreviousPosition() const;
128
130 inline const osg::Vec3& getAngle() const;
131
133 inline const osg::Vec3& getAngularVelocity() const;
134
136 inline const osg::Vec3& getPreviousAngle() const;
137
139 inline const osg::Vec4& getCurrentColor() const { return _current_color; }
140
142 inline float getCurrentAlpha() const { return _current_alpha; }
143
145 inline float getSTexCoord() const { return _s_coord; }
146
148 inline float getTTexCoord() const { return _t_coord; }
149
151 inline float getSTexTile() const { return _s_tile; }
152
154 inline float getTTexTile() const { return _t_tile; }
155
156
158 inline int getTileS() const;
159
161 inline int getTileT() const;
162
164 inline int getNumTiles() const { return _end_tile - _start_tile + 1; }
165
170 inline void kill();
171
173 inline void setLifeTime(double t);
174
176 inline void setSizeRange(const rangef& r);
177
179 inline void setAlphaRange(const rangef& r);
180
182 inline void setColorRange(const rangev4& r);
183
185 inline void setSizeInterpolator(Interpolator* ri);
186 template<class T> void setSizeInterpolator(const osg::ref_ptr<T>& ri) { setSizeInterpolator(ri.get()); }
187
189 inline void setAlphaInterpolator(Interpolator* ai);
190 template<class T> void setAlphaInterpolator(const osg::ref_ptr<T>& ri) { setAlphaInterpolator(ri.get()); }
191
193 inline void setColorInterpolator(Interpolator* ci);
194 template<class T> void setColorInterpolator(const osg::ref_ptr<T>& ri) { setColorInterpolator(ri.get()); }
195
199 inline void setRadius(float r);
200
204 inline void setMass(float m);
205
207 inline void setPosition(const osg::Vec3& p);
208
213 inline void setVelocity(const osg::Vec3& v);
214
216 inline void addVelocity(const osg::Vec3& dv);
217
219 inline void transformPositionVelocity(const osg::Matrix& xform);
220
222 void transformPositionVelocity(const osg::Matrix& xform1, const osg::Matrix& xform2, float r);
223
225 inline void setAngle(const osg::Vec3& a);
226
231 inline void setAngularVelocity(const osg::Vec3& v);
232
234 inline void addAngularVelocity(const osg::Vec3& dv);
235
237 inline void transformAngleVelocity(const osg::Matrix& xform);
238
245 bool update(double dt, bool onlyTimeStamp);
246
248 inline float getCurrentSize() const;
249
253 inline void setTextureTileRange(int sTile, int tTile, int startTile, int endTile);
254
256 inline void setTextureTile(int sTile, int tTile, int end = -1);
257
258 inline int getStartTile() const;
259
260 inline int getEndTile() const;
261
263 inline void setPreviousParticle(int previous) { _previousParticle = previous; }
264
266 inline int getPreviousParticle() const { return _previousParticle; }
267
269 inline void setNextParticle(int next) { _nextParticle = next; }
270
272 inline int getNextParticle() const { return _nextParticle; }
273
275 inline void setDepth(double d) { _depth = d; }
276
278 inline double getDepth() const { return _depth; }
279
281 bool operator<(const Particle &P) const { return _depth < P._depth; }
282
285
286 protected:
287
289
293
297
299 double _lifeTime;
300
301 float _radius;
302 float _mass;
303 float _massinv;
307
311
312 double _t0;
313
314 float _alive;
317 osg::Vec3 _base_prop; // [0] _alive [1] _current_size [2] _current_alpha
319
320 float _s_tile;
321 float _t_tile;
325 float _s_coord;
326 float _t_coord;
327
328 // previous and next Particles are only used in ConnectedParticleSystems
331
332 // the depth of the particle is used only when sorting is enabled
333 double _depth;
334 };
335
336 // INLINE FUNCTIONS
337
339 {
340 return _shape;
341 }
342
344 {
345 _shape = s;
346 }
347
348 inline bool Particle::isAlive() const
349 {
350 return _alive>0.0f;
351 }
352
353 inline double Particle::getLifeTime() const
354 {
355 return _lifeTime;
356 }
357
358 inline double Particle::getAge() const
359 {
360 return _t0;
361 }
362
363 inline float Particle::getRadius() const
364 {
365 return _radius;
366 }
367
368 inline void Particle::setRadius(float r)
369 {
370 _radius = r;
371 }
372
373 inline const rangef& Particle::getSizeRange() const
374 {
375 return _sr;
376 }
377
378 inline const rangef& Particle::getAlphaRange() const
379 {
380 return _ar;
381 }
382
383 inline const rangev4& Particle::getColorRange() const
384 {
385 return _cr;
386 }
387
389 {
390 return _si.get();
391 }
392
394 {
395 return _ai.get();
396 }
397
399 {
400 return _ci.get();
401 }
402
403 inline const osg::Vec3& Particle::getPosition() const
404 {
405 return _position;
406 }
407
408 inline const osg::Vec3& Particle::getVelocity() const
409 {
410 return _velocity;
411 }
412
414 {
415 return _prev_pos;
416 }
417
418 inline const osg::Vec3& Particle::getAngle() const
419 {
420 return _angle;
421 }
422
424 {
425 return _angul_arvel;
426 }
427
429 {
430 return _prev_angle;
431 }
432
433 inline int Particle::getTileS() const
434 {
435 return (_s_tile>0.0f) ? static_cast<int>(1.0f / _s_tile) : 1;
436 }
437
438 inline int Particle::getTileT() const
439 {
440 return (_t_tile>0.0f) ? static_cast<int>(1.0f / _t_tile) : 1;
441 }
442
443 inline void Particle::kill()
444 {
445 _mustdie = true;
446 }
447
448 inline void Particle::setLifeTime(double t)
449 {
450 _lifeTime = t;
451 }
452
453 inline void Particle::setSizeRange(const rangef& r)
454 {
455 _sr = r;
456 }
457
458 inline void Particle::setAlphaRange(const rangef& r)
459 {
460 _ar = r;
461 }
462
463 inline void Particle::setColorRange(const rangev4& r)
464 {
465 _cr = r;
466 }
467
469 {
470 _si = ri;
471 }
472
474 {
475 _ai = ai;
476 }
477
479 {
480 _ci = ci;
481 }
482
483 inline void Particle::setPosition(const osg::Vec3& p)
484 {
485 _position = p;
486 }
487
488 inline void Particle::setVelocity(const osg::Vec3& v)
489 {
490 _velocity = v;
491 }
492
493 inline void Particle::addVelocity(const osg::Vec3& dv)
494 {
495 _velocity += dv;
496 }
497
503
504 inline void Particle::transformPositionVelocity(const osg::Matrix& xform1, const osg::Matrix& xform2, float r)
505 {
506 osg::Vec3 position1 = xform1.preMult(_position);
507 osg::Vec3 velocity1 = osg::Matrix::transform3x3(_velocity, xform1);
508 osg::Vec3 position2 = xform2.preMult(_position);
509 osg::Vec3 velocity2 = osg::Matrix::transform3x3(_velocity, xform2);
510 float one_minus_r = 1.0f-r;
511 _position = position1*r + position2*one_minus_r;
512 _velocity = velocity1*r + velocity2*one_minus_r;
513 }
514
515 inline void Particle::setAngle(const osg::Vec3& a)
516 {
517 _angle = a;
518 }
519
521 {
522 _angul_arvel = v;
523 }
524
526 {
527 _angul_arvel += dv;
528 }
529
531 {
532 // this should be optimized!
533
535
536 _angle = xform.preMult(_angle);
537 a1 = xform.preMult(a1);
538
539 _angul_arvel = a1 - _angle;
540 }
541
542 inline float Particle::getMass() const
543 {
544 return _mass;
545 }
546
547 inline float Particle::getMassInv() const
548 {
549 return _massinv;
550 }
551
552 inline void Particle::setMass(float m)
553 {
554 _mass = m;
555 _massinv = 1 / m;
556 }
557
558 inline float Particle::getCurrentSize() const
559 {
560 return _current_size;
561 }
562
563
564 inline void Particle::setTextureTile(int sTile, int tTile, int end)
565 {
566 setTextureTileRange(sTile, tTile, -1, end);
567 }
568
569 inline void Particle::setTextureTileRange(int sTile, int tTile, int startTile, int endTile)
570 {
571 _s_tile = (sTile>0) ? 1.0f / static_cast<float>(sTile) : 1.0f;
572 _t_tile = (tTile>0) ? 1.0f / static_cast<float>(tTile) : 1.0f;
573
574 if(startTile == -1)
575 {
576 _start_tile = 0;
577 }
578 else
579 {
580 _start_tile = startTile;
581 }
582
583 if(endTile == -1)
584 {
585 _end_tile = sTile * tTile;
586 }
587 else
588 {
589 _end_tile = endTile;
590 }
591 }
592
593 inline int Particle::getStartTile() const
594 {
595 return _start_tile;
596 }
597
598 inline int Particle::getEndTile() const
599 {
600 return _end_tile;
601 }
602}
603
604#endif
605
Vec3f Vec3
Definition Vec3:21
Matrixd Matrix
Definition Matrix:27
Vec4f Vec4
Definition Vec4:21
The osgParticle library is a NodeKit that extends the core scene graph to support particle effects.
Definition AccelOperator:27
range< osg::Vec4 > rangev4
Range of osg::Vec4s.
Definition range:85
range< float > rangef
Range of floats.
Definition range:76
static Vec3f transform3x3(const Vec3f &v, const Matrixd &m)
apply a 3x3 transform of v*M[0..2,0..2].
Definition Matrixd:665
Vec3f preMult(const Vec3f &v) const
Definition Matrixd:618
Smart pointer for handling referenced counted objects.
Definition ref_ptr:32
T * get() const
Definition ref_ptr:117
An abstract base class for implementing interpolators.
Definition Interpolator:30
float getTTexCoord() const
Get the t texture coordinate of the bottom left of the particle.
Definition Particle:148
void transformAngleVelocity(const osg::Matrix &xform)
Transform angle and angularVelocity vectors by a matrix.
Definition Particle:530
bool update(double dt, bool onlyTimeStamp)
Update the particle (don't call this method manually).
float _s_tile
Definition Particle:320
Shape _shape
Definition Particle:288
int getTileT() const
Get height of texture tile.
Definition Particle:438
rangev4 _cr
Definition Particle:292
osg::ref_ptr< Interpolator > _si
Definition Particle:294
float _current_size
Definition Particle:315
osg::Vec3 _prev_pos
Definition Particle:304
void setPreviousParticle(int previous)
Set the previous particle.
Definition Particle:263
float getSTexTile() const
Get the texture coordinate width of the particle.
Definition Particle:151
void setDepth(double d)
Set the depth of the particle.
Definition Particle:275
osg::Vec3 _angle
Definition Particle:309
void setSizeRange(const rangef &r)
Set the minimum and maximum values for polygon size.
Definition Particle:453
double getDepth() const
Get the depth of the particle.
Definition Particle:278
void setShape(Shape s)
Set the shape of the particle.
Definition Particle:343
float _radius
Definition Particle:301
const rangef & getAlphaRange() const
Get the minimum and maximum values for alpha.
Definition Particle:378
friend class ParticleSystem
Definition Particle:48
void addAngularVelocity(const osg::Vec3 &dv)
Add a vector to the angular velocity vector.
Definition Particle:525
void setTextureTile(int sTile, int tTile, int end=-1)
Same as above, range starts at 0 and ends at end.
Definition Particle:564
int getTileS() const
Get width of texture tile.
Definition Particle:433
float _t_tile
Definition Particle:321
osg::Vec3 _velocity
Definition Particle:306
int _start_tile
Definition Particle:322
double getLifeTime() const
Get the life time of the particle (in seconds).
Definition Particle:353
osg::Vec3 _prev_angle
Definition Particle:308
osg::ref_ptr< Interpolator > _ci
Definition Particle:296
const Interpolator * getColorInterpolator() const
Get the interpolator for computing color values.
Definition Particle:398
osg::Vec4 _current_color
Definition Particle:318
void addVelocity(const osg::Vec3 &dv)
Add a vector to the velocity vector.
Definition Particle:493
void setAlphaInterpolator(Interpolator *ai)
Set the interpolator for computing alpha values.
Definition Particle:473
float getMass() const
Get the mass of the particle.
Definition Particle:542
float _massinv
Definition Particle:303
void setColorRange(const rangev4 &r)
Set the minimum and maximum values for color.
Definition Particle:463
void setTextureTileRange(int sTile, int tTile, int startTile, int endTile)
Specify how the particle texture is tiled.
Definition Particle:569
osg::Vec3 _position
Definition Particle:305
void setAngle(const osg::Vec3 &a)
Set the angle vector.
Definition Particle:515
float getMassInv() const
Get 1 / getMass().
Definition Particle:547
const osg::Vec3 & getPreviousAngle() const
Get the previous angle vector.
Definition Particle:428
int _end_tile
Definition Particle:323
const osg::Vec4 & getCurrentColor() const
Get the current color.
Definition Particle:139
double _t0
Definition Particle:312
void setPosition(const osg::Vec3 &p)
Set the position vector.
Definition Particle:483
double _depth
Definition Particle:333
float getCurrentSize() const
Get the current (interpolated) polygon size. Valid only after the first call to update().
Definition Particle:558
osg::Vec3 _angul_arvel
Definition Particle:310
@ INVALID_INDEX
Definition Particle:53
osg::ref_ptr< Interpolator > _ai
Definition Particle:295
void setAlphaRange(const rangef &r)
Set the minimum and maximum values for alpha.
Definition Particle:458
const Interpolator * getAlphaInterpolator() const
Get the interpolator for computing alpha values.
Definition Particle:393
double _lifeTime
Definition Particle:299
int getNumTiles() const
Get number of texture tiles.
Definition Particle:164
rangef _sr
Definition Particle:290
void setSizeInterpolator(Interpolator *ri)
Set the interpolator for computing size values.
Definition Particle:468
float _t_coord
Definition Particle:326
float getSTexCoord() const
Get the s texture coordinate of the bottom left of the particle.
Definition Particle:145
int getEndTile() const
Definition Particle:598
bool _mustdie
Definition Particle:298
const rangev4 & getColorRange() const
Get the minimum and maximum values for color.
Definition Particle:383
void kill()
Kill the particle on next update NOTE: after calling this function, the isAlive() method will still r...
Definition Particle:443
bool isAlive() const
Get whether the particle is still alive.
Definition Particle:348
float _mass
Definition Particle:302
const osg::Vec3 & getPreviousPosition() const
Get the previous position (the position before last update).
Definition Particle:413
float getTTexTile() const
Get the texture coordinate height of the particle.
Definition Particle:154
void setNextParticle(int next)
Set the next particle.
Definition Particle:269
void setSizeInterpolator(const osg::ref_ptr< T > &ri)
Definition Particle:186
void setAlphaInterpolator(const osg::ref_ptr< T > &ri)
Definition Particle:190
Shape
Shape of particles.
Definition Particle:60
@ USER
Definition Particle:66
@ POINT
Definition Particle:61
@ LINE
Definition Particle:65
@ QUAD_TRIANGLESTRIP
Definition Particle:63
@ HEXAGON
Definition Particle:64
@ QUAD
Definition Particle:62
int getStartTile() const
Definition Particle:593
rangef _ar
Definition Particle:291
void setMass(float m)
Set the mass of the particle.
Definition Particle:552
void setColorInterpolator(Interpolator *ci)
Set the interpolator for computing color values.
Definition Particle:478
int _cur_tile
Definition Particle:324
Shape getShape() const
Get the shape of the particle.
Definition Particle:338
const rangef & getSizeRange() const
Get the minimum and maximum values for polygon size.
Definition Particle:373
double getAge() const
Get the age of the particle (in seconds).
Definition Particle:358
osg::Vec3 _base_prop
Definition Particle:317
bool operator<(const Particle &P) const
Sorting operator.
Definition Particle:281
const osg::Vec3 & getPosition() const
Get the position vector.
Definition Particle:403
float _s_coord
Definition Particle:325
void setLifeTime(double t)
Set the life time of the particle.
Definition Particle:448
int getPreviousParticle() const
Get the previous particle.
Definition Particle:266
int _previousParticle
Definition Particle:329
float _alive
Definition Particle:314
void setUpTexCoordsAsPartOfConnectedParticleSystem(ParticleSystem *ps)
Method for initializing a particles texture coords as part of a connected particle system.
int _nextParticle
Definition Particle:330
const osg::Vec3 & getVelocity() const
Get the velocity vector.
Definition Particle:408
float getCurrentAlpha() const
Get the current alpha.
Definition Particle:142
void transformPositionVelocity(const osg::Matrix &xform)
Transform position and velocity vectors by a matrix.
Definition Particle:498
float getRadius() const
Get the physical radius of the particle.
Definition Particle:363
float _current_alpha
Definition Particle:316
void setVelocity(const osg::Vec3 &v)
Set the velocity vector.
Definition Particle:488
void setColorInterpolator(const osg::ref_ptr< T > &ri)
Definition Particle:194
int getNextParticle() const
Get the const next particle.
Definition Particle:272
void setRadius(float r)
Set the physical radius of the particle.
Definition Particle:368
const Interpolator * getSizeInterpolator() const
Get the interpolator for computing the size of polygons.
Definition Particle:388
const osg::Vec3 & getAngularVelocity() const
Get the rotational velocity vector.
Definition Particle:423
const osg::Vec3 & getAngle() const
Get the angle vector.
Definition Particle:418
void setAngularVelocity(const osg::Vec3 &v)
Set the angular velocity vector.
Definition Particle:520
#define OSGPARTICLE_EXPORT
Definition Export:40

osg logo
Generated at Sun Jul 20 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.