Bladeren bron

add virtual functions

David Rose 24 jaren geleden
bovenliggende
commit
520ace5e68
2 gewijzigde bestanden met toevoegingen van 142 en 13 verwijderingen
  1. 129 12
      panda/src/pgraph/pandaNode.cxx
  2. 13 1
      panda/src/pgraph/pandaNode.h

+ 129 - 12
panda/src/pgraph/pandaNode.cxx

@@ -65,9 +65,24 @@ PandaNode(const string &name) :
 {
 {
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: PandaNode::Destructor
+//       Access: Published, Virtual
+//  Description:
+////////////////////////////////////////////////////////////////////
+PandaNode::
+~PandaNode() {
+  // We shouldn't have any parents left by the time we destruct, or
+  // there's a refcount fault somewhere.
+  CDReader cdata(_cycler);
+  nassertv(cdata->_up.empty());
+
+  remove_all_children();
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PandaNode::Copy Constructor
 //     Function: PandaNode::Copy Constructor
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 PandaNode::
 PandaNode::
@@ -88,7 +103,7 @@ PandaNode(const PandaNode &copy) :
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PandaNode::Copy Assignment Operator
 //     Function: PandaNode::Copy Assignment Operator
-//       Access: Published
+//       Access: Public
 //  Description:
 //  Description:
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void PandaNode::
 void PandaNode::
@@ -106,18 +121,120 @@ operator = (const PandaNode &copy) {
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-//     Function: PandaNode::Destructor
-//       Access: Published, Virtual
-//  Description:
+//     Function: PandaNode::make_copy
+//       Access: Public, Virtual
+//  Description: Returns a newly-allocated PandaNode that is a shallow copy
+//               of this one.  It will be a different pointer, but its
+//               internal data may or may not be shared with that of
+//               the original PandaNode.  No children will be copied.
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
-PandaNode::
-~PandaNode() {
-  // We shouldn't have any parents left by the time we destruct, or
-  // there's a refcount fault somewhere.
-  CDReader cdata(_cycler);
-  nassertv(cdata->_up.empty());
+PandaNode *PandaNode::
+make_copy() const {
+  return new PandaNode(*this);
+}
 
 
-  remove_all_children();
+////////////////////////////////////////////////////////////////////
+//     Function: PandaNode::copy_subgraph
+//       Access: Public
+//  Description: Allocates and returns a complete copy of this
+//               PandaNode and the entire scene graph rooted at this
+//               PandaNode.  Some data may still be shared from the
+//               original (e.g. vertex index tables), but nothing that
+//               will impede normal use of the PandaNode.
+////////////////////////////////////////////////////////////////////
+PandaNode *PandaNode::
+copy_subgraph() const {
+  //*** Do something here.
+  nassertr(false, (PandaNode *)NULL);
+  return (PandaNode *)NULL;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PandaNode::safe_to_flatten
+//       Access: Public, Virtual
+//  Description: Returns true if it is generally safe to flatten out
+//               this particular kind of PandaNode by duplicating
+//               instances, false otherwise (for instance, a Camera
+//               cannot be safely flattened, because the Camera
+//               pointer itself is meaningful).
+////////////////////////////////////////////////////////////////////
+bool PandaNode::
+safe_to_flatten() const {
+  return true;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PandaNode::safe_to_transform
+//       Access: Public, Virtual
+//  Description: Returns true if it is generally safe to transform
+//               this particular kind of PandaNode by calling the
+//               xform() method, false otherwise.  For instance, it's
+//               usually a bad idea to attempt to xform a Character.
+////////////////////////////////////////////////////////////////////
+bool PandaNode::
+safe_to_transform() const {
+  return true;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PandaNode::safe_to_combine
+//       Access: Public, Virtual
+//  Description: Returns true if it is generally safe to combine this
+//               particular kind of PandaNode with other kinds of
+//               PandaNodes, adding children or whatever.  For
+//               instance, an LODNode should not be combined with any
+//               other PandaNode, because its set of children is
+//               meaningful.
+////////////////////////////////////////////////////////////////////
+bool PandaNode::
+safe_to_combine() const {
+  return true;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PandaNode::xform
+//       Access: Public, Virtual
+//  Description: Transforms the contents of this PandaNode by the
+//               indicated matrix, if it means anything to do so.  For
+//               most kinds of PandaNodes, this does nothing.
+////////////////////////////////////////////////////////////////////
+void PandaNode::
+xform(const LMatrix4f &) {
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PandaNode::combine_with
+//       Access: Public, Virtual
+//  Description: Collapses this PandaNode with the other PandaNode, if
+//               possible, and returns a pointer to the combined
+//               PandaNode, or NULL if the two PandaNodes cannot
+//               safely be combined.
+//
+//               The return value may be this, other, or a new
+//               PandaNode altogether.
+//
+//               This function is called from GraphReducer::flatten(),
+//               and need not deal with children; its job is just to
+//               decide whether to collapse the two PandaNodes and
+//               what the collapsed PandaNode should look like.
+////////////////////////////////////////////////////////////////////
+PandaNode *PandaNode::
+combine_with(PandaNode *other) {
+  // An unadorned PandaNode always combines with any other PandaNodes by
+  // yielding completely.  However, if we are actually some fancy PandaNode
+  // type that derives from PandaNode but didn't redefine this function, we
+  // should refuse to combine.
+  if (is_exact_type(get_class_type())) {
+    // No, we're an ordinary PandaNode.
+    return other;
+
+  } else if (other->is_exact_type(get_class_type())) {
+    // We're not an ordinary PandaNode, but the other one is.
+    return this;
+  }
+
+  // We're something other than an ordinary PandaNode.  Don't combine.
+  return (PandaNode *)NULL;
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////

+ 13 - 1
panda/src/pgraph/pandaNode.h

@@ -49,10 +49,22 @@ class EXPCL_PANDA PandaNode : public TypedWritable, public Namable,
                               virtual public ReferenceCount {
                               virtual public ReferenceCount {
 PUBLISHED:
 PUBLISHED:
   PandaNode(const string &name);
   PandaNode(const string &name);
+  virtual ~PandaNode();
+
+public:
   PandaNode(const PandaNode &copy);
   PandaNode(const PandaNode &copy);
   void operator = (const PandaNode &copy);
   void operator = (const PandaNode &copy);
-  virtual ~PandaNode();
 
 
+  virtual PandaNode *make_copy() const;
+  PandaNode *copy_subgraph() const;
+
+  virtual bool safe_to_flatten() const;
+  virtual bool safe_to_transform() const;
+  virtual bool safe_to_combine() const;
+  virtual void xform(const LMatrix4f &mat);
+  virtual PandaNode *combine_with(PandaNode *other); 
+
+PUBLISHED:
   INLINE int get_num_parents() const;
   INLINE int get_num_parents() const;
   INLINE PandaNode *get_parent(int n) const;
   INLINE PandaNode *get_parent(int n) const;
   INLINE int find_parent(PandaNode *node) const;
   INLINE int find_parent(PandaNode *node) const;