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

panda/src/particlesystem/lineParticleRenderer.cxx

Go to the documentation of this file.
00001 // Filename: lineParticleRenderer.cxx
00002 // Created by:  darren (06Oct00)
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 "lineParticleRenderer.h"
00020 
00021 #include <boundingSphere.h>
00022 
00023 ////////////////////////////////////////////////////////////////////
00024 //    Function : LineParticleRenderer
00025 //      Access : Public
00026 // Description : Default Constructor
00027 ////////////////////////////////////////////////////////////////////
00028 
00029 LineParticleRenderer::
00030 LineParticleRenderer(void) :
00031   _head_color(Colorf(1.0f, 1.0f, 1.0f, 1.0f)),
00032   _tail_color(Colorf(1.0f, 1.0f, 1.0f, 1.0f)) {
00033   _line_primitive = new GeomLine;
00034   init_geoms();
00035 }
00036 
00037 ////////////////////////////////////////////////////////////////////
00038 //    Function : LineParticleRenderer
00039 //      Access : Public
00040 // Description : Constructor
00041 ////////////////////////////////////////////////////////////////////
00042 
00043 LineParticleRenderer::
00044 LineParticleRenderer(const Colorf& head,
00045                      const Colorf& tail,
00046                      ParticleRendererAlphaMode alpha_mode) :
00047   BaseParticleRenderer(alpha_mode),
00048   _head_color(head), _tail_color(tail)
00049 {
00050   _line_primitive = new GeomLine;
00051   init_geoms();
00052 }
00053 
00054 ////////////////////////////////////////////////////////////////////
00055 //    Function : LineParticleRenderer
00056 //      Access : Public
00057 // Description : Copy Constructor
00058 ////////////////////////////////////////////////////////////////////
00059 
00060 LineParticleRenderer::
00061 LineParticleRenderer(const LineParticleRenderer& copy) :
00062   BaseParticleRenderer(copy) {
00063   _head_color = copy._head_color;
00064   _tail_color = copy._tail_color;
00065 
00066   _line_primitive = new GeomLine;
00067   init_geoms();
00068 }
00069 
00070 ////////////////////////////////////////////////////////////////////
00071 //    Function : ~LineParticleRenderer
00072 //      Access : Public
00073 // Description : Destructor
00074 ////////////////////////////////////////////////////////////////////
00075 
00076 LineParticleRenderer::
00077 ~LineParticleRenderer(void) {
00078 }
00079 
00080 ////////////////////////////////////////////////////////////////////
00081 //    Function : make copy
00082 //      Access : Public
00083 // Description : child virtual for spawning systems
00084 ////////////////////////////////////////////////////////////////////
00085 
00086 BaseParticleRenderer *LineParticleRenderer::
00087 make_copy(void) {
00088   return new LineParticleRenderer(*this);
00089 }
00090 
00091 ////////////////////////////////////////////////////////////////////
00092 //    Function : birth_particle
00093 //      Access : Private, virtual
00094 // Description : child birth
00095 ////////////////////////////////////////////////////////////////////
00096 
00097 void LineParticleRenderer::
00098 birth_particle(int) {
00099 }
00100 
00101 ////////////////////////////////////////////////////////////////////
00102 //    Function : kill_particle
00103 //      Access : Private, virtual
00104 // Description : child kill
00105 ////////////////////////////////////////////////////////////////////
00106 
00107 void LineParticleRenderer::
00108 kill_particle(int) {
00109 }
00110 
00111 ////////////////////////////////////////////////////////////////////
00112 //    Function : resize_pool
00113 //      Access : private
00114 // Description : resizes the render pool.  Reference counting
00115 //               makes this easy.
00116 ////////////////////////////////////////////////////////////////////
00117 
00118 void LineParticleRenderer::
00119 resize_pool(int new_size) {
00120   _vertex_array = PTA_Vertexf::empty_array(new_size * 2);
00121   _color_array = PTA_Colorf::empty_array(new_size * 2);
00122 
00123   _line_primitive->set_coords(_vertex_array);
00124   _line_primitive->set_colors(_color_array, G_PER_VERTEX);
00125 
00126   _max_pool_size = new_size;
00127 
00128   init_geoms();
00129 }
00130 
00131 ////////////////////////////////////////////////////////////////////
00132 //    Function : init_geoms
00133 //      Access : private
00134 // Description : initializes the geomnodes
00135 ////////////////////////////////////////////////////////////////////
00136 
00137 void LineParticleRenderer::
00138 init_geoms(void) {
00139   _line_primitive->set_num_prims(0);
00140 
00141   GeomNode *render_node = get_render_node();
00142   render_node->remove_all_geoms();
00143   render_node->add_geom(_line_primitive, _render_state);
00144 }
00145 
00146 ////////////////////////////////////////////////////////////////////
00147 //    Function : render
00148 //      Access : private
00149 // Description : populates the GeomLine
00150 ////////////////////////////////////////////////////////////////////
00151 
00152 void LineParticleRenderer::
00153 render(pvector< PT(PhysicsObject) >& po_vector, int ttl_particles) {
00154 
00155   if (!ttl_particles)
00156     return;
00157 
00158   BaseParticle *cur_particle;
00159 
00160   int remaining_particles = ttl_particles;
00161   int i;
00162 
00163   Vertexf *cur_vert = &_vertex_array[0];
00164   Colorf *cur_color = &_color_array[0];
00165 
00166   // init the aabb
00167 
00168   _aabb_min.set(99999.0f, 99999.0f, 99999.0f);
00169   _aabb_max.set(-99999.0f, -99999.0f, -99999.0f);
00170 
00171   // run through the array
00172 
00173   for (i = 0; i < (int)po_vector.size(); i++) {
00174     cur_particle = (BaseParticle *) po_vector[i].p();
00175 
00176     if (cur_particle->get_alive() == false)
00177       continue;
00178 
00179     LPoint3f pos = cur_particle->get_position();
00180 
00181     // adjust the aabb
00182 
00183     if (pos.get_x() > _aabb_max.get_x())
00184       _aabb_max[0] = pos.get_x();
00185     if (pos.get_x() < _aabb_min.get_x())
00186       _aabb_min[0] = pos.get_x();
00187 
00188     if (pos.get_y() > _aabb_max.get_y())
00189       _aabb_max[1] = pos.get_y();
00190     if (pos.get_y() < _aabb_min.get_y())
00191       _aabb_min[1] = pos.get_y();
00192 
00193     if (pos.get_z() > _aabb_max.get_z())
00194       _aabb_max[2] = pos.get_z();
00195     if (pos.get_z() < _aabb_min.get_z())
00196       _aabb_min[2] = pos.get_z();
00197 
00198     // draw the particle.
00199 
00200     Colorf head_color = _head_color;
00201     Colorf tail_color = _tail_color;
00202 
00203     // handle alpha
00204 
00205     if (_alpha_mode != PR_ALPHA_NONE) {
00206 
00207           float alpha;
00208 
00209       if (_alpha_mode == PR_ALPHA_USER) {
00210                   alpha = get_user_alpha();
00211           } else {
00212                   alpha = cur_particle->get_parameterized_age();
00213                   if (_alpha_mode == PR_ALPHA_OUT)
00214                           alpha = 1.0f - alpha;
00215           }
00216 
00217       head_color[3] = alpha;
00218       tail_color[3] = alpha;
00219     }
00220 
00221     // one line from current position to last position
00222 
00223     *cur_vert++ = pos;
00224     *cur_vert++ = cur_particle->get_last_position();
00225 
00226     *cur_color++ = head_color;
00227     *cur_color++ = tail_color;
00228 
00229     remaining_particles--;
00230     if (remaining_particles == 0)
00231       break;
00232   }
00233 
00234   _line_primitive->set_num_prims(ttl_particles);
00235 
00236   // done filling geomline node, now do the bb stuff
00237 
00238   LPoint3f aabb_center = (_aabb_min + _aabb_max) * 0.5f;
00239   float radius = (aabb_center - _aabb_min).length();
00240 
00241   _line_primitive->set_bound(BoundingSphere(aabb_center, radius));
00242   get_render_node()->mark_bound_stale();
00243 }

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