Panda3D

geomTransformer.h

00001 // Filename: geomTransformer.h
00002 // Created by:  drose (14Mar02)
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 GEOMTRANSFORMER_H
00016 #define GEOMTRANSFORMER_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "luse.h"
00021 #include "geom.h"
00022 #include "geomVertexData.h"
00023 
00024 class GeomNode;
00025 class RenderState;
00026 class InternalName;
00027 class GeomMunger;
00028 class Texture;
00029 
00030 ////////////////////////////////////////////////////////////////////
00031 //       Class : GeomTransformer
00032 // Description : An object specifically designed to transform the
00033 //               vertices of a Geom without disturbing indexing or
00034 //               affecting any other Geoms that may share the same
00035 //               vertex arrays, and without needlessly wasting memory
00036 //               when different Geoms sharing the same vertex arrays
00037 //               are transformed by the same amount.
00038 //
00039 //               If you create a single GeomTransformer and use it to
00040 //               transform a number of different Geoms by various
00041 //               transformations, then those Geoms which happen to
00042 //               share the same arrays and are transformed by the same
00043 //               amounts will still share the same arrays as each
00044 //               other (but different from the original arrays).
00045 ////////////////////////////////////////////////////////////////////
00046 class EXPCL_PANDA_PGRAPH GeomTransformer {
00047 public:
00048   GeomTransformer();
00049   GeomTransformer(const GeomTransformer &copy);
00050   ~GeomTransformer();
00051 
00052   INLINE int get_max_collect_vertices() const;
00053   INLINE void set_max_collect_vertices(int max_collect_vertices);
00054 
00055   void register_vertices(Geom *geom, bool might_have_unused);
00056   void register_vertices(GeomNode *node, bool might_have_unused);
00057 
00058   bool transform_vertices(Geom *geom, const LMatrix4 &mat);
00059   bool transform_vertices(GeomNode *node, const LMatrix4 &mat);
00060 
00061   bool transform_texcoords(Geom *geom, const InternalName *from_name,
00062                            InternalName *to_name, const LMatrix4 &mat);
00063   bool transform_texcoords(GeomNode *node, const InternalName *from_name,
00064                            InternalName *to_name, const LMatrix4 &mat);
00065 
00066   bool set_color(Geom *geom, const LColor &color);
00067   bool set_color(GeomNode *node, const LColor &color);
00068 
00069   bool transform_colors(Geom *geom, const LVecBase4 &scale);
00070   bool transform_colors(GeomNode *node, const LVecBase4 &scale);
00071 
00072   bool apply_texture_colors(Geom *geom, TextureStage *ts, Texture *tex, 
00073                             const TexMatrixAttrib *tma,
00074                             const LColor &base_color, bool keep_vertex_color);
00075   bool apply_texture_colors(GeomNode *node, const RenderState *state);
00076 
00077   bool apply_state(GeomNode *node, const RenderState *state);
00078 
00079   bool set_format(Geom *geom, const GeomVertexFormat *new_format);
00080   bool remove_column(Geom *geom, const InternalName *column);
00081   bool remove_column(GeomNode *node, const InternalName *column);
00082 
00083   bool make_compatible_state(GeomNode *node);
00084 
00085   bool reverse_normals(Geom *geom);
00086   bool doubleside(GeomNode *node);
00087   bool reverse(GeomNode *node);
00088 
00089   void finish_apply();
00090 
00091   int collect_vertex_data(Geom *geom, int collect_bits, bool format_only);
00092   int collect_vertex_data(GeomNode *node, int collect_bits, bool format_only);
00093   int finish_collect(bool format_only);
00094 
00095   PT(Geom) premunge_geom(const Geom *geom, GeomMunger *munger);
00096 
00097 private:
00098   int _max_collect_vertices;
00099 
00100   typedef pvector<PT(Geom) > GeomList;
00101 
00102   // Keeps track of the Geoms that are associated with a particular
00103   // GeomVertexData.  Also tracks whether the vertex data might have
00104   // unused vertices because of our actions.
00105   class VertexDataAssoc {
00106   public:
00107     INLINE VertexDataAssoc();
00108     bool _might_have_unused;
00109     GeomList _geoms;
00110     void remove_unused_vertices(const GeomVertexData *vdata);
00111   };
00112   typedef pmap<CPT(GeomVertexData), VertexDataAssoc> VertexDataAssocMap;
00113   VertexDataAssocMap _vdata_assoc;
00114 
00115   // Corresponds to a new GeomVertexData created as needed during an
00116   // apply operation.
00117   class NewVertexData {
00118   public:
00119     CPT(GeomVertexData) _vdata;
00120   };
00121 
00122   // The table of GeomVertexData objects that have been transformed by
00123   // a particular matrix.
00124   class SourceVertices {
00125   public:
00126     INLINE bool operator < (const SourceVertices &other) const;
00127 
00128     LMatrix4 _mat;
00129     CPT(GeomVertexData) _vertex_data;
00130   };
00131   typedef pmap<SourceVertices, NewVertexData> NewVertices;
00132   NewVertices _vertices;
00133 
00134   // The table of GeomVertexData objects whose texture coordinates
00135   // have been transformed by a particular matrix.
00136   class SourceTexCoords {
00137   public:
00138     INLINE bool operator < (const SourceTexCoords &other) const;
00139 
00140     LMatrix4 _mat;
00141     CPT(InternalName) _from;
00142     CPT(InternalName) _to;
00143     CPT(GeomVertexData) _vertex_data;
00144   };
00145   typedef pmap<SourceTexCoords, NewVertexData> NewTexCoords;
00146   NewTexCoords _texcoords;
00147 
00148   // The table of GeomVertexData objects whose colors have been
00149   // modified.
00150   class SourceColors {
00151   public:
00152     INLINE bool operator < (const SourceColors &other) const;
00153 
00154     LVecBase4 _color;
00155     CPT(GeomVertexData) _vertex_data;
00156   };
00157   typedef pmap<SourceColors, NewVertexData> NewColors;
00158 
00159   // We have two concepts of colors: the "fixed" colors, which are
00160   // slapped in as a complete replacement of the original colors
00161   // (e.g. via a ColorAttrib), and the "transformed" colors, which are
00162   // modified from the original colors (e.g. via a ColorScaleAttrib).
00163   NewColors _fcolors, _tcolors;
00164 
00165   // The table of GeomVertexData objects whose texture colors have
00166   // been applied.
00167   class SourceTextureColors {
00168   public:
00169     INLINE bool operator < (const SourceTextureColors &other) const;
00170 
00171     TextureStage *_ts;
00172     Texture *_tex;
00173     const TexMatrixAttrib *_tma;
00174     LColor _base_color;
00175     bool _keep_vertex_color;
00176     CPT(GeomVertexData) _vertex_data;
00177   };
00178   typedef pmap<SourceTextureColors, NewVertexData> NewTextureColors;
00179   NewTextureColors _tex_colors;
00180 
00181   // The table of GeomVertexData objects whose vertex formats have
00182   // been modified. For set_format(): record (format + vertex_data) ->
00183   // vertex_data.
00184   class SourceFormat {
00185   public:
00186     INLINE bool operator < (const SourceFormat &other) const;
00187 
00188     CPT(GeomVertexFormat) _format;
00189     CPT(GeomVertexData) _vertex_data;
00190   };
00191   typedef pmap<SourceFormat, NewVertexData> NewFormat;
00192   NewFormat _format;
00193 
00194   // The table of GeomVertexData objects whose normals have been
00195   // reversed.
00196   typedef pmap<CPT(GeomVertexData), NewVertexData> ReversedNormals;
00197   ReversedNormals _reversed_normals;
00198 
00199   class NewCollectedKey {
00200   public:
00201     INLINE bool operator < (const NewCollectedKey &other) const;
00202 
00203     string _name;
00204     CPT(GeomVertexFormat) _format;
00205     Geom::UsageHint _usage_hint;
00206     Geom::AnimationType _animation_type;
00207   };
00208 
00209   class SourceData {
00210   public:
00211     const GeomVertexData *_vdata;
00212     int _num_vertices;
00213   };
00214   typedef pvector<SourceData> SourceDatas;
00215   class SourceGeom {
00216   public:
00217     Geom *_geom;
00218     int _vertex_offset;
00219   };
00220   typedef pvector<SourceGeom> SourceGeoms;
00221   class NewCollectedData {
00222   public:
00223     ALLOC_DELETED_CHAIN(NewCollectedData);
00224 
00225     NewCollectedData(const GeomVertexData *source_data);
00226     void add_source_data(const GeomVertexData *source_data);
00227     int apply_format_only_changes();
00228     int apply_collect_changes();
00229 
00230     CPT(GeomVertexFormat) _new_format;
00231     string _vdata_name;
00232     GeomEnums::UsageHint _usage_hint;
00233     SourceDatas _source_datas;
00234     SourceGeoms _source_geoms;
00235     int _num_vertices;
00236 
00237   private:
00238     // These are used just during apply_changes().
00239     void append_vdata(const GeomVertexData *vdata, int vertex_offset);
00240     void update_geoms();
00241 
00242     typedef vector_int IndexMap;
00243 
00244     PT(GeomVertexData) _new_data;
00245     PT(TransformBlendTable) _new_btable;
00246     SparseArray _new_btable_rows;
00247 
00248     // We need a TypeHandle just for ALLOC_DELETED_CHAIN.
00249   public:
00250     static TypeHandle get_class_type() {
00251       return _type_handle;
00252     }
00253     static void init_type() {
00254       register_type(_type_handle, "GeomTransformer::NewCollectedData");
00255     }
00256     
00257   private:
00258     static TypeHandle _type_handle;
00259   };
00260   typedef pvector<NewCollectedData *> NewCollectedList;
00261   typedef pmap<NewCollectedKey, NewCollectedData *> NewCollectedMap;
00262   NewCollectedList _new_collected_list;
00263   NewCollectedMap _new_collected_map;
00264 
00265   class AlreadyCollectedData {
00266   public:
00267     NewCollectedData *_ncd;
00268     int _vertex_offset;
00269   };
00270   typedef pmap<CPT(GeomVertexData), AlreadyCollectedData> AlreadyCollectedMap;
00271   AlreadyCollectedMap _already_collected_map;
00272 
00273   static PStatCollector _apply_vertex_collector;
00274   static PStatCollector _apply_texcoord_collector;
00275   static PStatCollector _apply_set_color_collector;
00276   static PStatCollector _apply_scale_color_collector;
00277   static PStatCollector _apply_texture_color_collector;
00278   static PStatCollector _apply_set_format_collector;
00279     
00280 public:
00281   static void init_type() {
00282     NewCollectedData::init_type();
00283   }
00284 };
00285 
00286 #include "geomTransformer.I"
00287 
00288 #endif
00289 
 All Classes Functions Variables Enumerations