Panda3D
geomTransformer.h
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 geomTransformer.h
10  * @author drose
11  * @date 2002-03-14
12  */
13 
14 #ifndef GEOMTRANSFORMER_H
15 #define GEOMTRANSFORMER_H
16 
17 #include "pandabase.h"
18 
19 #include "luse.h"
20 #include "geom.h"
21 #include "geomVertexData.h"
22 #include "texMatrixAttrib.h"
23 
24 class GeomNode;
25 class RenderState;
26 class InternalName;
27 class GeomMunger;
28 class Texture;
29 
30 /**
31  * An object specifically designed to transform the vertices of a Geom without
32  * disturbing indexing or affecting any other Geoms that may share the same
33  * vertex arrays, and without needlessly wasting memory when different Geoms
34  * sharing the same vertex arrays are transformed by the same amount.
35  *
36  * If you create a single GeomTransformer and use it to transform a number of
37  * different Geoms by various transformations, then those Geoms which happen
38  * to share the same arrays and are transformed by the same amounts will still
39  * share the same arrays as each other (but different from the original
40  * arrays).
41  */
42 class EXPCL_PANDA_PGRAPH GeomTransformer {
43 public:
45  GeomTransformer(const GeomTransformer &copy);
46  ~GeomTransformer();
47 
48  INLINE int get_max_collect_vertices() const;
49  INLINE void set_max_collect_vertices(int max_collect_vertices);
50 
51  void register_vertices(Geom *geom, bool might_have_unused);
52  void register_vertices(GeomNode *node, bool might_have_unused);
53 
54  bool transform_vertices(Geom *geom, const LMatrix4 &mat);
55  bool transform_vertices(GeomNode *node, const LMatrix4 &mat);
56 
57  bool transform_texcoords(Geom *geom, const InternalName *from_name,
58  InternalName *to_name, const LMatrix4 &mat);
59  bool transform_texcoords(GeomNode *node, const InternalName *from_name,
60  InternalName *to_name, const LMatrix4 &mat);
61 
62  bool set_color(Geom *geom, const LColor &color);
63  bool set_color(GeomNode *node, const LColor &color);
64 
65  bool transform_colors(Geom *geom, const LVecBase4 &scale);
66  bool transform_colors(GeomNode *node, const LVecBase4 &scale);
67 
68  bool apply_texture_colors(Geom *geom, TextureStage *ts, Texture *tex,
69  const TexMatrixAttrib *tma,
70  const LColor &base_color, bool keep_vertex_color);
71  bool apply_texture_colors(GeomNode *node, const RenderState *state);
72 
73  bool apply_state(GeomNode *node, const RenderState *state);
74 
75  bool set_format(Geom *geom, const GeomVertexFormat *new_format);
76  bool remove_column(Geom *geom, const InternalName *column);
77  bool remove_column(GeomNode *node, const InternalName *column);
78 
79  bool make_compatible_state(GeomNode *node);
80 
81  bool reverse_normals(Geom *geom);
82  bool doubleside(GeomNode *node);
83  bool reverse(GeomNode *node);
84 
85  void finish_apply();
86 
87  int collect_vertex_data(Geom *geom, int collect_bits, bool format_only);
88  int collect_vertex_data(GeomNode *node, int collect_bits, bool format_only);
89  int finish_collect(bool format_only);
90 
91  PT(Geom) premunge_geom(const Geom *geom, GeomMunger *munger);
92 
93 private:
94  int _max_collect_vertices;
95 
96  typedef pvector<PT(Geom) > GeomList;
97 
98  // Keeps track of the Geoms that are associated with a particular
99  // GeomVertexData. Also tracks whether the vertex data might have unused
100  // vertices because of our actions.
101  class VertexDataAssoc {
102  public:
103  INLINE VertexDataAssoc();
104  bool _might_have_unused;
105  GeomList _geoms;
106  void remove_unused_vertices(const GeomVertexData *vdata);
107  };
108  typedef pmap<CPT(GeomVertexData), VertexDataAssoc> VertexDataAssocMap;
109  VertexDataAssocMap _vdata_assoc;
110 
111  // Corresponds to a new GeomVertexData created as needed during an apply
112  // operation.
113  class NewVertexData {
114  public:
115  CPT(GeomVertexData) _vdata;
116  };
117 
118  // The table of GeomVertexData objects that have been transformed by a
119  // particular matrix.
120  class SourceVertices {
121  public:
122  INLINE bool operator < (const SourceVertices &other) const;
123 
124  LMatrix4 _mat;
125  CPT(GeomVertexData) _vertex_data;
126  };
128  NewVertices _vertices;
129 
130  // The table of GeomVertexData objects whose texture coordinates have been
131  // transformed by a particular matrix.
132  class SourceTexCoords {
133  public:
134  INLINE bool operator < (const SourceTexCoords &other) const;
135 
136  LMatrix4 _mat;
137  CPT(InternalName) _from;
138  CPT(InternalName) _to;
139  CPT(GeomVertexData) _vertex_data;
140  };
142  NewTexCoords _texcoords;
143 
144  // The table of GeomVertexData objects whose colors have been modified.
145  class SourceColors {
146  public:
147  INLINE bool operator < (const SourceColors &other) const;
148 
149  LVecBase4 _color;
150  CPT(GeomVertexData) _vertex_data;
151  };
153 
154  // We have two concepts of colors: the "fixed" colors, which are slapped in
155  // as a complete replacement of the original colors (e.g. via a
156  // ColorAttrib), and the "transformed" colors, which are modified from the
157  // original colors (e.g. via a ColorScaleAttrib).
158  NewColors _fcolors, _tcolors;
159 
160  // The table of GeomVertexData objects whose texture colors have been
161  // applied.
162  class SourceTextureColors {
163  public:
164  INLINE bool operator < (const SourceTextureColors &other) const;
165 
166  TextureStage *_ts;
167  Texture *_tex;
168  const TexMatrixAttrib *_tma;
169  LColor _base_color;
170  bool _keep_vertex_color;
171  CPT(GeomVertexData) _vertex_data;
172  };
174  NewTextureColors _tex_colors;
175 
176  // The table of GeomVertexData objects whose vertex formats have been
177  // modified. For set_format(): record (format + vertex_data) ->
178  // vertex_data.
179  class SourceFormat {
180  public:
181  INLINE bool operator < (const SourceFormat &other) const;
182 
183  CPT(GeomVertexFormat) _format;
184  CPT(GeomVertexData) _vertex_data;
185  };
187  NewFormat _format;
188 
189  // The table of GeomVertexData objects whose normals have been reversed.
190  typedef pmap<CPT(GeomVertexData), NewVertexData> ReversedNormals;
191  ReversedNormals _reversed_normals;
192 
193  class NewCollectedKey {
194  public:
195  INLINE bool operator < (const NewCollectedKey &other) const;
196 
197  std::string _name;
198  CPT(GeomVertexFormat) _format;
199  Geom::UsageHint _usage_hint;
200  Geom::AnimationType _animation_type;
201  };
202 
203  class SourceData {
204  public:
205  const GeomVertexData *_vdata;
206  int _num_vertices;
207  };
209  class SourceGeom {
210  public:
211  Geom *_geom;
212  int _vertex_offset;
213  };
215  class NewCollectedData {
216  public:
217  ALLOC_DELETED_CHAIN(NewCollectedData);
218 
219  NewCollectedData(const GeomVertexData *source_data);
220  void add_source_data(const GeomVertexData *source_data);
221  int apply_format_only_changes();
222  int apply_collect_changes();
223 
224  CPT(GeomVertexFormat) _new_format;
225  std::string _vdata_name;
226  GeomEnums::UsageHint _usage_hint;
227  SourceDatas _source_datas;
228  SourceGeoms _source_geoms;
229  int _num_vertices;
230 
231  private:
232  // These are used just during apply_changes().
233  void append_vdata(const GeomVertexData *vdata, int vertex_offset);
234  void update_geoms();
235 
236  typedef vector_int IndexMap;
237 
238  PT(GeomVertexData) _new_data;
239  PT(TransformBlendTable) _new_btable;
240  SparseArray _new_btable_rows;
241 
242  // We need a TypeHandle just for ALLOC_DELETED_CHAIN.
243  public:
244  static TypeHandle get_class_type() {
245  return _type_handle;
246  }
247  static void init_type() {
248  register_type(_type_handle, "GeomTransformer::NewCollectedData");
249  }
250 
251  private:
252  static TypeHandle _type_handle;
253  };
256  NewCollectedList _new_collected_list;
257  NewCollectedMap _new_collected_map;
258 
259  class AlreadyCollectedData {
260  public:
261  NewCollectedData *_ncd;
262  int _vertex_offset;
263  };
264  typedef pmap<CPT(GeomVertexData), AlreadyCollectedData> AlreadyCollectedMap;
265  AlreadyCollectedMap _already_collected_map;
266 
267  static PStatCollector _apply_vertex_collector;
268  static PStatCollector _apply_texcoord_collector;
269  static PStatCollector _apply_set_color_collector;
270  static PStatCollector _apply_scale_color_collector;
271  static PStatCollector _apply_texture_color_collector;
272  static PStatCollector _apply_set_format_collector;
273 
274 public:
275  static void init_type() {
276  NewCollectedData::init_type();
277  }
278 };
279 
280 #include "geomTransformer.I"
281 
282 #endif
This class records a set of integers, where each integer is either present or not present in the set.
Definition: sparseArray.h:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL map.
Definition: pmap.h:49
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Objects of this class are used to convert vertex data from a Geom into a format suitable for passing ...
Definition: geomMunger.h:50
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 register_type(TypeHandle &type_handle, const std::string &name)
This inline function is just a convenient way to call TypeRegistry::register_type(),...
Definition: register_type.I:22
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
A lightweight class that represents a single element that may be timed and/or counted via stats.
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
A container for geometry primitives.
Definition: geom.h:54
Encodes a string name in a hash table, mapping it to a pointer.
Definition: internalName.h:38
This represents a unique collection of RenderAttrib objects that correspond to a particular renderabl...
Definition: renderState.h:47
This class defines the physical layout of the vertex data stored within a Geom.
Applies a transform matrix to UV's before they are rendered.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This structure collects together the different combinations of transforms and blend amounts used by a...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
Defines the properties of a named stage of the multitexture pipeline.
Definition: textureStage.h:35
A node that holds Geom objects, renderable pieces of geometry.
Definition: geomNode.h:34
An object specifically designed to transform the vertices of a Geom without disturbing indexing or af...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.