Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/particlesystem/sparkleParticleRenderer.cxx

Go to the documentation of this file.
00001 // Filename: sparkleParticleRenderer.cxx
00002 // Created by:  charles (27Jun00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "sparkleParticleRenderer.h"
00020 
00021 #include "boundingSphere.h"
00022 #include "geomNode.h"
00023 
00024 ////////////////////////////////////////////////////////////////////
00025 //    Function : SparkleParticleRenderer
00026 //      Access : Public
00027 // Description : Default Constructor
00028 ////////////////////////////////////////////////////////////////////
00029 
00030 SparkleParticleRenderer::
00031 SparkleParticleRenderer(void) :
00032   BaseParticleRenderer(PR_ALPHA_NONE),
00033   _center_color(Colorf(1.0f, 1.0f, 1.0f, 1.0f)),
00034   _edge_color(Colorf(1.0f, 1.0f, 1.0f, 1.0f)),
00035   _birth_radius(0.1f), _death_radius(0.1f)
00036 {
00037   _line_primitive = new GeomLine;
00038   init_geoms();
00039 }
00040 
00041 ////////////////////////////////////////////////////////////////////
00042 //    Function : SparkleParticleRenderer
00043 //      Access : Public
00044 // Description : Constructor
00045 ////////////////////////////////////////////////////////////////////
00046 
00047 SparkleParticleRenderer::
00048 SparkleParticleRenderer(const Colorf& center, const Colorf& edge,
00049                         float birth_radius, float death_radius,
00050                         SparkleParticleLifeScale life_scale,
00051                         ParticleRendererAlphaMode alpha_mode) :
00052   BaseParticleRenderer(alpha_mode),
00053   _center_color(center), _edge_color(edge), _birth_radius(birth_radius),
00054   _death_radius(death_radius), _life_scale(life_scale)
00055 {
00056   _line_primitive = new GeomLine;
00057   init_geoms();
00058 }
00059 
00060 ////////////////////////////////////////////////////////////////////
00061 //    Function : SparkleParticleRenderer
00062 //      Access : Public
00063 // Description : Copy Constructor
00064 ////////////////////////////////////////////////////////////////////
00065 
00066 SparkleParticleRenderer::
00067 SparkleParticleRenderer(const SparkleParticleRenderer& copy) :
00068   BaseParticleRenderer(copy) {
00069   _center_color = copy._center_color;
00070   _edge_color = copy._edge_color;
00071   _birth_radius = copy._birth_radius;
00072   _death_radius = copy._death_radius;
00073   _life_scale = copy._life_scale;
00074 
00075   _line_primitive = new GeomLine;
00076   init_geoms();
00077 }
00078 
00079 ////////////////////////////////////////////////////////////////////
00080 //    Function : ~SparkleParticleRenderer
00081 //      Access : Public
00082 // Description : Destructor
00083 ////////////////////////////////////////////////////////////////////
00084 
00085 SparkleParticleRenderer::
00086 ~SparkleParticleRenderer(void) {
00087 }
00088 
00089 ////////////////////////////////////////////////////////////////////
00090 //    Function : make copy
00091 //      Access : Public
00092 // Description : child virtual for spawning systems
00093 ////////////////////////////////////////////////////////////////////
00094 
00095 BaseParticleRenderer *SparkleParticleRenderer::
00096 make_copy(void) {
00097   return new SparkleParticleRenderer(*this);
00098 }
00099 
00100 ////////////////////////////////////////////////////////////////////
00101 //    Function : birth_particle
00102 //      Access : Private, virtual
00103 // Description : child birth
00104 ////////////////////////////////////////////////////////////////////
00105 
00106 void SparkleParticleRenderer::
00107 birth_particle(int) {
00108 }
00109 
00110 ////////////////////////////////////////////////////////////////////
00111 //    Function : kill_particle
00112 //      Access : Private, virtual
00113 // Description : child kill
00114 ////////////////////////////////////////////////////////////////////
00115 
00116 void SparkleParticleRenderer::
00117 kill_particle(int) {
00118 }
00119 
00120 ////////////////////////////////////////////////////////////////////
00121 //    Function : resize_pool
00122 //      Access : private
00123 // Description : resizes the render pool.  Reference counting
00124 //               makes this easy.
00125 ////////////////////////////////////////////////////////////////////
00126 
00127 void SparkleParticleRenderer::
00128 resize_pool(int new_size) {
00129   _vertex_array = PTA_Vertexf::empty_array(new_size * 12);
00130   _color_array = PTA_Colorf::empty_array(new_size * 12);
00131 
00132   _line_primitive->set_coords(_vertex_array);
00133   _line_primitive->set_colors(_color_array, G_PER_VERTEX);
00134 
00135   _max_pool_size = new_size;
00136 
00137   init_geoms();
00138 }
00139 
00140 ////////////////////////////////////////////////////////////////////
00141 //    Function : init_geoms
00142 //      Access : private
00143 // Description : initializes the geomnodes
00144 ////////////////////////////////////////////////////////////////////
00145 
00146 void SparkleParticleRenderer::
00147 init_geoms(void) {
00148   _line_primitive->set_num_prims(0);
00149 
00150   GeomNode *render_node = get_render_node();
00151   render_node->remove_all_geoms();
00152   render_node->add_geom(_line_primitive, _render_state);
00153 }
00154 
00155 ////////////////////////////////////////////////////////////////////
00156 //    Function : render
00157 //      Access : private
00158 // Description : populates the GeomLine
00159 ////////////////////////////////////////////////////////////////////
00160 
00161 void SparkleParticleRenderer::
00162 render(pvector< PT(PhysicsObject) >& po_vector, int ttl_particles) {
00163 
00164   if (!ttl_particles)
00165     return;
00166 
00167   BaseParticle *cur_particle;
00168 
00169   int remaining_particles = ttl_particles;
00170   int i;
00171 
00172   Vertexf *cur_vert = &_vertex_array[0];
00173   Colorf *cur_color = &_color_array[0];
00174 
00175   // init the aabb
00176 
00177   _aabb_min.set(99999.0f, 99999.0f, 99999.0f);
00178   _aabb_max.set(-99999.0f, -99999.0f, -99999.0f);
00179 
00180   // run through the array
00181 
00182   for (i = 0; i < (int)po_vector.size(); i++) {
00183     cur_particle = (BaseParticle *) po_vector[i].p();
00184 
00185     if (cur_particle->get_alive() == false)
00186       continue;
00187 
00188     // adjust the aabb
00189 
00190     if (cur_particle->get_position().get_x() > _aabb_max.get_x())
00191       _aabb_max[0] = cur_particle->get_position().get_x();
00192     else if (cur_particle->get_position().get_x() < _aabb_min.get_x())
00193       _aabb_min[0] = cur_particle->get_position().get_x();
00194 
00195     if (cur_particle->get_position().get_y() > _aabb_max.get_y())
00196       _aabb_max[1] = cur_particle->get_position().get_y();
00197     else if (cur_particle->get_position().get_y() < _aabb_min.get_y())
00198       _aabb_min[1] = cur_particle->get_position().get_y();
00199 
00200     if (cur_particle->get_position().get_z() > _aabb_max.get_z())
00201       _aabb_max[2] = cur_particle->get_position().get_z();
00202     else if (cur_particle->get_position().get_z() < _aabb_min.get_z())
00203       _aabb_min[2] = cur_particle->get_position().get_z();
00204 
00205     // draw the particle.
00206 
00207     float radius = get_radius(cur_particle);
00208     float neg_radius = -radius;
00209     float alpha;
00210 
00211     LPoint3f pos = cur_particle->get_position();
00212     Colorf center_color = _center_color;
00213     Colorf edge_color = _edge_color;
00214 
00215     // handle alpha
00216 
00217     if (_alpha_mode != PR_ALPHA_NONE) {
00218       if(_alpha_mode == PR_ALPHA_USER) {
00219         alpha = get_user_alpha();
00220       } else {
00221         alpha = cur_particle->get_parameterized_age();
00222         if (_alpha_mode == PR_ALPHA_OUT)
00223           alpha = 1.0f - alpha;
00224 
00225         alpha *= get_user_alpha();
00226       }
00227 
00228       center_color[3] = alpha;
00229       edge_color[3] = alpha;
00230     }
00231 
00232     // 6 lines coming from the center point.
00233 
00234     *cur_vert++ = pos;
00235     *cur_vert++ = pos + Vertexf(radius, 0.0f, 0.0f);
00236     *cur_vert++ = pos;
00237     *cur_vert++ = pos + Vertexf(neg_radius, 0.0f, 0.0f);
00238     *cur_vert++ = pos;
00239     *cur_vert++ = pos + Vertexf(0.0f, radius, 0.0f);
00240     *cur_vert++ = pos;
00241     *cur_vert++ = pos + Vertexf(0.0f, neg_radius, 0.0f);
00242     *cur_vert++ = pos;
00243     *cur_vert++ = pos + Vertexf(0.0f, 0.0f, radius);
00244     *cur_vert++ = pos;
00245     *cur_vert++ = pos + Vertexf(0.0f, 0.0f, neg_radius);
00246 
00247     *cur_color++ = center_color;
00248     *cur_color++ = edge_color;
00249     *cur_color++ = center_color;
00250     *cur_color++ = edge_color;
00251     *cur_color++ = center_color;
00252     *cur_color++ = edge_color;
00253     *cur_color++ = center_color;
00254     *cur_color++ = edge_color;
00255     *cur_color++ = center_color;
00256     *cur_color++ = edge_color;
00257     *cur_color++ = center_color;
00258     *cur_color++ = edge_color;
00259 
00260     remaining_particles--;
00261     if (remaining_particles == 0)
00262       break;
00263   }
00264 
00265   _line_primitive->set_num_prims(6 * ttl_particles);
00266 
00267   // done filling geomline node, now do the bb stuff
00268 
00269   LPoint3f aabb_center = _aabb_min + ((_aabb_max - _aabb_min) * 0.5f);
00270   float radius = (aabb_center - _aabb_min).length();
00271 
00272   _line_primitive->set_bound(BoundingSphere(aabb_center, radius));
00273   get_render_node()->mark_bound_stale();
00274 }

Generated on Fri May 2 00:41:00 2003 for Panda by doxygen1.3