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

panda/src/particlesystem/pointParticleRenderer.cxx

Go to the documentation of this file.
00001 // Filename: pointParticleRenderer.cxx
00002 // Created by:  charles (20Jun00)
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 "pointParticleRenderer.h"
00020 #include "boundingSphere.h"
00021 #include "geomNode.h"
00022 
00023 ////////////////////////////////////////////////////////////////////
00024 //    Function : PointParticleRenderer
00025 //      Access : Public
00026 // Description : special constructor
00027 ////////////////////////////////////////////////////////////////////
00028 
00029 PointParticleRenderer::
00030 PointParticleRenderer(ParticleRendererAlphaMode am,
00031                       float point_size,
00032                       PointParticleBlendType bt,
00033                       ParticleRendererBlendMethod bm,
00034                       const Colorf& sc, const Colorf& ec) :
00035   BaseParticleRenderer(am),
00036   _start_color(sc), _end_color(ec),
00037   _point_size(point_size),
00038   _blend_type(bt), _blend_method(bm)
00039 {
00040   _point_primitive = new GeomPoint;
00041   init_geoms();
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //    Function : PointParticleRenderer
00046 //      Access : Public
00047 // Description : Copy constructor
00048 ////////////////////////////////////////////////////////////////////
00049 
00050 PointParticleRenderer::
00051 PointParticleRenderer(const PointParticleRenderer& copy) :
00052   BaseParticleRenderer(copy),
00053   _max_pool_size(0)
00054 {
00055   _blend_type = copy._blend_type;
00056   _blend_method = copy._blend_method;
00057   _start_color = copy._start_color;
00058   _end_color = copy._end_color;
00059   _point_primitive = new GeomPoint;
00060   init_geoms();
00061 }
00062 
00063 ////////////////////////////////////////////////////////////////////
00064 //    Function : ~PointParticleRenderer
00065 //      Access : Public
00066 // Description : Simple destructor
00067 ////////////////////////////////////////////////////////////////////
00068 
00069 PointParticleRenderer::
00070 ~PointParticleRenderer(void) {
00071 }
00072 
00073 ////////////////////////////////////////////////////////////////////
00074 //    Function : make_copy
00075 //      Access : Public
00076 // Description : for spawning systems from dead particles
00077 ////////////////////////////////////////////////////////////////////
00078 
00079 BaseParticleRenderer *PointParticleRenderer::
00080 make_copy(void) {
00081   return new PointParticleRenderer(*this);
00082 }
00083 
00084 ////////////////////////////////////////////////////////////////////
00085 //    Function : resize_pool
00086 //      Access : Public
00087 // Description : reallocate the space for the vertex and color
00088 //               pools
00089 ////////////////////////////////////////////////////////////////////
00090 
00091 void PointParticleRenderer::
00092 resize_pool(int new_size) {
00093   if (new_size == _max_pool_size)
00094     return;
00095 
00096   _max_pool_size = new_size;
00097 
00098   _vertex_array = PTA_Vertexf::empty_array(new_size);
00099   _color_array = PTA_Colorf::empty_array(new_size);
00100 
00101   _point_primitive->set_coords(_vertex_array);
00102   _point_primitive->set_colors(_color_array, G_PER_VERTEX);
00103 
00104   init_geoms();
00105 }
00106 
00107 ////////////////////////////////////////////////////////////////////
00108 //    Function : init_geoms
00109 //      Access : Private
00110 // Description : On-construction initialization
00111 ////////////////////////////////////////////////////////////////////
00112 
00113 void PointParticleRenderer::
00114 init_geoms(void) {
00115 
00116   _point_primitive->set_num_prims(0);
00117   _point_primitive->set_size(_point_size);
00118   
00119   GeomNode *render_node = get_render_node();
00120   render_node->remove_all_geoms();
00121   render_node->add_geom(_point_primitive, _render_state);
00122 }
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //    Function : birth_particle
00126 //      Access : Private, virtual
00127 // Description : child birth
00128 ////////////////////////////////////////////////////////////////////
00129 
00130 void PointParticleRenderer::
00131 birth_particle(int) {
00132 }
00133 
00134 ////////////////////////////////////////////////////////////////////
00135 //    Function : kill_particle
00136 //      Access : Private, virtual
00137 // Description : child kill
00138 ////////////////////////////////////////////////////////////////////
00139 
00140 void PointParticleRenderer::
00141 kill_particle(int) {
00142 }
00143 
00144 ////////////////////////////////////////////////////////////////////
00145 //    Function : create_color
00146 //      Access : Private
00147 // Description : Generates the point color based on the render_type
00148 ////////////////////////////////////////////////////////////////////
00149 
00150 Colorf PointParticleRenderer::
00151 create_color(const BaseParticle *p) {
00152   Colorf color;
00153   float life_t, vel_t;
00154   float parameterized_age = 1.0f;
00155   bool have_alpha_t = false;
00156 
00157   switch (_blend_type) {
00158 
00159     //// Constant solid color
00160 
00161   case PP_ONE_COLOR:
00162     color = _start_color;
00163     break;
00164 
00165     //// Blending colors based on life
00166 
00167   case PP_BLEND_LIFE:
00168     parameterized_age = p->get_parameterized_age();
00169     life_t = parameterized_age;
00170     have_alpha_t = true;
00171 
00172     if (_blend_method == PP_BLEND_CUBIC)
00173       life_t = CUBIC_T(life_t);
00174     
00175     color = LERP(life_t, _start_color, _end_color);
00176     
00177     break;
00178     
00179     //// Blending colors based on vel
00180 
00181   case PP_BLEND_VEL:
00182     vel_t = p->get_parameterized_vel();
00183 
00184     if (_blend_method == PP_BLEND_CUBIC)
00185       vel_t = CUBIC_T(vel_t);
00186 
00187     color = LERP(vel_t, _start_color, _end_color);
00188 
00189     break;
00190   }
00191 
00192   // handle alpha channel
00193 
00194   if(_alpha_mode != PR_ALPHA_NONE) {
00195     if(_alpha_mode == PR_ALPHA_USER) {
00196       parameterized_age = 1.0;
00197     } else {
00198       if(!have_alpha_t)
00199         parameterized_age = p->get_parameterized_age();
00200 
00201       if(_alpha_mode==PR_ALPHA_OUT) {
00202         parameterized_age = 1.0f - parameterized_age;
00203       }
00204     }
00205     color[3] = parameterized_age * get_user_alpha();
00206   }
00207 
00208   return color;
00209 }
00210 
00211 ////////////////////////////////////////////////////////////////////
00212 //    Function : render
00213 //      Access : Public
00214 // Description : renders the particle system out to a GeomNode
00215 ////////////////////////////////////////////////////////////////////
00216 
00217 void PointParticleRenderer::
00218 render(pvector< PT(PhysicsObject) >& po_vector, int ttl_particles) {
00219 
00220   BaseParticle *cur_particle;
00221 
00222   int remaining_particles = ttl_particles;
00223   int i;
00224 
00225   Vertexf *cur_vert = &_vertex_array[0];
00226   Colorf *cur_color = &_color_array[0];
00227 
00228   // init the aabb
00229 
00230   _aabb_min.set(99999.0f, 99999.0f, 99999.0f);
00231   _aabb_max.set(-99999.0f, -99999.0f, -99999.0f);
00232 
00233   // run through every filled slot
00234 
00235   for (i = 0; i < (int)po_vector.size(); i++) {
00236     cur_particle = (BaseParticle *) po_vector[i].p();
00237 
00238     if (cur_particle->get_alive() == false)
00239       continue;
00240 
00241     // x aabb adjust
00242 
00243     if (cur_particle->get_position().get_x() > _aabb_max.get_x())
00244       _aabb_max[0] = cur_particle->get_position().get_x();
00245     else if (cur_particle->get_position().get_x() < _aabb_min.get_x())
00246       _aabb_min[0] = cur_particle->get_position().get_x();
00247 
00248     // y aabb adjust
00249 
00250     if (cur_particle->get_position().get_y() > _aabb_max.get_y())
00251       _aabb_max[1] = cur_particle->get_position().get_y();
00252     else if (cur_particle->get_position().get_y() < _aabb_min.get_y())
00253       _aabb_min[1] = cur_particle->get_position().get_y();
00254 
00255     // z aabb adjust
00256 
00257     if (cur_particle->get_position().get_z() > _aabb_max.get_z())
00258       _aabb_max[2] = cur_particle->get_position().get_z();
00259     else if (cur_particle->get_position().get_z() < _aabb_min.get_z())
00260       _aabb_min[2] = cur_particle->get_position().get_z();
00261 
00262     // stuff it into the arrays
00263 
00264     *cur_vert++ = cur_particle->get_position();
00265     *cur_color++ = create_color(cur_particle);
00266 
00267     // maybe jump out early?
00268 
00269     remaining_particles--;
00270     if (remaining_particles == 0)
00271       break;
00272   }
00273 
00274   _point_primitive->set_num_prims(ttl_particles);
00275 
00276   // done filling geompoint node, now do the bb stuff
00277 
00278   LPoint3f aabb_center = _aabb_min + ((_aabb_max - _aabb_min) * 0.5f);
00279   float radius = (aabb_center - _aabb_min).length();
00280 
00281   _point_primitive->set_bound(BoundingSphere(aabb_center, radius));
00282   get_render_node()->mark_bound_stale();
00283 }

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