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