| 1 | #include <osgParticle/ParticleProcessor> |
|---|
| 2 | |
|---|
| 3 | #include <osg/Node> |
|---|
| 4 | #include <osg/NodeVisitor> |
|---|
| 5 | #include <osg/CopyOp> |
|---|
| 6 | #include <osg/Matrix> |
|---|
| 7 | #include <osg/Notify> |
|---|
| 8 | |
|---|
| 9 | #include <osgUtil/CullVisitor> |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | using namespace osg; |
|---|
| 13 | |
|---|
| 14 | osgParticle::ParticleProcessor::ParticleProcessor() |
|---|
| 15 | : osg::Node(), |
|---|
| 16 | _rf(RELATIVE_RF), |
|---|
| 17 | _enabled(true), |
|---|
| 18 | _t0(-1), |
|---|
| 19 | _ps(0), |
|---|
| 20 | _first_ltw_compute(true), |
|---|
| 21 | _need_ltw_matrix(false), |
|---|
| 22 | _first_wtl_compute(true), |
|---|
| 23 | _need_wtl_matrix(false), |
|---|
| 24 | _current_nodevisitor(0), |
|---|
| 25 | _endless(true), |
|---|
| 26 | _lifeTime(0.0), |
|---|
| 27 | _startTime(0.0), |
|---|
| 28 | _currentTime(0.0), |
|---|
| 29 | _resetTime(0.0), |
|---|
| 30 | _frameNumber(0) |
|---|
| 31 | { |
|---|
| 32 | setCullingActive(false); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | osgParticle::ParticleProcessor::ParticleProcessor(const ParticleProcessor& copy, const osg::CopyOp& copyop) |
|---|
| 36 | : osg::Node(copy, copyop), |
|---|
| 37 | _rf(copy._rf), |
|---|
| 38 | _enabled(copy._enabled), |
|---|
| 39 | _t0(copy._t0), |
|---|
| 40 | _ps(static_cast<ParticleSystem* >(copyop(copy._ps.get()))), |
|---|
| 41 | _first_ltw_compute(copy._first_ltw_compute), |
|---|
| 42 | _need_ltw_matrix(copy._need_ltw_matrix), |
|---|
| 43 | _first_wtl_compute(copy._first_wtl_compute), |
|---|
| 44 | _need_wtl_matrix(copy._need_wtl_matrix), |
|---|
| 45 | _current_nodevisitor(0), |
|---|
| 46 | _endless(copy._endless), |
|---|
| 47 | _lifeTime(copy._lifeTime), |
|---|
| 48 | _startTime(copy._startTime), |
|---|
| 49 | _currentTime(copy._currentTime), |
|---|
| 50 | _resetTime(copy._resetTime) |
|---|
| 51 | { |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | void osgParticle::ParticleProcessor::traverse(osg::NodeVisitor& nv) |
|---|
| 55 | { |
|---|
| 56 | |
|---|
| 57 | osgUtil::CullVisitor* cv = dynamic_cast<osgUtil::CullVisitor*>(&nv); |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | if (cv) { |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | if (_ps.valid()) |
|---|
| 64 | { |
|---|
| 65 | if (nv.getFrameStamp()) |
|---|
| 66 | { |
|---|
| 67 | ParticleSystem::ScopedWriteLock lock(*(_ps->getReadWriteMutex())); |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | if(_frameNumber < nv.getFrameStamp()->getFrameNumber()) |
|---|
| 72 | { |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | double t = nv.getFrameStamp()->getSimulationTime(); |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | if ((_currentTime >= _resetTime) && (_resetTime > 0)) |
|---|
| 80 | { |
|---|
| 81 | _currentTime = 0; |
|---|
| 82 | _t0 = -1; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | if (_t0 != -1) |
|---|
| 87 | { |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | bool alive = false; |
|---|
| 91 | if (_currentTime >= _startTime) |
|---|
| 92 | { |
|---|
| 93 | if (_endless || (_currentTime < (_startTime + _lifeTime))) |
|---|
| 94 | alive = true; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | _currentTime += t - _t0; |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | if (alive && |
|---|
| 102 | _enabled && |
|---|
| 103 | !_ps->isFrozen() && |
|---|
| 104 | ((_ps->getLastFrameNumber()+1) >= (nv.getFrameStamp()->getFrameNumber()) || !_ps->getFreezeOnCull())) |
|---|
| 105 | { |
|---|
| 106 | |
|---|
| 107 | _need_ltw_matrix = true; |
|---|
| 108 | _need_wtl_matrix = true; |
|---|
| 109 | _current_nodevisitor = &nv; |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | process( t - _t0 ); |
|---|
| 113 | } else { |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | _first_ltw_compute = true; |
|---|
| 117 | _first_wtl_compute = true; |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | _t0 = t; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | _frameNumber = nv.getFrameStamp()->getFrameNumber(); |
|---|
| 126 | } |
|---|
| 127 | else |
|---|
| 128 | { |
|---|
| 129 | OSG_WARN << "osgParticle::ParticleProcessor::traverse(NodeVisitor&) requires a valid FrameStamp to function, particles not updated.\n"; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | } else |
|---|
| 133 | { |
|---|
| 134 | OSG_WARN << "ParticleProcessor \"" << getName() << "\": invalid particle system\n"; |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | Node::traverse(nv); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | osg::BoundingSphere osgParticle::ParticleProcessor::computeBound() const |
|---|
| 144 | { |
|---|
| 145 | return osg::BoundingSphere(); |
|---|
| 146 | } |
|---|