Panda3D
 All Classes Functions Variables Enumerations
spriteParticleRenderer.I
1 // Filename: spriteParticleRenderer.I
2 // Created by: charles (13Jul00)
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 
16 ////////////////////////////////////////////////////////////////////
17 // Function : SpriteParticleRenderer::set_texture
18 // Access : Published
19 // Description : Sets the renderer up to render the entire texture
20 // image. The scale of each particle is based on the
21 // size of the texture in each dimension, modified by
22 // texels_per_unit.
23 //
24 // Used to set the size of the particles. Will clear
25 // all previously loaded textures and animations.
26 ////////////////////////////////////////////////////////////////////
27 INLINE void SpriteParticleRenderer::
28 set_texture(Texture *tex, PN_stdfloat texels_per_unit) {
29  if (tex != (Texture *)NULL) {
30  // Clear all texture information
31  _anims.clear();
32 
33  // Insert the single texture
34  _anims.push_back(new SpriteAnim(tex,LTexCoord(0.0f, 0.0f),LTexCoord(1.0f, 1.0f)));
35 
36  // We scale the particle size by the size of the texture.
37  set_size(tex->get_x_size() / texels_per_unit,
38  tex->get_y_size() / texels_per_unit);
39  get_last_anim()->set_source_info(tex->get_filename());
40  }
41  init_geoms();
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function : SpriteParticleRenderer::add_texture
46 // Access : Published
47 // Description : Adds texture to image pool, effectively creating a
48 // single frame animation that can be selected at
49 // particle birth. This should only be called after
50 // a previous call to set_texture().
51 ////////////////////////////////////////////////////////////////////
52 INLINE void SpriteParticleRenderer::
53 add_texture(Texture *tex, PN_stdfloat texels_per_unit, bool resize) {
54  if (_anims.size() == 0) {
55  set_texture(tex, texels_per_unit);
56  } else {
57  if(tex != (Texture *)NULL) {
58  if (tex != (Texture *)NULL) {
59  _anims.push_back(new SpriteAnim(tex,LTexCoord(0.0f, 0.0f),LTexCoord(1.0f, 1.0f)));
60  }
61  if(resize) {
62  // We scale the particle size by the size of the texture.
63  set_size(tex->get_x_size() / texels_per_unit,
64  tex->get_y_size() / texels_per_unit);
65  }
66  get_last_anim()->set_source_info(tex->get_filename());
67  init_geoms();
68  }
69  }
70 }
71 
72 ////////////////////////////////////////////////////////////////////
73 // Function : SpriteParticleRenderer::remove_animation
74 // Access : Published
75 // Description : Removes an animation texture set from the renderer.
76 ////////////////////////////////////////////////////////////////////
77 INLINE void SpriteParticleRenderer::
78 remove_animation(const int n) {
79  nassertv(n < (int)_anims.size());
80  int i,j;
81 
82  for (i = 0; i < (int)_anims.size(); ++i) {
83  for (j = 0; j < (int)_anim_size[i]; ++j) {
84  _sprites[i][j]->clear_vertices();
85  }
86  }
87 
88  _anims.erase(_anims.begin()+n);
89  _animation_removed = true;
90  init_geoms();
91 }
92 
93 ////////////////////////////////////////////////////////////////////
94 // Function : SpriteParticleRenderer::set_ll_uv
95 // Access : public
96 // Description : Sets the UV coordinate of the lower-left corner of
97 // all the sprites generated by this renderer. Normally
98 // this is (0, 0), but it might be set to something else
99 // to use only a portion of the texture.
100 ////////////////////////////////////////////////////////////////////
101 INLINE void SpriteParticleRenderer::
102 set_ll_uv(const LTexCoord &ll_uv) {
103  set_ll_uv(ll_uv,0,0);
104 }
105 
106 ////////////////////////////////////////////////////////////////////
107 // Function : SpriteParticleRenderer::set_ll_uv
108 // Access : public
109 // Description : Sets the UV coordinate of the lower-left corner of
110 // all the sprites generated by this renderer. Normally
111 // this is (0, 0), but it might be set to something else
112 // to use only a portion of the texture.
113 ////////////////////////////////////////////////////////////////////
114 INLINE void SpriteParticleRenderer::
115 set_ll_uv(const LTexCoord &ll_uv, const int anim, const int frame) {
116  if(anim < (int)_anims.size() && frame < (int)_anims[anim]->get_num_frames()) {
117  _anims[anim]->set_ll(frame,ll_uv);
118  }
119 }
120 
121 ////////////////////////////////////////////////////////////////////
122 // Function : SpriteParticleRenderer::set_ur_uv
123 // Access : public
124 // Description : Sets the UV coordinate of the upper-right corner of
125 // all the sprites generated by this renderer. Normally
126 // this is (1, 1), but it might be set to something else
127 // to use only a portion of the texture.
128 ////////////////////////////////////////////////////////////////////
129 INLINE void SpriteParticleRenderer::
130 set_ur_uv(const LTexCoord &ur_uv) {
131  set_ur_uv(ur_uv,0,0);
132 }
133 
134 ////////////////////////////////////////////////////////////////////
135 // Function : SpriteParticleRenderer::set_ur_uv
136 // Access : public
137 // Description : Sets the UV coordinate of the upper-right corner of
138 // all the sprites generated by this renderer. Normally
139 // this is (1, 1), but it might be set to something else
140 // to use only a portion of the texture.
141 ////////////////////////////////////////////////////////////////////
142 INLINE void SpriteParticleRenderer::
143 set_ur_uv(const LTexCoord &ur_uv, const int anim, const int frame) {
144  if(anim < (int)_anims.size() && frame < (int)_anims[anim]->get_num_frames()) {
145  _anims[anim]->set_ur(frame,ur_uv);
146  }
147 }
148 
149 ////////////////////////////////////////////////////////////////////
150 // Function : SpriteParticleRenderer::set_size
151 // Access : public
152 // Description : Sets the size of each particle in world units.
153 ////////////////////////////////////////////////////////////////////
154 INLINE void SpriteParticleRenderer::
155 set_size(PN_stdfloat width, PN_stdfloat height) {
156  _width = width;
157  _height = height;
158  init_geoms();
159 }
160 
161 ////////////////////////////////////////////////////////////////////
162 // Function : SpriteParticleRenderer::set_color
163 // Access : public
164 ////////////////////////////////////////////////////////////////////
165 INLINE void SpriteParticleRenderer::
166 set_color(const LColor &color) {
167  _color = color;
168  _color_interpolation_manager->set_default_color(_color);
169 }
170 
171 ////////////////////////////////////////////////////////////////////
172 // Function : SpriteParticleRenderer::set_x_scale_flag
173 // Access : public
174 ////////////////////////////////////////////////////////////////////
175 INLINE void SpriteParticleRenderer::
176 set_x_scale_flag(bool animate_x_ratio) {
177  _animate_x_ratio = animate_x_ratio;
178  init_geoms();
179 }
180 
181 ////////////////////////////////////////////////////////////////////
182 // Function : SpriteParticleRenderer::set_y_scale_flag
183 // Access : public
184 ////////////////////////////////////////////////////////////////////
185 INLINE void SpriteParticleRenderer::
186 set_y_scale_flag(bool animate_y_ratio) {
187  _animate_y_ratio = animate_y_ratio;
188  init_geoms();
189 }
190 
191 ////////////////////////////////////////////////////////////////////
192 // Function : SpriteParticleRenderer::set_anim_angle_flag
193 // Access : public
194 ////////////////////////////////////////////////////////////////////
195 INLINE void SpriteParticleRenderer::
196 set_anim_angle_flag(bool animate_theta) {
197  _animate_theta = animate_theta;
198  init_geoms();
199 }
200 
201 ////////////////////////////////////////////////////////////////////
202 // Function : SpriteParticleRenderer::set_initial_x_scale
203 // Access : public
204 ////////////////////////////////////////////////////////////////////
205 INLINE void SpriteParticleRenderer::
206 set_initial_x_scale(PN_stdfloat initial_x_scale) {
207  _initial_x_scale = initial_x_scale;
208  init_geoms();
209 }
210 
211 ////////////////////////////////////////////////////////////////////
212 // Function : SpriteParticleRenderer::set_final_x_scale
213 // Access : public
214 ////////////////////////////////////////////////////////////////////
215 INLINE void SpriteParticleRenderer::
216 set_final_x_scale(PN_stdfloat final_x_scale) {
217  _final_x_scale = final_x_scale;
218 }
219 
220 ////////////////////////////////////////////////////////////////////
221 // Function : SpriteParticleRenderer::set_initial_y_scale
222 // Access : public
223 ////////////////////////////////////////////////////////////////////
224 INLINE void SpriteParticleRenderer::
225 set_initial_y_scale(PN_stdfloat initial_y_scale) {
226  _initial_y_scale = initial_y_scale;
227  init_geoms();
228 }
229 
230 ////////////////////////////////////////////////////////////////////
231 // Function : SpriteParticleRenderer::set_final_y_scale
232 // Access : public
233 ////////////////////////////////////////////////////////////////////
234 INLINE void SpriteParticleRenderer::
235 set_final_y_scale(PN_stdfloat final_y_scale) {
236  _final_y_scale = final_y_scale;
237 }
238 
239 ////////////////////////////////////////////////////////////////////
240 // Function : SpriteParticleRenderer::set_nonanimated_theta
241 // Access : public
242 ////////////////////////////////////////////////////////////////////
243 INLINE void SpriteParticleRenderer::
244 set_nonanimated_theta(PN_stdfloat theta) {
245  _theta = theta;
246  init_geoms();
247 }
248 
249 ////////////////////////////////////////////////////////////////////
250 // Function : SpriteParticleRenderer::set_alpha_blend_method
251 // Access : public
252 ////////////////////////////////////////////////////////////////////
253 INLINE void SpriteParticleRenderer::
254 set_alpha_blend_method(ParticleRendererBlendMethod bm) {
255  _blend_method = bm;
256 }
257 
258 ////////////////////////////////////////////////////////////////////
259 // Function : SpriteParticleRenderer::set_alpha_disable
260 // Access : public
261 ////////////////////////////////////////////////////////////////////
262 INLINE void SpriteParticleRenderer::
263 set_alpha_disable(bool ad) {
264  _alpha_disable = ad;
265 }
266 
267 ////////////////////////////////////////////////////////////////////
268 // Function : SpriteParticleRenderer::set_animate_frames_enable
269 // Access : public
270 ////////////////////////////////////////////////////////////////////
271 INLINE void SpriteParticleRenderer::
272 set_animate_frames_enable(bool an) {
273  _animate_frames = an;
274 }
275 
276 ////////////////////////////////////////////////////////////////////
277 // Function : SpriteParticleRenderer::set_animate_frames_rate
278 // Access : public
279 ////////////////////////////////////////////////////////////////////
280 INLINE void SpriteParticleRenderer::
281 set_animate_frames_rate(PN_stdfloat r) {
282  nassertv( r >= 0.0);
283  _animate_frames_rate = r;
284 }
285 
286 ////////////////////////////////////////////////////////////////////
287 // Function : SpriteParticleRenderer::set_animate_frames_index
288 // Access : public
289 // Purpose : Sets the frame to be used when animation is disabled.
290 ////////////////////////////////////////////////////////////////////
291 INLINE void SpriteParticleRenderer::
292 set_animate_frames_index(int i) {
293  nassertv(i < (int)_anims[0]->get_num_frames());
294  _animate_frames_index = i;
295 }
296 
297 ////////////////////////////////////////////////////////////////////
298 // Function : SpriteParticleRenderer::get_texture
299 // Access : public
300 ////////////////////////////////////////////////////////////////////
301 INLINE Texture *SpriteParticleRenderer::
302 get_texture() const {
303  return get_texture(0,0);
304 }
305 
306 ////////////////////////////////////////////////////////////////////
307 // Function : SpriteParticleRenderer::get_texture
308 // Access : public
309 ////////////////////////////////////////////////////////////////////
310 INLINE Texture *SpriteParticleRenderer::
311 get_texture(const int anim, const int frame) const {
312  if(_anims.size() == 0) {
313  return (Texture*)NULL;
314  }
315  nassertr(anim < (int)_anims.size() && anim >= 0, (Texture*)NULL);
316  nassertr(frame < (int)_anims[anim]->get_num_frames() && frame >= 0,_anims[anim]->get_frame(0));
317  return _anims[anim]->get_frame(frame);
318 }
319 
320 INLINE int SpriteParticleRenderer::
321 get_num_anims() const {
322  return _anims.size();
323 }
324 
325 INLINE SpriteAnim *SpriteParticleRenderer::
326 get_anim(const int n) const {
327  nassertr(n < (int)_anims.size(), (SpriteAnim*)NULL);
328  return _anims[n];
329 }
330 
331 INLINE SpriteAnim *SpriteParticleRenderer::
332 get_last_anim() const {
333  if (_anims.size()) {
334  return *(_anims.end()-1);
335  } else {
336  return (SpriteAnim *)NULL;
337  }
338 }
339 
340 ////////////////////////////////////////////////////////////////////
341 // Function : SpriteParticleRenderer::get_ll_uv
342 // Access : public
343 // Description : Returns the UV coordinate of the lower-left corner;
344 // see set_ll_uv().
345 ////////////////////////////////////////////////////////////////////
347 get_ll_uv() const {
348  return get_ll_uv(0,0);
349 }
350 
351 ////////////////////////////////////////////////////////////////////
352 // Function : SpriteParticleRenderer::get_ll_uv
353 // Access : public
354 // Description : Returns the UV coordinate of the lower-left corner;
355 // see set_ll_uv().
356 ////////////////////////////////////////////////////////////////////
358 get_ll_uv(const int anim, const int frame) const {
359  int a = anim < (int)_anims.size()?anim:0;
360  int f = frame < (int)_anims[a]->get_num_frames()?frame:0;
361  return _anims[a]->get_ll(f);
362 }
363 
364 ////////////////////////////////////////////////////////////////////
365 // Function : SpriteParticleRenderer::get_ur_uv
366 // Access : public
367 // Description : Returns the UV coordinate of the lower-left corner;
368 // see set_ur_uv().
369 ////////////////////////////////////////////////////////////////////
371 get_ur_uv() const {
372  return get_ur_uv(0,0);
373 }
374 
375 ////////////////////////////////////////////////////////////////////
376 // Function : SpriteParticleRenderer::get_ur_uv
377 // Access : public
378 // Description : Returns the UV coordinate of the upper-right corner;
379 // see set_ur_uv().
380 ////////////////////////////////////////////////////////////////////
382 get_ur_uv(const int anim, const int frame) const {
383  int a = anim < (int)_anims.size()?anim:0;
384  int f = frame < (int)_anims[a]->get_num_frames()?frame:0;
385  return _anims[a]->get_ur(f);
386 }
387 
388 ////////////////////////////////////////////////////////////////////
389 // Function : SpriteParticleRenderer::get_width
390 // Access : public
391 // Description : Returns the width of each particle in world units.
392 ////////////////////////////////////////////////////////////////////
393 INLINE PN_stdfloat SpriteParticleRenderer::
394 get_width() const {
395  return _width;
396 }
397 
398 ////////////////////////////////////////////////////////////////////
399 // Function : SpriteParticleRenderer::get_height
400 // Access : public
401 // Description : Returns the height of each particle in world units.
402 ////////////////////////////////////////////////////////////////////
403 INLINE PN_stdfloat SpriteParticleRenderer::
404 get_height() const {
405  return _height;
406 }
407 
408 ////////////////////////////////////////////////////////////////////
409 // Function : SpriteParticleRenderer::get_color
410 // Access : public
411 ////////////////////////////////////////////////////////////////////
412 INLINE LColor SpriteParticleRenderer::
413 get_color() const {
414  return _color;
415 }
416 
417 ////////////////////////////////////////////////////////////////////
418 // Function : SpriteParticleRenderer::get_x_scale_flag
419 // Access : public
420 ////////////////////////////////////////////////////////////////////
421 INLINE bool SpriteParticleRenderer::
422 get_x_scale_flag() const {
423  return _animate_x_ratio;
424 }
425 
426 ////////////////////////////////////////////////////////////////////
427 // Function : SpriteParticleRenderer::get_y_scale_flag
428 // Access : public
429 ////////////////////////////////////////////////////////////////////
430 INLINE bool SpriteParticleRenderer::
431 get_y_scale_flag() const {
432  return _animate_y_ratio;
433 }
434 
435 ////////////////////////////////////////////////////////////////////
436 // Function : SpriteParticleRenderer::get_anim_angle_flag
437 // Access : public
438 ////////////////////////////////////////////////////////////////////
439 INLINE bool SpriteParticleRenderer::
440 get_anim_angle_flag() const {
441  return _animate_theta;
442 }
443 
444 ////////////////////////////////////////////////////////////////////
445 // Function : SpriteParticleRenderer::get_initial_x_scale
446 // Access : public
447 ////////////////////////////////////////////////////////////////////
448 INLINE PN_stdfloat SpriteParticleRenderer::
449 get_initial_x_scale() const {
450  return _initial_x_scale;
451 }
452 
453 ////////////////////////////////////////////////////////////////////
454 // Function : SpriteParticleRenderer::get_final_x_scale
455 // Access : public
456 ////////////////////////////////////////////////////////////////////
457 INLINE PN_stdfloat SpriteParticleRenderer::
458 get_final_x_scale() const {
459  return _final_x_scale;
460 }
461 
462 ////////////////////////////////////////////////////////////////////
463 // Function : SpriteParticleRenderer::get_initial_y_scale
464 // Access : public
465 ////////////////////////////////////////////////////////////////////
466 INLINE PN_stdfloat SpriteParticleRenderer::
467 get_initial_y_scale() const {
468  return _initial_y_scale;
469 }
470 
471 ////////////////////////////////////////////////////////////////////
472 // Function : SpriteParticleRenderer::get_final_y_scale
473 // Access : public
474 ////////////////////////////////////////////////////////////////////
475 INLINE PN_stdfloat SpriteParticleRenderer::
476 get_final_y_scale() const {
477  return _final_y_scale;
478 }
479 
480 ////////////////////////////////////////////////////////////////////
481 // Function : SpriteParticleRenderer::get_nonanimated_theta
482 // Access : public
483 ////////////////////////////////////////////////////////////////////
484 INLINE PN_stdfloat SpriteParticleRenderer::
485 get_nonanimated_theta() const {
486  return _theta;
487 }
488 
489 ////////////////////////////////////////////////////////////////////
490 // Function : SpriteParticleRenderer::get_alpha_blend_method
491 // Access : public
492 ////////////////////////////////////////////////////////////////////
493 INLINE BaseParticleRenderer::ParticleRendererBlendMethod SpriteParticleRenderer::
494 get_alpha_blend_method() const {
495  return _blend_method;
496 }
497 
498 ////////////////////////////////////////////////////////////////////
499 // Function : SpriteParticleRenderer::get_alpha_disable
500 // Access : public
501 ////////////////////////////////////////////////////////////////////
502 INLINE bool SpriteParticleRenderer::
503 get_alpha_disable() const {
504  return _alpha_disable;
505 }
506 
507 ////////////////////////////////////////////////////////////////////
508 // Function : SpriteParticleRenderer::get_animate_frames_enable
509 // Access : public
510 ////////////////////////////////////////////////////////////////////
511 INLINE bool SpriteParticleRenderer::
512 get_animate_frames_enable() const {
513  return _animate_frames;
514 }
515 
516 ////////////////////////////////////////////////////////////////////
517 // Function : SpriteParticleRenderer::get_animate_frames_rate
518 // Access : public
519 ////////////////////////////////////////////////////////////////////
520 INLINE PN_stdfloat SpriteParticleRenderer::
521 get_animate_frames_rate() const {
522  return _animate_frames_rate;
523 }
524 
525 ////////////////////////////////////////////////////////////////////
526 // Function : SpriteParticleRenderer::get_animate_frames_index
527 // Access : public
528 // Purpose : Gets the frame to be used when animation is disabled.
529 ////////////////////////////////////////////////////////////////////
530 INLINE int SpriteParticleRenderer::
531 get_animate_frames_index() const {
532  return _animate_frames_index;
533 }
534 
535 ////////////////////////////////////////////////////////////////////
536 // Function : SpriteParticleRenderer::get_color_interpolation_manager
537 // Access : public
538 ////////////////////////////////////////////////////////////////////
539 INLINE ColorInterpolationManager* SpriteParticleRenderer::
540 get_color_interpolation_manager() const {
541  return _color_interpolation_manager;
542 }
void remove_animation(const int n)
Removes an animation texture set from the renderer.
Helper class used by SpriteParticleRenderer to keep track of its textures and their respective UVs an...
void set_ll_uv(const LTexCoord &ll_uv)
Sets the UV coordinate of the lower-left corner of all the sprites generated by this renderer...
Represents a texture object, which is typically a single 2-d image but may also represent a 1-d or 3-...
Definition: texture.h:75
void set_ur_uv(const LTexCoord &ur_uv)
Sets the UV coordinate of the upper-right corner of all the sprites generated by this renderer...
void add_texture(Texture *tex, PN_stdfloat texels_per_unit=1.0f, bool resize=false)
Adds texture to image pool, effectively creating a single frame animation that can be selected at par...
High level class for color interpolation.
LTexCoord get_ll_uv() const
Returns the UV coordinate of the lower-left corner; see set_ll_uv().
This is the base class for all three-component vectors and points.
Definition: lvecBase4.h:111
PN_stdfloat get_height() const
Returns the height of each particle in world units.
PN_stdfloat get_width() const
Returns the width of each particle in world units.
This is a two-component point in space.
Definition: lpoint2.h:92
void set_texture(Texture *tex, PN_stdfloat texels_per_unit=1.0f)
Sets the renderer up to render the entire texture image.
LTexCoord get_ur_uv() const
Returns the UV coordinate of the lower-left corner; see set_ur_uv().
int get_y_size() const
Returns the height of the texture image in texels.
Definition: texture.I:650
void set_size(PN_stdfloat width, PN_stdfloat height)
Sets the size of each particle in world units.
int get_x_size() const
Returns the width of the texture image in texels.
Definition: texture.I:638
const Filename & get_filename() const
Returns the filename that has been set.
Definition: texture.I:549