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