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