Panda3D
|
00001 // Filename: geomVertexFormat.h 00002 // Created by: drose (07Mar05) 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 GEOMVERTEXFORMAT_H 00016 #define GEOMVERTEXFORMAT_H 00017 00018 #include "pandabase.h" 00019 #include "typedWritableReferenceCount.h" 00020 #include "geomVertexAnimationSpec.h" 00021 #include "geomVertexArrayFormat.h" 00022 #include "geomEnums.h" 00023 #include "internalName.h" 00024 #include "luse.h" 00025 #include "pointerTo.h" 00026 #include "pmap.h" 00027 #include "pset.h" 00028 #include "pvector.h" 00029 #include "indirectCompareTo.h" 00030 #include "lightReMutex.h" 00031 00032 class FactoryParams; 00033 class GeomVertexData; 00034 class GeomMunger; 00035 00036 //////////////////////////////////////////////////////////////////// 00037 // Class : GeomVertexFormat 00038 // Description : This class defines the physical layout of the vertex 00039 // data stored within a Geom. The layout consists of a 00040 // list of named columns, each of which has a numeric 00041 // type and a size. 00042 // 00043 // The columns are typically interleaved within a single 00044 // array, but they may also be distributed among 00045 // multiple different arrays; at the extreme, each 00046 // column may be alone within its own array (which 00047 // amounts to a parallel-array definition). 00048 // 00049 // Thus, a GeomVertexFormat is really a list of 00050 // GeomVertexArrayFormats, each of which contains a list 00051 // of columns. However, a particular column name should 00052 // not appear more than once in the format, even between 00053 // different arrays. 00054 // 00055 // There are a handful of standard pre-defined 00056 // GeomVertexFormat objects, or you may define your own 00057 // as needed. You may record any combination of 00058 // standard and/or user-defined columns in your custom 00059 // GeomVertexFormat constructions. 00060 //////////////////////////////////////////////////////////////////// 00061 class EXPCL_PANDA_GOBJ GeomVertexFormat : public TypedWritableReferenceCount, public GeomEnums { 00062 PUBLISHED: 00063 GeomVertexFormat(); 00064 GeomVertexFormat(const GeomVertexArrayFormat *array_format); 00065 GeomVertexFormat(const GeomVertexFormat ©); 00066 void operator = (const GeomVertexFormat ©); 00067 virtual ~GeomVertexFormat(); 00068 00069 virtual bool unref() const; 00070 00071 INLINE bool is_registered() const; 00072 INLINE static CPT(GeomVertexFormat) register_format(const GeomVertexFormat *format); 00073 INLINE static CPT(GeomVertexFormat) register_format(const GeomVertexArrayFormat *format); 00074 00075 INLINE const GeomVertexAnimationSpec &get_animation() const; 00076 INLINE void set_animation(const GeomVertexAnimationSpec &animation); 00077 00078 CPT(GeomVertexFormat) get_post_animated_format() const; 00079 CPT(GeomVertexFormat) get_union_format(const GeomVertexFormat *other) const; 00080 00081 INLINE int get_num_arrays() const; 00082 INLINE const GeomVertexArrayFormat *get_array(int array) const; 00083 MAKE_SEQ(get_arrays, get_num_arrays, get_array); 00084 GeomVertexArrayFormat *modify_array(int array); 00085 void set_array(int array, const GeomVertexArrayFormat *format); 00086 void remove_array(int array); 00087 int add_array(const GeomVertexArrayFormat *array_format); 00088 void insert_array(int array, const GeomVertexArrayFormat *array_format); 00089 void clear_arrays(); 00090 00091 int get_num_columns() const; 00092 int get_array_with(int i) const; 00093 const GeomVertexColumn *get_column(int i) const; 00094 00095 int get_array_with(const InternalName *name) const; 00096 const GeomVertexColumn *get_column(const InternalName *name) const; 00097 INLINE bool has_column(const InternalName *name) const; 00098 00099 MAKE_SEQ(get_columns, get_num_columns, get_column); 00100 00101 void remove_column(const InternalName *name); 00102 void pack_columns(); 00103 00104 INLINE int get_num_points() const; 00105 INLINE const InternalName *get_point(int n) const; 00106 MAKE_SEQ(get_points, get_num_points, get_point); 00107 00108 INLINE int get_num_vectors() const; 00109 INLINE const InternalName *get_vector(int n) const; 00110 MAKE_SEQ(get_vectors, get_num_vectors, get_vector); 00111 00112 INLINE int get_num_texcoords() const; 00113 INLINE const InternalName *get_texcoord(int n) const; 00114 MAKE_SEQ(get_texcoords, get_num_texcoords, get_texcoord); 00115 00116 INLINE int get_num_morphs() const; 00117 INLINE const InternalName *get_morph_slider(int n) const; 00118 INLINE const InternalName *get_morph_base(int n) const; 00119 INLINE const InternalName *get_morph_delta(int n) const; 00120 MAKE_SEQ(get_morph_sliders, get_num_morphs, get_morph_slider); 00121 MAKE_SEQ(get_morph_bases, get_num_morphs, get_morph_base); 00122 MAKE_SEQ(get_morph_deltas, get_num_morphs, get_morph_delta); 00123 00124 void output(ostream &out) const; 00125 void write(ostream &out, int indent_level = 0) const; 00126 void write_with_data(ostream &out, int indent_level, 00127 const GeomVertexData *data) const; 00128 00129 // Some standard vertex formats. No particular requirement to use 00130 // one of these, but the DirectX renderers can use these formats 00131 // directly, whereas any other format will have to be converted 00132 // first. 00133 INLINE static const GeomVertexFormat *get_v3(); 00134 INLINE static const GeomVertexFormat *get_v3n3(); 00135 INLINE static const GeomVertexFormat *get_v3t2(); 00136 INLINE static const GeomVertexFormat *get_v3n3t2(); 00137 00138 // These formats, with the DirectX-style packed color, are not 00139 // supported directly by OpenGL. If you use them, the 00140 // GLGraphicsStateGuardian will automatically convert to OpenGL 00141 // form (with a small runtime overhead). 00142 INLINE static const GeomVertexFormat *get_v3cp(); 00143 INLINE static const GeomVertexFormat *get_v3cpt2(); 00144 INLINE static const GeomVertexFormat *get_v3n3cp(); 00145 INLINE static const GeomVertexFormat *get_v3n3cpt2(); 00146 00147 // These formats, with an OpenGL-style four-byte color, are not 00148 // supported directly by DirectX. If you use them, the 00149 // DXGraphicsStateGuardian will automatically convert to DirectX 00150 // form (with a larger runtime overhead, since DirectX8, and old 00151 // DirectX9 drivers, require everything to be interleaved together). 00152 INLINE static const GeomVertexFormat *get_v3c4(); 00153 INLINE static const GeomVertexFormat *get_v3c4t2(); 00154 INLINE static const GeomVertexFormat *get_v3n3c4(); 00155 INLINE static const GeomVertexFormat *get_v3n3c4t2(); 00156 00157 public: 00158 bool get_array_info(const InternalName *name, 00159 int &array_index, 00160 const GeomVertexColumn *&column) const; 00161 00162 INLINE int get_vertex_array_index() const; 00163 INLINE const GeomVertexColumn *get_vertex_column() const; 00164 INLINE int get_normal_array_index() const; 00165 INLINE const GeomVertexColumn *get_normal_column() const; 00166 INLINE int get_color_array_index() const; 00167 INLINE const GeomVertexColumn *get_color_column() const; 00168 00169 int compare_to(const GeomVertexFormat &other) const; 00170 00171 private: 00172 class Registry; 00173 INLINE static Registry *get_registry(); 00174 static void make_registry(); 00175 00176 void do_register(); 00177 void do_unregister(); 00178 00179 bool _is_registered; 00180 00181 GeomVertexAnimationSpec _animation; 00182 00183 typedef pvector< PT(GeomVertexArrayFormat) > Arrays; 00184 Arrays _arrays; 00185 00186 class DataTypeRecord { 00187 public: 00188 int _array_index; 00189 int _column_index; 00190 }; 00191 00192 typedef pmap<const InternalName *, DataTypeRecord> DataTypesByName; 00193 DataTypesByName _columns_by_name; 00194 00195 int _vertex_array_index; 00196 const GeomVertexColumn *_vertex_column; 00197 int _normal_array_index; 00198 const GeomVertexColumn *_normal_column; 00199 int _color_array_index; 00200 const GeomVertexColumn *_color_column; 00201 00202 typedef pvector< CPT(InternalName) > Columns; 00203 Columns _points; 00204 Columns _vectors; 00205 Columns _texcoords; 00206 00207 class MorphRecord { 00208 public: 00209 CPT(InternalName) _slider; 00210 CPT(InternalName) _base; 00211 CPT(InternalName) _delta; 00212 }; 00213 typedef pvector<MorphRecord> Morphs; 00214 Morphs _morphs; 00215 00216 const GeomVertexFormat *_post_animated_format; 00217 00218 // This is the global registry of all currently-in-use formats. 00219 typedef pset<GeomVertexFormat *, IndirectCompareTo<GeomVertexFormat> > Formats; 00220 class EXPCL_PANDA_GOBJ Registry { 00221 public: 00222 Registry(); 00223 void make_standard_formats(); 00224 00225 CPT(GeomVertexFormat) register_format(GeomVertexFormat *format); 00226 INLINE CPT(GeomVertexFormat) register_format(GeomVertexArrayFormat *format); 00227 void unregister_format(GeomVertexFormat *format); 00228 00229 Formats _formats; 00230 LightReMutex _lock; 00231 00232 CPT(GeomVertexFormat) _v3; 00233 CPT(GeomVertexFormat) _v3n3; 00234 CPT(GeomVertexFormat) _v3t2; 00235 CPT(GeomVertexFormat) _v3n3t2; 00236 00237 CPT(GeomVertexFormat) _v3cp; 00238 CPT(GeomVertexFormat) _v3n3cp; 00239 CPT(GeomVertexFormat) _v3cpt2; 00240 CPT(GeomVertexFormat) _v3n3cpt2; 00241 00242 CPT(GeomVertexFormat) _v3c4; 00243 CPT(GeomVertexFormat) _v3n3c4; 00244 CPT(GeomVertexFormat) _v3c4t2; 00245 CPT(GeomVertexFormat) _v3n3c4t2; 00246 }; 00247 00248 static Registry *_registry; 00249 00250 00251 public: 00252 static void register_with_read_factory(); 00253 virtual void write_datagram(BamWriter *manager, Datagram &dg); 00254 virtual int complete_pointers(TypedWritable **plist, BamReader *manager); 00255 00256 protected: 00257 static TypedWritable *make_from_bam(const FactoryParams ¶ms); 00258 void fillin(DatagramIterator &scan, BamReader *manager); 00259 00260 public: 00261 static TypeHandle get_class_type() { 00262 return _type_handle; 00263 } 00264 static void init_type() { 00265 TypedWritableReferenceCount::init_type(); 00266 register_type(_type_handle, "GeomVertexFormat", 00267 TypedWritableReferenceCount::get_class_type()); 00268 } 00269 virtual TypeHandle get_type() const { 00270 return get_class_type(); 00271 } 00272 virtual TypeHandle force_init_type() {init_type(); return get_class_type();} 00273 00274 private: 00275 static TypeHandle _type_handle; 00276 00277 friend class GeomVertexFormat::Registry; 00278 friend class GeomMunger; 00279 }; 00280 00281 INLINE ostream &operator << (ostream &out, const GeomVertexFormat &obj); 00282 00283 #include "geomVertexFormat.I" 00284 00285 #endif