Panda3D
 All Classes Functions Variables Enumerations
eggPrimitive.h
00001 // Filename: eggPrimitive.h
00002 // Created by:  drose (16Jan99)
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 EGGPRIMITIVE_H
00016 #define EGGPRIMITIVE_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "eggNode.h"
00021 #include "eggAttributes.h"
00022 #include "eggVertex.h"
00023 #include "eggTexture.h"
00024 #include "eggMaterial.h"
00025 #include "eggRenderMode.h"
00026 #include "pt_EggTexture.h"
00027 #include "pt_EggMaterial.h"
00028 #include "vector_PT_EggVertex.h"
00029 #include "vector_PT_EggTexture.h"
00030 
00031 #include "pointerTo.h"
00032 #include "pvector.h"
00033 
00034 #include <algorithm>
00035 
00036 class EggVertexPool;
00037 
00038 ////////////////////////////////////////////////////////////////////
00039 //       Class : EggPrimitive
00040 // Description : A base class for any of a number of kinds of geometry
00041 //               primitives: polygons, point lights, nurbs patches,
00042 //               parametrics curves, etc.  Things with a set of
00043 //               vertices and some rendering properties like color.
00044 //
00045 //               An EggPrimitive is an STL-style container of pointers
00046 //               to EggVertex's.  In fact, it IS a vector, and can be
00047 //               manipulated in all the ways that vectors can.
00048 //               However, it is necessary that all vertices belong to
00049 //               the same vertex pool.
00050 ////////////////////////////////////////////////////////////////////
00051 class EXPCL_PANDAEGG EggPrimitive : public EggNode, public EggAttributes,
00052                      public EggRenderMode
00053 {
00054 
00055   // This is a bit of private interface stuff that must be here as a
00056   // forward reference.  This allows us to define the EggPrimitive as
00057   // an STL container.
00058 
00059 private:
00060   typedef vector_PT_EggVertex Vertices;
00061 
00062   // Here begins the actual public interface to EggPrimitive.
00063 
00064 PUBLISHED:
00065   enum Shading {
00066     // The order here is important.  The later choices are more
00067     // specific than the earlier ones.
00068     S_unknown,
00069     S_overall,
00070     S_per_face,
00071     S_per_vertex
00072   };
00073 
00074   INLINE EggPrimitive(const string &name = "");
00075   INLINE EggPrimitive(const EggPrimitive &copy);
00076   INLINE EggPrimitive &operator = (const EggPrimitive &copy);
00077   INLINE ~EggPrimitive();
00078 
00079   virtual EggRenderMode *determine_alpha_mode();
00080   virtual EggRenderMode *determine_depth_write_mode();
00081   virtual EggRenderMode *determine_depth_test_mode();
00082   virtual EggRenderMode *determine_visibility_mode();
00083   virtual EggRenderMode *determine_depth_offset();
00084   virtual EggRenderMode *determine_draw_order();
00085   virtual EggRenderMode *determine_bin();
00086 
00087   INLINE string get_sort_name() const;
00088 
00089   virtual Shading get_shading() const;
00090   INLINE void clear_connected_shading();
00091   INLINE Shading get_connected_shading() const;
00092 
00093   INLINE void set_texture(EggTexture *texture);
00094   INLINE bool has_texture() const;
00095   INLINE bool has_texture(EggTexture *texture) const;
00096   INLINE EggTexture *get_texture() const;
00097 
00098   INLINE void add_texture(EggTexture *texture);
00099   INLINE void clear_texture();
00100   INLINE int get_num_textures() const;
00101   INLINE EggTexture *get_texture(int n) const;
00102   MAKE_SEQ(get_textures, get_num_textures, get_texture);
00103 
00104   INLINE void set_material(EggMaterial *material);
00105   INLINE void clear_material();
00106   INLINE EggMaterial *get_material() const;
00107   INLINE bool has_material() const;
00108 
00109   INLINE void set_bface_flag(bool flag);
00110   INLINE bool get_bface_flag() const;
00111 
00112   void copy_attributes(const EggAttributes &other);
00113   void copy_attributes(const EggPrimitive &other);
00114 
00115   bool has_vertex_normal() const;
00116   bool has_vertex_color() const;
00117 
00118   virtual void unify_attributes(Shading shading);
00119   virtual void apply_last_attribute();
00120   virtual void apply_first_attribute();
00121   virtual void post_apply_flat_attribute();
00122   virtual void reverse_vertex_ordering();
00123   virtual bool cleanup();
00124 
00125   void remove_doubled_verts(bool closed);
00126   void remove_nonunique_verts();
00127   virtual bool has_primitives() const;
00128   virtual bool joint_has_primitives() const;
00129   virtual bool has_normals() const;
00130 
00131 
00132   // The EggPrimitive itself appears to be an STL container of
00133   // pointers to EggVertex objects.  The set of vertices is read-only,
00134   // however, except through the limited add_vertex/remove_vertex or
00135   // insert/erase interface.  The following implements this.
00136 public:
00137 #if defined(WIN32_VC) || defined(WIN64_VC)
00138   typedef PT_EggVertex *pointer;
00139   typedef PT_EggVertex *const_pointer;
00140 #else
00141   typedef Vertices::const_pointer pointer;
00142   typedef Vertices::const_pointer const_pointer;
00143 #endif
00144   typedef Vertices::const_reference reference;
00145   typedef Vertices::const_reference const_reference;
00146   typedef Vertices::const_iterator iterator;
00147   typedef Vertices::const_iterator const_iterator;
00148   typedef Vertices::const_reverse_iterator reverse_iterator;
00149   typedef Vertices::const_reverse_iterator const_reverse_iterator;
00150   typedef Vertices::size_type size_type;
00151   typedef Vertices::difference_type difference_type;
00152 
00153   INLINE iterator begin() const;
00154   INLINE iterator end() const;
00155   INLINE reverse_iterator rbegin() const;
00156   INLINE reverse_iterator rend() const;
00157   INLINE bool empty() const;
00158   INLINE size_type size() const;
00159 
00160   INLINE EggVertex *operator [] (int index) const;
00161 
00162   INLINE iterator insert(iterator position, EggVertex *x);
00163   INLINE iterator erase(iterator position);
00164   iterator erase(iterator first, iterator last);
00165   INLINE void replace(iterator position, EggVertex *vertex);
00166   iterator find(EggVertex *vertex);
00167 
00168 PUBLISHED:
00169   INLINE void clear();
00170 
00171   EggVertex *add_vertex(EggVertex *vertex);
00172   EggVertex *remove_vertex(EggVertex *vertex);
00173   void copy_vertices(const EggPrimitive &other);
00174 
00175   // These are shorthands if you don't want to use the iterators.
00176   INLINE int get_num_vertices() const;
00177   INLINE void set_vertex(int index, EggVertex *vertex);
00178   INLINE EggVertex *get_vertex(int index) const;
00179   MAKE_SEQ(get_vertices, get_num_vertices, get_vertex);
00180 
00181   INLINE EggVertexPool *get_pool() const;
00182 
00183   virtual void write(ostream &out, int indent_level) const=0;
00184 
00185 #ifdef _DEBUG
00186   void test_vref_integrity() const;
00187 #else
00188   void test_vref_integrity() const { }
00189 #endif  // _DEBUG
00190 
00191 protected:
00192   Vertices _vertices;
00193 
00194   // Don't try to use these private functions.  User code should add
00195   // and remove vertices via add_vertex()/remove_vertex(), or via the
00196   // STL-like push_back()/pop_back() or insert()/erase(), above.
00197   virtual void prepare_add_vertex(EggVertex *vertex, int i, int n);
00198   virtual void prepare_remove_vertex(EggVertex *vertex, int i, int n);
00199 
00200 protected:
00201   void write_body(ostream &out, int indent_level) const;
00202 
00203   virtual bool egg_start_parse_body();
00204   virtual void r_transform(const LMatrix4d &mat, const LMatrix4d &inv,
00205                            CoordinateSystem to_cs);
00206   virtual void r_flatten_transforms();
00207   virtual void r_apply_texmats(EggTextureCollection &textures);
00208 
00209   void do_apply_flat_attribute(int vertex_index, EggAttributes *attrib);
00210 
00211 private:
00212   void set_connected_shading(Shading shading, const EggAttributes *neighbor);
00213 
00214   class ConnectedShadingNode {
00215   public:
00216     Shading _shading;
00217     const EggAttributes *_neighbor;
00218   };
00219   typedef pvector<ConnectedShadingNode> ConnectedShadingNodes;
00220 
00221   void r_set_connected_shading(int depth_count,
00222                                Shading shading, const EggAttributes *neighbor,
00223                                ConnectedShadingNodes &connected_nodes);
00224 
00225 private:
00226   typedef vector_PT_EggTexture Textures;
00227   Textures _textures;
00228   PT_EggMaterial _material;
00229   bool _bface;
00230   Shading _connected_shading;
00231 
00232 public:
00233 
00234   static TypeHandle get_class_type() {
00235     return _type_handle;
00236   }
00237   static void init_type() {
00238     EggNode::init_type();
00239     EggAttributes::init_type();
00240     EggRenderMode::get_class_type();
00241     register_type(_type_handle, "EggPrimitive",
00242                   EggNode::get_class_type(),
00243                   EggAttributes::get_class_type(),
00244                   EggRenderMode::get_class_type());
00245   }
00246   virtual TypeHandle get_type() const {
00247     return get_class_type();
00248   }
00249   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00250 
00251 private:
00252   static TypeHandle _type_handle;
00253 
00254   friend class EggTextureCollection;
00255 };
00256 
00257 #include "eggPrimitive.I"
00258 
00259 #endif
 All Classes Functions Variables Enumerations