Panda3D

eggGroupNode.h

00001 // Filename: eggGroupNode.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 EGGGROUPNODE_H
00016 #define EGGGROUPNODE_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "eggNode.h"
00021 #include "eggVertexPool.h"
00022 
00023 #include "coordinateSystem.h"
00024 #include "typedObject.h"
00025 #include "pointerTo.h"
00026 #include "luse.h"
00027 #include "globPattern.h"
00028 #include "plist.h"
00029 
00030 class EggTextureCollection;
00031 class EggMaterialCollection;
00032 class EggPolygon;
00033 class EggVertex;
00034 class EggVertexPool;
00035 class DSearchPath;
00036 class BamCacheRecord;
00037 
00038 ////////////////////////////////////////////////////////////////////
00039 //       Class : EggGroupNode
00040 // Description : A base class for nodes in the hierarchy that are not
00041 //               leaf nodes.  (See also EggGroup, which is
00042 //               specifically the "<Group>" node in egg.)
00043 //
00044 //               An EggGroupNode is an STL-style container of pointers
00045 //               to EggNodes, like a vector.  Functions
00046 //               push_back()/pop_back() and insert()/erase() are
00047 //               provided to manipulate the list.  The list may also
00048 //               be operated on (read-only) via iterators and
00049 //               begin()/end().
00050 ////////////////////////////////////////////////////////////////////
00051 class EXPCL_PANDAEGG EggGroupNode : public EggNode {
00052 
00053   // This is a bit of private interface stuff that must be here as a
00054   // forward reference.  This allows us to define the EggGroupNode as
00055   // an STL container.
00056 
00057 private:
00058   // We define the list of children as a list and not a vector, so we
00059   // can avoid the bad iterator-invalidating properties of vectors as
00060   // we insert/delete elements.
00061   typedef plist< PT(EggNode) > Children;
00062 
00063   // Here begins the actual public interface to EggGroupNode.
00064 
00065 PUBLISHED:
00066   EggGroupNode(const string &name = "") : EggNode(name) { }
00067   EggGroupNode(const EggGroupNode &copy);
00068   EggGroupNode &operator = (const EggGroupNode &copy);
00069   virtual ~EggGroupNode();
00070 
00071   virtual void write(ostream &out, int indent_level) const;
00072 
00073   // The EggGroupNode itself appears to be an STL container of
00074   // pointers to EggNodes.  The set of children is read-only, however,
00075   // except through the limited add_child/remove_child or insert/erase
00076   // interface.  The following implements this.
00077 public:
00078 #if defined(WIN32_VC) || defined(WIN64_VC)
00079   typedef const PT(EggNode) *pointer;
00080   typedef const PT(EggNode) *const_pointer;
00081 #else
00082   typedef Children::const_pointer pointer;
00083   typedef Children::const_pointer const_pointer;
00084 #endif
00085   typedef Children::const_reference reference;
00086   typedef Children::const_reference const_reference;
00087   typedef Children::const_iterator iterator;
00088   typedef Children::const_iterator const_iterator;
00089   typedef Children::const_reverse_iterator reverse_iterator;
00090   typedef Children::const_reverse_iterator const_reverse_iterator;
00091   typedef Children::size_type size_type;
00092   typedef Children::difference_type difference_type;
00093 
00094   iterator begin() const;
00095   iterator end() const;
00096   reverse_iterator rbegin() const;
00097   reverse_iterator rend() const;
00098 
00099   iterator insert(iterator position, PT(EggNode) x);
00100   iterator erase(iterator position);
00101   iterator erase(iterator first, iterator last);
00102   void replace(iterator position, PT(EggNode) x);
00103 
00104 PUBLISHED:
00105   bool empty() const;
00106   size_type size() const;
00107   void clear();
00108 
00109   // This is an alternate way to traverse the list of children.  It is
00110   // mainly provided for scripting code, which can't use the iterators
00111   // defined above (they don't export through interrogate very well).
00112   // These are, of course, non-thread-safe.
00113   EggNode *get_first_child();
00114   EggNode *get_next_child();
00115 
00116   EggNode *add_child(EggNode *node);
00117   PT(EggNode) remove_child(EggNode *node);
00118   void steal_children(EggGroupNode &other);
00119 
00120   EggNode *find_child(const string &name) const;
00121 
00122   bool has_absolute_pathnames() const;
00123   void resolve_filenames(const DSearchPath &searchpath);
00124   void force_filenames(const Filename &directory);
00125   void reverse_vertex_ordering();
00126 
00127   void recompute_vertex_normals(double threshold, CoordinateSystem cs = CS_default);
00128   void recompute_polygon_normals(CoordinateSystem cs = CS_default);
00129   void strip_normals();
00130 
00131   bool recompute_tangent_binormal(const GlobPattern &uv_name);
00132   bool recompute_tangent_binormal(const vector_string &names);
00133   bool recompute_tangent_binormal_auto();
00134 
00135   enum TriangulateFlags {
00136     T_polygon     = 0x001,
00137     T_convex      = 0x002,
00138     T_composite   = 0x004,
00139     T_recurse     = 0x008,
00140     T_flat_shaded = 0x010,
00141   };
00142 
00143   int triangulate_polygons(int flags);
00144   void mesh_triangles(int flags);
00145 
00146   int rename_nodes(vector_string strip_prefix, bool recurse);
00147 
00148   int remove_unused_vertices(bool recurse);
00149   int remove_invalid_primitives(bool recurse);
00150   void clear_connected_shading();
00151   void get_connected_shading();
00152   void unify_attributes(bool use_connected_shading, bool allow_per_primitive,
00153                         bool recurse);
00154   void apply_last_attribute(bool recurse);
00155   void apply_first_attribute(bool recurse);
00156   void post_apply_flat_attribute(bool recurse);
00157   virtual bool has_primitives() const;
00158   virtual bool joint_has_primitives() const;
00159   virtual bool has_normals() const;
00160 
00161 public:
00162   void rebuild_vertex_pools(EggVertexPools &vertex_pools, 
00163                             unsigned int max_vertices,
00164                             bool recurse);
00165 
00166 protected:
00167   virtual void update_under(int depth_offset);
00168 
00169   virtual void r_transform(const LMatrix4d &mat, const LMatrix4d &inv,
00170                            CoordinateSystem to_cs);
00171   virtual void r_transform_vertices(const LMatrix4d &mat);
00172   virtual void r_mark_coordsys(CoordinateSystem cs);
00173   virtual void r_flatten_transforms();
00174   virtual void r_apply_texmats(EggTextureCollection &textures);
00175 
00176 
00177   CoordinateSystem find_coordsys_entry();
00178   int find_textures(EggTextureCollection *collection);
00179   int find_materials(EggMaterialCollection *collection);
00180   bool r_load_externals(const DSearchPath &searchpath, 
00181                         CoordinateSystem coordsys,
00182                         BamCacheRecord *record);
00183 
00184 PUBLISHED:
00185   INLINE static bool is_right(const LVector2d &v1, const LVector2d &v2);
00186 
00187 private:
00188   Children _children;
00189   const_iterator _gnc_iterator;
00190 
00191   // Don't try to use these private functions.  User code should add
00192   // and remove children via add_child()/remove_child(), or via the
00193   // STL-like push_back()/pop_back() or insert()/erase(), above.
00194   void prepare_add_child(EggNode *node);
00195   void prepare_remove_child(EggNode *node);
00196 
00197   // This bit is in support of recompute_vertex_normals().
00198   class NVertexReference {
00199   public:
00200     EggPolygon *_polygon;
00201     Normald _normal;
00202     size_t _vertex;
00203   };
00204   typedef pvector<NVertexReference> NVertexGroup;
00205   typedef pmap<Vertexd, NVertexGroup> NVertexCollection;
00206 
00207   void r_collect_vertex_normals(NVertexCollection &collection,
00208                                 double threshold, CoordinateSystem cs);
00209   void do_compute_vertex_normals(const NVertexGroup &group);
00210 
00211   // This bit is in support of recompute_tangent_binormal().
00212   class TBNVertexReference {
00213   public:
00214     EggPolygon *_polygon;
00215     size_t _vertex;
00216     LVector3d _sdir;
00217     LVector3d _tdir;
00218   };
00219   class TBNVertexValue {
00220   public:
00221     INLINE bool operator < (const TBNVertexValue &other) const;
00222     Vertexd _pos;
00223     Normald _normal;
00224     string _uv_name;
00225     TexCoordd _uv;
00226     bool _facing;
00227   };
00228   typedef pvector<TBNVertexReference> TBNVertexGroup;
00229   typedef pmap<TBNVertexValue, TBNVertexGroup> TBNVertexCollection;
00230 
00231   void r_collect_tangent_binormal(const GlobPattern &uv_name,
00232                                   TBNVertexCollection &collection);
00233   void do_compute_tangent_binormal(const TBNVertexValue &value,
00234                                    const TBNVertexGroup &group);
00235 
00236 public:
00237   static TypeHandle get_class_type() {
00238     return _type_handle;
00239   }
00240   static void init_type() {
00241     EggNode::init_type();
00242     register_type(_type_handle, "EggGroupNode",
00243                   EggNode::get_class_type());
00244   }
00245   virtual TypeHandle get_type() const {
00246     return get_class_type();
00247   }
00248   virtual TypeHandle force_init_type() {init_type(); return get_class_type();}
00249 
00250 private:
00251   static TypeHandle _type_handle;
00252 
00253   friend class EggTextureCollection;
00254   friend class EggMaterialCollection;
00255 };
00256 
00257 #include "eggGroupNode.I"
00258 
00259 #endif
00260 
 All Classes Functions Variables Enumerations