Panda3D

geomVertexColumn.h

00001 // Filename: geomVertexColumn.h
00002 // Created by:  drose (06Mar05)
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 GEOMVERTEXCOLUMN_H
00016 #define GEOMVERTEXCOLUMN_H
00017 
00018 #include "pandabase.h"
00019 #include "geomEnums.h"
00020 #include "internalName.h"
00021 #include "pointerTo.h"
00022 #include "luse.h"
00023 
00024 class TypedWritable;
00025 class BamWriter;
00026 class BamReader;
00027 class Datagram;
00028 class DatagramIterator;
00029 
00030 class GeomVertexReader;
00031 class GeomVertexWriter;
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //       Class : GeomVertexColumn
00035 // Description : This defines how a single column is interleaved
00036 //               within a vertex array stored within a Geom.  The
00037 //               GeomVertexArrayFormat class maintains a list of these
00038 //               to completely define a particular array structure.
00039 ////////////////////////////////////////////////////////////////////
00040 class EXPCL_PANDA_GOBJ GeomVertexColumn : public GeomEnums {
00041 PUBLISHED:
00042 private:
00043   INLINE GeomVertexColumn();
00044 PUBLISHED:
00045   INLINE GeomVertexColumn(InternalName *name, int num_components,
00046                           NumericType numeric_type, Contents contents,
00047                           int start);
00048   INLINE GeomVertexColumn(const GeomVertexColumn &copy);
00049   void operator = (const GeomVertexColumn &copy);
00050   INLINE ~GeomVertexColumn();
00051 
00052   INLINE InternalName *get_name() const;
00053   INLINE int get_num_components() const;
00054   INLINE int get_num_values() const;
00055   INLINE NumericType get_numeric_type() const;
00056   INLINE Contents get_contents() const;
00057   INLINE int get_start() const;
00058   INLINE int get_component_bytes() const;
00059   INLINE int get_total_bytes() const;
00060   INLINE bool has_homogeneous_coord() const;
00061 
00062   INLINE bool overlaps_with(int start_byte, int num_bytes) const;
00063   INLINE bool is_bytewise_equivalent(const GeomVertexColumn &other) const;
00064 
00065   void output(ostream &out) const;
00066 
00067 public:
00068   INLINE bool is_packed_argb() const;
00069   INLINE bool is_uint8_rgba() const;
00070 
00071   INLINE int compare_to(const GeomVertexColumn &other) const;
00072   INLINE bool operator == (const GeomVertexColumn &other) const;
00073   INLINE bool operator != (const GeomVertexColumn &other) const;
00074   INLINE bool operator < (const GeomVertexColumn &other) const;
00075 
00076 private:
00077   class Packer;
00078 
00079   void setup();
00080   Packer *make_packer() const;
00081 
00082 public:
00083   void write_datagram(BamWriter *manager, Datagram &dg);
00084   int complete_pointers(TypedWritable **plist, BamReader *manager);
00085   void fillin(DatagramIterator &scan, BamReader *manager);
00086 
00087 private:
00088   PT(InternalName) _name;
00089   int _num_components;
00090   int _num_values;
00091   NumericType _numeric_type;
00092   Contents _contents;
00093   int _start;
00094   int _component_bytes;
00095   int _total_bytes;
00096   Packer *_packer;
00097 
00098   // This nested class provides the implementation for packing and
00099   // unpacking data in a very general way, but also provides the hooks
00100   // for implementing the common, very direct code paths (for
00101   // instance, 3-component float32 to LVecBase3f) as quickly as
00102   // possible.
00103   class Packer {
00104   public:
00105     virtual ~Packer();
00106     virtual float get_data1f(const unsigned char *pointer);
00107     virtual const LVecBase2f &get_data2f(const unsigned char *pointer);
00108     virtual const LVecBase3f &get_data3f(const unsigned char *pointer);
00109     virtual const LVecBase4f &get_data4f(const unsigned char *pointer);
00110     virtual int get_data1i(const unsigned char *pointer);
00111     virtual const int *get_data2i(const unsigned char *pointer);
00112     virtual const int *get_data3i(const unsigned char *pointer);
00113     virtual const int *get_data4i(const unsigned char *pointer);
00114 
00115     virtual void set_data1f(unsigned char *pointer, float data);
00116     virtual void set_data2f(unsigned char *pointer, const LVecBase2f &data);
00117     virtual void set_data3f(unsigned char *pointer, const LVecBase3f &data);
00118     virtual void set_data4f(unsigned char *pointer, const LVecBase4f &data);
00119     
00120     virtual void set_data1i(unsigned char *pointer, int a);
00121     virtual void set_data2i(unsigned char *pointer, int a, int b);
00122     virtual void set_data3i(unsigned char *pointer, int a, int b, int c);
00123     virtual void set_data4i(unsigned char *pointer, int a, int b, int c, int d);
00124 
00125     virtual const char *get_name() const {
00126       return "Packer";
00127     }
00128 
00129     INLINE float maybe_scale_color(unsigned int value);
00130     INLINE void maybe_scale_color(unsigned int a, unsigned int b);
00131     INLINE void maybe_scale_color(unsigned int a, unsigned int b,
00132                                   unsigned int c);
00133     INLINE void maybe_scale_color(unsigned int a, unsigned int b,
00134                                   unsigned int c, unsigned int d);
00135 
00136     INLINE unsigned int maybe_unscale_color(float data);
00137     INLINE void maybe_unscale_color(const LVecBase2f &data);
00138     INLINE void maybe_unscale_color(const LVecBase3f &data);
00139     INLINE void maybe_unscale_color(const LVecBase4f &data);
00140 
00141     const GeomVertexColumn *_column;
00142     LVecBase2f _v2;
00143     LVecBase3f _v3;
00144     LVecBase4f _v4;
00145     int _i[4];
00146     unsigned int _a, _b, _c, _d;
00147   };
00148 
00149 
00150   // This is a specialization on the generic Packer that handles
00151   // points, which are special because the fourth component, if not
00152   // present in the data, is implicitly 1.0; and if it is present,
00153   // than any three-component or smaller return is implicitly divided
00154   // by the fourth component.
00155   class Packer_point : public Packer {
00156   public:
00157     virtual float get_data1f(const unsigned char *pointer);
00158     virtual const LVecBase2f &get_data2f(const unsigned char *pointer);
00159     virtual const LVecBase3f &get_data3f(const unsigned char *pointer);
00160     virtual const LVecBase4f &get_data4f(const unsigned char *pointer);
00161     virtual void set_data1f(unsigned char *pointer, float data);
00162     virtual void set_data2f(unsigned char *pointer, const LVecBase2f &data);
00163     virtual void set_data3f(unsigned char *pointer, const LVecBase3f &data);
00164     virtual void set_data4f(unsigned char *pointer, const LVecBase4f &data);
00165 
00166     virtual const char *get_name() const {
00167       return "Packer_point";
00168     }
00169   };
00170 
00171   // This is similar to Packer_point, in that the fourth component is
00172   // implicitly 1.0 if it is not present in the data, but we never
00173   // divide by alpha.
00174   class Packer_color : public Packer {
00175   public:
00176     virtual const LVecBase4f &get_data4f(const unsigned char *pointer);
00177     virtual void set_data1f(unsigned char *pointer, float data);
00178     virtual void set_data2f(unsigned char *pointer, const LVecBase2f &data);
00179     virtual void set_data3f(unsigned char *pointer, const LVecBase3f &data);
00180 
00181     virtual const char *get_name() const {
00182       return "Packer_color";
00183     }
00184   };
00185 
00186 
00187   // These are the specializations on the generic Packer that handle
00188   // the direct code paths.
00189 
00190   class Packer_float32_3 : public Packer {
00191   public:
00192     virtual const LVecBase3f &get_data3f(const unsigned char *pointer);
00193     virtual void set_data3f(unsigned char *pointer, const LVecBase3f &value);
00194 
00195     virtual const char *get_name() const {
00196       return "Packer_float32_3";
00197     }
00198   };
00199 
00200   class Packer_point_float32_2 : public Packer_point {
00201   public:
00202     virtual const LVecBase2f &get_data2f(const unsigned char *pointer);
00203     virtual void set_data2f(unsigned char *pointer, const LVecBase2f &value);
00204 
00205     virtual const char *get_name() const {
00206       return "Packer_point_float32_2";
00207     }
00208   };
00209 
00210   class Packer_point_float32_3 : public Packer_point {
00211   public:
00212     virtual const LVecBase3f &get_data3f(const unsigned char *pointer);
00213     virtual void set_data3f(unsigned char *pointer, const LVecBase3f &value);
00214 
00215     virtual const char *get_name() const {
00216       return "Packer_point_float32_3";
00217     }
00218   };
00219 
00220   class Packer_point_float32_4 : public Packer_point {
00221   public:
00222     virtual const LVecBase4f &get_data4f(const unsigned char *pointer);
00223     virtual void set_data4f(unsigned char *pointer, const LVecBase4f &value);
00224 
00225     virtual const char *get_name() const {
00226       return "Packer_point_float32_4";
00227     }
00228   };
00229 
00230   class Packer_nativefloat_3 : public Packer_float32_3 {
00231   public:
00232     virtual const LVecBase3f &get_data3f(const unsigned char *pointer);
00233 
00234     virtual const char *get_name() const {
00235       return "Packer_nativefloat_3";
00236     }
00237   };
00238 
00239   class Packer_point_nativefloat_2 : public Packer_point_float32_2 {
00240   public:
00241     virtual const LVecBase2f &get_data2f(const unsigned char *pointer);
00242 
00243     virtual const char *get_name() const {
00244       return "Packer_nativefloat_2";
00245     }
00246   };
00247 
00248   class Packer_point_nativefloat_3 : public Packer_point_float32_3 {
00249   public:
00250     virtual const LVecBase3f &get_data3f(const unsigned char *pointer);
00251 
00252     virtual const char *get_name() const {
00253       return "Packer_point_nativefloat_3";
00254     }
00255   };
00256 
00257   class Packer_point_nativefloat_4 : public Packer_point_float32_4 {
00258   public:
00259     virtual const LVecBase4f &get_data4f(const unsigned char *pointer);
00260 
00261     virtual const char *get_name() const {
00262       return "Packer_point_nativefloat_4";
00263     }
00264   };
00265 
00266   class Packer_argb_packed : public Packer_color {
00267   public:
00268     virtual const LVecBase4f &get_data4f(const unsigned char *pointer);
00269     virtual void set_data4f(unsigned char *pointer, const LVecBase4f &value);
00270 
00271     virtual const char *get_name() const {
00272       return "Packer_argb_packed";
00273     }
00274   };
00275 
00276   class Packer_rgba_uint8_4 : public Packer_color {
00277   public:
00278     virtual const LVecBase4f &get_data4f(const unsigned char *pointer);
00279     virtual void set_data4f(unsigned char *pointer, const LVecBase4f &value);
00280 
00281     virtual const char *get_name() const {
00282       return "Packer_rgba_uint8_4";
00283     }
00284   };
00285 
00286   class Packer_rgba_float32_4 : public Packer_color {
00287   public:
00288     virtual const LVecBase4f &get_data4f(const unsigned char *pointer);
00289     virtual void set_data4f(unsigned char *pointer, const LVecBase4f &value);
00290 
00291     virtual const char *get_name() const {
00292       return "Packer_rgba_float32_4";
00293     }
00294   };
00295 
00296   class Packer_rgba_nativefloat_4 : public Packer_rgba_float32_4 {
00297   public:
00298     virtual const LVecBase4f &get_data4f(const unsigned char *pointer);
00299 
00300     virtual const char *get_name() const {
00301       return "Packer_rgba_nativefloat_4";
00302     }
00303   };
00304 
00305   class Packer_uint16_1 : public Packer {
00306   public:
00307     virtual int get_data1i(const unsigned char *pointer);
00308     virtual void set_data1i(unsigned char *pointer, int value);
00309 
00310     virtual const char *get_name() const {
00311       return "Packer_uint16_1";
00312     }
00313   };
00314 
00315   friend class GeomVertexArrayFormat;
00316   friend class GeomVertexReader;
00317   friend class GeomVertexWriter;
00318 };
00319 
00320 INLINE ostream &operator << (ostream &out, const GeomVertexColumn &obj);
00321 
00322 #include "geomVertexColumn.I"
00323 
00324 #endif
 All Classes Functions Variables Enumerations