Panda3D
 All Classes Functions Variables Enumerations
colorInterpolationManager.cxx
1 // Filename: colorInterpolationManager.cxx
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 #include "colorInterpolationManager.h"
15 #include "mathNumbers.h"
16 
17 TypeHandle ColorInterpolationFunction::_type_handle;
18 TypeHandle ColorInterpolationFunctionConstant::_type_handle;
19 TypeHandle ColorInterpolationFunctionLinear::_type_handle;
20 TypeHandle ColorInterpolationFunctionStepwave::_type_handle;
21 TypeHandle ColorInterpolationFunctionSinusoid::_type_handle;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Function : ColorInterpolationFunction::ColorInterpolationFunction
25 // Access : public
26 // Description : constructor
27 ////////////////////////////////////////////////////////////////////
28 
31 }
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function : ColorInterpolationFunction::~ColorInterpolationFunction
35 // Access : public
36 // Description : destructor
37 ////////////////////////////////////////////////////////////////////
38 
41 }
42 
43 ////////////////////////////////////////////////////////////////////
44 // Function : ColorInterpolationFunctionConstant::ColorInterpolationFunctionConstant
45 // Access : public
46 // Description : default constructor
47 ////////////////////////////////////////////////////////////////////
48 
51  _c_a(1.0f,1.0f,1.0f,1.0f) {
52 }
53 
54 ////////////////////////////////////////////////////////////////////
55 // Function : ColorInterpolationFunctionConstant::ColorInterpolationFunctionConstant
56 // Access : public
57 // Description : constructor
58 ////////////////////////////////////////////////////////////////////
59 
62  _c_a(color_a) {
63 }
64 
65 ////////////////////////////////////////////////////////////////////
66 // Function : ColorInterpolationFunctionConstant::interpolate
67 // Access : protected
68 // Description : Returns the color associated with this instance.
69 ////////////////////////////////////////////////////////////////////
70 
71 LColor ColorInterpolationFunctionConstant::
72 interpolate(const PN_stdfloat t) const {
73  return _c_a;
74 }
75 
76 ////////////////////////////////////////////////////////////////////
77 // Function : ColorInterpolationFunctionLinear::ColorInterpolationFunctionLinear
78 // Access : public
79 // Description : default constructor
80 ////////////////////////////////////////////////////////////////////
81 
84  _c_b(1.0f,1.0f,1.0f,1.0f) {
85 }
86 
87 ////////////////////////////////////////////////////////////////////
88 // Function : ColorInterpolationFunctionLinear::ColorInterpolationFunctionLinear
89 // Access : public
90 // Description : constructor
91 ////////////////////////////////////////////////////////////////////
92 
95  const LColor &color_b) :
97  _c_b(color_b) {
98 }
99 
100 ////////////////////////////////////////////////////////////////////
101 // Function : ColorInterpolationFunctionLinear::interpolate
102 // Access : protected
103 // Description : Returns the linear mixture of A and B according to 't'.
104 ////////////////////////////////////////////////////////////////////
105 
106 LColor ColorInterpolationFunctionLinear::
107 interpolate(const PN_stdfloat t) const {
108  return (1.0f-t)*_c_a + t*_c_b;
109 }
110 
111 ////////////////////////////////////////////////////////////////////
112 // Function : ColorInterpolationFunctionStepwave::ColorInterpolationFunctionStepwave
113 // Access : public
114 // Description : default constructor
115 ////////////////////////////////////////////////////////////////////
116 
119  _w_a(0.5f),
120  _w_b(0.5f) {
121 }
122 
123 ////////////////////////////////////////////////////////////////////
124 // Function : ColorInterpolationFunctionStepwave::ColorInterpolationFunctionStepwave
125 // Access : public
126 // Description : constructor
127 ////////////////////////////////////////////////////////////////////
128 
131  const LColor &color_b,
132  const PN_stdfloat width_a,
133  const PN_stdfloat width_b) :
134  ColorInterpolationFunctionLinear(color_a,color_b),
135  _w_a(width_a),
136  _w_b(width_b) {
137 }
138 
139 ////////////////////////////////////////////////////////////////////
140 // Function : ColorInterpolationFunctionStepwave::interpolate
141 // Access : protected
142 // Description : Returns either A or B.
143 ////////////////////////////////////////////////////////////////////
144 
145 LColor ColorInterpolationFunctionStepwave::
146 interpolate(const PN_stdfloat t) const {
147  if(fmodf(t,(_w_a+_w_b))<_w_a) {
148  return _c_a;
149  }
150  return _c_b;
151 }
152 
153 ////////////////////////////////////////////////////////////////////
154 // Function : ColorInterpolationFunctionSinusoid::ColorInterpolationFunctionSinusoid
155 // Access : public
156 // Description : default constructor
157 ////////////////////////////////////////////////////////////////////
158 
161  _period(1.0f) {
162 }
163 
164 ////////////////////////////////////////////////////////////////////
165 // Function : ColorInterpolationFunctionSinusoid::ColorInterpolationFunctionSinusoid
166 // Access : public
167 // Description : constructor
168 ////////////////////////////////////////////////////////////////////
169 
172  const LColor &color_b,
173  const PN_stdfloat period) :
174  ColorInterpolationFunctionLinear(color_a,color_b),
175  _period(period) {
176 }
177 
178 ////////////////////////////////////////////////////////////////////
179 // Function : ColorInterpolationFunctionSinusoid::interpolate
180 // Access : protected
181 // Description : Returns a sinusoidal blended color between A and B.
182 // Period defines the time it will take to return to
183 // A.
184 ////////////////////////////////////////////////////////////////////
185 
186 LColor ColorInterpolationFunctionSinusoid::
187 interpolate(const PN_stdfloat t) const {
188  PN_stdfloat weight_a = (1.0f+cos(t*MathNumbers::pi_f*2.0f/_period))/2.0f;
189  return (weight_a*_c_a)+((1.0f-weight_a)*_c_b);
190 }
191 
192 ////////////////////////////////////////////////////////////////////
193 // Function : ColorInterpolationSegment::ColorInterpolationSegment
194 // Access : public
195 // Description : constructor
196 ////////////////////////////////////////////////////////////////////
197 
200  const PN_stdfloat &time_begin,
201  const PN_stdfloat &time_end,
202  const bool is_modulated,
203  const int id) :
204  _color_inter_func(function),
205  _t_begin(time_begin),
206  _t_end(time_end),
207  _t_total(time_end-time_begin),
208  _is_modulated(is_modulated),
209  _enabled(true),
210  _id(id) {
211 }
212 
213 ////////////////////////////////////////////////////////////////////
214 // Function : ColorInterpolationSegment::ColorInterpolationSegment
215 // Access : public
216 // Description : copy constructor
217 ////////////////////////////////////////////////////////////////////
218 
221  _color_inter_func(copy._color_inter_func),
222  _t_begin(copy._t_begin),
223  _t_end(copy._t_end),
224  _t_total(copy._t_total),
225  _is_modulated(copy._is_modulated),
226  _enabled(copy._enabled),
227  _id(copy._id) {
228 }
229 
230 ////////////////////////////////////////////////////////////////////
231 // Function : ColorInterpolationSegment::~ColorInterpolationSegment
232 // Access : public
233 // Description : destructor
234 ////////////////////////////////////////////////////////////////////
235 
238 }
239 
240 ////////////////////////////////////////////////////////////////////
241 // Function : ColorInterpolationSegment::interpolateColor
242 // Access : public
243 // Description : Returns the interpolated color according to the
244 // segment's function and start and end times. 't' is
245 // a value in [0-1] where corresponds to beginning of
246 // the segment and 1 corresponds to the end.
247 ////////////////////////////////////////////////////////////////////
248 
250 interpolateColor(const PN_stdfloat t) const {
251  return _color_inter_func->interpolate((t-_t_begin)/_t_total);
252 }
253 
254 ////////////////////////////////////////////////////////////////////
255 // Function : ColorInterpolationManager::ColorInterpolationManager
256 // Access : public
257 // Description : default constructor
258 ////////////////////////////////////////////////////////////////////
259 
262  _default_color(LColor(1.0f,1.0f,1.0f,1.0f)),
263  _id_generator(0) {
264 }
265 
266 ////////////////////////////////////////////////////////////////////
267 // Function : ColorInterpolationManager::ColorInterpolationManager
268 // Access : public
269 // Description : constructor
270 ////////////////////////////////////////////////////////////////////
271 
274  _default_color(c),
275  _id_generator(0) {
276 }
277 
278 ////////////////////////////////////////////////////////////////////
279 // Function : ColorInterpolationManager::ColorInterpolationManager
280 // Access : public
281 // Description : copy constructor
282 ////////////////////////////////////////////////////////////////////
283 
286  _default_color(copy._default_color),
287  _i_segs(copy._i_segs),
288  _id_generator(copy._id_generator) {
289 }
290 
291 ////////////////////////////////////////////////////////////////////
292 // Function : ColorInterpolationManager::~ColorInterpolationManager
293 // Access : public
294 // Description : destructor
295 ////////////////////////////////////////////////////////////////////
296 
299 }
300 
301 ////////////////////////////////////////////////////////////////////
302 // Function : ColorInterpolationManager::add_constant
303 // Access : public
304 // Description : Adds a constant segment of the specified color to the
305 // manager and returns the segment's id as known
306 // by the manager.
307 ////////////////////////////////////////////////////////////////////
308 
310 add_constant(const PN_stdfloat time_begin, const PN_stdfloat time_end, const LColor &color, const bool is_modulated) {
312  PT(ColorInterpolationSegment) sPtr = new ColorInterpolationSegment(fPtr,time_begin,time_end,is_modulated,_id_generator);
313 
314  _i_segs.push_back(sPtr);
315 
316  return _id_generator++;
317 }
318 
319 ////////////////////////////////////////////////////////////////////
320 // Function : ColorInterpolationManager::add_linear
321 // Access : public
322 // Description : Adds a linear segment between two colors to the manager
323 // and returns the segment's id as known by the manager.
324 ////////////////////////////////////////////////////////////////////
325 
327 add_linear(const PN_stdfloat time_begin, const PN_stdfloat time_end, const LColor &color_a, const LColor &color_b, const bool is_modulated) {
329  PT(ColorInterpolationSegment) sPtr = new ColorInterpolationSegment(fPtr,time_begin,time_end,is_modulated,_id_generator);
330 
331  _i_segs.push_back(sPtr);
332 
333  return _id_generator++;
334 }
335 
336 ////////////////////////////////////////////////////////////////////
337 // Function : ColorInterpolationManager::add_stepwave
338 // Access : public
339 // Description : Adds a stepwave segment of two colors to the manager
340 // and returns the segment's id as known by the manager.
341 ////////////////////////////////////////////////////////////////////
342 
344 add_stepwave(const PN_stdfloat time_begin, const PN_stdfloat time_end, const LColor &color_a, const LColor &color_b, const PN_stdfloat width_a, const PN_stdfloat width_b,const bool is_modulated) {
345  PT(ColorInterpolationFunctionStepwave) fPtr = new ColorInterpolationFunctionStepwave(color_a, color_b, width_a, width_b);
346  PT(ColorInterpolationSegment) sPtr = new ColorInterpolationSegment(fPtr,time_begin,time_end,is_modulated,_id_generator);
347 
348  _i_segs.push_back(sPtr);
349 
350  return _id_generator++;
351 }
352 
353 ////////////////////////////////////////////////////////////////////
354 // Function : ColorInterpolationManager::add_sinusoid
355 // Access : public
356 // Description : Adds a stepwave segment of two colors and a specified
357 // period to the manager and returns the segment's
358 // id as known by the manager.
359 ////////////////////////////////////////////////////////////////////
360 
362 add_sinusoid(const PN_stdfloat time_begin, const PN_stdfloat time_end, const LColor &color_a, const LColor &color_b, const PN_stdfloat period,const bool is_modulated) {
363  PT(ColorInterpolationFunctionSinusoid) fPtr = new ColorInterpolationFunctionSinusoid(color_a, color_b, period);
364  PT(ColorInterpolationSegment) sPtr = new ColorInterpolationSegment(fPtr,time_begin,time_end,is_modulated,_id_generator);
365 
366  _i_segs.push_back(sPtr);
367 
368  return _id_generator++;
369 }
370 
371 ////////////////////////////////////////////////////////////////////
372 // Function : ColorInterpolationManager::clear_segment
373 // Access : public
374 // Description : Removes the segment of 'id' from the manager.
375 ////////////////////////////////////////////////////////////////////
376 
378 clear_segment(const int seg_id) {
380 
381  for(iter = _i_segs.begin();iter != _i_segs.end();++iter) {
382  if( seg_id == (*iter)->get_id() ) {
383  _i_segs.erase(iter);
384  return;
385  }
386  }
387 }
388 
389 ////////////////////////////////////////////////////////////////////
390 // Function : ColorInterpolationManager::clear_to_initial
391 // Access : public
392 // Description : Removes all segments from the manager.
393 ////////////////////////////////////////////////////////////////////
394 
397  _i_segs.clear();
398  _id_generator = 0;
399 }
400 
401 ////////////////////////////////////////////////////////////////////
402 // Function : ColorInterpolationManager::
403 // Access : public
404 // Description : For time 'interpolated_time', this returns the
405 // additive composite color of all segments that influence
406 // that instant in the particle's lifetime. If no segments
407 // cover that time, the manager's default color is returned.
408 ////////////////////////////////////////////////////////////////////
409 
411 generateColor(const PN_stdfloat interpolated_time) {
412  bool segment_found = false;
413  LColor out(_default_color);
414  ColorInterpolationSegment *cur_seg;
416 
417  for (iter = _i_segs.begin();iter != _i_segs.end();++iter) {
418  cur_seg = (*iter);
419  if( cur_seg->is_enabled() &&
420  interpolated_time >= cur_seg->get_time_begin()
421  && interpolated_time <= cur_seg->get_time_end() ) {
422  segment_found = true;
423  LColor cur_color = cur_seg->interpolateColor(interpolated_time);
424  if( cur_seg->is_modulated() ) {
425  out[0] *= cur_color[0];
426  out[1] *= cur_color[1];
427  out[2] *= cur_color[2];
428  out[3] *= cur_color[3];
429  }
430  else {
431  out[0] += cur_color[0];
432  out[1] += cur_color[1];
433  out[2] += cur_color[2];
434  out[3] += cur_color[3];
435  }
436  }
437  }
438 
439  if(segment_found) {
440  out[0] = max((PN_stdfloat)0.0, min(out[0], (PN_stdfloat)1.0));
441  out[1] = max((PN_stdfloat)0.0, min(out[1], (PN_stdfloat)1.0));
442  out[2] = max((PN_stdfloat)0.0, min(out[2], (PN_stdfloat)1.0));
443  out[3] = max((PN_stdfloat)0.0, min(out[3], (PN_stdfloat)1.0));
444  return out;
445  }
446 
447  return _default_color;
448 }
PN_stdfloat get_time_begin() const
Returns the point in the particle&#39;s lifetime at which this segment begins its effect.
PN_stdfloat get_time_end() const
Returns the point in the particle&#39;s lifetime at which this segment&#39;s effect stops.
virtual ~ColorInterpolationSegment()
destructor
ColorInterpolationManager()
default constructor
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)
Adds a stepwave segment of two colors and a specified period to the manager and returns the segment&#39;s...
bool is_modulated() const
Returns whether the function is additive or modulated.
A single unit of interpolation.
ColorInterpolationFunctionConstant()
default constructor
void clear_segment(const int seg_id)
Removes the segment of &#39;id&#39; from the manager.
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)
Adds a constant segment of the specified color to the manager and returns the segment&#39;s id as known b...
virtual ~ColorInterpolationManager()
destructor
ColorInterpolationSegment(ColorInterpolationFunction *function, const PN_stdfloat &time_begin, const PN_stdfloat &time_end, const bool is_modulated, const int id)
constructor
ColorInterpolationFunctionLinear()
default constructor
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
void clear_to_initial()
Removes all segments from the manager.
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)
Adds a linear segment between two colors to the manager and returns the segment&#39;s id as known by the ...
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
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)
Adds a stepwave segment of two colors to the manager and returns the segment&#39;s id as known by the man...
LColor generateColor(const PN_stdfloat interpolated_time)
For time &#39;interpolated_time&#39;, this returns the additive composite color of all segments that influenc...
LColor interpolateColor(const PN_stdfloat t) const
Returns the interpolated color according to the segment&#39;s function and start and end times...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
virtual ~ColorInterpolationFunction()
destructor
Defines a constant color over the lifetime of the segment.
Defines a linear interpolation over the lifetime of the segment.
bool is_enabled() const
Returns whether the segments effects are being applied.