Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/gobj/geom.h

Go to the documentation of this file.
00001 // Filename: geom.h
00002 // Created by:  mike (09Jan97)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 #ifndef GEOM_H
00019 #define GEOM_H
00020 
00021 #include "pandabase.h"
00022 
00023 #include "drawable.h"
00024 
00025 #include "vector_typedWritable.h"
00026 #include "pointerTo.h"
00027 #include "pointerToArray.h"
00028 #include "typedef.h"
00029 #include "luse.h"
00030 #include "pta_Vertexf.h"
00031 #include "pta_Normalf.h"
00032 #include "pta_Colorf.h"
00033 #include "pta_TexCoordf.h"
00034 #include "pta_ushort.h"
00035 #include "pta_int.h"
00036 #include "texture.h"
00037 
00038 class Datagram;
00039 class DatagramIterator;
00040 class BamReader;
00041 class BamWriter;
00042 
00043 ////////////////////////////////////////////////////////////////////
00044 // Defines
00045 ////////////////////////////////////////////////////////////////////
00046 BEGIN_PUBLISH
00047 enum GeomBindType
00048 {
00049     G_OFF,
00050     G_OVERALL,
00051     G_PER_PRIM,
00052     G_PER_COMPONENT,
00053     G_PER_VERTEX
00054 };
00055 END_PUBLISH
00056 static const int num_GeomBindTypes = 5;
00057 
00058 enum GeomAttrType
00059 {
00060     G_COORD,
00061     G_COLOR,
00062     G_NORMAL,
00063     G_TEXCOORD
00064 };
00065 static const int num_GeomAttrTypes = 4;
00066 
00067 ostream &operator << (ostream &out, GeomBindType t);
00068 ostream &operator << (ostream &out, GeomAttrType t);
00069 
00070 ////////////////////////////////////////////////////////////////////
00071 //       Class : Geom
00072 // Description : Geometry parent class
00073 ////////////////////////////////////////////////////////////////////
00074 class EXPCL_PANDA Geom : public dDrawable {
00075 public:
00076 
00077   // These classes are used to iterate through all the vertices,
00078   // normals, etc.  They're returned by make_vertex_iterator(), and
00079   // operated on by get_next_vertex(), etc.
00080   class VertexIterator {
00081   public:
00082     const Vertexf *_array;
00083     const ushort *_index;
00084   };
00085   class NormalIterator {
00086   public:
00087     const Normalf *_array;
00088     const ushort *_index;
00089   };
00090   class TexCoordIterator {
00091   public:
00092     const TexCoordf *_array;
00093     const ushort *_index;
00094   };
00095   class ColorIterator {
00096   public:
00097     const Colorf *_array;
00098     const ushort *_index;
00099   };
00100 
00101   // Declare some function types.  This declares several typenames
00102   // which are pointers to function types--these are not themselves
00103   // functions.  A function pointed to by a variable of this type,
00104   // when given an iterator of the appropriate type from the Geom,
00105   // will retrieve the next element from the array and increment the
00106   // iterator appropriately for next time.
00107   typedef const Vertexf &GetNextVertex(VertexIterator &);
00108   typedef const Normalf &GetNextNormal(NormalIterator &);
00109   typedef const TexCoordf &GetNextTexCoord(TexCoordIterator &);
00110   typedef const Colorf &GetNextColor(ColorIterator &);
00111 
00112 
00113   Geom();
00114   Geom(const Geom &copy);
00115   ~Geom();
00116 
00117   void operator = (const Geom &copy);
00118   virtual Geom *make_copy() const=0;
00119 
00120 PUBLISHED:
00121   void write(ostream &out, int indent_level = 0) const;
00122   virtual void output(ostream &out) const;
00123   void write_verbose(ostream &out, int indent_level) const;
00124 
00125 public:
00126   // From parent dDrawable
00127   virtual void draw(GraphicsStateGuardianBase *gsg);
00128 
00129   // From parent Configurable
00130   virtual void config();
00131 
00132   // Immediate mode drawing functions - issue graphics commands
00133   virtual void draw_immediate(GraphicsStateGuardianBase *gsg, GeomContext *gc) = 0;
00134   virtual void print_draw_immediate() const = 0;
00135 
00136 public:
00137   
00138   void calc_tight_bounds(LPoint3f &min_point, LPoint3f &max_point,
00139                          bool &found_any) const;
00140   void transform_vertices(const LMatrix4f &mat);
00141 
00142   void set_coords(const PTA_Vertexf &coords,
00143                    const PTA_ushort &vindex =
00144                    PTA_ushort());
00145   void set_coords(const PTA_Vertexf &coords,
00146                    GeomBindType bind,
00147                    const PTA_ushort &vindex =
00148                    PTA_ushort());
00149   void set_normals(const PTA_Normalf &norms,
00150                    GeomBindType bind,
00151                    const PTA_ushort &nindex =
00152                    PTA_ushort());
00153   void set_colors(const PTA_Colorf &colors,
00154                   GeomBindType bind,
00155                   const PTA_ushort &cindex =
00156                   PTA_ushort());
00157   void set_texcoords(const PTA_TexCoordf &texcoords,
00158                      GeomBindType bind,
00159                      const PTA_ushort &tindex =
00160                      PTA_ushort());
00161 
00162   void get_coords(PTA_Vertexf &coords,
00163                   GeomBindType &bind,
00164                   PTA_ushort &vindex) const;
00165 
00166   void get_coords(PTA_Vertexf &coords,
00167                   PTA_ushort &vindex) const;
00168 
00169   void get_normals(PTA_Normalf &norms,
00170                    GeomBindType &bind,
00171                    PTA_ushort &nindex) const;
00172   void get_colors(PTA_Colorf &colors,
00173                   GeomBindType &bind,
00174                   PTA_ushort &cindex) const;
00175   void get_texcoords(PTA_TexCoordf &texcoords,
00176                      GeomBindType &bind,
00177                      PTA_ushort &tindex) const;
00178 
00179   virtual bool is_dynamic() const;
00180 
00181 PUBLISHED:
00182   INLINE GeomBindType get_binding(int attr) const;
00183   INLINE const PTA_Vertexf &get_coords_array() const;
00184   INLINE const PTA_Normalf &get_normals_array() const;
00185   INLINE const PTA_Colorf &get_colors_array() const;
00186   INLINE const PTA_TexCoordf &get_texcoords_array() const;
00187   INLINE const PTA_ushort &get_coords_index() const;
00188   INLINE const PTA_ushort &get_normals_index() const;
00189   INLINE const PTA_ushort &get_colors_index() const;
00190   INLINE const PTA_ushort &get_texcoords_index() const;
00191 
00192 public:
00193   INLINE void set_num_prims(int num);
00194   INLINE int get_num_prims() const;
00195 
00196   INLINE void set_lengths(const PTA_int &lengths);
00197   INLINE PTA_int get_lengths() const;
00198 
00199   virtual int get_num_vertices_per_prim() const=0;
00200   virtual int get_num_more_vertices_than_components() const=0;
00201   virtual bool uses_components() const=0;
00202 
00203   int get_num_vertices() const;
00204 
00205   // Returns the length of the indicated primitive.  Often this is the
00206   // same for all primitives in the Geom.  However, geoms which use
00207   // the lengths array will redefine this appropriately.
00208   virtual int get_length(int prim) const=0;
00209 
00210 
00211   virtual Geom *explode() const;
00212   virtual PTA_ushort get_tris() const;
00213 
00214 
00215   INLINE VertexIterator make_vertex_iterator() const;
00216   INLINE const Vertexf &get_next_vertex(VertexIterator &viterator) const;
00217 
00218   INLINE NormalIterator make_normal_iterator() const;
00219   INLINE const Normalf &get_next_normal(NormalIterator &niterator) const;
00220 
00221   INLINE TexCoordIterator make_texcoord_iterator() const;
00222   INLINE const TexCoordf &get_next_texcoord(TexCoordIterator &tciterator) const;
00223 
00224   INLINE ColorIterator make_color_iterator() const;
00225   INLINE const Colorf &get_next_color(ColorIterator &citerator) const;
00226 
00227   GeomContext *prepare(GraphicsStateGuardianBase *gsg);
00228   void unprepare();
00229   void unprepare(GraphicsStateGuardianBase *gsg);
00230   void clear_gsg(GraphicsStateGuardianBase *gsg);
00231 
00232 protected:
00233   void init();
00234   virtual BoundingVolume *recompute_bound();
00235 
00236 protected:
00237 
00238   PTA_Vertexf _coords;
00239   PTA_Normalf _norms;
00240   PTA_Colorf _colors;
00241   PTA_TexCoordf _texcoords;
00242 
00243   PTA_ushort _vindex;
00244   PTA_ushort _nindex;
00245   PTA_ushort _cindex;
00246   PTA_ushort _tindex;
00247 
00248   int _numprims,_num_vertices;
00249   PTA_int _primlengths;
00250   enum GeomBindType _bind[num_GeomAttrTypes];
00251 
00252   // Functions to extract component values, one at a time.
00253   GetNextVertex *_get_vertex;
00254   GetNextNormal *_get_normal;
00255   GetNextTexCoord *_get_texcoord;
00256   GetNextColor *_get_color;
00257 
00258   // Unlike a Texture, a Geom only stores the pointer to one GSG that
00259   // it has been prepared into.  If it is prepared into another GSG,
00260   // it automatically unprepares itself from the first one.  This is
00261   // intended to reduce memory overhead that would otherwise be
00262   // required to support a little-used feature (having two
00263   // simultaneous GSG's).
00264   GraphicsStateGuardianBase *_prepared_gsg;
00265   GeomContext *_prepared_context;
00266 
00267 public:
00268   //static void register_with_read_factory(void);
00269   virtual void write_datagram(BamWriter* manager, Datagram &me);
00270 
00271   //static TypedWritable *make_Generic(const FactoryParams &params);
00272 
00273 protected:
00274   void fillin(DatagramIterator& scan, BamReader* manager);
00275 
00276 public:
00277   static TypeHandle get_class_type() {
00278     return _type_handle;
00279   }
00280   static void init_type() {
00281     dDrawable::init_type();
00282     register_type(_type_handle, "Geom",
00283                   dDrawable::get_class_type());
00284   }
00285   virtual TypeHandle get_type() const {
00286     return get_class_type();
00287   }
00288   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00289 
00290 private:
00291   static TypeHandle _type_handle;
00292 
00293 };
00294 
00295 INLINE ostream &operator <<(ostream &out, const Geom &geom) {
00296   geom.output(out);
00297   return out;
00298 }
00299 
00300 #include "geom.I"
00301 
00302 #endif

Generated on Fri May 2 00:39:21 2003 for Panda by doxygen1.3