Open Source Tomb Raider Engine
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Particle.cpp 2.6KB

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