Panda3D
|
00001 // Filename: colorInterpolationManager.h 00002 // Created by: joswilso (02Jun05) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #ifndef COLORINTERPOLATIONMANAGER_H 00016 #define COLORINTERPOLATIONMANAGER_H 00017 00018 #include "luse.h" 00019 #include "pvector.h" 00020 #include "typedObject.h" 00021 #include "typedReferenceCount.h" 00022 00023 //////////////////////////////////////////////////////////////////// 00024 // Class : ColorInterpolationFunction 00025 // Description : Abstract class from which all other functions 00026 // should inherit. Defines the virtual interpolate() 00027 // function. 00028 //////////////////////////////////////////////////////////////////// 00029 00030 class ColorInterpolationFunction : public TypedReferenceCount { 00031 PUBLISHED: 00032 // virtual string get_type(); 00033 00034 public: 00035 ColorInterpolationFunction(); 00036 virtual ~ColorInterpolationFunction(); 00037 00038 virtual LColor interpolate(const PN_stdfloat t = 0) const = 0; 00039 00040 public: 00041 static TypeHandle get_class_type() { 00042 return _type_handle; 00043 } 00044 00045 static void init_type() { 00046 TypedReferenceCount::init_type(); 00047 register_type(_type_handle, "ColorInterpolationFunction", 00048 TypedReferenceCount::get_class_type()); 00049 } 00050 00051 virtual TypeHandle get_type() const { 00052 return get_class_type(); 00053 } 00054 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00055 private: 00056 static TypeHandle _type_handle; 00057 }; 00058 00059 //////////////////////////////////////////////////////////////////// 00060 // Class : ColorInterpolationFunctionConstant 00061 // Description : Defines a constant color over the lifetime of 00062 // the segment. 00063 //////////////////////////////////////////////////////////////////// 00064 00065 class ColorInterpolationFunctionConstant : public ColorInterpolationFunction { 00066 PUBLISHED: 00067 INLINE LColor get_color_a() const; 00068 00069 INLINE void set_color_a(const LColor &c); 00070 00071 public: 00072 ColorInterpolationFunctionConstant(); 00073 ColorInterpolationFunctionConstant(const LColor &color_a); 00074 00075 protected: 00076 virtual LColor interpolate(const PN_stdfloat t = 0) const; 00077 // virtual string get_type(); 00078 00079 LColor _c_a; 00080 00081 public: 00082 static TypeHandle get_class_type() { 00083 return _type_handle; 00084 } 00085 00086 static void init_type() { 00087 ColorInterpolationFunction::init_type(); 00088 register_type(_type_handle, "ColorInterpolationFunctionConstant", 00089 ColorInterpolationFunction::get_class_type()); 00090 } 00091 00092 virtual TypeHandle get_type() const { 00093 return get_class_type(); 00094 } 00095 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00096 private: 00097 static TypeHandle _type_handle; 00098 }; 00099 00100 //////////////////////////////////////////////////////////////////// 00101 // Class : ColorInterpolationFunctionLinear 00102 // Description : Defines a linear interpolation over the lifetime of 00103 // the segment. 00104 //////////////////////////////////////////////////////////////////// 00105 00106 class ColorInterpolationFunctionLinear : public ColorInterpolationFunctionConstant { 00107 PUBLISHED: 00108 INLINE LColor get_color_b() const; 00109 00110 INLINE void set_color_b(const LColor &c); 00111 00112 public: 00113 ColorInterpolationFunctionLinear(); 00114 ColorInterpolationFunctionLinear(const LColor &color_a, const LColor &color_b); 00115 00116 protected: 00117 LColor interpolate(const PN_stdfloat t = 0) const; 00118 // virtual string get_type(); 00119 00120 LColor _c_b; 00121 00122 public: 00123 static TypeHandle get_class_type() { 00124 return _type_handle; 00125 } 00126 00127 static void init_type() { 00128 ColorInterpolationFunctionConstant::init_type(); 00129 register_type(_type_handle, "ColorInterpolationFunctionLinear", 00130 ColorInterpolationFunctionConstant::get_class_type()); 00131 } 00132 00133 virtual TypeHandle get_type() const { 00134 return get_class_type(); 00135 } 00136 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00137 private: 00138 static TypeHandle _type_handle; 00139 }; 00140 00141 //////////////////////////////////////////////////////////////////// 00142 // Class : ColorInterpolationFunctionStepwave 00143 // Description : Defines a discrete cyclical transition between two colors. 00144 // The widths describe a portion of the segment's lifetime 00145 // for which the corresponding color should be selected. If 00146 // their sum is less than 1, the function repeats until 00147 // the end of the segment. 00148 //////////////////////////////////////////////////////////////////// 00149 00150 class ColorInterpolationFunctionStepwave : public ColorInterpolationFunctionLinear { 00151 PUBLISHED: 00152 INLINE PN_stdfloat get_width_a() const; 00153 INLINE PN_stdfloat get_width_b() const; 00154 00155 INLINE void set_width_a(const PN_stdfloat w); 00156 INLINE void set_width_b(const PN_stdfloat w); 00157 00158 public: 00159 ColorInterpolationFunctionStepwave(); 00160 ColorInterpolationFunctionStepwave(const LColor &color_a, const LColor &color_b, const PN_stdfloat width_a, const PN_stdfloat width_b); 00161 00162 protected: 00163 LColor interpolate(const PN_stdfloat t = 0) const; 00164 // virtual string get_type(); 00165 00166 PN_stdfloat _w_a; 00167 PN_stdfloat _w_b; 00168 00169 public: 00170 static TypeHandle get_class_type() { 00171 return _type_handle; 00172 } 00173 00174 static void init_type() { 00175 ColorInterpolationFunctionLinear::init_type(); 00176 register_type(_type_handle, "ColorInterpolationFunctionStepwave", 00177 ColorInterpolationFunctionLinear::get_class_type()); 00178 } 00179 00180 virtual TypeHandle get_type() const { 00181 return get_class_type(); 00182 } 00183 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00184 private: 00185 static TypeHandle _type_handle; 00186 }; 00187 00188 //////////////////////////////////////////////////////////////////// 00189 // Class : ColorInterpolationFunctionSinusoid 00190 // Description : Defines a sinusoidal blending between two colors. 00191 // A period of "1" corresponds to a single transition 00192 // from color_a to color_b and then back to color_a 00193 // over the course of the segment's lifetime. A 00194 // shorter period will result in a higher frequency 00195 // cycle. 00196 //////////////////////////////////////////////////////////////////// 00197 00198 class ColorInterpolationFunctionSinusoid : public ColorInterpolationFunctionLinear { 00199 PUBLISHED: 00200 INLINE PN_stdfloat get_period() const; 00201 00202 INLINE void set_period(const PN_stdfloat p); 00203 00204 public: 00205 ColorInterpolationFunctionSinusoid(); 00206 ColorInterpolationFunctionSinusoid(const LColor &color_a, const LColor &color_b, const PN_stdfloat period); 00207 00208 protected: 00209 LColor interpolate(const PN_stdfloat t = 0) const; 00210 // virtual string get_type(); 00211 00212 PN_stdfloat _period; 00213 00214 public: 00215 static TypeHandle get_class_type() { 00216 return _type_handle; 00217 } 00218 00219 static void init_type() { 00220 ColorInterpolationFunctionLinear::init_type(); 00221 register_type(_type_handle, "ColorInterpolationFunctionSinusoid", 00222 ColorInterpolationFunctionLinear::get_class_type()); 00223 } 00224 00225 virtual TypeHandle get_type() const { 00226 return get_class_type(); 00227 } 00228 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00229 private: 00230 static TypeHandle _type_handle; 00231 }; 00232 00233 //////////////////////////////////////////////////////////////////// 00234 // Class : ColorInterpolationSegment 00235 // Description : A single unit of interpolation. The begin and end 00236 // times are interpolated over the lifetime of the 00237 // particle, thus have the range of [0,1]. Each segment 00238 // also has a function associated with it. 00239 //////////////////////////////////////////////////////////////////// 00240 00241 class ColorInterpolationSegment : public ReferenceCount { 00242 PUBLISHED: 00243 ColorInterpolationSegment(ColorInterpolationFunction* function, const PN_stdfloat &time_begin, const PN_stdfloat &time_end, const bool is_modulated, const int id); 00244 ColorInterpolationSegment(const ColorInterpolationSegment &s); 00245 virtual ~ColorInterpolationSegment(); 00246 00247 // INLINE ColorInterpolationFunction* get_function() const; 00248 INLINE TypedReferenceCount* get_function() const; 00249 INLINE PN_stdfloat get_time_begin() const; 00250 INLINE PN_stdfloat get_time_end() const; 00251 INLINE bool is_modulated() const; 00252 INLINE int get_id() const; 00253 INLINE bool is_enabled() const; 00254 00255 INLINE void set_function(ColorInterpolationFunction* function); 00256 INLINE void set_time_begin(const PN_stdfloat time); 00257 INLINE void set_time_end(const PN_stdfloat time); 00258 INLINE void set_is_modulated(const bool flag); 00259 INLINE void set_enabled(const bool enabled); 00260 00261 public: 00262 LColor interpolateColor(const PN_stdfloat t) const; 00263 00264 protected: 00265 PT(ColorInterpolationFunction) _color_inter_func; 00266 PN_stdfloat _t_begin; 00267 PN_stdfloat _t_end; 00268 PN_stdfloat _t_total; 00269 bool _is_modulated; 00270 bool _enabled; 00271 const int _id; 00272 }; 00273 00274 //////////////////////////////////////////////////////////////////// 00275 // Class : ColorInterpolationManager 00276 // Description : High level class for color interpolation. Segments 00277 // must be added to the manager in order to achieve 00278 // results using the "add_*****()" functions. Access 00279 // to these segments is provided but not necessary 00280 // general use. 00281 //////////////////////////////////////////////////////////////////// 00282 00283 class ColorInterpolationManager : public ReferenceCount { 00284 PUBLISHED: 00285 ColorInterpolationManager(); 00286 ColorInterpolationManager(const LColor &c); 00287 ColorInterpolationManager(const ColorInterpolationManager& copy); 00288 virtual ~ColorInterpolationManager(); 00289 00290 int add_constant(const PN_stdfloat time_begin = 0.0f, const PN_stdfloat time_end = 1.0f, const LColor &color = LColor(1.0f,1.0f,1.0f,1.0f), const bool is_modulated = true); 00291 int add_linear(const PN_stdfloat time_begin = 0.0f, const PN_stdfloat time_end = 1.0f, const LColor &color_a = LColor(1.0f,0.0f,0.0f,1.0f), const LColor &color_b = LColor(0.0f,1.0f,0.0f,1.0f), const bool is_modulated = true); 00292 int add_stepwave(const PN_stdfloat time_begin = 0.0f, const PN_stdfloat time_end = 1.0f, const LColor &color_a = LColor(1.0f,0.0f,0.0f,1.0f), const LColor &color_b = LColor(0.0f,1.0f,0.0f,1.0f), const PN_stdfloat width_a = 0.5f, const PN_stdfloat width_b = 0.5f, const bool is_modulated = true); 00293 int add_sinusoid(const PN_stdfloat time_begin = 0.0f, const PN_stdfloat time_end = 1.0f, const LColor &color_a = LColor(1.0f,0.0f,0.0f,1.0f), const LColor &color_b = LColor(0.0f,1.0f,0.0f,1.0f), const PN_stdfloat period = 1.0f, const bool is_modulated = true); 00294 00295 INLINE void set_default_color(const LColor &c); 00296 INLINE ColorInterpolationSegment* get_segment(const int seg_id); 00297 INLINE string get_segment_id_list(); 00298 void clear_segment(const int seg_id); 00299 void clear_to_initial(); 00300 00301 public: 00302 LColor generateColor(const PN_stdfloat interpolated_time); 00303 00304 private: 00305 LColor _default_color; 00306 pvector<PT(ColorInterpolationSegment)> _i_segs; 00307 int _id_generator; 00308 }; 00309 00310 #include "colorInterpolationManager.I" 00311 00312 #endif //COLORINTERPOLATIONMANAGER_H