Panda3D
 All Classes Functions Variables Enumerations
spriteParticleRenderer.h
1 // Filename: spriteParticleRenderer.h
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 #ifndef SPRITEPARTICLERENDERER_H
16 #define SPRITEPARTICLERENDERER_H
17 
18 #include "pandabase.h"
19 #include "pvector.h"
20 #include "baseParticleRenderer.h"
21 #include "baseParticle.h"
22 #include "texture.h"
23 #include "pointerTo.h"
24 #include "geom.h"
25 #include "geomVertexData.h"
26 #include "geomPoints.h"
27 #include "colorInterpolationManager.h"
28 #include "geomVertexWriter.h"
29 #include "textureCollection.h"
30 #include "nodePathCollection.h"
31 #include "vector_int.h"
32 #include "pStatCollector.h"
33 
34 class NodePath;
35 
36 ////////////////////////////////////////////////////////////////////
37 // Class : SpriteWriter
38 // Description : Helper class used by SpriteParticleRenderer to
39 // keep track of the various GeomVertexWriters
40 // associated with each geom created in
41 // SpriteParticleRenderer::init_geoms().
42 ////////////////////////////////////////////////////////////////////
43 class SpriteWriter {
44 public:
45  SpriteWriter() {
46  }
47  SpriteWriter(const SpriteWriter &copy):
48  vertex(copy.vertex),
49  color(copy.color),
50  rotate(copy.rotate),
51  size(copy.size),
52  aspect_ratio(copy.aspect_ratio) {
53  };
54 
55  void clear() {
56  vertex.clear();
57  color.clear();
58  rotate.clear();
59  size.clear();
60  aspect_ratio.clear();
61  }
62 
63  GeomVertexWriter vertex;
65  GeomVertexWriter rotate;
66  GeomVertexWriter size;
67  GeomVertexWriter aspect_ratio;
68 };
69 
70 ////////////////////////////////////////////////////////////////////
71 // Class : SpriteAnim
72 // Description : Helper class used by SpriteParticleRenderer to
73 // keep track of its textures and their respective UVs
74 // and source types.
75 ////////////////////////////////////////////////////////////////////
76 class SpriteAnim : public ReferenceCount{
77 PUBLISHED:
78  enum SourceType {
79  ST_texture,
80  ST_from_node,
81  };
82 
83  void set_source_info(const string &tex) {
84  _source_type = ST_texture;
85  _source_tex = tex;
86  }
87 
88  void set_source_info(const string &model, const string &node) {
89  _source_type = ST_from_node;
90  _source_model = model;
91  _source_node = node;
92  }
93 
94  SourceType get_source_type() const {
95  return _source_type;
96  }
97 
98  string get_tex_source() const {
99  return _source_tex;
100  }
101 
102  string get_model_source() const {
103  return _source_model;
104  }
105 
106  string get_node_source() const {
107  return _source_node;
108  }
109 
110  int get_num_frames() const {
111  return textures.size();
112  }
113 
114 public:
115  SpriteAnim(Texture* t, LTexCoord ll, LTexCoord ur) {
116  textures.push_back(t);
117  this->ll.push_back(ll);
118  this->ur.push_back(ur);
119  };
120 
121  SpriteAnim(const TextureCollection &t, const pvector< LTexCoord > &lls, const pvector< LTexCoord > &urs) :
122  ll(lls),
123  ur(urs) {
124  for (int i = 0; i < t.get_num_textures(); ++i) {
125  textures.push_back(t.get_texture(i));
126  }
127  };
128 
129  void set_ll(const int n, LTexCoord c) {
130  ll[n] = c;
131  }
132 
133  void set_ur(const int n, LTexCoord c) {
134  ur[n] = c;
135  }
136 
137  Texture *get_frame(const int n) const {
138  return textures[n];
139  };
140 
141  LTexCoord get_ll(const int n) const {
142  return ll[n];
143  }
144 
145  LTexCoord get_ur(const int n) const {
146  return ur[n];
147  }
148 
149 private:
150  pvector< PT(Texture) > textures;
151  pvector< LTexCoord > ll,ur;
152  SourceType _source_type;
153  string _source_tex,_source_model,_source_node;
154 };
155 
156 ////////////////////////////////////////////////////////////////////
157 // Class : SpriteParticleRenderer
158 // Description : Renders a particle system with high-speed nasty
159 // trick sprites.
160 ////////////////////////////////////////////////////////////////////
161 class EXPCL_PANDAPHYSICS SpriteParticleRenderer : public BaseParticleRenderer {
162 PUBLISHED:
163  SpriteParticleRenderer(Texture *tex = (Texture *) NULL);
165  virtual ~SpriteParticleRenderer();
166 
167 public:
168  virtual BaseParticleRenderer *make_copy();
169 
170 PUBLISHED:
171  void set_from_node(const NodePath &node_path, bool size_from_texels = false);
172  void set_from_node(const NodePath &node_path, const string &model, const string &node, bool size_from_texels = false);
173  void add_from_node(const NodePath &node_path, bool size_from_texels = false, bool resize = false);
174  void add_from_node(const NodePath &node_path, const string &model, const string &node, bool size_from_texels = false, bool resize = false);
175 
176  INLINE void set_texture(Texture *tex, PN_stdfloat texels_per_unit = 1.0f);
177  INLINE void add_texture(Texture *tex, PN_stdfloat texels_per_unit = 1.0f, bool resize = false);
178  INLINE void remove_animation(const int n);
179  INLINE void set_ll_uv(const LTexCoord &ll_uv);
180  INLINE void set_ll_uv(const LTexCoord &ll_uv, const int anim, const int frame);
181  INLINE void set_ur_uv(const LTexCoord &ur_uv);
182  INLINE void set_ur_uv(const LTexCoord &ur_uv, const int anim, const int frame);
183  INLINE void set_size(PN_stdfloat width, PN_stdfloat height);
184  INLINE void set_color(const LColor &color);
185  INLINE void set_x_scale_flag(bool animate_x_ratio);
186  INLINE void set_y_scale_flag(bool animate_y_ratio);
187  INLINE void set_anim_angle_flag(bool animate_theta);
188  INLINE void set_initial_x_scale(PN_stdfloat initial_x_scale);
189  INLINE void set_final_x_scale(PN_stdfloat final_x_scale);
190  INLINE void set_initial_y_scale(PN_stdfloat initial_y_scale);
191  INLINE void set_final_y_scale(PN_stdfloat final_y_scale);
192  INLINE void set_nonanimated_theta(PN_stdfloat theta);
193  INLINE void set_alpha_blend_method(ParticleRendererBlendMethod bm);
194  INLINE void set_alpha_disable(bool ad);
195  INLINE void set_animate_frames_enable(bool an);
196  INLINE void set_animate_frames_rate(PN_stdfloat r);
197  INLINE void set_animate_frames_index(int i);
198 
199  INLINE Texture *get_texture() const;
200  INLINE Texture *get_texture(const int anim, const int frame) const;
201  INLINE int get_num_anims() const;
202  INLINE SpriteAnim *get_anim(const int n) const;
203  MAKE_SEQ(get_anims, get_num_anims, get_anim);
204  INLINE SpriteAnim *get_last_anim() const;
205  INLINE ColorInterpolationManager* get_color_interpolation_manager() const;
206  INLINE LTexCoord get_ll_uv() const;
207  INLINE LTexCoord get_ll_uv(const int anim, const int frame) const;
208  INLINE LTexCoord get_ur_uv() const;
209  INLINE LTexCoord get_ur_uv(const int anim, const int frame) const;
210  INLINE PN_stdfloat get_width() const;
211  INLINE PN_stdfloat get_height() const;
212  INLINE LColor get_color() const;
213  INLINE bool get_x_scale_flag() const;
214  INLINE bool get_y_scale_flag() const;
215  INLINE bool get_anim_angle_flag() const;
216  INLINE PN_stdfloat get_initial_x_scale() const;
217  INLINE PN_stdfloat get_final_x_scale() const;
218  INLINE PN_stdfloat get_initial_y_scale() const;
219  INLINE PN_stdfloat get_final_y_scale() const;
220  INLINE PN_stdfloat get_nonanimated_theta() const;
221  INLINE ParticleRendererBlendMethod get_alpha_blend_method() const;
222  INLINE bool get_alpha_disable() const;
223  INLINE bool get_animate_frames_enable() const;
224  INLINE PN_stdfloat get_animate_frames_rate() const;
225  INLINE int get_animate_frames_index() const;
226 
227  virtual void output(ostream &out) const;
228  virtual void write(ostream &out, int indent_level = 0) const;
229 
230 private:
231  pvector< pvector< PT(Geom) > > _sprite_primitive;
233  pvector< pvector< SpriteWriter > > _sprite_writer;
235 
236  pvector< PT(SpriteAnim) > _anims; // Stores texture references and UV info for each geom.
237 
238  LColor _color;
239 
240  PN_stdfloat _height;
241  PN_stdfloat _width;
242  PN_stdfloat _initial_x_scale;
243  PN_stdfloat _final_x_scale;
244  PN_stdfloat _initial_y_scale;
245  PN_stdfloat _final_y_scale;
246  PN_stdfloat _theta;
247  PN_stdfloat _base_y_scale;
248  PN_stdfloat _aspect_ratio;
249  PN_stdfloat _animate_frames_rate;
250  int _animate_frames_index;
251 
252  bool _animate_x_ratio;
253  bool _animate_y_ratio;
254  bool _animate_theta;
255  bool _alpha_disable;
256  bool _animate_frames;
257  bool _animation_removed;
258 
259  ParticleRendererBlendMethod _blend_method;
260  PT(ColorInterpolationManager) _color_interpolation_manager;
261 
262  LVertex _aabb_min;
263  LVertex _aabb_max;
264 
265  int _pool_size;
266 
267  virtual void birth_particle(int index);
268  virtual void kill_particle(int index);
269  virtual void init_geoms();
270  virtual void render(pvector< PT(PhysicsObject) > &po_vector,
271  int ttl_particles);
272  virtual void resize_pool(int new_size);
273  int extract_textures_from_node(const NodePath &node_path, NodePathCollection &np_col, TextureCollection &tex_col);
274 
275  vector_int _anim_size; // Holds the number of frames in each animation.
276  pvector<int*> _ttl_count; // _ttl_count[i][j] holds the number of particles attached to animation 'i' at frame 'j'.
277  vector_int _birth_list; // Holds the list of particles that need a new random animation to start on.
278 
279  static PStatCollector _render_collector;
280 };
281 
282 #include "spriteParticleRenderer.I"
283 
284 #endif // SPRITEPARTICLERENDERER_H
Renders a particle system with high-speed nasty trick sprites.
Helper class used by SpriteParticleRenderer to keep track of its textures and their respective UVs an...
This object provides a high-level interface for quickly writing a sequence of numeric values from a v...
void clear()
Resets the GeomVertexWriter to the initial state.
virtual void output(ostream &out) const
Write a string representation of this instance to &lt;out&gt;.
A body on which physics will be applied.
Definition: physicsObject.h:29
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
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:39
A lightweight class that represents a single element that may be timed and/or counted via stats...
Pure virtual particle renderer base class.
High level class for color interpolation.
Manages a list of Texture objects, as returned by TexturePool::find_all_textures().
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.
virtual void write(ostream &out, int indent=0) const
Write a string representation of this instance to &lt;out&gt;.
Texture * get_texture(int index) const
Returns the nth Texture in the collection.
This is a two-component point in space.
Definition: lpoint2.h:92
int get_num_textures() const
Returns the number of Textures in the collection.
Helper class used by SpriteParticleRenderer to keep track of the various GeomVertexWriters associated...
NodePath is the fundamental system for disambiguating instances, and also provides a higher-level int...
Definition: nodePath.h:165
This is a set of zero or more NodePaths.