Panda3D
colorInterpolationManager.h
1 // Filename: colorInterpolationManager.h
2 // Created by: joswilso (02Jun05)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef COLORINTERPOLATIONMANAGER_H
16 #define COLORINTERPOLATIONMANAGER_H
17 
18 #include "luse.h"
19 #include "pvector.h"
20 #include "typedObject.h"
21 #include "typedReferenceCount.h"
22 
23 ////////////////////////////////////////////////////////////////////
24 // Class : ColorInterpolationFunction
25 // Description : Abstract class from which all other functions
26 // should inherit. Defines the virtual interpolate()
27 // function.
28 ////////////////////////////////////////////////////////////////////
29 
30 class EXPCL_PANDAPHYSICS ColorInterpolationFunction : public TypedReferenceCount {
31 PUBLISHED:
32 // virtual string get_type();
33 
34 public:
36  virtual ~ColorInterpolationFunction();
37 
38  virtual LColor interpolate(const PN_stdfloat t = 0) const = 0;
39 
40 public:
41  static TypeHandle get_class_type() {
42  return _type_handle;
43  }
44 
45  static void init_type() {
46  TypedReferenceCount::init_type();
47  register_type(_type_handle, "ColorInterpolationFunction",
48  TypedReferenceCount::get_class_type());
49  }
50 
51  virtual TypeHandle get_type() const {
52  return get_class_type();
53  }
54  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
55 private:
56  static TypeHandle _type_handle;
57 };
58 
59 ////////////////////////////////////////////////////////////////////
60 // Class : ColorInterpolationFunctionConstant
61 // Description : Defines a constant color over the lifetime of
62 // the segment.
63 ////////////////////////////////////////////////////////////////////
64 
66 PUBLISHED:
67  INLINE LColor get_color_a() const;
68 
69  INLINE void set_color_a(const LColor &c);
70 
71 public:
74 
75 protected:
76  virtual LColor interpolate(const PN_stdfloat t = 0) const;
77  // virtual string get_type();
78 
79  LColor _c_a;
80 
81 public:
82  static TypeHandle get_class_type() {
83  return _type_handle;
84  }
85 
86  static void init_type() {
87  ColorInterpolationFunction::init_type();
88  register_type(_type_handle, "ColorInterpolationFunctionConstant",
89  ColorInterpolationFunction::get_class_type());
90  }
91 
92  virtual TypeHandle get_type() const {
93  return get_class_type();
94  }
95  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
96 private:
97  static TypeHandle _type_handle;
98 };
99 
100 ////////////////////////////////////////////////////////////////////
101 // Class : ColorInterpolationFunctionLinear
102 // Description : Defines a linear interpolation over the lifetime of
103 // the segment.
104 ////////////////////////////////////////////////////////////////////
105 
107 PUBLISHED:
108  INLINE LColor get_color_b() const;
109 
110  INLINE void set_color_b(const LColor &c);
111 
112 public:
114  ColorInterpolationFunctionLinear(const LColor &color_a, const LColor &color_b);
115 
116 protected:
117  LColor interpolate(const PN_stdfloat t = 0) const;
118  // virtual string get_type();
119 
120  LColor _c_b;
121 
122 public:
123  static TypeHandle get_class_type() {
124  return _type_handle;
125  }
126 
127  static void init_type() {
128  ColorInterpolationFunctionConstant::init_type();
129  register_type(_type_handle, "ColorInterpolationFunctionLinear",
130  ColorInterpolationFunctionConstant::get_class_type());
131  }
132 
133  virtual TypeHandle get_type() const {
134  return get_class_type();
135  }
136  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
137 private:
138  static TypeHandle _type_handle;
139 };
140 
141 ////////////////////////////////////////////////////////////////////
142 // Class : ColorInterpolationFunctionStepwave
143 // Description : Defines a discrete cyclical transition between two colors.
144 // The widths describe a portion of the segment's lifetime
145 // for which the corresponding color should be selected. If
146 // their sum is less than 1, the function repeats until
147 // the end of the segment.
148 ////////////////////////////////////////////////////////////////////
149 
151 PUBLISHED:
152  INLINE PN_stdfloat get_width_a() const;
153  INLINE PN_stdfloat get_width_b() const;
154 
155  INLINE void set_width_a(const PN_stdfloat w);
156  INLINE void set_width_b(const PN_stdfloat w);
157 
158 public:
160  ColorInterpolationFunctionStepwave(const LColor &color_a, const LColor &color_b, const PN_stdfloat width_a, const PN_stdfloat width_b);
161 
162 protected:
163  LColor interpolate(const PN_stdfloat t = 0) const;
164  // virtual string get_type();
165 
166  PN_stdfloat _w_a;
167  PN_stdfloat _w_b;
168 
169 public:
170  static TypeHandle get_class_type() {
171  return _type_handle;
172  }
173 
174  static void init_type() {
175  ColorInterpolationFunctionLinear::init_type();
176  register_type(_type_handle, "ColorInterpolationFunctionStepwave",
177  ColorInterpolationFunctionLinear::get_class_type());
178  }
179 
180  virtual TypeHandle get_type() const {
181  return get_class_type();
182  }
183  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
184 private:
185  static TypeHandle _type_handle;
186 };
187 
188 ////////////////////////////////////////////////////////////////////
189 // Class : ColorInterpolationFunctionSinusoid
190 // Description : Defines a sinusoidal blending between two colors.
191 // A period of "1" corresponds to a single transition
192 // from color_a to color_b and then back to color_a
193 // over the course of the segment's lifetime. A
194 // shorter period will result in a higher frequency
195 // cycle.
196 ////////////////////////////////////////////////////////////////////
197 
199 PUBLISHED:
200  INLINE PN_stdfloat get_period() const;
201 
202  INLINE void set_period(const PN_stdfloat p);
203 
204 public:
206  ColorInterpolationFunctionSinusoid(const LColor &color_a, const LColor &color_b, const PN_stdfloat period);
207 
208 protected:
209  LColor interpolate(const PN_stdfloat t = 0) const;
210  // virtual string get_type();
211 
212  PN_stdfloat _period;
213 
214 public:
215  static TypeHandle get_class_type() {
216  return _type_handle;
217  }
218 
219  static void init_type() {
220  ColorInterpolationFunctionLinear::init_type();
221  register_type(_type_handle, "ColorInterpolationFunctionSinusoid",
222  ColorInterpolationFunctionLinear::get_class_type());
223  }
224 
225  virtual TypeHandle get_type() const {
226  return get_class_type();
227  }
228  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
229 private:
230  static TypeHandle _type_handle;
231 };
232 
233 ////////////////////////////////////////////////////////////////////
234 // Class : ColorInterpolationSegment
235 // Description : A single unit of interpolation. The begin and end
236 // times are interpolated over the lifetime of the
237 // particle, thus have the range of [0,1]. Each segment
238 // also has a function associated with it.
239 ////////////////////////////////////////////////////////////////////
240 
241 class EXPCL_PANDAPHYSICS ColorInterpolationSegment : public ReferenceCount {
242 PUBLISHED:
243  ColorInterpolationSegment(ColorInterpolationFunction* function, const PN_stdfloat &time_begin, const PN_stdfloat &time_end, const bool is_modulated, const int id);
245  virtual ~ColorInterpolationSegment();
246 
247  // INLINE ColorInterpolationFunction* get_function() const;
248  INLINE TypedReferenceCount* get_function() const;
249  INLINE PN_stdfloat get_time_begin() const;
250  INLINE PN_stdfloat get_time_end() const;
251  INLINE bool is_modulated() const;
252  INLINE int get_id() const;
253  INLINE bool is_enabled() const;
254 
255  INLINE void set_function(ColorInterpolationFunction* function);
256  INLINE void set_time_begin(const PN_stdfloat time);
257  INLINE void set_time_end(const PN_stdfloat time);
258  INLINE void set_is_modulated(const bool flag);
259  INLINE void set_enabled(const bool enabled);
260 
261 public:
262  LColor interpolateColor(const PN_stdfloat t) const;
263 
264 protected:
265  PT(ColorInterpolationFunction) _color_inter_func;
266  PN_stdfloat _t_begin;
267  PN_stdfloat _t_end;
268  PN_stdfloat _t_total;
269  bool _is_modulated;
270  bool _enabled;
271  const int _id;
272 };
273 
274 ////////////////////////////////////////////////////////////////////
275 // Class : ColorInterpolationManager
276 // Description : High level class for color interpolation. Segments
277 // must be added to the manager in order to achieve
278 // results using the "add_*****()" functions. Access
279 // to these segments is provided but not necessary
280 // general use.
281 ////////////////////////////////////////////////////////////////////
282 
283 class EXPCL_PANDAPHYSICS ColorInterpolationManager : public ReferenceCount {
284 PUBLISHED:
288  virtual ~ColorInterpolationManager();
289 
290  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);
291  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);
292  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);
293  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);
294 
295  INLINE void set_default_color(const LColor &c);
296  INLINE ColorInterpolationSegment* get_segment(const int seg_id);
297  INLINE string get_segment_id_list();
298  void clear_segment(const int seg_id);
299  void clear_to_initial();
300 
301 public:
302  LColor generateColor(const PN_stdfloat interpolated_time);
303 
304 private:
305  LColor _default_color;
307  int _id_generator;
308 };
309 
310 #include "colorInterpolationManager.I"
311 
312 #endif //COLORINTERPOLATIONMANAGER_H
A single unit of interpolation.
A base class for things which need to inherit from both TypedObject and from ReferenceCount.
High level class for color interpolation.
Defines a discrete cyclical transition between two colors.
Defines a sinusoidal blending between two colors.
Abstract class from which all other functions should inherit.
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
A base class for all things that want to be reference-counted.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
Defines a constant color over the lifetime of the segment.
Defines a linear interpolation over the lifetime of the segment.