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 LMatrix4f &mat);
00059   bool transform_vertices(GeomNode *node, const LMatrix4f &mat);
00060 
00061   bool transform_texcoords(Geom *geom, const InternalName *from_name,
00062                            InternalName *to_name, const LMatrix4f &mat);
00063   bool transform_texcoords(GeomNode *node, const InternalName *from_name,
00064                            InternalName *to_name, const LMatrix4f &mat);
00065 
00066   bool set_color(Geom *geom, const Colorf &color);
00067   bool set_color(GeomNode *node, const Colorf &color);
00068 
00069   bool transform_colors(Geom *geom, const LVecBase4f &scale);
00070   bool transform_colors(GeomNode *node, const LVecBase4f &scale);
00071 
00072   bool apply_texture_colors(Geom *geom, TextureStage *ts, Texture *tex, 
00073                             const TexMatrixAttrib *tma,
00074                             const Colorf &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   static void get_texel_color(Colorf &color, float u, float v, 
00099                               const unsigned char *image, 
00100                               int x_size, int y_size);
00101 
00102 
00103 private:
00104   int _max_collect_vertices;
00105 
00106   typedef pvector<PT(Geom) > GeomList;
00107 
00108   // Keeps track of the Geoms that are associated with a particular
00109   // GeomVertexData.  Also tracks whether the vertex data might have
00110   // unused vertices because of our actions.
00111   class VertexDataAssoc {
00112   public:
00113     INLINE VertexDataAssoc();
00114     bool _might_have_unused;
00115     GeomList _geoms;
00116     void remove_unused_vertices(const GeomVertexData *vdata);
00117   };
00118   typedef pmap<CPT(GeomVertexData), VertexDataAssoc> VertexDataAssocMap;
00119   VertexDataAssocMap _vdata_assoc;
00120 
00121   // Corresponds to a new GeomVertexData created as needed during an
00122   // apply operation.
00123   class NewVertexData {
00124   public:
00125     CPT(GeomVertexData) _vdata;
00126   };
00127 
00128   // The table of GeomVertexData objects that have been transformed by
00129   // a particular matrix.
00130   class SourceVertices {
00131   public:
00132     INLINE bool operator < (const SourceVertices &other) const;
00133 
00134     LMatrix4f _mat;
00135     CPT(GeomVertexData) _vertex_data;
00136   };
00137   typedef pmap<SourceVertices, NewVertexData> NewVertices;
00138   NewVertices _vertices;
00139 
00140   // The table of GeomVertexData objects whose texture coordinates
00141   // have been transformed by a particular matrix.
00142   class SourceTexCoords {
00143   public:
00144     INLINE bool operator < (const SourceTexCoords &other) const;
00145 
00146     LMatrix4f _mat;
00147     CPT(InternalName) _from;
00148     CPT(InternalName) _to;
00149     CPT(GeomVertexData) _vertex_data;
00150   };
00151   typedef pmap<SourceTexCoords, NewVertexData> NewTexCoords;
00152   NewTexCoords _texcoords;
00153 
00154   // The table of GeomVertexData objects whose colors have been
00155   // modified.
00156   class SourceColors {
00157   public:
00158     INLINE bool operator < (const SourceColors &other) const;
00159 
00160     LVecBase4f _color;
00161     CPT(GeomVertexData) _vertex_data;
00162   };
00163   typedef pmap<SourceColors, NewVertexData> NewColors;
00164 
00165   // We have two concepts of colors: the "fixed" colors, which are
00166   // slapped in as a complete replacement of the original colors
00167   // (e.g. via a ColorAttrib), and the "transformed" colors, which are
00168   // modified from the original colors (e.g. via a ColorScaleAttrib).
00169   NewColors _fcolors, _tcolors;
00170 
00171   // The table of GeomVertexData objects whose texture colors have
00172   // been applied.
00173   class SourceTextureColors {
00174   public:
00175     INLINE bool operator < (const SourceTextureColors &other) const;
00176 
00177     TextureStage *_ts;
00178     Texture *_tex;
00179     const TexMatrixAttrib *_tma;
00180     Colorf _base_color;
00181     bool _keep_vertex_color;
00182     CPT(GeomVertexData) _vertex_data;
00183   };
00184   typedef pmap<SourceTextureColors, NewVertexData> NewTextureColors;
00185   NewTextureColors _tex_colors;
00186 
00187   // The table of GeomVertexData objects whose vertex formats have
00188   // been modified. For set_format(): record (format + vertex_data) ->
00189   // vertex_data.
00190   class SourceFormat {
00191   public:
00192     INLINE bool operator < (const SourceFormat &other) const;
00193 
00194     CPT(GeomVertexFormat) _format;
00195     CPT(GeomVertexData) _vertex_data;
00196   };
00197   typedef pmap<SourceFormat, NewVertexData> NewFormat;
00198   NewFormat _format;
00199 
00200   // The table of GeomVertexData objects whose normals have been
00201   // reversed.
00202   typedef pmap<CPT(GeomVertexData), NewVertexData> ReversedNormals;
00203   ReversedNormals _reversed_normals;
00204 
00205   class NewCollectedKey {
00206   public:
00207     INLINE bool operator < (const NewCollectedKey &other) const;
00208 
00209     string _name;
00210     CPT(GeomVertexFormat) _format;
00211     Geom::UsageHint _usage_hint;
00212     Geom::AnimationType _animation_type;
00213   };
00214 
00215   class SourceData {
00216   public:
00217     const GeomVertexData *_vdata;
00218     int _num_vertices;
00219   };
00220   typedef pvector<SourceData> SourceDatas;
00221   class SourceGeom {
00222   public:
00223     Geom *_geom;
00224     int _vertex_offset;
00225   };
00226   typedef pvector<SourceGeom> SourceGeoms;
00227   class NewCollectedData {
00228   public:
00229     ALLOC_DELETED_CHAIN(NewCollectedData);
00230 
00231     NewCollectedData(const GeomVertexData *source_data);
00232     void add_source_data(const GeomVertexData *source_data);
00233     int apply_format_only_changes();
00234     int apply_collect_changes();
00235 
00236     CPT(GeomVertexFormat) _new_format;
00237     string _vdata_name;
00238     GeomEnums::UsageHint _usage_hint;
00239     SourceDatas _source_datas;
00240     SourceGeoms _source_geoms;
00241     int _num_vertices;
00242 
00243   private:
00244     // These are used just during apply_changes().
00245     void append_vdata(const GeomVertexData *vdata, int vertex_offset);
00246     void update_geoms();
00247 
00248     typedef vector_int IndexMap;
00249 
00250     PT(GeomVertexData) _new_data;
00251     PT(TransformBlendTable) _new_btable;
00252     SparseArray _new_btable_rows;
00253 
00254     // We need a TypeHandle just for ALLOC_DELETED_CHAIN.
00255   public:
00256     static TypeHandle get_class_type() {
00257       return _type_handle;
00258     }
00259     static void init_type() {
00260       register_type(_type_handle, "GeomTransformer::NewCollectedData");
00261     }
00262     
00263   private:
00264     static TypeHandle _type_handle;
00265   };
00266   typedef pvector<NewCollectedData *> NewCollectedList;
00267   typedef pmap<NewCollectedKey, NewCollectedData *> NewCollectedMap;
00268   NewCollectedList _new_collected_list;
00269   NewCollectedMap _new_collected_map;
00270 
00271   class AlreadyCollectedData {
00272   public:
00273     NewCollectedData *_ncd;
00274     int _vertex_offset;
00275   };
00276   typedef pmap<CPT(GeomVertexData), AlreadyCollectedData> AlreadyCollectedMap;
00277   AlreadyCollectedMap _already_collected_map;
00278 
00279   static PStatCollector _apply_vertex_collector;
00280   static PStatCollector _apply_texcoord_collector;
00281   static PStatCollector _apply_set_color_collector;
00282   static PStatCollector _apply_scale_color_collector;
00283   static PStatCollector _apply_texture_color_collector;
00284   static PStatCollector _apply_set_format_collector;
00285     
00286 public:
00287   static void init_type() {
00288     NewCollectedData::init_type();
00289   }
00290 };
00291 
00292 #include "geomTransformer.I"
00293 
00294 #endif
00295 
 All Classes Functions Variables Enumerations