Panda3D
geomVertexFormat.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 geomVertexFormat.h
10  * @author drose
11  * @date 2005-03-07
12  */
13 
14 #ifndef GEOMVERTEXFORMAT_H
15 #define GEOMVERTEXFORMAT_H
16 
17 #include "pandabase.h"
20 #include "geomVertexArrayFormat.h"
21 #include "geomEnums.h"
22 #include "internalName.h"
23 #include "luse.h"
24 #include "pointerTo.h"
25 #include "pmap.h"
26 #include "pset.h"
27 #include "pvector.h"
28 #include "indirectCompareTo.h"
29 #include "lightReMutex.h"
30 
31 class FactoryParams;
32 class GeomVertexData;
33 class GeomMunger;
34 
35 /**
36  * This class defines the physical layout of the vertex data stored within a
37  * Geom. The layout consists of a list of named columns, each of which has a
38  * numeric type and a size.
39  *
40  * The columns are typically interleaved within a single array, but they may
41  * also be distributed among multiple different arrays; at the extreme, each
42  * column may be alone within its own array (which amounts to a parallel-array
43  * definition).
44  *
45  * Thus, a GeomVertexFormat is really a list of GeomVertexArrayFormats, each
46  * of which contains a list of columns. However, a particular column name
47  * should not appear more than once in the format, even between different
48  * arrays.
49  *
50  * There are a handful of standard pre-defined GeomVertexFormat objects, or
51  * you may define your own as needed. You may record any combination of
52  * standard and/or user-defined columns in your custom GeomVertexFormat
53  * constructions.
54  */
55 class EXPCL_PANDA_GOBJ GeomVertexFormat final : public TypedWritableReferenceCount, public GeomEnums {
56 PUBLISHED:
58  GeomVertexFormat(const GeomVertexArrayFormat *array_format);
60  void operator = (const GeomVertexFormat &copy);
61  virtual ~GeomVertexFormat();
62 
63  virtual bool unref() const;
64 
65  INLINE bool is_registered() const;
66  INLINE static CPT(GeomVertexFormat) register_format(const GeomVertexFormat *format);
67  INLINE static CPT(GeomVertexFormat) register_format(const GeomVertexArrayFormat *format);
68  MAKE_PROPERTY(registered, is_registered);
69 
70  INLINE const GeomVertexAnimationSpec &get_animation() const;
71  INLINE void set_animation(const GeomVertexAnimationSpec &animation);
72  MAKE_PROPERTY(animation, get_animation, set_animation);
73 
74  CPT(GeomVertexFormat) get_post_animated_format() const;
75  CPT(GeomVertexFormat) get_union_format(const GeomVertexFormat *other) const;
76 
77  INLINE size_t get_num_arrays() const;
78  INLINE const GeomVertexArrayFormat *get_array(size_t array) const;
79  MAKE_SEQ(get_arrays, get_num_arrays, get_array);
80  GeomVertexArrayFormat *modify_array(size_t array);
81  void set_array(size_t array, const GeomVertexArrayFormat *format);
82  void remove_array(size_t array);
83  size_t add_array(const GeomVertexArrayFormat *array_format);
84  void insert_array(size_t array, const GeomVertexArrayFormat *array_format);
85  void clear_arrays();
86  void remove_empty_arrays();
87 
88  size_t get_num_columns() const;
89  int get_array_with(size_t i) const;
90  const GeomVertexColumn *get_column(size_t i) const;
91 
92  int get_array_with(const InternalName *name) const;
93  const GeomVertexColumn *get_column(const InternalName *name) const;
94  INLINE bool has_column(const InternalName *name) const;
95  const InternalName *get_column_name(size_t i) const;
96 
97  MAKE_SEQ(get_columns, get_num_columns, get_column);
98 
99  void remove_column(const InternalName *name, bool keep_empty_array = false);
100  void pack_columns();
101  void align_columns_for_animation();
102  void maybe_align_columns_for_animation();
103 
104  INLINE size_t get_num_points() const;
105  INLINE const InternalName *get_point(size_t n) const;
106  MAKE_SEQ(get_points, get_num_points, get_point);
107 
108  INLINE size_t get_num_vectors() const;
109  INLINE const InternalName *get_vector(size_t n) const;
110  MAKE_SEQ(get_vectors, get_num_vectors, get_vector);
111 
112  INLINE size_t get_num_texcoords() const;
113  INLINE const InternalName *get_texcoord(size_t n) const;
114  MAKE_SEQ(get_texcoords, get_num_texcoords, get_texcoord);
115 
116  INLINE size_t get_num_morphs() const;
117  INLINE const InternalName *get_morph_slider(size_t n) const;
118  INLINE const InternalName *get_morph_base(size_t n) const;
119  INLINE const InternalName *get_morph_delta(size_t n) const;
120  MAKE_SEQ(get_morph_sliders, get_num_morphs, get_morph_slider);
121  MAKE_SEQ(get_morph_bases, get_num_morphs, get_morph_base);
122  MAKE_SEQ(get_morph_deltas, get_num_morphs, get_morph_delta);
123 
124  MAKE_SEQ_PROPERTY(arrays, get_num_arrays, get_array, set_array, remove_array, insert_array);
125  MAKE_SEQ_PROPERTY(points, get_num_points, get_point);
126  MAKE_SEQ_PROPERTY(vectors, get_num_vectors, get_vector);
127 
128  // We also define this as a mapping interface, for lookups by name.
129  MAKE_MAP_PROPERTY(columns, has_column, get_column);
130  MAKE_MAP_KEYS_SEQ(columns, get_num_columns, get_column_name);
131 
132  void output(std::ostream &out) const;
133  void write(std::ostream &out, int indent_level = 0) const;
134  void write_with_data(std::ostream &out, int indent_level,
135  const GeomVertexData *data) const;
136 
137  INLINE static const GeomVertexFormat *get_empty();
138 
139  // Some standard vertex formats. No particular requirement to use one of
140  // these, but the DirectX renderers can use these formats directly, whereas
141  // any other format will have to be converted first.
142  INLINE static const GeomVertexFormat *get_v3();
143  INLINE static const GeomVertexFormat *get_v3n3();
144  INLINE static const GeomVertexFormat *get_v3t2();
145  INLINE static const GeomVertexFormat *get_v3n3t2();
146 
147  // These formats, with the DirectX-style packed color, may not be supported
148  // directly by OpenGL. If you use them and the driver does not support
149  // them, the GLGraphicsStateGuardian will automatically convert to native
150  // OpenGL form (with a small runtime overhead).
151  INLINE static const GeomVertexFormat *get_v3cp();
152  INLINE static const GeomVertexFormat *get_v3cpt2();
153  INLINE static const GeomVertexFormat *get_v3n3cp();
154  INLINE static const GeomVertexFormat *get_v3n3cpt2();
155 
156  // These formats, with an OpenGL-style four-byte color, are not supported
157  // directly by DirectX. If you use them, the DXGraphicsStateGuardian will
158  // automatically convert to DirectX form (with a larger runtime overhead,
159  // since DirectX8, and old DirectX9 drivers, require everything to be
160  // interleaved together).
161  INLINE static const GeomVertexFormat *get_v3c4();
162  INLINE static const GeomVertexFormat *get_v3c4t2();
163  INLINE static const GeomVertexFormat *get_v3n3c4();
164  INLINE static const GeomVertexFormat *get_v3n3c4t2();
165 
166 public:
167  bool get_array_info(const InternalName *name,
168  int &array_index,
169  const GeomVertexColumn *&column) const;
170 
171  INLINE int get_vertex_array_index() const;
172  INLINE const GeomVertexColumn *get_vertex_column() const;
173  INLINE int get_normal_array_index() const;
174  INLINE const GeomVertexColumn *get_normal_column() const;
175  INLINE int get_color_array_index() const;
176  INLINE const GeomVertexColumn *get_color_column() const;
177 
178  int compare_to(const GeomVertexFormat &other) const;
179 
180 private:
181  class Registry;
182  INLINE static Registry *get_registry();
183  static void make_registry();
184 
185  void do_register();
186  void do_unregister();
187 
188  bool _is_registered;
189 
190  GeomVertexAnimationSpec _animation;
191 
192  typedef pvector< PT(GeomVertexArrayFormat) > Arrays;
193  Arrays _arrays;
194 
195  class DataTypeRecord {
196  public:
197  int _array_index;
198  int _column_index;
199  };
200 
201  typedef pmap<const InternalName *, DataTypeRecord> DataTypesByName;
202  DataTypesByName _columns_by_name;
203 
204  int _vertex_array_index;
205  const GeomVertexColumn *_vertex_column;
206  int _normal_array_index;
207  const GeomVertexColumn *_normal_column;
208  int _color_array_index;
209  const GeomVertexColumn *_color_column;
210 
211  typedef pvector<CPT_InternalName> Columns;
212  Columns _points;
213  Columns _vectors;
214  Columns _texcoords;
215 
216  class MorphRecord {
217  public:
218  CPT_InternalName _slider;
219  CPT_InternalName _base;
220  CPT_InternalName _delta;
221  };
222  typedef pvector<MorphRecord> Morphs;
223  Morphs _morphs;
224 
225  const GeomVertexFormat *_post_animated_format;
226 
227  // This is the global registry of all currently-in-use formats.
229  class EXPCL_PANDA_GOBJ Registry {
230  public:
231  Registry();
232  void make_standard_formats();
233 
234  CPT(GeomVertexFormat) register_format(GeomVertexFormat *format);
235  INLINE CPT(GeomVertexFormat) register_format(GeomVertexArrayFormat *format);
236  void unregister_format(GeomVertexFormat *format);
237 
238  Formats _formats;
239  LightReMutex _lock;
240 
241  CPT(GeomVertexFormat) _empty;
242 
243  CPT(GeomVertexFormat) _v3;
244  CPT(GeomVertexFormat) _v3n3;
245  CPT(GeomVertexFormat) _v3t2;
246  CPT(GeomVertexFormat) _v3n3t2;
247 
248  CPT(GeomVertexFormat) _v3cp;
249  CPT(GeomVertexFormat) _v3n3cp;
250  CPT(GeomVertexFormat) _v3cpt2;
251  CPT(GeomVertexFormat) _v3n3cpt2;
252 
253  CPT(GeomVertexFormat) _v3c4;
254  CPT(GeomVertexFormat) _v3n3c4;
255  CPT(GeomVertexFormat) _v3c4t2;
256  CPT(GeomVertexFormat) _v3n3c4t2;
257  };
258 
259  static Registry *_registry;
260 
261 
262 public:
263  static void register_with_read_factory();
264  virtual void write_datagram(BamWriter *manager, Datagram &dg);
265  virtual int complete_pointers(TypedWritable **plist, BamReader *manager);
266 
267 protected:
268  static TypedWritable *make_from_bam(const FactoryParams &params);
269  void fillin(DatagramIterator &scan, BamReader *manager);
270 
271 public:
272  static TypeHandle get_class_type() {
273  return _type_handle;
274  }
275  static void init_type() {
276  TypedWritableReferenceCount::init_type();
277  register_type(_type_handle, "GeomVertexFormat",
278  TypedWritableReferenceCount::get_class_type());
279  }
280  virtual TypeHandle get_type() const {
281  return get_class_type();
282  }
283  virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
284 
285 private:
286  static TypeHandle _type_handle;
287 
288  friend class GeomVertexFormat::Registry;
289  friend class GeomMunger;
290 };
291 
292 INLINE std::ostream &operator << (std::ostream &out, const GeomVertexFormat &obj);
293 
294 #include "geomVertexFormat.I"
295 
296 #endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is a const pointer to an InternalName, and should be used in lieu of a CPT(InternalName) in func...
Definition: internalName.h:193
This is the fundamental interface for extracting binary objects from a Bam file, as generated by a Ba...
Definition: bamReader.h:110
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This object describes how the vertex animation, if any, represented in a GeomVertexData is encoded.
Objects of this class are used to convert vertex data from a Geom into a format suitable for passing ...
Definition: geomMunger.h:50
This class exists just to provide scoping for the various enumerated types used by Geom,...
Definition: geomEnums.h:24
Base class for objects that can be written to and read from Bam files.
Definition: typedWritable.h:35
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.
A lightweight reentrant mutex.
Definition: lightReMutex.h:30
This is the fundamental interface for writing binary objects to a Bam file, to be extracted later by ...
Definition: bamWriter.h:63
This is our own Panda specialization on the default STL list.
Definition: plist.h:35
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This defines how a single column is interleaved within a vertex array stored within a Geom.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This is our own Panda specialization on the default STL vector.
Definition: pvector.h:42
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This defines the actual numeric vertex data stored in a Geom, in the structure defined by a particula...
An instance of this class is passed to the Factory when requesting it to do its business and construc...
Definition: factoryParams.h:36
A base class for things which need to inherit from both TypedWritable and from ReferenceCount.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
Encodes a string name in a hash table, mapping it to a pointer.
Definition: internalName.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This class defines the physical layout of the vertex data stored within a Geom.
An STL function object class, this is intended to be used on any ordered collection of class objects ...
Definition: stl_compares.h:73
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This describes the structure of a single array within a Geom data.
This is our own Panda specialization on the default STL set.
Definition: pset.h:49
A class to retrieve the individual data elements previously stored in a Datagram.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual bool unref() const
Explicitly decrements the reference count.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.