sparkleParticleRenderer.cxx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Filename: sparkleParticleRenderer.cxx
  2. // Created by: charles (27Jun00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "sparkleParticleRenderer.h"
  19. #include "boundingSphere.h"
  20. #include "geomNode.h"
  21. ////////////////////////////////////////////////////////////////////
  22. // Function : SparkleParticleRenderer
  23. // Access : Public
  24. // Description : Default Constructor
  25. ////////////////////////////////////////////////////////////////////
  26. SparkleParticleRenderer::
  27. SparkleParticleRenderer() :
  28. BaseParticleRenderer(PR_ALPHA_NONE),
  29. _center_color(Colorf(1.0f, 1.0f, 1.0f, 1.0f)),
  30. _edge_color(Colorf(1.0f, 1.0f, 1.0f, 1.0f)),
  31. _birth_radius(0.1f), _death_radius(0.1f)
  32. {
  33. _line_primitive = new GeomLine;
  34. init_geoms();
  35. }
  36. ////////////////////////////////////////////////////////////////////
  37. // Function : SparkleParticleRenderer
  38. // Access : Public
  39. // Description : Constructor
  40. ////////////////////////////////////////////////////////////////////
  41. SparkleParticleRenderer::
  42. SparkleParticleRenderer(const Colorf& center, const Colorf& edge,
  43. float birth_radius, float death_radius,
  44. SparkleParticleLifeScale life_scale,
  45. ParticleRendererAlphaMode alpha_mode) :
  46. BaseParticleRenderer(alpha_mode),
  47. _center_color(center), _edge_color(edge), _birth_radius(birth_radius),
  48. _death_radius(death_radius), _life_scale(life_scale)
  49. {
  50. _line_primitive = new GeomLine;
  51. init_geoms();
  52. }
  53. ////////////////////////////////////////////////////////////////////
  54. // Function : SparkleParticleRenderer
  55. // Access : Public
  56. // Description : Copy Constructor
  57. ////////////////////////////////////////////////////////////////////
  58. SparkleParticleRenderer::
  59. SparkleParticleRenderer(const SparkleParticleRenderer& copy) :
  60. BaseParticleRenderer(copy) {
  61. _center_color = copy._center_color;
  62. _edge_color = copy._edge_color;
  63. _birth_radius = copy._birth_radius;
  64. _death_radius = copy._death_radius;
  65. _life_scale = copy._life_scale;
  66. _line_primitive = new GeomLine;
  67. init_geoms();
  68. }
  69. ////////////////////////////////////////////////////////////////////
  70. // Function : ~SparkleParticleRenderer
  71. // Access : Public
  72. // Description : Destructor
  73. ////////////////////////////////////////////////////////////////////
  74. SparkleParticleRenderer::
  75. ~SparkleParticleRenderer() {
  76. }
  77. ////////////////////////////////////////////////////////////////////
  78. // Function : make copy
  79. // Access : Public
  80. // Description : child virtual for spawning systems
  81. ////////////////////////////////////////////////////////////////////
  82. BaseParticleRenderer *SparkleParticleRenderer::
  83. make_copy() {
  84. return new SparkleParticleRenderer(*this);
  85. }
  86. ////////////////////////////////////////////////////////////////////
  87. // Function : birth_particle
  88. // Access : Private, virtual
  89. // Description : child birth
  90. ////////////////////////////////////////////////////////////////////
  91. void SparkleParticleRenderer::
  92. birth_particle(int) {
  93. }
  94. ////////////////////////////////////////////////////////////////////
  95. // Function : kill_particle
  96. // Access : Private, virtual
  97. // Description : child kill
  98. ////////////////////////////////////////////////////////////////////
  99. void SparkleParticleRenderer::
  100. kill_particle(int) {
  101. }
  102. ////////////////////////////////////////////////////////////////////
  103. // Function : resize_pool
  104. // Access : private
  105. // Description : resizes the render pool. Reference counting
  106. // makes this easy.
  107. ////////////////////////////////////////////////////////////////////
  108. void SparkleParticleRenderer::
  109. resize_pool(int new_size) {
  110. _vertex_array = PTA_Vertexf::empty_array(new_size * 12);
  111. _color_array = PTA_Colorf::empty_array(new_size * 12);
  112. _line_primitive->set_coords(_vertex_array);
  113. _line_primitive->set_colors(_color_array, G_PER_VERTEX);
  114. _max_pool_size = new_size;
  115. init_geoms();
  116. }
  117. ////////////////////////////////////////////////////////////////////
  118. // Function : init_geoms
  119. // Access : private
  120. // Description : initializes the geomnodes
  121. ////////////////////////////////////////////////////////////////////
  122. void SparkleParticleRenderer::
  123. init_geoms() {
  124. _line_primitive->set_num_prims(0);
  125. GeomNode *render_node = get_render_node();
  126. render_node->remove_all_geoms();
  127. render_node->add_geom(_line_primitive, _render_state);
  128. }
  129. ////////////////////////////////////////////////////////////////////
  130. // Function : render
  131. // Access : private
  132. // Description : populates the GeomLine
  133. ////////////////////////////////////////////////////////////////////
  134. void SparkleParticleRenderer::
  135. render(pvector< PT(PhysicsObject) >& po_vector, int ttl_particles) {
  136. if (!ttl_particles)
  137. return;
  138. BaseParticle *cur_particle;
  139. int remaining_particles = ttl_particles;
  140. int i;
  141. Vertexf *cur_vert = &_vertex_array[0];
  142. Colorf *cur_color = &_color_array[0];
  143. // init the aabb
  144. _aabb_min.set(99999.0f, 99999.0f, 99999.0f);
  145. _aabb_max.set(-99999.0f, -99999.0f, -99999.0f);
  146. // run through the array
  147. for (i = 0; i < (int)po_vector.size(); i++) {
  148. cur_particle = (BaseParticle *) po_vector[i].p();
  149. if (cur_particle->get_alive() == false)
  150. continue;
  151. // adjust the aabb
  152. if (cur_particle->get_position().get_x() > _aabb_max.get_x())
  153. _aabb_max[0] = cur_particle->get_position().get_x();
  154. else if (cur_particle->get_position().get_x() < _aabb_min.get_x())
  155. _aabb_min[0] = cur_particle->get_position().get_x();
  156. if (cur_particle->get_position().get_y() > _aabb_max.get_y())
  157. _aabb_max[1] = cur_particle->get_position().get_y();
  158. else if (cur_particle->get_position().get_y() < _aabb_min.get_y())
  159. _aabb_min[1] = cur_particle->get_position().get_y();
  160. if (cur_particle->get_position().get_z() > _aabb_max.get_z())
  161. _aabb_max[2] = cur_particle->get_position().get_z();
  162. else if (cur_particle->get_position().get_z() < _aabb_min.get_z())
  163. _aabb_min[2] = cur_particle->get_position().get_z();
  164. // draw the particle.
  165. float radius = get_radius(cur_particle);
  166. float neg_radius = -radius;
  167. float alpha;
  168. LPoint3f pos = cur_particle->get_position();
  169. Colorf center_color = _center_color;
  170. Colorf edge_color = _edge_color;
  171. // handle alpha
  172. if (_alpha_mode != PR_ALPHA_NONE) {
  173. if(_alpha_mode == PR_ALPHA_USER) {
  174. alpha = get_user_alpha();
  175. } else {
  176. alpha = cur_particle->get_parameterized_age();
  177. if (_alpha_mode == PR_ALPHA_OUT)
  178. alpha = 1.0f - alpha;
  179. alpha *= get_user_alpha();
  180. }
  181. center_color[3] = alpha;
  182. edge_color[3] = alpha;
  183. }
  184. // 6 lines coming from the center point.
  185. *cur_vert++ = pos;
  186. *cur_vert++ = pos + Vertexf(radius, 0.0f, 0.0f);
  187. *cur_vert++ = pos;
  188. *cur_vert++ = pos + Vertexf(neg_radius, 0.0f, 0.0f);
  189. *cur_vert++ = pos;
  190. *cur_vert++ = pos + Vertexf(0.0f, radius, 0.0f);
  191. *cur_vert++ = pos;
  192. *cur_vert++ = pos + Vertexf(0.0f, neg_radius, 0.0f);
  193. *cur_vert++ = pos;
  194. *cur_vert++ = pos + Vertexf(0.0f, 0.0f, radius);
  195. *cur_vert++ = pos;
  196. *cur_vert++ = pos + Vertexf(0.0f, 0.0f, neg_radius);
  197. *cur_color++ = center_color;
  198. *cur_color++ = edge_color;
  199. *cur_color++ = center_color;
  200. *cur_color++ = edge_color;
  201. *cur_color++ = center_color;
  202. *cur_color++ = edge_color;
  203. *cur_color++ = center_color;
  204. *cur_color++ = edge_color;
  205. *cur_color++ = center_color;
  206. *cur_color++ = edge_color;
  207. *cur_color++ = center_color;
  208. *cur_color++ = edge_color;
  209. remaining_particles--;
  210. if (remaining_particles == 0)
  211. break;
  212. }
  213. _line_primitive->set_num_prims(6 * ttl_particles);
  214. // done filling geomline node, now do the bb stuff
  215. LPoint3f aabb_center = _aabb_min + ((_aabb_max - _aabb_min) * 0.5f);
  216. float radius = (aabb_center - _aabb_min).length();
  217. _line_primitive->set_bound(BoundingSphere(aabb_center, radius));
  218. get_render_node()->mark_bound_stale();
  219. }