From bcd675aa89b3e4f641a715aaf36619e192550519 Mon Sep 17 00:00:00 2001 From: Darren Ranalli Date: Wed, 11 Oct 2000 23:48:59 +0000 Subject: [PATCH] *** empty log message *** --- panda/src/particlesystem/Sources.pp | 6 +- .../src/particlesystem/lineParticleRenderer.I | 40 ++++ .../particlesystem/lineParticleRenderer.cxx | 219 ++++++++++++++++++ .../src/particlesystem/lineParticleRenderer.h | 66 ++++++ .../src/particlesystem/particleCommonFuncs.h | 32 +++ 5 files changed, 361 insertions(+), 2 deletions(-) create mode 100644 panda/src/particlesystem/lineParticleRenderer.I create mode 100644 panda/src/particlesystem/lineParticleRenderer.cxx create mode 100644 panda/src/particlesystem/lineParticleRenderer.h create mode 100644 panda/src/particlesystem/particleCommonFuncs.h diff --git a/panda/src/particlesystem/Sources.pp b/panda/src/particlesystem/Sources.pp index 929501623b..670308527a 100644 --- a/panda/src/particlesystem/Sources.pp +++ b/panda/src/particlesystem/Sources.pp @@ -34,7 +34,8 @@ spriteParticleRenderer.h tangentRingEmitter.I \ tangentRingEmitter.cxx tangentRingEmitter.h zSpinParticle.I \ zSpinParticle.cxx zSpinParticle.h zSpinParticleFactory.I \ - zSpinParticleFactory.cxx zSpinParticleFactory.h + zSpinParticleFactory.cxx zSpinParticleFactory.h \ + particleCommonFuncs.h #define INSTALL_HEADERS \ baseParticle.I baseParticle.h baseParticleEmitter.I \ @@ -55,7 +56,8 @@ sphereSurfaceEmitter.h sphereVolumeEmitter.I sphereVolumeEmitter.h \ spriteParticleRenderer.I spriteParticleRenderer.h \ tangentRingEmitter.I tangentRingEmitter.h zSpinParticle.I \ - zSpinParticle.h zSpinParticleFactory.I zSpinParticleFactory.h + zSpinParticle.h zSpinParticleFactory.I zSpinParticleFactory.h \ + particleCommonFuncs.h #define IGATESCAN all diff --git a/panda/src/particlesystem/lineParticleRenderer.I b/panda/src/particlesystem/lineParticleRenderer.I new file mode 100644 index 0000000000..d3ebc4904a --- /dev/null +++ b/panda/src/particlesystem/lineParticleRenderer.I @@ -0,0 +1,40 @@ +// Filename: lineParticleRenderer.I +// Created by: darren (06Oct00) +// +//////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function : set_head_color +// Access : public +//////////////////////////////////////////////////////////////////// +INLINE void LineParticleRenderer:: +set_head_color(const Colorf& c) { + _head_color = c; +} + +//////////////////////////////////////////////////////////////////// +// Function : set_tail_color +// Access : public +//////////////////////////////////////////////////////////////////// +INLINE void LineParticleRenderer:: +set_tail_color(const Colorf& c) { + _tail_color = c; +} + +//////////////////////////////////////////////////////////////////// +// Function : get_head_color +// Access : public +//////////////////////////////////////////////////////////////////// +INLINE const Colorf& LineParticleRenderer:: +get_head_color(void) const { + return _head_color; +} + +//////////////////////////////////////////////////////////////////// +// Function : get_tail_color +// Access : public +//////////////////////////////////////////////////////////////////// +INLINE const Colorf& LineParticleRenderer:: +get_tail_color(void) const { + return _tail_color; +} diff --git a/panda/src/particlesystem/lineParticleRenderer.cxx b/panda/src/particlesystem/lineParticleRenderer.cxx new file mode 100644 index 0000000000..d34f86ca92 --- /dev/null +++ b/panda/src/particlesystem/lineParticleRenderer.cxx @@ -0,0 +1,219 @@ +// Filename: lineParticleRenderer.cxx +// Created by: darren (06Oct00) +// +//////////////////////////////////////////////////////////////////// + +#include "lineParticleRenderer.h" + +#include + +//////////////////////////////////////////////////////////////////// +// Function : LineParticleRenderer +// Access : Public +// Description : Default Constructor +//////////////////////////////////////////////////////////////////// + +LineParticleRenderer:: +LineParticleRenderer(void) : + _head_color(Colorf(1.0f, 1.0f, 1.0f, 1.0f)), + _tail_color(Colorf(1.0f, 1.0f, 1.0f, 1.0f)) { + _line_primitive = new GeomLine; + init_geoms(); +} + +//////////////////////////////////////////////////////////////////// +// Function : LineParticleRenderer +// Access : Public +// Description : Constructor +//////////////////////////////////////////////////////////////////// + +LineParticleRenderer:: +LineParticleRenderer(const Colorf& head, + const Colorf& tail, + ParticleRendererAlphaMode alpha_mode) : + _head_color(head), _tail_color(tail), + BaseParticleRenderer(alpha_mode) { + _line_primitive = new GeomLine; + init_geoms(); +} + +//////////////////////////////////////////////////////////////////// +// Function : LineParticleRenderer +// Access : Public +// Description : Copy Constructor +//////////////////////////////////////////////////////////////////// + +LineParticleRenderer:: +LineParticleRenderer(const LineParticleRenderer& copy) : + BaseParticleRenderer(copy) { + _head_color = copy._head_color; + _tail_color = copy._tail_color; + + _line_primitive = new GeomLine; + init_geoms(); +} + +//////////////////////////////////////////////////////////////////// +// Function : ~LineParticleRenderer +// Access : Public +// Description : Destructor +//////////////////////////////////////////////////////////////////// + +LineParticleRenderer:: +~LineParticleRenderer(void) { +} + +//////////////////////////////////////////////////////////////////// +// Function : make copy +// Access : Public +// Description : child virtual for spawning systems +//////////////////////////////////////////////////////////////////// + +BaseParticleRenderer *LineParticleRenderer:: +make_copy(void) { + return new LineParticleRenderer(*this); +} + +//////////////////////////////////////////////////////////////////// +// Function : birth_particle +// Access : Private, virtual +// Description : child birth +//////////////////////////////////////////////////////////////////// + +void LineParticleRenderer:: +birth_particle(int) { +} + +//////////////////////////////////////////////////////////////////// +// Function : kill_particle +// Access : Private, virtual +// Description : child kill +//////////////////////////////////////////////////////////////////// + +void LineParticleRenderer:: +kill_particle(int) { +} + +//////////////////////////////////////////////////////////////////// +// Function : resize_pool +// Access : private +// Description : resizes the render pool. Reference counting +// makes this easy. +//////////////////////////////////////////////////////////////////// + +void LineParticleRenderer:: +resize_pool(int new_size) { + _vertex_array = PTA_Vertexf(new_size * 2); + _color_array = PTA_Colorf(new_size * 2); + + _line_primitive->set_coords(_vertex_array, G_PER_VERTEX); + _line_primitive->set_colors(_color_array, G_PER_VERTEX); + + _max_pool_size = new_size; +} + +//////////////////////////////////////////////////////////////////// +// Function : init_geoms +// Access : private +// Description : initializes the geomnodes +//////////////////////////////////////////////////////////////////// + +void LineParticleRenderer:: +init_geoms(void) { + _line_primitive->set_num_prims(0); + + _interface_node->clear(); + _interface_node->add_geom(_line_primitive); +} + +//////////////////////////////////////////////////////////////////// +// Function : render +// Access : private +// Description : populates the GeomLine +//////////////////////////////////////////////////////////////////// + +void LineParticleRenderer:: +render(vector< PT(PhysicsObject) >& po_vector, int ttl_particles) { + + if (!ttl_particles) + return; + + BaseParticle *cur_particle; + + int cur_index = 0; + int remaining_particles = ttl_particles; + int i; + + Vertexf *cur_vert = &_vertex_array[0]; + Colorf *cur_color = &_color_array[0]; + + // init the aabb + + _aabb_min.set(99999.0f, 99999.0f, 99999.0f); + _aabb_max.set(-99999.0f, -99999.0f, -99999.0f); + + // run through the array + + for (i = 0; i < po_vector.size(); i++) { + cur_particle = (BaseParticle *) po_vector[i].p(); + + if (cur_particle->get_alive() == false) + continue; + + // adjust the aabb + + if (cur_particle->get_position().get_x() > _aabb_max.get_x()) + _aabb_max[0] = cur_particle->get_position().get_x(); + else if (cur_particle->get_position().get_x() < _aabb_min.get_x()) + _aabb_min[0] = cur_particle->get_position().get_x(); + + if (cur_particle->get_position().get_y() > _aabb_max.get_y()) + _aabb_max[1] = cur_particle->get_position().get_y(); + else if (cur_particle->get_position().get_y() < _aabb_min.get_y()) + _aabb_min[1] = cur_particle->get_position().get_y(); + + if (cur_particle->get_position().get_z() > _aabb_max.get_z()) + _aabb_max[2] = cur_particle->get_position().get_z(); + else if (cur_particle->get_position().get_z() < _aabb_min.get_z()) + _aabb_min[2] = cur_particle->get_position().get_z(); + + // draw the particle. + + LPoint3f pos = cur_particle->get_position(); + Colorf head_color = _head_color; + Colorf tail_color = _tail_color; + + // handle alpha + + if (_alpha_mode != PR_ALPHA_NONE) { + float alpha = cur_particle->get_parameterized_age(); + + if (_alpha_mode == PR_ALPHA_OUT) + alpha = 1.0f - alpha; + + head_color[3] = alpha; + tail_color[3] = alpha; + } + + // 1 line from current position to last position + + *cur_vert++ = cur_particle->get_position(); + *cur_vert++ = cur_particle->get_last_position(); + + *cur_color++ = head_color; + *cur_color++ = tail_color; + + remaining_particles--; + if (remaining_particles == 0) + break; + } + + _line_primitive->set_num_prims(ttl_particles); + + // done filling geomline node, now do the bb stuff + + LPoint3f aabb_center = _aabb_min + ((_aabb_max - _aabb_min) * 0.5f); + float radius = (aabb_center - _aabb_min).length(); + + _interface_node->set_bound(BoundingSphere(aabb_center, radius)); +} diff --git a/panda/src/particlesystem/lineParticleRenderer.h b/panda/src/particlesystem/lineParticleRenderer.h new file mode 100644 index 0000000000..a8219eeea3 --- /dev/null +++ b/panda/src/particlesystem/lineParticleRenderer.h @@ -0,0 +1,66 @@ +// Filename: lineParticleRenderer.h +// Created by: darren (06Oct00) +// +//////////////////////////////////////////////////////////////////// + +#ifndef LINEPARTICLERENDERER_H +#define LINEPARTICLERENDERER_H + +#include "baseParticle.h" +#include "baseParticleRenderer.h" + +#include +#include +#include +#include + +//////////////////////////////////////////////////////////////////// +// Class : LineParticleRenderer +// Description : renders a line from last position to current +// position -- good for rain, sparks, etc. +//////////////////////////////////////////////////////////////////// + +class EXPCL_PANDAPHYSICS LineParticleRenderer : public BaseParticleRenderer { +private: + + Colorf _head_color; + Colorf _tail_color; + + PT(GeomLine) _line_primitive; + + PTA_Vertexf _vertex_array; + PTA_Colorf _color_array; + + int _max_pool_size; + + LPoint3f _aabb_min, _aabb_max; + + virtual void birth_particle(int index); + virtual void kill_particle(int index); + virtual void init_geoms(void); + virtual void render(vector< PT(PhysicsObject) >& po_vector, + int ttl_particles); + virtual void resize_pool(int new_size); + +public: + + LineParticleRenderer(void); + LineParticleRenderer(const LineParticleRenderer& copy); + LineParticleRenderer(const Colorf& head, + const Colorf& tail, + ParticleRendererAlphaMode alpha_mode); + + virtual ~LineParticleRenderer(void); + + virtual BaseParticleRenderer *make_copy(void); + + INLINE void set_head_color(const Colorf& c); + INLINE void set_tail_color(const Colorf& c); + + INLINE const Colorf& get_head_color(void) const; + INLINE const Colorf& get_tail_color(void) const; +}; + +#include "lineParticleRenderer.I" + +#endif // LINEPARTICLERENDERER_H diff --git a/panda/src/particlesystem/particleCommonFuncs.h b/panda/src/particlesystem/particleCommonFuncs.h new file mode 100644 index 0000000000..93f8920ff9 --- /dev/null +++ b/panda/src/particlesystem/particleCommonFuncs.h @@ -0,0 +1,32 @@ +// Filename: particleCommonFuncs.h +// Created by: darren (02Oct00) +// +//////////////////////////////////////////////////////////////////// + +#ifndef PARTICLECOMMONFUNCS_H +#define PARTICLECOMMONFUNCS_H + +// evaluates to a float in the range [0,1] +#define NORMALIZED_RAND() ((float)rand() / (float)RAND_MAX) + +// linear interpolation +// t is in [0,1] +// result is in [X0,X1] +#define LERP(t,X0,X1) ((X0) + ((t) * ((X1) - (X0)))) + +// linear t -> cubic t +// t is in [0,1] +// result is in [0,1] +#define CUBIC_T(t) ((t)*(t)*(3-(2*(t)))) + +// cubic interpolation +// t is in [0,1] +// result is in [X0,X1] +#define CLERP(t,X0,X1) LERP(CUBIC_T(t), (X0), (X1)) + +// spread calculator +// spread is non-negative spread magnitude +// result is in [-spread,spread] +#define SPREAD(magnitude) ((magnitude) - (NORMALIZED_RAND() * 2.0f * (magnitude))) + +#endif // PARTICLECOMMONFUNCS_H