From 254dd1df4ff55d39bf12eafdceed50b619ef13e2 Mon Sep 17 00:00:00 2001 From: Josh Wilson Date: Thu, 9 Jun 2005 20:59:24 +0000 Subject: [PATCH] *** empty log message *** --- .../colorInterpolationManager.I | 259 +++++++++ .../colorInterpolationManager.cxx | 536 ++++++++++++++++++ .../colorInterpolationManager.h | 221 ++++++++ 3 files changed, 1016 insertions(+) create mode 100755 panda/src/particlesystem/colorInterpolationManager.I create mode 100755 panda/src/particlesystem/colorInterpolationManager.cxx create mode 100755 panda/src/particlesystem/colorInterpolationManager.h diff --git a/panda/src/particlesystem/colorInterpolationManager.I b/panda/src/particlesystem/colorInterpolationManager.I new file mode 100755 index 0000000000..3065335269 --- /dev/null +++ b/panda/src/particlesystem/colorInterpolationManager.I @@ -0,0 +1,259 @@ +// Filename: colorInterpolationManager.I +// Created by: joswilso (02Jun05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionConstant::get_color_a +// Access : public +// Description : Returns the primary color of the function. +//////////////////////////////////////////////////////////////////// + +INLINE Colorf ColorInterpolationFunctionConstant:: +get_color_a(void) const { + return _c_a; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionConstant::set_color_a +// Access : public +// Description : Sets the primary color of the function. +//////////////////////////////////////////////////////////////////// + +INLINE void ColorInterpolationFunctionConstant:: +set_color_a(const Colorf c) { + _c_a = c; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionLinear::get_color_b +// Access : public +// Description : Returns the secondary color of the function. +//////////////////////////////////////////////////////////////////// + +INLINE Colorf ColorInterpolationFunctionLinear:: +get_color_b(void) const { + return _c_b; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionLinear::set_color_b +// Access : public +// Description : Sets the secondary color of the function. +//////////////////////////////////////////////////////////////////// + +INLINE void ColorInterpolationFunctionLinear:: +set_color_b(const Colorf c) { + _c_b = c; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionStepwave::get_width_a +// Access : public +// Description : Returns the primary width of the function. +//////////////////////////////////////////////////////////////////// + +INLINE float ColorInterpolationFunctionStepwave:: +get_width_a(void) const { + return _w_a; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionStepwave::get_width_b +// Access : public +// Description : Returns the secondary width of the function. +//////////////////////////////////////////////////////////////////// + +INLINE float ColorInterpolationFunctionStepwave:: +get_width_b(void) const { + return _w_b; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionStepwave::set_width_a +// Access : public +// Description : Sets the primary width of the function. +//////////////////////////////////////////////////////////////////// + +INLINE void ColorInterpolationFunctionStepwave:: +set_width_a(const float w) { + _w_a = w; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionStepwave::set_width_b +// Access : public +// Description : Sets the secondary width of the function. +//////////////////////////////////////////////////////////////////// + +INLINE void ColorInterpolationFunctionStepwave:: +set_width_b(const float w) { + _w_b = w; +} + + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionSinusoid::get_period +// Access : public +// Description : Returns the time to transition from A to B then back +// to A again. +//////////////////////////////////////////////////////////////////// + +INLINE float ColorInterpolationFunctionSinusoid:: +get_period(void) const { + return _period; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionSinusoid::set_period +// Access : public +// Description : Sets the time to transition from A to B then back +// to A again. +//////////////////////////////////////////////////////////////////// + +INLINE void ColorInterpolationFunctionSinusoid:: +set_period(const float p) { + _period = p; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationSegment::get_function +// Access : public +// Description : Returns a reference to the function object +// corresponding to this segment. It should be downcast +// using the function's get_type() and the manager's +// downcastFunctionTo*****() functions. +//////////////////////////////////////////////////////////////////// + +INLINE ColorInterpolationFunction* ColorInterpolationSegment:: +get_function(void) const { + return _color_inter_func; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationSegment::get_time_begin +// Access : public +// Description : Returns the point in the particle's lifetime at which +// this segment begins its effect. It is an interpolated +// value in the range [0,1]. +//////////////////////////////////////////////////////////////////// + +INLINE float ColorInterpolationSegment:: +get_time_begin(void) const { + return _t_begin; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationSegment::get_time_end +// Access : public +// Description : Returns the point in the particle's lifetime at which +// this segment's effect stops. It is an interpolated +// value in the range [0,1]. +//////////////////////////////////////////////////////////////////// + +INLINE float ColorInterpolationSegment:: +get_time_end(void) const { + return _t_end; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationSegment::get_id +// Access : public +// Description : Returns the id assigned to this segment by the +// manager that created it. +//////////////////////////////////////////////////////////////////// + +INLINE int ColorInterpolationSegment:: +get_id(void) const { + return _id; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationSegment::set_function +// Access : public +// Description : Sets the function that the segment will use for +// its interpolation calculations. +//////////////////////////////////////////////////////////////////// + +INLINE void ColorInterpolationSegment:: +set_function(ColorInterpolationFunction* function) { + _color_inter_func = function; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationSegment::set_time_begin +// Access : public +// Description : Sets the point in the particle's lifetime at which +// this segment begins its effect. It is an interpolated +// value in the range [0,1]. +//////////////////////////////////////////////////////////////////// + +INLINE void ColorInterpolationSegment:: +set_time_begin(const float time) { + _t_begin = time; + _t_total = _t_end-_t_begin; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationSegment::set_time_end +// Access : public +// Description : Sets the point in the particle's lifetime at which +// this segment's effect ends. It is an interpolated +// value in the range [0,1]. +//////////////////////////////////////////////////////////////////// + +INLINE void ColorInterpolationSegment:: +set_time_end(const float time) { + _t_end = time; + _t_total = _t_end-_t_begin; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::get_segment +// Access : public +// Description : Returns the segment that corresponds to 'seg_id'. +//////////////////////////////////////////////////////////////////// + +INLINE ColorInterpolationSegment* ColorInterpolationManager:: +get_segment(const int seg_id) { + pvector::iterator iter; + + for(iter = _i_segs.begin();iter != _i_segs.end();++iter) + if( seg_id == (*iter)->get_id() ) + return (*iter); + + return NULL; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::get_segment_id_list +// Access : public +// Description : Returns a space delimited list of all of the ids +// in the manager at the time. +//////////////////////////////////////////////////////////////////// + +INLINE string ColorInterpolationManager:: +get_segment_id_list(void) { + pvector::iterator iter; + stringstream output; + + for(iter = _i_segs.begin();iter != _i_segs.end();++iter) + output << (*iter)->get_id() << " "; + + return output.str().substr(0,output.str().length()-1); +} diff --git a/panda/src/particlesystem/colorInterpolationManager.cxx b/panda/src/particlesystem/colorInterpolationManager.cxx new file mode 100755 index 0000000000..5298fa159e --- /dev/null +++ b/panda/src/particlesystem/colorInterpolationManager.cxx @@ -0,0 +1,536 @@ +// Filename: colorInterpolationManager.cxx +// Created by: joswilso (02Jun05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// +#include "colorInterpolationManager.h" +#include "mathNumbers.h" + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunction::ColorInterpolationFunction +// Access : public +// Description : constructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationFunction:: +ColorInterpolationFunction(void) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunction::~ColorInterpolationFunction +// Access : public +// Description : destructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationFunction:: +~ColorInterpolationFunction(void) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunction::get_type +// Access : public +// Description : Returns a string of type of the current object. +// Since it is virtual, derived classes should define +// their own version. +//////////////////////////////////////////////////////////////////// + +string ColorInterpolationFunction:: +get_type(void) { + return "ColorInterpolationFunction"; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionConstant::ColorInterpolationFunctionConstant +// Access : public +// Description : default constructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationFunctionConstant:: +ColorInterpolationFunctionConstant(void) : + _c_a(1.0f,1.0f,1.0f,1.0f) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionConstant::ColorInterpolationFunctionConstant +// Access : public +// Description : constructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationFunctionConstant:: +ColorInterpolationFunctionConstant(const Colorf color_a) : + _c_a(color_a) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionConstant::interpolate +// Access : protected +// Description : Returns the color associated with this instance. +//////////////////////////////////////////////////////////////////// + +Colorf ColorInterpolationFunctionConstant:: +interpolate(const float t) const { + return _c_a; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionConstant::get_type +// Access : protected +// Description : Returns a string representing this class type. +//////////////////////////////////////////////////////////////////// + +string ColorInterpolationFunctionConstant:: +get_type(void) { + return "ColorInterpolationFunctionConstant"; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionLinear::ColorInterpolationFunctionLinear +// Access : public +// Description : default constructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationFunctionLinear:: +ColorInterpolationFunctionLinear(void) : + _c_b(1.0f,1.0f,1.0f,1.0f) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionLinear::ColorInterpolationFunctionLinear +// Access : public +// Description : constructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationFunctionLinear:: +ColorInterpolationFunctionLinear(const Colorf color_a, + const Colorf color_b) : + ColorInterpolationFunctionConstant(color_a), + _c_b(color_b) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionLinear::interpolate +// Access : protected +// Description : Returns the linear mixture of A and B according to 't'. +//////////////////////////////////////////////////////////////////// + +Colorf ColorInterpolationFunctionLinear:: +interpolate(const float t) const { + return (1.0f-t)*_c_a + t*_c_b; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionLinear::get_type +// Access : protected +// Description : Returns a string representing this class type. +//////////////////////////////////////////////////////////////////// + +string ColorInterpolationFunctionLinear:: +get_type(void) { + return "ColorInterpolationFunctionLinear"; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionStepwave::ColorInterpolationFunctionStepwave +// Access : public +// Description : default constructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationFunctionStepwave:: +ColorInterpolationFunctionStepwave(void) : + _w_a(0.5f), + _w_b(0.5f) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionStepwave::ColorInterpolationFunctionStepwave +// Access : public +// Description : constructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationFunctionStepwave:: +ColorInterpolationFunctionStepwave(const Colorf color_a, + const Colorf color_b, + const float width_a, + const float width_b) : + ColorInterpolationFunctionLinear(color_a,color_b), + _w_a(width_a), + _w_b(width_b) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionStepwave::interpolate +// Access : protected +// Description : Returns either A or B. +//////////////////////////////////////////////////////////////////// + +Colorf ColorInterpolationFunctionStepwave:: +interpolate(const float t) const { + if(fmodf(t,(_w_a+_w_b))<_w_a) { + return _c_a; + } + return _c_b; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionStepwave::get_type +// Access : protected +// Description : Returns a string representing this class type. +//////////////////////////////////////////////////////////////////// + +string ColorInterpolationFunctionStepwave:: +get_type(void) { + return "ColorInterpolationFunctionStepwave"; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionSinusoid::ColorInterpolationFunctionSinusoid +// Access : public +// Description : default constructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationFunctionSinusoid:: +ColorInterpolationFunctionSinusoid(void) : + _period(1.0f) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionSinusoid::ColorInterpolationFunctionSinusoid +// Access : public +// Description : constructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationFunctionSinusoid:: +ColorInterpolationFunctionSinusoid(const Colorf color_a, + const Colorf color_b, + const float period) : + ColorInterpolationFunctionLinear(color_a,color_b), + _period(period) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionSinusoid::interpolate +// Access : protected +// Description : Returns a sinusoidal blended color between A and B. +// Period defines the time it will take to return to +// A. +//////////////////////////////////////////////////////////////////// + +Colorf ColorInterpolationFunctionSinusoid:: +interpolate(const float t) const { + float weight_a = (1.0f+cos(t*MathNumbers::pi_f*2.0f/_period))/2.0f; + return (weight_a*_c_a)+((1.0f-weight_a)*_c_b); +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationFunctionSinusoid::get_type +// Access : protected +// Description : Returns a string representing this class type. +//////////////////////////////////////////////////////////////////// + +string ColorInterpolationFunctionSinusoid:: +get_type(void) { + return "ColorInterpolationFunctionSinusoid"; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationSegment::ColorInterpolationSegment +// Access : public +// Description : constructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationSegment:: +ColorInterpolationSegment(ColorInterpolationFunction* function, + const float &time_begin, + const float &time_end, + const int id) : + _color_inter_func(function), + _t_begin(time_begin), + _t_end(time_end), + _t_total(time_end-time_begin), + _id(id) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationSegment::ColorInterpolationSegment +// Access : public +// Description : copy constructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationSegment:: +ColorInterpolationSegment(const ColorInterpolationSegment ©) : + _color_inter_func(copy._color_inter_func), + _t_begin(copy._t_begin), + _t_end(copy._t_end), + _t_total(copy._t_total), + _id(copy._id) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationSegment::~ColorInterpolationSegment +// Access : public +// Description : destructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationSegment:: +~ColorInterpolationSegment(void) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationSegment::interpolateColor +// Access : public +// Description : Returns the interpolated color according to the +// segment's function and start and end times. 't' is +// a value in [0-1] where corresponds to beginning of +// the segment and 1 corresponds to the end. +//////////////////////////////////////////////////////////////////// + +Colorf ColorInterpolationSegment:: +interpolateColor(const float t) const { + return _color_inter_func->interpolate((t-_t_begin)/_t_total); +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::ColorInterpolationManager +// Access : public +// Description : default constructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationManager:: +ColorInterpolationManager(void) : + _default_color(Colorf(1.0f,1.0f,1.0f,1.0f)), + _id_generator(0) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::ColorInterpolationManager +// Access : public +// Description : constructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationManager:: +ColorInterpolationManager(const Colorf &c) : + _default_color(c), + _id_generator(0) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::ColorInterpolationManager +// Access : public +// Description : copy constructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationManager:: +ColorInterpolationManager(const ColorInterpolationManager& copy) : + _default_color(copy._default_color), + _i_segs(copy._i_segs), + _id_generator(copy._id_generator) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::~ColorInterpolationManager +// Access : public +// Description : destructor +//////////////////////////////////////////////////////////////////// + +ColorInterpolationManager:: +~ColorInterpolationManager(void) { +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::add_constant +// Access : public +// Description : Adds a constant segment of the specified color to the +// manager and returns the segment's id as known +// by the manager. +//////////////////////////////////////////////////////////////////// + +int ColorInterpolationManager:: +add_constant(const float time_begin, const float time_end, const Colorf color) { + PT(ColorInterpolationFunctionConstant) fPtr = new ColorInterpolationFunctionConstant(color); + PT(ColorInterpolationSegment) sPtr = new ColorInterpolationSegment(fPtr,time_begin,time_end,_id_generator); + + _i_segs.push_back(sPtr); + + return _id_generator++; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::add_linear +// Access : public +// Description : Adds a linear segment between two colors to the manager +// and returns the segment's id as known by the manager. +//////////////////////////////////////////////////////////////////// + +int ColorInterpolationManager:: +add_linear(const float time_begin, const float time_end, const Colorf color_a, const Colorf color_b) { + PT(ColorInterpolationFunctionLinear) fPtr = new ColorInterpolationFunctionLinear(color_a, color_b); + PT(ColorInterpolationSegment) sPtr = new ColorInterpolationSegment(fPtr,time_begin,time_end,_id_generator); + + _i_segs.push_back(sPtr); + + return _id_generator++; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::add_stepwave +// Access : public +// Description : Adds a stepwave segment of two colors to the manager +// and returns the segment's id as known by the manager. +//////////////////////////////////////////////////////////////////// + +int ColorInterpolationManager:: +add_stepwave(const float time_begin, const float time_end, const Colorf color_a, const Colorf color_b, const float width_a, const float width_b) { + PT(ColorInterpolationFunctionStepwave) fPtr = new ColorInterpolationFunctionStepwave(color_a, color_b, width_a, width_b); + PT(ColorInterpolationSegment) sPtr = new ColorInterpolationSegment(fPtr,time_begin,time_end,_id_generator); + + _i_segs.push_back(sPtr); + + return _id_generator++; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::add_sinusoid +// Access : public +// Description : Adds a stepwave segment of two colors and a specified +// period to the manager and returns the segment's +// id as known by the manager. +//////////////////////////////////////////////////////////////////// + +int ColorInterpolationManager:: +add_sinusoid(const float time_begin, const float time_end, const Colorf color_a, const Colorf color_b, const float period) { + PT(ColorInterpolationFunctionSinusoid) fPtr = new ColorInterpolationFunctionSinusoid(color_a, color_b, period); + PT(ColorInterpolationSegment) sPtr = new ColorInterpolationSegment(fPtr,time_begin,time_end,_id_generator); + + _i_segs.push_back(sPtr); + + return _id_generator++; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::downcast_function_to_constant +// Access : public +// Description : Downcasts a ptr to abstract type +// ColorInterpolationFunction to concrete +// type ColorInterpolationFunctionConstant +//////////////////////////////////////////////////////////////////// + +ColorInterpolationFunctionConstant* ColorInterpolationManager:: +downcast_function_to_constant(ColorInterpolationFunction* f) { + return (ColorInterpolationFunctionConstant*) f; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::downcast_function_to_linear +// Access : public +// Description : Downcasts a ptr to abstract type +// ColorInterpolationFunction to concrete +// type ColorInterpolationFunctionLinear +//////////////////////////////////////////////////////////////////// + +ColorInterpolationFunctionLinear* ColorInterpolationManager:: +downcast_function_to_linear(ColorInterpolationFunction* f) { + return (ColorInterpolationFunctionLinear*) f; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::downcast_function_to_stepwave +// Access : public +// Description : Downcasts a ptr to abstract type +// ColorInterpolationFunction to concrete +// type ColorInterpolationFunctionStepwave +//////////////////////////////////////////////////////////////////// + +ColorInterpolationFunctionStepwave* ColorInterpolationManager:: +downcast_function_to_stepwave(ColorInterpolationFunction* f) { + return (ColorInterpolationFunctionStepwave*) f; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::downcast_function_to_sinusoid +// Access : public +// Description : Downcasts a ptr to abstract type +// ColorInterpolationFunction to concrete +// type ColorInterpolationFunctionSinusoid +//////////////////////////////////////////////////////////////////// + +ColorInterpolationFunctionSinusoid* ColorInterpolationManager:: +downcast_function_to_sinusoid(ColorInterpolationFunction* f) { + return (ColorInterpolationFunctionSinusoid*) f; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::clear_segment +// Access : public +// Description : Removes the segment of 'id' from the manager. +//////////////////////////////////////////////////////////////////// + +void ColorInterpolationManager:: +clear_segment(const int seg_id) { + pvector::iterator iter; + + for(iter = _i_segs.begin();iter != _i_segs.end();++iter) { + if( seg_id == (*iter)->get_id() ) { + _i_segs.erase(iter); + return; + } + } +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager::clear_to_initial +// Access : public +// Description : Removes all segments from the manager. +//////////////////////////////////////////////////////////////////// + +void ColorInterpolationManager:: +clear_to_initial(void) { + _i_segs.clear(); + _id_generator = 0; +} + +//////////////////////////////////////////////////////////////////// +// Function : ColorInterpolationManager:: +// Access : public +// Description : For time 'interpolated_time', this returns the +// additive composite color of all segments that influence +// that instant in the particle's lifetime. If no segments +// cover that time, the manager's default color is returned. +//////////////////////////////////////////////////////////////////// + +Colorf ColorInterpolationManager:: +generateColor(const float interpolated_time) { + bool segment_found = false; + Colorf out(0.0f,0.0f,0.0f,0.0f); + ColorInterpolationSegment *cur_seg; + pvector::iterator iter; + + for(iter = _i_segs.begin();iter != _i_segs.end();++iter) { + cur_seg = (*iter); + if( interpolated_time >= cur_seg->get_time_begin() && interpolated_time <= cur_seg->get_time_end() ) { + segment_found = true; + out += cur_seg->interpolateColor(interpolated_time); + out[0] = max(0,min(out[0],1.0f)); + out[1] = max(0,min(out[1],1.0f)); + out[2] = max(0,min(out[2],1.0f)); + out[3] = max(0,min(out[3],1.0f)); + } + } + + if(segment_found) { + return out; + } + + return _default_color; +} diff --git a/panda/src/particlesystem/colorInterpolationManager.h b/panda/src/particlesystem/colorInterpolationManager.h new file mode 100755 index 0000000000..f4e5ae8916 --- /dev/null +++ b/panda/src/particlesystem/colorInterpolationManager.h @@ -0,0 +1,221 @@ +// Filename: colorInterpolationManager.h +// Created by: joswilso (02Jun05) +// +//////////////////////////////////////////////////////////////////// +// +// PANDA 3D SOFTWARE +// Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved +// +// All use of this software is subject to the terms of the Panda 3d +// Software license. You should have received a copy of this license +// along with this source code; you will also find a current copy of +// the license at http://etc.cmu.edu/panda3d/docs/license/ . +// +// To contact the maintainers of this program write to +// panda3d-general@lists.sourceforge.net . +// +//////////////////////////////////////////////////////////////////// + +#ifndef COLORINTERPOLATIONMANAGER_H +#define COLORINTERPOLATIONMANAGER_H + +#include "luse.h" +#include "pvector.h" + +//////////////////////////////////////////////////////////////////// +// Class : ColorInterpolationFunction +// Description : Abstract class from which all other functions +// should inherit. Defines the virtual interpolate() +// function. +//////////////////////////////////////////////////////////////////// + +class ColorInterpolationFunction : public ReferenceCount { +PUBLISHED: + virtual string get_type(void); + +public: + ColorInterpolationFunction(void); + virtual ~ColorInterpolationFunction(void); + + virtual Colorf interpolate(const float t = 0) const = 0; +}; + +//////////////////////////////////////////////////////////////////// +// Class : ColorInterpolationFunctionConstant +// Description : Defines a constant color over the lifetime of +// the segment. +//////////////////////////////////////////////////////////////////// + +class ColorInterpolationFunctionConstant : public ColorInterpolationFunction { +PUBLISHED: + INLINE Colorf get_color_a(void) const; + + INLINE void set_color_a(const Colorf c); + +public: + ColorInterpolationFunctionConstant(void); + ColorInterpolationFunctionConstant(const Colorf color_a); + +protected: + virtual Colorf interpolate(const float t = 0) const; + virtual string get_type(void); + + Colorf _c_a; +}; + +//////////////////////////////////////////////////////////////////// +// Class : ColorInterpolationFunctionLinear +// Description : Defines a linear interpolation over the lifetime of +// the segment. +//////////////////////////////////////////////////////////////////// + +class ColorInterpolationFunctionLinear : public ColorInterpolationFunctionConstant { +PUBLISHED: + INLINE Colorf get_color_b(void) const; + + INLINE void set_color_b(const Colorf c); + +public: + ColorInterpolationFunctionLinear(void); + ColorInterpolationFunctionLinear(const Colorf color_a, const Colorf color_b); + +protected: + Colorf interpolate(const float t = 0) const; + virtual string get_type(void); + + Colorf _c_b; +}; + +//////////////////////////////////////////////////////////////////// +// Class : ColorInterpolationFunctionStepwave +// Description : Defines a discrete cyclical transition between two colors. +// The widths describe a portion of the segment's lifetime +// for which the corresponding color should be selected. If +// their sum is less than 1, the function repeats until +// the end of the segment. +//////////////////////////////////////////////////////////////////// + +class ColorInterpolationFunctionStepwave : public ColorInterpolationFunctionLinear { +PUBLISHED: + INLINE float get_width_a(void) const; + INLINE float get_width_b(void) const; + + INLINE void set_width_a(const float w); + INLINE void set_width_b(const float w); + +public: + ColorInterpolationFunctionStepwave(void); + ColorInterpolationFunctionStepwave(const Colorf color_a, const Colorf color_b, const float width_a, const float width_b); + +protected: + Colorf interpolate(const float t = 0) const; + virtual string get_type(void); + + float _w_a; + float _w_b; +}; + +//////////////////////////////////////////////////////////////////// +// Class : ColorInterpolationFunctionSinusoid +// Description : Defines a sinusoidal blending between two colors. +// A period of "1" corresponds to a single transition +// from color_a to color_b and then back to color_a +// over the course of the segment's lifetime. A +// shorter period will result in a higher frequency +// cycle. +//////////////////////////////////////////////////////////////////// + +class ColorInterpolationFunctionSinusoid : public ColorInterpolationFunctionLinear { +PUBLISHED: + INLINE float get_period(void) const; + + INLINE void set_period(const float p); + +public: + ColorInterpolationFunctionSinusoid(void); + ColorInterpolationFunctionSinusoid(const Colorf color_a, const Colorf color_b, const float period); + +protected: + Colorf interpolate(const float t = 0) const; + virtual string get_type(void); + + float _period; +}; + +//////////////////////////////////////////////////////////////////// +// Class : ColorInterpolationSegment +// Description : A single unit of interpolation. The begin and end +// times are interpolated over the lifetime of the +// particle, thus have the range of [0,1]. Each segment +// also has a function associated with it. +//////////////////////////////////////////////////////////////////// + +class ColorInterpolationSegment : public ReferenceCount { +PUBLISHED: + ColorInterpolationSegment(ColorInterpolationFunction* function, const float &time_begin, const float &time_end, const int id); + ColorInterpolationSegment(const ColorInterpolationSegment &s); + virtual ~ColorInterpolationSegment(void); + + INLINE ColorInterpolationFunction* get_function(void) const; + INLINE float get_time_begin(void) const; + INLINE float get_time_end(void) const; + INLINE int get_id(void) const; + + INLINE void set_function(ColorInterpolationFunction* function); + INLINE void set_time_begin(const float time); + INLINE void set_time_end(const float time); + +public: + Colorf interpolateColor(const float t) const; + +protected: + PT(ColorInterpolationFunction) _color_inter_func; + float _t_begin; + float _t_end; + float _t_total; + const int _id; +}; + +//////////////////////////////////////////////////////////////////// +// Class : ColorInterpolationManager +// Description : High level class for color interpolation. Segments +// must be added to the manager in order to achieve +// results using the "add_*****()" functions. Access +// to these segments is provided but not necessary +// general use. +//////////////////////////////////////////////////////////////////// + +class ColorInterpolationManager : public ReferenceCount { +PUBLISHED: +ColorInterpolationManager(void); + ColorInterpolationManager(const Colorf &c); + ColorInterpolationManager(const ColorInterpolationManager& copy); + virtual ~ColorInterpolationManager(void); + + int add_constant(const float time_begin = 0.0f, const float time_end = 1.0f, const Colorf color = Colorf(1.0f,1.0f,1.0f,1.0f)); + int add_linear(const float time_begin = 0.0f, const float time_end = 1.0f, const Colorf color_a = Colorf(1.0f,0.0f,0.0f,1.0f), const Colorf color_b = Colorf(0.0f,1.0f,0.0f,1.0f)); + int add_stepwave(const float time_begin = 0.0f, const float time_end = 1.0f, const Colorf color_a = Colorf(1.0f,0.0f,0.0f,1.0f), const Colorf color_b = Colorf(0.0f,1.0f,0.0f,1.0f), const float width_a = 0.5f, const float width_b = 0.5f); + int add_sinusoid(const float time_begin = 0.0f, const float time_end = 1.0f, const Colorf color_a = Colorf(1.0f,0.0f,0.0f,1.0f), const Colorf color_b = Colorf(0.0f,1.0f,0.0f,1.0f), const float period = 1.0f); + + static ColorInterpolationFunctionConstant* downcast_function_to_constant(ColorInterpolationFunction* f); + static ColorInterpolationFunctionLinear* downcast_function_to_linear(ColorInterpolationFunction* f); + static ColorInterpolationFunctionStepwave* downcast_function_to_stepwave(ColorInterpolationFunction* f); + static ColorInterpolationFunctionSinusoid* downcast_function_to_sinusoid(ColorInterpolationFunction* f); + + INLINE ColorInterpolationSegment* get_segment(const int seg_id); + INLINE string get_segment_id_list(void); + void clear_segment(const int seg_id); + void clear_to_initial(void); + +public: + Colorf generateColor(const float interpolated_time); + +private: + Colorf _default_color; + pvector _i_segs; + int _id_generator; +}; + +#include "colorInterpolationManager.I" + +#endif //COLORINTERPOLATIONMANAGER_H