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   void make_point_primitives();
00146 
00147   int rename_nodes(vector_string strip_prefix, bool recurse);
00148 
00149   int remove_unused_vertices(bool recurse);
00150   int remove_invalid_primitives(bool recurse);
00151   void clear_connected_shading();
00152   void get_connected_shading();
00153   void unify_attributes(bool use_connected_shading, bool allow_per_primitive,
00154                         bool recurse);
00155   void apply_last_attribute(bool recurse);
00156   void apply_first_attribute(bool recurse);
00157   void post_apply_flat_attribute(bool recurse);
00158   virtual bool has_primitives() const;
00159   virtual bool joint_has_primitives() const;
00160   virtual bool has_normals() const;
00161 
00162 public:
00163   void rebuild_vertex_pools(EggVertexPools &vertex_pools, 
00164                             unsigned int max_vertices,
00165                             bool recurse);
00166 
00167 protected:
00168   virtual void update_under(int depth_offset);
00169 
00170   virtual void r_transform(const LMatrix4d &mat, const LMatrix4d &inv,
00171                            CoordinateSystem to_cs);
00172   virtual void r_transform_vertices(const LMatrix4d &mat);
00173   virtual void r_mark_coordsys(CoordinateSystem cs);
00174   virtual void r_flatten_transforms();
00175   virtual void r_apply_texmats(EggTextureCollection &textures);
00176 
00177 
00178   CoordinateSystem find_coordsys_entry();
00179   int find_textures(EggTextureCollection *collection);
00180   int find_materials(EggMaterialCollection *collection);
00181   bool r_load_externals(const DSearchPath &searchpath, 
00182                         CoordinateSystem coordsys,
00183                         BamCacheRecord *record);
00184 
00185 PUBLISHED:
00186   INLINE static bool is_right(const LVector2d &v1, const LVector2d &v2);
00187 
00188 private:
00189   Children _children;
00190   const_iterator _gnc_iterator;
00191 
00192   // Don't try to use these private functions.  User code should add
00193   // and remove children via add_child()/remove_child(), or via the
00194   // STL-like push_back()/pop_back() or insert()/erase(), above.
00195   void prepare_add_child(EggNode *node);
00196   void prepare_remove_child(EggNode *node);
00197 
00198   // This bit is in support of recompute_vertex_normals().
00199   class NVertexReference {
00200   public:
00201     EggPolygon *_polygon;
00202     LNormald _normal;
00203     size_t _vertex;
00204   };
00205   typedef pvector<NVertexReference> NVertexGroup;
00206   typedef pmap<LVertexd, NVertexGroup> NVertexCollection;
00207 
00208   void r_collect_vertex_normals(NVertexCollection &collection,
00209                                 double threshold, CoordinateSystem cs);
00210   void do_compute_vertex_normals(const NVertexGroup &group);
00211 
00212   // This bit is in support of recompute_tangent_binormal().
00213   class TBNVertexReference {
00214   public:
00215     EggPolygon *_polygon;
00216     size_t _vertex;
00217     LVector3d _sdir;
00218     LVector3d _tdir;
00219   };
00220   class TBNVertexValue {
00221   public:
00222     INLINE bool operator < (const TBNVertexValue &other) const;
00223     LVertexd _pos;
00224     LNormald _normal;
00225     string _uv_name;
00226     LTexCoordd _uv;
00227     bool _facing;
00228   };
00229   typedef pvector<TBNVertexReference> TBNVertexGroup;
00230   typedef pmap<TBNVertexValue, TBNVertexGroup> TBNVertexCollection;
00231 
00232   void r_collect_tangent_binormal(const GlobPattern &uv_name,
00233                                   TBNVertexCollection &collection);
00234   void do_compute_tangent_binormal(const TBNVertexValue &value,
00235                                    const TBNVertexGroup &group);
00236 
00237 public:
00238   static TypeHandle get_class_type() {
00239     return _type_handle;
00240   }
00241   static void init_type() {
00242     EggNode::init_type();
00243     register_type(_type_handle, "EggGroupNode",
00244                   EggNode::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   friend class EggMaterialCollection;
00256 };
00257 
00258 #include "eggGroupNode.I"
00259 
00260 #endif
00261 
 All Classes Functions Variables Enumerations