Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Particle.cpp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*!
  2. * \file src/Particle.cpp
  3. * \brief Particle system base implementation
  4. *
  5. * \author Mongoose
  6. */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #include "Particle.h"
  11. Particle::Particle()
  12. {
  13. setActive(true);
  14. TextureId(0);
  15. Speed(2000.0f, 2000.0f, 2000.0f);
  16. Color(1.0f, 1.0f, 1.0f);
  17. Force(0.0f, 0.8f, 0.0f);
  18. Reset();
  19. }
  20. void Particle::TextureId(int id)
  21. {
  22. _texture = id;
  23. }
  24. void Particle::setActive(bool active)
  25. {
  26. _active = active;
  27. }
  28. void Particle::Speed(float x, float y, float z)
  29. {
  30. _speed[0] = x;
  31. _speed[1] = y;
  32. _speed[2] = z;
  33. }
  34. void Particle::Color(float r, float g, float b)
  35. {
  36. _color[0] = r;
  37. _color[1] = g;
  38. _color[2] = b;
  39. }
  40. void Particle::Force(float x, float y, float z)
  41. {
  42. _force[0] = x;
  43. _force[1] = y;
  44. _force[2] = z;
  45. }
  46. void Particle::Reset()
  47. {
  48. // Mongoose 2002.01.01, Ah, how old is that code?
  49. #ifdef OBSOLETE
  50. _active = true;
  51. _life = 1.0;
  52. _blend = (float)(rand() % 100) / 1000.0 + 0.003;
  53. _pos[0] = _pos[1] = _pos[2] = 0.0;
  54. _dir[0] = (float)((rand() % 50) - 26.0) * 10.0;
  55. _dir[1] = (float)((rand() % 50) - 25.0) * 10.0;
  56. _dir[2] = _dir[1];
  57. _force[0] = 0.0;
  58. _force[1] = -0.8;
  59. _force[2] = 0.0;
  60. #else
  61. //! \fixme _blend prob should have nonstatic range
  62. _blend = (float)(0.003 + (0.1 * rand() / (RAND_MAX + 1.0))); // high order
  63. //_blend = (float)(rand() % 100) / 1000.0 + 0.003;
  64. //! \fixme Reset these using some nonstatic functions and values later
  65. _life = 1.0;
  66. _pos[0] = _pos[1] = _pos[2] = 0.0;
  67. _dir[0] = (float)(-26.0 + (50.0 * rand() / (RAND_MAX + 1.0))); // high order
  68. _dir[0] *= 10.0;
  69. //_dir[0] = (float)((rand() % 50) - 26.0) * 10.0;
  70. _dir[1] = (float)(-25.0 + (50.0 * rand() / (RAND_MAX + 1.0))); // high order
  71. _dir[1] *= 10.0;
  72. //_dir[1] = (float)((rand() % 50) - 25.0) * 10.0;
  73. _dir[2] = _dir[1];
  74. #endif
  75. }
  76. void Particle::Pos(float *x, float *y, float *z)
  77. {
  78. assert(x != NULL);
  79. assert(y != NULL);
  80. assert(z != NULL);
  81. *x = _pos[0];
  82. *y = _pos[1];
  83. *z = _pos[2];
  84. }
  85. void Particle::Color(float *r, float *g, float *b)
  86. {
  87. assert(r != NULL);
  88. assert(g != NULL);
  89. assert(b != NULL);
  90. *r = _color[0];
  91. *g = _color[1];
  92. *b = _color[2];
  93. }
  94. float Particle::Life()
  95. {
  96. return _life;
  97. }
  98. void Particle::Update()
  99. {
  100. // Adjust particle position
  101. _pos[0] += _dir[0] / _speed[0];
  102. _pos[1] += _dir[1] / _speed[1];
  103. _pos[2] += _dir[2] / _speed[2];
  104. // Adjust particle direction
  105. _dir[0] += _force[0];
  106. _dir[1] += _force[1];
  107. _dir[2] += _force[2];
  108. // Adjust particle blending/life
  109. _life -= _blend;
  110. // Reset 'dead' OR fully blended particles
  111. if (_life < 0.0)
  112. {
  113. Reset();
  114. }
  115. }
  116. int Particle::Texture()
  117. {
  118. return _texture;
  119. }
  120. bool Particle::isActive()
  121. {
  122. return _active;
  123. }