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

panda/src/particlesystem/geomParticleRenderer.cxx

Go to the documentation of this file.
00001 // Filename: geomParticleRenderer.cxx
00002 // Created by:  charles (05Jul00)
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 "geomParticleRenderer.h"
00020 #include "baseParticle.h"
00021 
00022 #include "transformState.h"
00023 #include "colorAttrib.h"
00024 
00025 ////////////////////////////////////////////////////////////////////
00026 //    Function : GeomParticleRenderer
00027 //      Access : public
00028 // Description : constructor
00029 ////////////////////////////////////////////////////////////////////
00030 
00031 GeomParticleRenderer::
00032 GeomParticleRenderer(ParticleRendererAlphaMode am, PandaNode *geom_node) :
00033   BaseParticleRenderer(am),  _geom_node(geom_node), _pool_size(0) {
00034 
00035   if (_geom_node.is_null())
00036     _geom_node = new PandaNode("empty");
00037 }
00038 
00039 ////////////////////////////////////////////////////////////////////
00040 //    Function : GeomParticleRenderer
00041 //      Access : public
00042 // Description : copy constructor
00043 ////////////////////////////////////////////////////////////////////
00044 
00045 GeomParticleRenderer::
00046 GeomParticleRenderer(const GeomParticleRenderer& copy) :
00047   BaseParticleRenderer(copy), _pool_size(0) {
00048   _geom_node = copy._geom_node;
00049 }
00050 
00051 ////////////////////////////////////////////////////////////////////
00052 //    Function : ~GeomParticleRenderer
00053 //      Access : public
00054 // Description : destructor
00055 ////////////////////////////////////////////////////////////////////
00056 
00057 GeomParticleRenderer::
00058 ~GeomParticleRenderer(void) {
00059   kill_nodes();
00060 }
00061 
00062 ////////////////////////////////////////////////////////////////////
00063 //    Function : make_copy
00064 //      Access : public
00065 // Description : dynamic copying
00066 ////////////////////////////////////////////////////////////////////
00067 
00068 BaseParticleRenderer *GeomParticleRenderer::
00069 make_copy(void) {
00070   return new GeomParticleRenderer(*this);
00071 }
00072 
00073 ////////////////////////////////////////////////////////////////////
00074 //    Function : init_geoms
00075 //      Access : private
00076 // Description : links the child nodes to the parent stuff
00077 ////////////////////////////////////////////////////////////////////
00078 
00079 void GeomParticleRenderer::
00080 init_geoms(void) {
00081 }
00082 
00083 ////////////////////////////////////////////////////////////////////
00084 //    Function : resize_pool
00085 //      Access : private
00086 // Description : handles renderer-size resizing.
00087 ////////////////////////////////////////////////////////////////////
00088 
00089 void GeomParticleRenderer::
00090 resize_pool(int new_size) {
00091   kill_nodes();
00092 
00093   // now repopulate the vector with a bunch of NULLS, representing
00094   // potential instances of the _geom_node.
00095 
00096   int i;
00097   for (i = 0; i < new_size; i++) {
00098     _node_vector.push_back(NULL);
00099   }
00100 
00101   _pool_size = new_size;
00102 }
00103 
00104 ////////////////////////////////////////////////////////////////////
00105 //  Function : kill_nodes
00106 //    Access : private
00107 ////////////////////////////////////////////////////////////////////
00108 
00109 void GeomParticleRenderer::
00110 kill_nodes(void) {
00111   pvector< PT(PandaNode) >::iterator vec_iter = _node_vector.begin();
00112 
00113   PandaNode *render_node = get_render_node();
00114   for (; vec_iter != _node_vector.end(); vec_iter++) {
00115     PandaNode *node = *vec_iter;
00116     if (node != (PandaNode *)NULL) {
00117       render_node->remove_child(node);
00118     }
00119   }
00120 
00121   _node_vector.erase(_node_vector.begin(), _node_vector.end());
00122 }
00123 
00124 ////////////////////////////////////////////////////////////////////
00125 //    Function : birth_particle
00126 //      Access : Private, virtual
00127 // Description : child birth
00128 ////////////////////////////////////////////////////////////////////
00129 
00130 void GeomParticleRenderer::
00131 birth_particle(int index) {
00132   if (_node_vector[index] == (PandaNode *)NULL) {
00133     PandaNode *node = new PandaNode("");
00134     get_render_node()->add_child(node);
00135     node->add_child(_geom_node);
00136     _node_vector[index] = node;
00137   }
00138 }
00139 
00140 ////////////////////////////////////////////////////////////////////
00141 //    Function : kill_particle
00142 //      Access : Private, virtual
00143 // Description : child kill
00144 ////////////////////////////////////////////////////////////////////
00145 
00146 void GeomParticleRenderer::
00147 kill_particle(int index) {
00148   if (_node_vector[index] != (PandaNode *)NULL) {
00149     get_render_node()->remove_child(_node_vector[index]);
00150     _node_vector[index] = (PandaNode *)NULL;
00151   }
00152 }
00153 
00154 ////////////////////////////////////////////////////////////////////
00155 //    Function : render
00156 //      Access : private
00157 // Description : sets the transitions on each arc
00158 ////////////////////////////////////////////////////////////////////
00159 
00160 void GeomParticleRenderer::
00161 render(pvector< PT(PhysicsObject) >& po_vector, int ttl_particles) {
00162   BaseParticle *cur_particle;
00163   LPoint3f pos;
00164   int i, remaining_particles = ttl_particles;
00165 
00166   pvector< PT(PandaNode) >::iterator cur_node_iter = _node_vector.begin();
00167 
00168   // run through the particle vector
00169 
00170   for (i = 0; i < (int)po_vector.size(); i++) {
00171     PandaNode *cur_node;
00172 
00173     cur_particle = (BaseParticle *) po_vector[i].p();
00174     cur_node = *cur_node_iter;
00175 
00176     if (cur_particle->get_alive()) {
00177       // living particle
00178       if (cur_node == (PandaNode *)NULL) {
00179         birth_particle(i);
00180         cur_node = *cur_node_iter;
00181       }
00182       nassertv(cur_node != (PandaNode *)NULL);
00183 
00184       pos = cur_particle->get_position();
00185 
00186       cur_node->set_state(_render_state);
00187 
00188       if ((_alpha_mode != PR_ALPHA_NONE)) {
00189         float alpha_scalar;
00190 
00191         if(_alpha_mode == PR_ALPHA_USER) {
00192           alpha_scalar = get_user_alpha();
00193         } else {
00194           alpha_scalar = cur_particle->get_parameterized_age();
00195           if (_alpha_mode == PR_ALPHA_OUT)
00196             alpha_scalar = 1.0f - alpha_scalar;
00197           alpha_scalar *= get_user_alpha();
00198         }
00199         
00200         cur_node->set_attrib(ColorAttrib::make_flat
00201                              (Colorf(1.0f, 1.0f, 1.0f, alpha_scalar)));
00202       }
00203 
00204       cur_node->set_transform(TransformState::make_pos(pos));
00205 
00206       // maybe get out early if possible.
00207 
00208       remaining_particles--;
00209 
00210       if (remaining_particles == 0)
00211         break;
00212     }
00213 
00214     cur_node_iter++;
00215   }
00216 }

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