Panda3D
 All Classes Functions Variables Enumerations
spriteParticleRenderer.h
00001 // Filename: spriteParticleRenderer.h
00002 // Created by:  charles (13Jul00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef SPRITEPARTICLERENDERER_H
00016 #define SPRITEPARTICLERENDERER_H
00017 
00018 #include "pandabase.h"
00019 #include "pvector.h"
00020 #include "baseParticleRenderer.h"
00021 #include "baseParticle.h"
00022 #include "texture.h"
00023 #include "pointerTo.h"
00024 #include "geom.h"
00025 #include "geomVertexData.h"
00026 #include "geomPoints.h"
00027 #include "colorInterpolationManager.h"
00028 #include "geomVertexWriter.h"
00029 #include "textureCollection.h"
00030 #include "nodePathCollection.h"
00031 #include "vector_int.h"
00032 #include "pStatCollector.h"
00033 
00034 class NodePath;
00035 
00036 ////////////////////////////////////////////////////////////////////
00037 //       Class : SpriteWriter
00038 // Description : Helper class used by SpriteParticleRenderer to
00039 //               keep track of the various GeomVertexWriters
00040 //               associated with each geom created in 
00041 //               SpriteParticleRenderer::init_geoms().
00042 ////////////////////////////////////////////////////////////////////
00043 class SpriteWriter {
00044 public:
00045   SpriteWriter() {
00046   }
00047   SpriteWriter(const SpriteWriter &copy):
00048     vertex(copy.vertex),
00049     color(copy.color),
00050     rotate(copy.rotate),
00051     size(copy.size),
00052     aspect_ratio(copy.aspect_ratio) {
00053   };
00054 
00055   void clear() {
00056     vertex.clear();
00057     color.clear();
00058     rotate.clear();
00059     size.clear();
00060     aspect_ratio.clear();
00061   }
00062 
00063   GeomVertexWriter vertex;
00064   GeomVertexWriter color;
00065   GeomVertexWriter rotate;
00066   GeomVertexWriter size;
00067   GeomVertexWriter aspect_ratio;
00068 };
00069 
00070 ////////////////////////////////////////////////////////////////////
00071 //       Class : SpriteAnim
00072 // Description : Helper class used by SpriteParticleRenderer to
00073 //               keep track of its textures and their respective UVs
00074 //               and source types.  
00075 ////////////////////////////////////////////////////////////////////
00076 class SpriteAnim : public ReferenceCount{
00077 PUBLISHED:
00078   enum SourceType {
00079     ST_texture,
00080     ST_from_node,
00081   };
00082 
00083   void set_source_info(const string &tex) {
00084     _source_type = ST_texture;
00085     _source_tex = tex;
00086   }
00087 
00088   void set_source_info(const string &model, const string &node) {
00089     _source_type = ST_from_node;
00090     _source_model = model;
00091     _source_node = node;
00092   }
00093 
00094   SourceType get_source_type() const {
00095     return _source_type;
00096   }
00097 
00098   string get_tex_source() const {
00099     return _source_tex;
00100   }
00101 
00102   string get_model_source() const {
00103     return _source_model;
00104   }
00105 
00106   string get_node_source() const {
00107     return _source_node;
00108   }
00109 
00110   int get_num_frames() const {
00111     return textures.size();
00112   }
00113 
00114 public:
00115   SpriteAnim(Texture* t, LTexCoord ll, LTexCoord ur) {
00116     textures.push_back(t);
00117     this->ll.push_back(ll);
00118     this->ur.push_back(ur);
00119   };
00120 
00121   SpriteAnim(const TextureCollection &t, const pvector< LTexCoord > &lls, const pvector< LTexCoord > &urs) :
00122     ll(lls),
00123     ur(urs) {
00124     for (int i = 0; i < t.get_num_textures(); ++i) {
00125       textures.push_back(t.get_texture(i));
00126     }
00127   };
00128   
00129   void set_ll(const int n, LTexCoord c) {
00130     ll[n] = c;
00131   }
00132 
00133   void set_ur(const int n, LTexCoord c) {
00134     ur[n] = c;
00135   }
00136 
00137   Texture *get_frame(const int n) const {
00138     return textures[n];
00139   };
00140 
00141   LTexCoord get_ll(const int n) const {
00142     return ll[n];
00143   }
00144 
00145   LTexCoord get_ur(const int n) const {
00146     return ur[n];
00147   }
00148 
00149 private:
00150   pvector< PT(Texture) > textures;
00151   pvector< LTexCoord > ll,ur;
00152   SourceType _source_type;
00153   string _source_tex,_source_model,_source_node;
00154 };
00155 
00156 ////////////////////////////////////////////////////////////////////
00157 //       Class : SpriteParticleRenderer
00158 // Description : Renders a particle system with high-speed nasty
00159 //               trick sprites.
00160 ////////////////////////////////////////////////////////////////////
00161 class EXPCL_PANDAPHYSICS SpriteParticleRenderer : public BaseParticleRenderer {
00162 PUBLISHED:
00163   SpriteParticleRenderer(Texture *tex = (Texture *) NULL);
00164   SpriteParticleRenderer(const SpriteParticleRenderer &copy);
00165   virtual ~SpriteParticleRenderer();
00166 
00167 public:
00168   virtual BaseParticleRenderer *make_copy();
00169 
00170 PUBLISHED:
00171   void set_from_node(const NodePath &node_path, bool size_from_texels = false);
00172   void set_from_node(const NodePath &node_path, const string &model, const string &node, bool size_from_texels = false);
00173   void add_from_node(const NodePath &node_path, bool size_from_texels = false, bool resize = false);
00174   void add_from_node(const NodePath &node_path, const string &model, const string &node, bool size_from_texels = false, bool resize = false);
00175 
00176   INLINE void set_texture(Texture *tex, PN_stdfloat texels_per_unit = 1.0f);
00177   INLINE void add_texture(Texture *tex, PN_stdfloat texels_per_unit = 1.0f, bool resize = false);
00178   INLINE void remove_animation(const int n);
00179   INLINE void set_ll_uv(const LTexCoord &ll_uv);
00180   INLINE void set_ll_uv(const LTexCoord &ll_uv, const int anim, const int frame);
00181   INLINE void set_ur_uv(const LTexCoord &ur_uv);
00182   INLINE void set_ur_uv(const LTexCoord &ur_uv, const int anim, const int frame);
00183   INLINE void set_size(PN_stdfloat width, PN_stdfloat height);
00184   INLINE void set_color(const LColor &color);
00185   INLINE void set_x_scale_flag(bool animate_x_ratio);
00186   INLINE void set_y_scale_flag(bool animate_y_ratio);
00187   INLINE void set_anim_angle_flag(bool animate_theta);
00188   INLINE void set_initial_x_scale(PN_stdfloat initial_x_scale);
00189   INLINE void set_final_x_scale(PN_stdfloat final_x_scale);  
00190   INLINE void set_initial_y_scale(PN_stdfloat initial_y_scale);
00191   INLINE void set_final_y_scale(PN_stdfloat final_y_scale);
00192   INLINE void set_nonanimated_theta(PN_stdfloat theta);
00193   INLINE void set_alpha_blend_method(ParticleRendererBlendMethod bm);
00194   INLINE void set_alpha_disable(bool ad);
00195   INLINE void set_animate_frames_enable(bool an);
00196   INLINE void set_animate_frames_rate(PN_stdfloat r);
00197   INLINE void set_animate_frames_index(int i);
00198 
00199   INLINE Texture *get_texture() const;
00200   INLINE Texture *get_texture(const int anim, const int frame) const;
00201   INLINE int get_num_anims() const;
00202   INLINE SpriteAnim *get_anim(const int n) const;
00203   MAKE_SEQ(get_anims, get_num_anims, get_anim);
00204   INLINE SpriteAnim *get_last_anim() const;
00205   INLINE ColorInterpolationManager* get_color_interpolation_manager() const;
00206   INLINE LTexCoord get_ll_uv() const;
00207   INLINE LTexCoord get_ll_uv(const int anim, const int frame) const;
00208   INLINE LTexCoord get_ur_uv() const;
00209   INLINE LTexCoord get_ur_uv(const int anim, const int frame) const;
00210   INLINE PN_stdfloat get_width() const;
00211   INLINE PN_stdfloat get_height() const;
00212   INLINE LColor get_color() const;
00213   INLINE bool get_x_scale_flag() const;
00214   INLINE bool get_y_scale_flag() const;
00215   INLINE bool get_anim_angle_flag() const;
00216   INLINE PN_stdfloat get_initial_x_scale() const;
00217   INLINE PN_stdfloat get_final_x_scale() const;
00218   INLINE PN_stdfloat get_initial_y_scale() const;
00219   INLINE PN_stdfloat get_final_y_scale() const;
00220   INLINE PN_stdfloat get_nonanimated_theta() const;
00221   INLINE ParticleRendererBlendMethod get_alpha_blend_method() const;
00222   INLINE bool get_alpha_disable() const;  
00223   INLINE bool get_animate_frames_enable() const;  
00224   INLINE PN_stdfloat get_animate_frames_rate() const;
00225   INLINE int get_animate_frames_index() const;
00226 
00227   virtual void output(ostream &out) const;
00228   virtual void write(ostream &out, int indent_level = 0) const;
00229 
00230 private:
00231   pvector< pvector< PT(Geom) > > _sprite_primitive;
00232   pvector< pvector< PT(GeomPoints) > > _sprites;
00233   pvector< pvector< SpriteWriter > > _sprite_writer;
00234   pvector< pvector< PT(GeomVertexData) > > _vdata;
00235 
00236   pvector< PT(SpriteAnim) > _anims;            // Stores texture references and UV info for each geom.
00237 
00238   LColor _color;
00239 
00240   PN_stdfloat _height;
00241   PN_stdfloat _width;
00242   PN_stdfloat _initial_x_scale;
00243   PN_stdfloat _final_x_scale;
00244   PN_stdfloat _initial_y_scale;
00245   PN_stdfloat _final_y_scale;
00246   PN_stdfloat _theta;
00247   PN_stdfloat _base_y_scale;
00248   PN_stdfloat _aspect_ratio;
00249   PN_stdfloat _animate_frames_rate;
00250   int _animate_frames_index;
00251 
00252   bool _animate_x_ratio;
00253   bool _animate_y_ratio;
00254   bool _animate_theta;
00255   bool _alpha_disable;
00256   bool _animate_frames;
00257   bool _animation_removed;
00258 
00259   ParticleRendererBlendMethod _blend_method;
00260   PT(ColorInterpolationManager) _color_interpolation_manager;
00261 
00262   LVertex _aabb_min;
00263   LVertex _aabb_max;
00264 
00265   int _pool_size;
00266 
00267   virtual void birth_particle(int index);
00268   virtual void kill_particle(int index);
00269   virtual void init_geoms();
00270   virtual void render(pvector< PT(PhysicsObject) > &po_vector,
00271                       int ttl_particles);
00272   virtual void resize_pool(int new_size);
00273   int extract_textures_from_node(const NodePath &node_path, NodePathCollection &np_col, TextureCollection &tex_col);
00274 
00275   vector_int _anim_size;   // Holds the number of frames in each animation.
00276   pvector<int*> _ttl_count;  // _ttl_count[i][j] holds the number of particles attached to animation 'i' at frame 'j'.
00277   vector_int _birth_list;  // Holds the list of particles that need a new random animation to start on.
00278 
00279   static PStatCollector _render_collector;
00280 };
00281 
00282 #include "spriteParticleRenderer.I"
00283 
00284 #endif // SPRITEPARTICLERENDERER_H
 All Classes Functions Variables Enumerations