Particle 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. #ifndef OSGPARTICLE_PARTICLE
  15. #define OSGPARTICLE_PARTICLE 1
  16. #include <osgParticle/Export>
  17. #include <osgParticle/Interpolator>
  18. #include <osgParticle/range>
  19. #include <osg/ref_ptr>
  20. #include <osg/Vec3>
  21. #include <osg/Vec4>
  22. #include <osg/Matrix>
  23. namespace osgParticle
  24. {
  25. // forward declare so we can reference it
  26. class ParticleSystem;
  27. /** Implementation of a <B>particle</B>.
  28. Objects of this class are particles, they have some graphical properties
  29. and some physical properties. Particles are created by emitters and then placed
  30. into Particle Systems, where they live and get updated at each frame.
  31. Particles can either live forever (lifeTime < 0), or die after a specified
  32. time (lifeTime >= 0). For each property which is defined as a range of values, a
  33. "current" value will be evaluated at each frame by interpolating the <I>min</I>
  34. and <I>max</I> values so that <I>curr_value = min</I> when <I>t == 0</I>, and
  35. <I>curr_value = max</I> when <I>t == lifeTime</I>.
  36. You may customize the interpolator objects to achieve any kind of transition.
  37. If you want the particle to live forever, set its lifetime to any value <= 0;
  38. in that case, no interpolation is done to compute real-time properties, and only
  39. minimum values are used.
  40. */
  41. class OSGPARTICLE_EXPORT Particle {
  42. friend class ParticleSystem;
  43. public:
  44. enum
  45. {
  46. INVALID_INDEX = -1
  47. };
  48. /**
  49. Shape of particles.
  50. NOTE: the LINE shape should be used in conjunction with FIXED alignment mode (see ParticleSystem).
  51. */
  52. enum Shape {
  53. POINT, // uses GL_POINTS as primitive
  54. QUAD, // uses GL_QUADS as primitive
  55. QUAD_TRIANGLESTRIP, // no longer supported, falls back to QUAD
  56. HEXAGON, // no longer supported, falls back to QUAD
  57. LINE, // uses GL_LINES to draw line segments that point to the direction of motion
  58. USER // no longer supported, falls back to QUAD
  59. };
  60. Particle();
  61. /// Get the shape of the particle.
  62. inline Shape getShape() const;
  63. /// Set the shape of the particle.
  64. inline void setShape(Shape s);
  65. /// Get whether the particle is still alive.
  66. inline bool isAlive() const;
  67. /// Get the life time of the particle (in seconds).
  68. inline double getLifeTime() const;
  69. /// Get the age of the particle (in seconds).
  70. inline double getAge() const;
  71. /// Get the minimum and maximum values for polygon size.
  72. inline const rangef& getSizeRange() const;
  73. /// Get the minimum and maximum values for alpha.
  74. inline const rangef& getAlphaRange() const;
  75. /// Get the minimum and maximum values for color.
  76. inline const rangev4& getColorRange() const;
  77. /// Get the interpolator for computing the size of polygons.
  78. inline const Interpolator* getSizeInterpolator() const;
  79. /// Get the interpolator for computing alpha values.
  80. inline const Interpolator* getAlphaInterpolator() const;
  81. /// Get the interpolator for computing color values.
  82. inline const Interpolator* getColorInterpolator() const;
  83. /** Get the physical radius of the particle.
  84. For built-in operators to work correctly, lengths must be expressed in meters.
  85. */
  86. inline float getRadius() const;
  87. /** Get the mass of the particle.
  88. For built-in operators to work correctly, remember that the mass is expressed in kg.
  89. */
  90. inline float getMass() const;
  91. /// Get <CODE>1 / getMass()</CODE>.
  92. inline float getMassInv() const;
  93. /// Get the position vector.
  94. inline const osg::Vec3& getPosition() const;
  95. /** Get the velocity vector.
  96. For built-in operators to work correctly, remember that velocity components are expressed
  97. in meters per second.
  98. */
  99. inline const osg::Vec3& getVelocity() const;
  100. /// Get the previous position (the position before last update).
  101. inline const osg::Vec3& getPreviousPosition() const;
  102. /// Get the angle vector.
  103. inline const osg::Vec3& getAngle() const;
  104. /// Get the rotational velocity vector.
  105. inline const osg::Vec3& getAngularVelocity() const;
  106. /// Get the previous angle vector.
  107. inline const osg::Vec3& getPreviousAngle() const;
  108. /// Get the current color
  109. inline const osg::Vec4& getCurrentColor() const { return _current_color; }
  110. /// Get the current alpha
  111. inline float getCurrentAlpha() const { return _current_alpha; }
  112. /// Get the s texture coordinate of the bottom left of the particle
  113. inline float getSTexCoord() const { return _s_coord; }
  114. /// Get the t texture coordinate of the bottom left of the particle
  115. inline float getTTexCoord() const { return _t_coord; }
  116. /// Get the texture coordinate width of the particle
  117. inline float getSTexTile() const { return _s_tile; }
  118. /// Get the texture coordinate height of the particle
  119. inline float getTTexTile() const { return _t_tile; }
  120. /// Get width of texture tile
  121. inline int getTileS() const;
  122. /// Get height of texture tile
  123. inline int getTileT() const;
  124. /// Get number of texture tiles
  125. inline int getNumTiles() const { return _end_tile - _start_tile + 1; }
  126. /** Kill the particle on next update
  127. NOTE: after calling this function, the <CODE>isAlive()</CODE> method will still
  128. return true until the particle is updated again.
  129. */
  130. inline void kill();
  131. /// Set the life time of the particle.
  132. inline void setLifeTime(double t);
  133. /// Set the minimum and maximum values for polygon size.
  134. inline void setSizeRange(const rangef& r);
  135. /// Set the minimum and maximum values for alpha.
  136. inline void setAlphaRange(const rangef& r);
  137. /// Set the minimum and maximum values for color.
  138. inline void setColorRange(const rangev4& r);
  139. /// Set the interpolator for computing size values.
  140. inline void setSizeInterpolator(Interpolator* ri);
  141. template<class T> void setSizeInterpolator(const osg::ref_ptr<T>& ri) { setSizeInterpolator(ri.get()); }
  142. /// Set the interpolator for computing alpha values.
  143. inline void setAlphaInterpolator(Interpolator* ai);
  144. template<class T> void setAlphaInterpolator(const osg::ref_ptr<T>& ri) { setAlphaInterpolator(ri.get()); }
  145. /// Set the interpolator for computing color values.
  146. inline void setColorInterpolator(Interpolator* ci);
  147. template<class T> void setColorInterpolator(const osg::ref_ptr<T>& ri) { setColorInterpolator(ri.get()); }
  148. /** Set the physical radius of the particle.
  149. For built-in operators to work correctly, lengths must be expressed in meters.
  150. */
  151. inline void setRadius(float r);
  152. /** Set the mass of the particle.
  153. For built-in operators to work correctly, remember that the mass is expressed in kg.
  154. */
  155. inline void setMass(float m);
  156. /// Set the position vector.
  157. inline void setPosition(const osg::Vec3& p);
  158. /** Set the velocity vector.
  159. For built-in operators to work correctly, remember that velocity components are expressed
  160. in meters per second.
  161. */
  162. inline void setVelocity(const osg::Vec3& v);
  163. /// Add a vector to the velocity vector.
  164. inline void addVelocity(const osg::Vec3& dv);
  165. /// Transform position and velocity vectors by a matrix.
  166. inline void transformPositionVelocity(const osg::Matrix& xform);
  167. /// Transform position and velocity vectors by a combination of two matrices
  168. void transformPositionVelocity(const osg::Matrix& xform1, const osg::Matrix& xform2, float r);
  169. /// Set the angle vector.
  170. inline void setAngle(const osg::Vec3& a);
  171. /**
  172. Set the angular velocity vector.
  173. Components x, y and z are angles of rotation around the respective axis (in radians).
  174. */
  175. inline void setAngularVelocity(const osg::Vec3& v);
  176. /// Add a vector to the angular velocity vector.
  177. inline void addAngularVelocity(const osg::Vec3& dv);
  178. /// Transform angle and angularVelocity vectors by a matrix.
  179. inline void transformAngleVelocity(const osg::Matrix& xform);
  180. /** Update the particle (don't call this method manually).
  181. This method is called automatically by <CODE>ParticleSystem::update()</CODE>; it
  182. updates the graphical properties of the particle for the current time,
  183. checks whether the particle is still alive, and then updates its position
  184. by computing <I>P = P + V * dt</I> (where <I>P</I> is the position and <I>V</I> is the velocity).
  185. */
  186. bool update(double dt, bool onlyTimeStamp);
  187. /// Get the current (interpolated) polygon size. Valid only after the first call to update().
  188. inline float getCurrentSize() const;
  189. /// Specify how the particle texture is tiled.
  190. /// All tiles in the given range are sequentially displayed during the lifetime
  191. /// of the particle. When no range is given, all tiles are displayed during the lifetime.
  192. inline void setTextureTileRange(int sTile, int tTile, int startTile, int endTile);
  193. /// Same as above, range starts at 0 and ends at end
  194. inline void setTextureTile(int sTile, int tTile, int end = -1);
  195. inline int getStartTile() const;
  196. inline int getEndTile() const;
  197. /// Set the previous particle
  198. inline void setPreviousParticle(int previous) { _previousParticle = previous; }
  199. /// Get the previous particle
  200. inline int getPreviousParticle() const { return _previousParticle; }
  201. /// Set the next particle
  202. inline void setNextParticle(int next) { _nextParticle = next; }
  203. /// Get the const next particle
  204. inline int getNextParticle() const { return _nextParticle; }
  205. /// Set the depth of the particle
  206. inline void setDepth(double d) { _depth = d; }
  207. /// Get the depth of the particle
  208. inline double getDepth() const { return _depth; }
  209. /// Sorting operator
  210. bool operator<(const Particle &P) const { return _depth < P._depth; }
  211. /// Method for initializing a particles texture coords as part of a connected particle system.
  212. void setUpTexCoordsAsPartOfConnectedParticleSystem(ParticleSystem* ps);
  213. protected:
  214. Shape _shape;
  215. rangef _sr;
  216. rangef _ar;
  217. rangev4 _cr;
  218. osg::ref_ptr<Interpolator> _si;
  219. osg::ref_ptr<Interpolator> _ai;
  220. osg::ref_ptr<Interpolator> _ci;
  221. bool _mustdie;
  222. double _lifeTime;
  223. float _radius;
  224. float _mass;
  225. float _massinv;
  226. osg::Vec3 _prev_pos;
  227. osg::Vec3 _position;
  228. osg::Vec3 _velocity;
  229. osg::Vec3 _prev_angle;
  230. osg::Vec3 _angle;
  231. osg::Vec3 _angul_arvel;
  232. double _t0;
  233. float _alive;
  234. float _current_size;
  235. float _current_alpha;
  236. osg::Vec3 _base_prop; // [0] _alive [1] _current_size [2] _current_alpha
  237. osg::Vec4 _current_color;
  238. float _s_tile;
  239. float _t_tile;
  240. int _start_tile;
  241. int _end_tile;
  242. int _cur_tile;
  243. float _s_coord;
  244. float _t_coord;
  245. // previous and next Particles are only used in ConnectedParticleSystems
  246. int _previousParticle;
  247. int _nextParticle;
  248. // the depth of the particle is used only when sorting is enabled
  249. double _depth;
  250. };
  251. // INLINE FUNCTIONS
  252. inline Particle::Shape Particle::getShape() const
  253. {
  254. return _shape;
  255. }
  256. inline void Particle::setShape(Shape s)
  257. {
  258. _shape = s;
  259. }
  260. inline bool Particle::isAlive() const
  261. {
  262. return _alive>0.0f;
  263. }
  264. inline double Particle::getLifeTime() const
  265. {
  266. return _lifeTime;
  267. }
  268. inline double Particle::getAge() const
  269. {
  270. return _t0;
  271. }
  272. inline float Particle::getRadius() const
  273. {
  274. return _radius;
  275. }
  276. inline void Particle::setRadius(float r)
  277. {
  278. _radius = r;
  279. }
  280. inline const rangef& Particle::getSizeRange() const
  281. {
  282. return _sr;
  283. }
  284. inline const rangef& Particle::getAlphaRange() const
  285. {
  286. return _ar;
  287. }
  288. inline const rangev4& Particle::getColorRange() const
  289. {
  290. return _cr;
  291. }
  292. inline const Interpolator* Particle::getSizeInterpolator() const
  293. {
  294. return _si.get();
  295. }
  296. inline const Interpolator* Particle::getAlphaInterpolator() const
  297. {
  298. return _ai.get();
  299. }
  300. inline const Interpolator* Particle::getColorInterpolator() const
  301. {
  302. return _ci.get();
  303. }
  304. inline const osg::Vec3& Particle::getPosition() const
  305. {
  306. return _position;
  307. }
  308. inline const osg::Vec3& Particle::getVelocity() const
  309. {
  310. return _velocity;
  311. }
  312. inline const osg::Vec3& Particle::getPreviousPosition() const
  313. {
  314. return _prev_pos;
  315. }
  316. inline const osg::Vec3& Particle::getAngle() const
  317. {
  318. return _angle;
  319. }
  320. inline const osg::Vec3& Particle::getAngularVelocity() const
  321. {
  322. return _angul_arvel;
  323. }
  324. inline const osg::Vec3& Particle::getPreviousAngle() const
  325. {
  326. return _prev_angle;
  327. }
  328. inline int Particle::getTileS() const
  329. {
  330. return (_s_tile>0.0f) ? static_cast<int>(1.0f / _s_tile) : 1;
  331. }
  332. inline int Particle::getTileT() const
  333. {
  334. return (_t_tile>0.0f) ? static_cast<int>(1.0f / _t_tile) : 1;
  335. }
  336. inline void Particle::kill()
  337. {
  338. _mustdie = true;
  339. }
  340. inline void Particle::setLifeTime(double t)
  341. {
  342. _lifeTime = t;
  343. }
  344. inline void Particle::setSizeRange(const rangef& r)
  345. {
  346. _sr = r;
  347. }
  348. inline void Particle::setAlphaRange(const rangef& r)
  349. {
  350. _ar = r;
  351. }
  352. inline void Particle::setColorRange(const rangev4& r)
  353. {
  354. _cr = r;
  355. }
  356. inline void Particle::setSizeInterpolator(Interpolator* ri)
  357. {
  358. _si = ri;
  359. }
  360. inline void Particle::setAlphaInterpolator(Interpolator* ai)
  361. {
  362. _ai = ai;
  363. }
  364. inline void Particle::setColorInterpolator(Interpolator* ci)
  365. {
  366. _ci = ci;
  367. }
  368. inline void Particle::setPosition(const osg::Vec3& p)
  369. {
  370. _position = p;
  371. }
  372. inline void Particle::setVelocity(const osg::Vec3& v)
  373. {
  374. _velocity = v;
  375. }
  376. inline void Particle::addVelocity(const osg::Vec3& dv)
  377. {
  378. _velocity += dv;
  379. }
  380. inline void Particle::transformPositionVelocity(const osg::Matrix& xform)
  381. {
  382. _position = xform.preMult(_position);
  383. _velocity = osg::Matrix::transform3x3(_velocity, xform);
  384. }
  385. inline void Particle::transformPositionVelocity(const osg::Matrix& xform1, const osg::Matrix& xform2, float r)
  386. {
  387. osg::Vec3 position1 = xform1.preMult(_position);
  388. osg::Vec3 velocity1 = osg::Matrix::transform3x3(_velocity, xform1);
  389. osg::Vec3 position2 = xform2.preMult(_position);
  390. osg::Vec3 velocity2 = osg::Matrix::transform3x3(_velocity, xform2);
  391. float one_minus_r = 1.0f-r;
  392. _position = position1*r + position2*one_minus_r;
  393. _velocity = velocity1*r + velocity2*one_minus_r;
  394. }
  395. inline void Particle::setAngle(const osg::Vec3& a)
  396. {
  397. _angle = a;
  398. }
  399. inline void Particle::setAngularVelocity(const osg::Vec3& v)
  400. {
  401. _angul_arvel = v;
  402. }
  403. inline void Particle::addAngularVelocity(const osg::Vec3& dv)
  404. {
  405. _angul_arvel += dv;
  406. }
  407. inline void Particle::transformAngleVelocity(const osg::Matrix& xform)
  408. {
  409. // this should be optimized!
  410. osg::Vec3 a1 = _angle + _angul_arvel;
  411. _angle = xform.preMult(_angle);
  412. a1 = xform.preMult(a1);
  413. _angul_arvel = a1 - _angle;
  414. }
  415. inline float Particle::getMass() const
  416. {
  417. return _mass;
  418. }
  419. inline float Particle::getMassInv() const
  420. {
  421. return _massinv;
  422. }
  423. inline void Particle::setMass(float m)
  424. {
  425. _mass = m;
  426. _massinv = 1 / m;
  427. }
  428. inline float Particle::getCurrentSize() const
  429. {
  430. return _current_size;
  431. }
  432. inline void Particle::setTextureTile(int sTile, int tTile, int end)
  433. {
  434. setTextureTileRange(sTile, tTile, -1, end);
  435. }
  436. inline void Particle::setTextureTileRange(int sTile, int tTile, int startTile, int endTile)
  437. {
  438. _s_tile = (sTile>0) ? 1.0f / static_cast<float>(sTile) : 1.0f;
  439. _t_tile = (tTile>0) ? 1.0f / static_cast<float>(tTile) : 1.0f;
  440. if(startTile == -1)
  441. {
  442. _start_tile = 0;
  443. }
  444. else
  445. {
  446. _start_tile = startTile;
  447. }
  448. if(endTile == -1)
  449. {
  450. _end_tile = sTile * tTile;
  451. }
  452. else
  453. {
  454. _end_tile = endTile;
  455. }
  456. }
  457. inline int Particle::getStartTile() const
  458. {
  459. return _start_tile;
  460. }
  461. inline int Particle::getEndTile() const
  462. {
  463. return _end_tile;
  464. }
  465. }
  466. #endif