OpenSceneGraph 3.6.5
ParticleSystem
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_PARTICLESYSTEM
16#define OSGPARTICLE_PARTICLESYSTEM 1
17
18#include <osgParticle/Export>
19#include <osgParticle/Particle>
20
21#include <vector>
22#include <stack>
23#include <algorithm>
24#include <string>
25
26#include <osg/Object>
27#include <osg/Drawable>
28#include <osg/CopyOp>
29#include <osg/State>
30#include <osg/Vec3>
31#include <osg/BoundingBox>
32
33// 9th Febrary 2009, disabled the use of ReadWriteMutex as it looks like this
34// is introducing threading problems due to threading problems in OpenThreads::ReadWriteMutex.
35// #define OSGPARTICLE_USE_ReadWriteMutex
36
37#ifdef OSGPARTICLE_USE_ReadWriteMutex
38 #include <OpenThreads/ReadWriteMutex>
39#else
40 #include <OpenThreads/Mutex>
41 #include <OpenThreads/ScopedLock>
42#endif
43
44
45namespace osgParticle
46{
47
55 public:
56
61
64
66
68 inline Alignment getParticleAlignment() const;
69
71 inline void setParticleAlignment(Alignment a);
72
74 inline const osg::Vec3& getAlignVectorX() const;
75
77 inline void setAlignVectorX(const osg::Vec3& v);
78
80 inline const osg::Vec3& getAlignVectorY() const;
81
83 inline void setAlignVectorY(const osg::Vec3& v);
84
86 inline void setAlignVectors(const osg::Vec3& X, const osg::Vec3& Y);
87
88
89
95
98
101
102
103
105 inline const osg::BoundingBox& getDefaultBoundingBox() const;
106
111 inline void setDefaultBoundingBox(const osg::BoundingBox& bbox);
112
114 bool getUseVertexArray() const { return _useVertexArray; }
115
122 void setUseVertexArray(bool v) { _useVertexArray = v; }
123
125 bool getUseShaders() const { return _useShaders; }
126
131 void setUseShaders(bool v) { _useShaders = v; _dirty_uniforms = true; }
132
134 inline bool getDoublePassRendering() const;
135
143 inline void setDoublePassRendering(bool v);
144
146 bool getFrozen() const { return _frozen; }
147 inline bool isFrozen() const;
148
152 inline void setFrozen(bool v);
153
155 inline int numParticles() const;
156
158 inline int numDeadParticles() const;
159
161 inline bool areAllParticlesDead() const { return numDeadParticles()==numParticles(); }
162
164 inline Particle* getParticle(int i);
165
167 inline const Particle* getParticle(int i) const;
168
170 virtual Particle* createParticle(const Particle* ptemplate);
171
173 inline virtual void destroyParticle(int i);
174
176 inline virtual void reuseParticle(int i) { _deadparts.push(&(_particles[i])); }
177
179 inline unsigned int getLastFrameNumber() const;
180
182 inline double& getDeltaTime( double currentTime );
183
185 inline Particle& getDefaultParticleTemplate();
186
188 inline const Particle& getDefaultParticleTemplate() const;
189
191 inline void setDefaultParticleTemplate(const Particle& p);
192
194 inline bool getFreezeOnCull() const;
195
197 inline void setFreezeOnCull(bool v);
198
202 void setDefaultAttributes(const std::string& texturefile = "", bool emissive_particles = true, bool lighting = false, int texture_unit = 0);
203
208 void setDefaultAttributesUsingShaders(const std::string& texturefile = "", bool emissive_particles = true, int texture_unit = 0);
209
211 inline int getLevelOfDetail() const;
212
216 inline void setLevelOfDetail(int v);
217
224
226 inline SortMode getSortMode() const;
227
231 inline void setSortMode(SortMode mode);
232
234 inline double getVisibilityDistance() const;
235
239 inline void setVisibilityDistance(double distance);
240
242 virtual void update(double dt, osg::NodeVisitor& nv);
243
244 virtual void drawImplementation(osg::RenderInfo& renderInfo) const;
245
247
248#ifdef OSGPARTICLE_USE_ReadWriteMutex
249 typedef OpenThreads::ReadWriteMutex ReadWriterMutex;
250 typedef OpenThreads::ScopedReadLock ScopedReadLock;
251 typedef OpenThreads::ScopedWriteLock ScopedWriteLock;
252#else
253 typedef OpenThreads::Mutex ReadWriterMutex;
254 typedef OpenThreads::ScopedLock<OpenThreads::Mutex> ScopedReadLock;
255 typedef OpenThreads::ScopedLock<OpenThreads::Mutex> ScopedWriteLock;
256#endif
257
259
261 virtual void resizeGLObjectBuffers(unsigned int maxSize);
262
266 virtual void releaseGLObjects(osg::State* state=0) const;
267
269
271
274
275 protected:
276
278
279 ParticleSystem& operator=(const ParticleSystem&) { return *this; }
280
281 inline void update_bounds(const osg::Vec3& p, float r);
282
283 typedef std::vector<Particle> Particle_vector;
284 typedef std::stack<Particle*> Death_stack;
285
288
290
295
299
302
305
308
310 mutable unsigned int _last_frame;
311 mutable bool _dirty_dt;
313
314 double _t0;
315 double _dt;
316
320
322
324
354
357 };
358
359 // INLINE FUNCTIONS
360
365
367 {
368 _alignment = a;
369 }
370
372 {
373 return _align_X_axis;
374 }
375
377 {
378 _align_X_axis = v;
379 }
380
382 {
383 return _align_Y_axis;
384 }
385
387 {
388 _align_Y_axis = v;
389 }
390
392 {
393 _align_X_axis = X;
394 _align_Y_axis = Y;
395 }
396
397 inline bool ParticleSystem::isFrozen() const
398 {
399 return _frozen;
400 }
401
402 inline void ParticleSystem::setFrozen(bool v)
403 {
404 _frozen = v;
405 }
406
408 {
409 return _def_bbox;
410 }
411
413 {
414 _def_bbox = bbox;
415 }
416
418 {
419 return _doublepass;
420 }
421
423 {
424 _doublepass = v;
425 }
426
428 {
429 return static_cast<int>(_particles.size());
430 }
431
433 {
434 return static_cast<int>(_deadparts.size());
435 }
436
438 {
439 return &_particles[i];
440 }
441
442 inline const Particle* ParticleSystem::getParticle(int i) const
443 {
444 return &_particles[i];
445 }
446
448 {
449 _particles[i].kill();
450 }
451
452 inline unsigned int ParticleSystem::getLastFrameNumber() const
453 {
454 return _last_frame;
455 }
456
457 inline double& ParticleSystem::getDeltaTime( double currentTime )
458 {
459 if ( _dirty_dt )
460 {
461 _dt = currentTime - _t0;
462 if ( _dt<0.0 ) _dt = 0.0;
463
464 _t0 = currentTime;
465 _dirty_dt = false;
466 }
467 return _dt;
468 }
469
470 inline void ParticleSystem::update_bounds(const osg::Vec3& p, float r)
471 {
472 if (_reset_bounds_flag) {
473 _reset_bounds_flag = false;
474 _bmin = p - osg::Vec3(r,r,r);
475 _bmax = p + osg::Vec3(r,r,r);
476 } else {
477 if (p.x() - r < _bmin.x()) _bmin.x() = p.x() - r;
478 if (p.y() - r < _bmin.y()) _bmin.y() = p.y() - r;
479 if (p.z() - r < _bmin.z()) _bmin.z() = p.z() - r;
480 if (p.x() + r > _bmax.x()) _bmax.x() = p.x() + r;
481 if (p.y() + r > _bmax.y()) _bmax.y() = p.y() + r;
482 if (p.z() + r > _bmax.z()) _bmax.z() = p.z() + r;
483 }
484 if (!_bounds_computed)
485 _bounds_computed = true;
486 }
487
492
494 {
495 return _def_ptemp;
496 }
497
499 {
500 _def_ptemp = p;
501 }
502
504 {
505 return _freeze_on_cull;
506 }
507
509 {
510 _freeze_on_cull = v;
511 }
512
514 {
515 return _detail;
516 }
517
519 {
520 if (v < 1) v = 1;
521 _detail = v;
522 }
523
525 {
526 return _sortMode;
527 }
528
530 {
531 _sortMode = mode;
532 }
533
535 {
536 return _visibilityDistance;
537 }
538
539 inline void ParticleSystem::setVisibilityDistance(double distance)
540 {
541 _visibilityDistance = distance;
542 if (_useShaders) _dirty_uniforms = true;
543 }
544
545}
546
547#endif
Vec3f Vec3
Definition Vec3:21
BoundingBoxd BoundingBox
Definition BoundingBox:257
The osgParticle library is a NodeKit that extends the core scene graph to support particle effects.
Definition AccelOperator:27
Copy Op(erator) used to control whether shallow or deep copy is used during copy construction and clo...
Definition CopyOp:41
@ SHALLOW_COPY
Definition CopyOp:47
Pure virtual base class for drawable geometry.
Definition Drawable:89
Visitor for type safe operations on osg::Nodes.
Definition NodeVisitor:82
Smart pointer for handling referenced counted objects.
Definition ref_ptr:32
Definition RenderInfo:28
Encapsulates the current applied OpenGL modes, attributes and vertex arrays settings,...
Definition State:80
value_type & z()
Definition Vec3f:82
value_type & y()
Definition Vec3f:81
value_type & x()
Definition Vec3f:80
Definition VertexArrayState:25
Implementation of a particle.
Definition Particle:47
ParticleScaleReferenceFrame
Definition ParticleSystem:91
@ WORLD_COORDINATES
Definition ParticleSystem:93
@ LOCAL_COORDINATES
Definition ParticleSystem:92
int getEstimatedMaxNumOfParticles() const
Definition ParticleSystem:273
std::stack< Particle * > Death_stack
Definition ParticleSystem:284
void setDefaultBoundingBox(const osg::BoundingBox &bbox)
Set the default bounding box.
Definition ParticleSystem:412
void setFrozen(bool v)
Set or reset the frozen state.
Definition ParticleSystem:402
int _detail
Definition ParticleSystem:317
SortMode
Definition ParticleSystem:219
@ SORT_BACK_TO_FRONT
Definition ParticleSystem:222
@ NO_SORT
Definition ParticleSystem:220
@ SORT_FRONT_TO_BACK
Definition ParticleSystem:221
virtual void resizeGLObjectBuffers(unsigned int maxSize)
Resize any per context GLObject buffers to specified size.
bool getUseShaders() const
Return true if shaders are required.
Definition ParticleSystem:125
osg::Vec3 _bmin
Definition ParticleSystem:303
bool _doublepass
Definition ParticleSystem:300
void setLevelOfDetail(int v)
(EXPERIMENTAL) Set the level of detail.
Definition ParticleSystem:518
int _estimatedMaxNumOfParticles
Definition ParticleSystem:323
bool getUseVertexArray() const
Return true if we use vertex arrays for rendering particles.
Definition ParticleSystem:114
bool _freeze_on_cull
Definition ParticleSystem:312
void setParticleScaleReferenceFrame(ParticleScaleReferenceFrame rf)
Set whether the particles should be scaled relative to world coordaintes or local coordinates.
Definition ParticleSystem:97
virtual void drawImplementation(osg::RenderInfo &renderInfo) const
drawImplementation(RenderInfo&) is a pure virtual method for the actual implementation of OpenGL draw...
OpenThreads::ScopedLock< OpenThreads::Mutex > ScopedReadLock
Definition ParticleSystem:254
virtual Particle * createParticle(const Particle *ptemplate)
Create a new particle from the specified template (or the default one if ptemplate is null).
double _t0
Definition ParticleSystem:314
Particle & getDefaultParticleTemplate()
Get a reference to the default particle template.
Definition ParticleSystem:488
virtual void destroyParticle(int i)
Destroy the i-th particle.
Definition ParticleSystem:447
std::vector< Particle > Particle_vector
Definition ParticleSystem:283
void update_bounds(const osg::Vec3 &p, float r)
Definition ParticleSystem:470
SortMode getSortMode() const
Get the sort mode.
Definition ParticleSystem:524
Particle_vector _particles
Definition ParticleSystem:286
unsigned int getLastFrameNumber() const
Get the last frame number.
Definition ParticleSystem:452
Particle _def_ptemp
Definition ParticleSystem:309
void setUseShaders(bool v)
Set to use GLSL shaders for rendering particles.
Definition ParticleSystem:131
Particle * getParticle(int i)
Get a pointer to the i-th particle.
Definition ParticleSystem:437
double _visibilityDistance
Definition ParticleSystem:319
bool _reset_bounds_flag
Definition ParticleSystem:306
ParticleSystem & operator=(const ParticleSystem &)
Definition ParticleSystem:279
virtual osg::VertexArrayState * createVertexArrayStateImplementation(osg::RenderInfo &renderInfo) const
Implementation of Create the VertexArrayState object.
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 StateAttribute's in one call.
osg::buffered_object< ArrayData > BufferedArrayData
Definition ParticleSystem:355
ReadWriterMutex _readWriteMutex
Definition ParticleSystem:321
void setAlignVectorY(const osg::Vec3 &v)
Set the Y-axis alignment vector.
Definition ParticleSystem:386
ParticleSystem(const ParticleSystem &copy, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
const osg::BoundingBox & getDefaultBoundingBox() const
Get the default bounding box.
Definition ParticleSystem:407
osg::Vec3 _bmax
Definition ParticleSystem:304
bool getFreezeOnCull() const
Get whether the particle system can freeze when culled.
Definition ParticleSystem:503
double _dt
Definition ParticleSystem:315
void setEstimatedMaxNumOfParticles(int num)
Definition ParticleSystem:272
virtual void reuseParticle(int i)
Reuse the i-th particle.
Definition ParticleSystem:176
virtual osg::BoundingBox computeBoundingBox() const
Compute the bounding box around Drawables's geometry.
bool _dirty_dt
Definition ParticleSystem:311
void setSortMode(SortMode mode)
Set the sort mode.
Definition ParticleSystem:529
bool isFrozen() const
Definition ParticleSystem:397
double getVisibilityDistance() const
Get the visibility distance.
Definition ParticleSystem:534
virtual void releaseGLObjects(osg::State *state=0) const
If State is non-zero, this function releases OpenGL objects for the specified graphics context.
META_Object(osgParticle, ParticleSystem)
void adjustEstimatedMaxNumOfParticles(int delta)
Definition ParticleSystem:270
BufferedArrayData _bufferedArrayData
Definition ParticleSystem:356
void setParticleAlignment(Alignment a)
Set the alignment type of particles.
Definition ParticleSystem:366
ReadWriterMutex * getReadWriteMutex() const
Definition ParticleSystem:258
Alignment getParticleAlignment() const
Get the alignment type of particles.
Definition ParticleSystem:361
void setFreezeOnCull(bool v)
Set whether the particle system can freeze when culled (default is true)
Definition ParticleSystem:508
const osg::Vec3 & getAlignVectorY() const
Get the Y-axis alignment vector.
Definition ParticleSystem:381
void setAlignVectors(const osg::Vec3 &X, const osg::Vec3 &Y)
Set the alignment vectors.
Definition ParticleSystem:391
bool _dirty_uniforms
Definition ParticleSystem:298
Alignment _alignment
Definition ParticleSystem:291
double & getDeltaTime(double currentTime)
Get the unique delta time for emitters and updaters to use.
Definition ParticleSystem:457
osg::Vec3 _align_Y_axis
Definition ParticleSystem:293
void setDefaultParticleTemplate(const Particle &p)
Set the default particle template (particle is copied).
Definition ParticleSystem:498
virtual void update(double dt, osg::NodeVisitor &nv)
Update the particles. Don't call this directly, use a ParticleSystemUpdater instead.
Death_stack _deadparts
Definition ParticleSystem:287
osg::Vec3 _align_X_axis
Definition ParticleSystem:292
void setDefaultAttributesUsingShaders(const std::string &texturefile="", bool emissive_particles=true, int texture_unit=0)
A useful method to set the most common StateAttribute and use GLSL shaders to draw particles.
ParticleScaleReferenceFrame _particleScaleReferenceFrame
Definition ParticleSystem:294
SortMode _sortMode
Definition ParticleSystem:318
OpenThreads::ScopedLock< OpenThreads::Mutex > ScopedWriteLock
Definition ParticleSystem:255
Alignment
Definition ParticleSystem:57
@ FIXED
Definition ParticleSystem:59
@ BILLBOARD
Definition ParticleSystem:58
ParticleScaleReferenceFrame getParticleScaleReferenceFrame() const
Get whether the particles should be scaled relative to world coordaintes or local coordinates.
Definition ParticleSystem:100
osg::BoundingBox _def_bbox
Definition ParticleSystem:289
int numDeadParticles() const
Get the number of dead particles.
Definition ParticleSystem:432
bool getFrozen() const
Return true if the particle system is frozen.
Definition ParticleSystem:146
bool getDoublePassRendering() const
Get the double pass rendering flag.
Definition ParticleSystem:417
const osg::Vec3 & getAlignVectorX() const
Get the X-axis alignment vector.
Definition ParticleSystem:371
int getLevelOfDetail() const
(EXPERIMENTAL) Get the level of detail.
Definition ParticleSystem:513
int numParticles() const
Get the number of allocated particles (alive + dead).
Definition ParticleSystem:427
void setAlignVectorX(const osg::Vec3 &v)
Set the X-axis alignment vector.
Definition ParticleSystem:376
bool _useShaders
Definition ParticleSystem:297
unsigned int _last_frame
Definition ParticleSystem:310
bool _useVertexArray
Definition ParticleSystem:296
bool _bounds_computed
Definition ParticleSystem:307
OpenThreads::Mutex ReadWriterMutex
Definition ParticleSystem:253
void setDoublePassRendering(bool v)
Set the double pass rendering flag.
Definition ParticleSystem:422
void setUseVertexArray(bool v)
Set to use vertex arrays for rendering particles.
Definition ParticleSystem:122
bool areAllParticlesDead() const
Get whether all particles are dead.
Definition ParticleSystem:161
bool _frozen
Definition ParticleSystem:301
void setVisibilityDistance(double distance)
Set the visibility distance which allows the particles to be rendered only when depth is inside the d...
Definition ParticleSystem:539
void resize(unsigned int numVertices)
osg::ref_ptr< osg::Vec2Array > texcoords2
Definition ParticleSystem:347
osg::ref_ptr< osg::Vec3Array > normals
Definition ParticleSystem:345
Primitives primitives
Definition ParticleSystem:352
osg::ref_ptr< osg::Vec3Array > vertices
Definition ParticleSystem:344
osg::ref_ptr< osg::Vec4Array > colors
Definition ParticleSystem:346
osg::ref_ptr< osg::Vec3Array > texcoords3
Definition ParticleSystem:348
std::pair< GLenum, unsigned int > ModeCount
Definition ParticleSystem:350
void releaseGLObjects(osg::State *state)
void resizeGLObjectBuffers(unsigned int maxSize)
osg::ref_ptr< osg::BufferObject > vertexBufferObject
Definition ParticleSystem:343
std::vector< ModeCount > Primitives
Definition ParticleSystem:351
void dispatchArrays(osg::State &state)
void reserve(unsigned int numVertices)
#define OSGPARTICLE_EXPORT
Definition Export:40

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