Browse Source

new joint fixing mechanism in at the panda level

Zachary Pavlov 18 years ago
parent
commit
f554899631

+ 14 - 58
panda/src/chan/animGroup.cxx

@@ -235,70 +235,26 @@ make_child_dynamic(const string &name) {
 
 
 ////////////////////////////////////////////////////////////////////
-//     Function: AnimGroup::make_child_fixed
+//     Function: AnimGroup::fix_child
 //       Access: Public
-//  Description: Finds the indicated child and replaces it with an
-//               AnimChannelMatrixFixed with the specified transform
-//               This may be called before binding the animation to a
-//               character to replace certain joints with
-//               frozen ones.
+//  Description: replaces childs animation matrix with a fixed one
 ////////////////////////////////////////////////////////////////////
 void AnimGroup::
-make_child_fixed(const string &name, const LMatrix4f &mat) {
-  Children::iterator ci;
-  for (ci = _children.begin(); ci != _children.end(); ++ci) {
-    AnimGroup *child = (*ci);
-    if (child->get_name() == name) {
-      AnimGroup *new_child = NULL;
-
-      if (child->is_of_type(AnimChannelMatrix::get_class_type())) {
-        AnimChannelMatrix *mchild = DCAST(AnimChannelMatrix, child);
-        AnimChannelMatrixFixed *new_mchild = 
-          new AnimChannelMatrixFixed(this, name, mat);
-        new_child = new_mchild;
-      } 
-
-      if (new_child != (AnimGroup *)NULL) {
-        new_child->_children.swap(child->_children);
-        nassertv(_children.back() == new_child);
-
-        // The new child was appended to the end of our children list
-        // by its constructor.  Reposition it to replace the original
-        // child.
-
-        // I would like to use these lines, but for some reason it
-        // crashes:
-        /*
-        {
-          (*ci) = new_child;
-          _children.pop_back();
-        }
-        */
-
-        // But this longer way of achieving the same result works
-        // instead:
-        {
-          Children::iterator nci;
-          Children new_children;
-          for (nci = _children.begin(); nci != _children.end(); ++nci) {
-            if ((*nci) == child) {
-              new_children.push_back(new_child);
-            } else if ((*nci) != new_child) {
-              new_children.push_back(*nci);
-            }
-          }
-          new_children.swap(_children);
-        }
-
-        return;
-      }
-    }
-    child->make_child_fixed(name, mat);
+fix_child(unsigned int index, const LMatrix4f &mat) {
+  AnimGroup *child = get_child(index);
+  AnimGroup *new_child = NULL;
+  if (child->is_of_type(AnimChannelMatrix::get_class_type())) {
+    AnimChannelMatrix *mchild = DCAST(AnimChannelMatrix, child);
+    AnimChannelMatrixFixed *new_mchild =  new AnimChannelMatrixFixed(this, child->get_name(), mat);
+    new_child = new_mchild;
+  }
+  if(new_child != (AnimGroup*)NULL) {
+    new_child->_children.swap(child->_children);
+    nassertv(_children.back() == new_child);
+    _children[index] = new_child;
   }
 }
 
-
-
 ////////////////////////////////////////////////////////////////////
 //     Function: AnimGroup::get_value_type
 //       Access: Public, Virtual

+ 2 - 1
panda/src/chan/animGroup.h

@@ -53,7 +53,6 @@ PUBLISHED:
   AnimGroup *find_child(const string &name) const;
 
   AnimGroup *make_child_dynamic(const string &name);
-  void make_child_fixed(const string &name, const LMatrix4f &mat);
 public:
   virtual TypeHandle get_value_type() const;
 
@@ -82,6 +81,8 @@ public:
 
   static TypedWritable *make_AnimGroup(const FactoryParams &params);
 
+  void fix_child(unsigned int index, LMatrix4f const & mat);
+
 protected:
   void fillin(DatagramIterator& scan, BamReader* manager);
 

+ 0 - 14
panda/src/chan/partBundle.I

@@ -206,17 +206,3 @@ get_control_effect(AnimControl *control) const {
   CDReader cdata(_cycler);
   return do_get_control_effect(control, cdata);
 }
-
-////////////////////////////////////////////////////////////////////
-//     Function: PartBundle::freeze_joint
-//       Access: Published
-//  Description: stores away a joint freeze for bind time
-////////////////////////////////////////////////////////////////////
-INLINE void PartBundle::
-freeze_joint(const string &jointName, const LMatrix4f &transform) {
-  JointTransform jt;
-  jt.name=jointName;
-  jt.transform=transform;
-
-  _frozen_joints.push_back(jt);
-}

+ 0 - 8
panda/src/chan/partBundle.cxx

@@ -222,14 +222,6 @@ bind_anim(AnimBundle *anim, int hierarchy_match_flags,
     return NULL;
   }
 
-  if (!_frozen_joints.empty()) {
-    ptanim = ptanim->copy_bundle();
-    JointTransformList::iterator jti;
-    for (jti = _frozen_joints.begin(); jti != _frozen_joints.end(); ++jti) {
-      ptanim->make_child_fixed((*jti).name, (*jti).transform);
-    }
-  }
-
   plist<int> holes;
   int channel_index = 0;
   pick_channel_index(holes, channel_index);

+ 0 - 12
panda/src/chan/partBundle.h

@@ -109,9 +109,6 @@ PUBLISHED:
   INLINE int get_num_nodes() const;
   INLINE PartBundleNode *get_node(int n) const;
 
-  INLINE void freeze_joint(const string &jointName, 
-                           const LMatrix4f &transform);
-
   void clear_control_effects();
   INLINE void set_control_effect(AnimControl *control, float effect);
   INLINE float get_control_effect(AnimControl *control) const;
@@ -197,15 +194,6 @@ public:
 private:
   static TypeHandle _type_handle;
 
-  typedef struct {
-    string name;
-    LMatrix4f transform;
-  } JointTransform ;
-
-  typedef pvector< JointTransform > JointTransformList;
-  
-  JointTransformList _frozen_joints;
-
   friend class PartBundleNode;
   friend class MovingPartBase;
   friend class MovingPartMatrix;

+ 18 - 2
panda/src/chan/partGroup.I

@@ -27,7 +27,8 @@
 INLINE PartGroup::
 PartGroup(const string &name) :
   Namable(name),
-  _children(get_class_type())
+  _children(get_class_type()),
+  _frozen(0)
 {
 }
 
@@ -41,8 +42,23 @@ PartGroup(const string &name) :
 INLINE PartGroup::
 PartGroup(const PartGroup &copy) :
   Namable(copy),
-  _children(get_class_type())
+  _children(get_class_type()),
+  _frozen(copy._frozen),
+  _frozen_transform(copy._frozen_transform)
 {
   // We don't copy children in the copy constructor.  However,
   // copy_subgraph() will do this.
 }
+
+
+////////////////////////////////////////////////////////////////////
+//     Function: PartGroup::freeze
+//       Access: Published
+//  Description: stores a joint freeze for later binding
+////////////////////////////////////////////////////////////////////
+INLINE void PartGroup::
+freeze_joint(LMatrix4f const &transform) 
+{
+  _frozen = true;
+  _frozen_transform=LMatrix4f(transform);
+}

+ 6 - 1
panda/src/chan/partGroup.cxx

@@ -44,8 +44,9 @@ PartGroup(PartGroup *parent, const string &name) :
   _children(get_class_type())
 {
   nassertv(parent != NULL);
-
+  
   parent->_children.push_back(this);
+  _frozen = false;
 }
 
 ////////////////////////////////////////////////////////////////////
@@ -467,6 +468,10 @@ bind_hierarchy(AnimGroup *anim, int channel_index, int &joint_index,
 
   while (i < part_num_children && j < anim_num_children) {
     PartGroup *pc = get_child(i);
+    if (pc->_frozen) {
+      anim->fix_child(j, pc->_frozen_transform);
+    }
+
     AnimGroup *ac = anim->get_child(j);
 
     if (pc->get_name() < ac->get_name()) {

+ 7 - 0
panda/src/chan/partGroup.h

@@ -79,6 +79,8 @@ PUBLISHED:
   virtual void write(ostream &out, int indent_level) const;
   virtual void write_with_value(ostream &out, int indent_level) const;
 
+  INLINE void freeze_joint(LMatrix4f const &transform);
+
 public:
   virtual TypeHandle get_value_type() const;
 
@@ -119,6 +121,9 @@ protected:
 private:
   int _num_children;
 
+  bool _frozen;
+  LMatrix4f _frozen_transform;
+
 public:
 
   virtual TypeHandle get_type() const {
@@ -136,11 +141,13 @@ public:
                   TypedWritableReferenceCount::get_class_type());
   }
 
+
 private:
   static TypeHandle _type_handle;
 
   friend class Character;
   friend class CharacterJointBundle;
+
 };
 
 #include "partGroup.I"