title: Bind Poses, Joint Transforms in Collada
body: <p>I'm trying to export a custom 3D model format to Collada. I've built the Collada data classes through XSD and now problems come when I try to fill them with data, especially for what concerns matrices.</p>
  
  <p>My Skeleton class is basically an array of Joint classes that I read from the binary file and every Joint looks like this (values of traslation and rotation are relative to the parent Joint, or Root if there is no parent, always):</p>
  
  <pre><code>class Joint
  {
      List&lt;Joint&gt; Children;
      Quaternion Rotation;
      Joint Parent;
      String Name;
      UInt32 Id;
      Vector3 Traslation;
  }
  </code></pre>
  
  <p>The first thing I do is to build the JOINT nodes in the "library_visual_scenes" section of the file. Which is quite simple and I get correct results:</p>
  
  <pre><code>foreach (Joint joint in hierarchicalJoints)
      WriteJointNodes(joint);
  
  private void WriteJointNodes(Joint joint)
  {
      Vector3 rotationEulers = Quaternion.ToEulers(joint.Rotation, EulersOrder.ZYX);
  
      WriteStartElement("node");
      WriteAttributeString("id", String.Concat(joint.Name, "_id"));
      WriteAttributeString("type", "JOINT");
      WriteAttributeString("name", joint.Name);
      {
          WriteElementString("translate", bone.Traslation);
          WriteElementStringAttributes("rotate", String.Concat("0.0 0.0 1.0 ", rotation.Z.ToDegrees()), { "sid", "rotateZ" });
          WriteElementStringAttributes("rotate", String.Concat("0.0 1.0 0.0 ", rotation.Y.ToDegrees()), { "sid", "rotateY" });
          WriteElementStringAttributes("rotate", String.Concat("1.0 0.0 0.0 ", rotation.X.ToDegrees()), { "sid", "rotateX" });
          WriteElementString("scale", "1.0 1.0 1.0");
  
          Joint[] children = joint.GetChildren();
  
          for (Int32 i = 0; i &lt; children.Length; ++i)
              WriteJointNodes(children[i]);
      }
      WriteEndElement();
  }
  </code></pre>
  
  <p>Here is an example of the output:</p>
  
  <pre><code>&lt;node id="bn_head_id" type="JOINT" name="bn_head"&gt;
      &lt;translate&gt;0.0732510 0.0000000 0.0000000&lt;/translate&gt;
      &lt;rotate sid="rotateZ"&gt;1.0 0.0 1.0 0.0&lt;/rotate&gt;
      &lt;rotate sid="rotateY"&gt;0.0 1.0 0.0 9.0&lt;/rotate&gt;
      &lt;rotate sid="rotateX"&gt;1.0 0.0 0.0 0.0&lt;/rotate&gt;
      &lt;scale&gt;1.0 1.0 1.0&lt;/scale&gt;
  </code></pre>
  
  <p>Now comes the tricky part, since I also have to export weights (skinning data) into the "library_controllers" section which looks like this:</p>
  
  <pre><code>&lt;skin source="#geometry1_id"&gt;
      &lt;bind_shape_matrix&gt;1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1&lt;/bind_shape_matrix&gt;
      &lt;source id="skinU3D1_id-joints"&gt;
          &lt;Name_array id="skinU3D1_id-joints-array" count="4"&gt;bn_head_id bn_jaw_id bn_lefteye_id bn_righteye_id&lt;/Name_array&gt;
          &lt;technique_common&gt;
              &lt;accessor source="#skinU3D1_id-joints-array" count="4" stride="1"&gt;
                  &lt;param name="JOINT" type="Name"/&gt;
              &lt;/accessor&gt;
          &lt;/technique_common&gt;
      &lt;/source&gt;
      &lt;source id="skinU3D1_id-bind_poses"&gt;
          &lt;float_array id="skinU3D1_id-bind_poses-array" count="64"&gt;0 0.999831 0.018391 -1.58086 -1 0 0 -0.000000 0 -0.018391 0.999831 0.041763 0 0 0 1 -0.00011 -0.374834 0.927092 0.564468 -1 -0.000506 -0.000323 0.000808 0.00059 -0.927092 -0.374834 1.45633 0 0 0 1 0 0.000036 1 -0.074606 1 0 0 0.032523 0 1 -0.000036 -1.638 0 0 0 1 -0.00004 0.000036 1 -0.074607 1 -0.000302 0.00004 -0.032021 0.000302 1 -0.000036 -1.63774 0 0 0 1&lt;/float_array&gt;
          &lt;technique_common&gt;
              &lt;accessor source="#skinU3D1_id-bind_poses-array" count="4" stride="16"&gt;
                  &lt;param name="TRANSFORM" type="float4x4"/&gt;
              &lt;/accessor&gt;
          &lt;/technique_common&gt;
          &lt;/source&gt;
          // &lt;&lt;Weights Source Here&gt;&gt;
          &lt;joints&gt;
              &lt;input semantic="JOINT" source="#skinU3D1_id-joints"/&gt;
              &lt;input semantic="INV_BIND_MATRIX" source="#skinU3D1_id-bind_poses"/&gt;
          &lt;/joints&gt;
          // &lt;&lt;Vertex Weights Here&gt;&gt;
  &lt;/skin&gt;
  </code></pre>
  
  <p>Here, the first 16 values of <code>skinU3D1_id-bind_poses-array</code> should represent the inverse bind pose of <code>bn_head</code> of my example.</p>
  
  <p>I can build the joints array properly and I can handle vertices weights without problems but I really don't understand how to obtain the matrices being used inside the skin controller. I need to output a Collada model with Y UP orientation and all I know is that Collada matrices are column-major.</p>
  
  <p>Starting from the data I have, my questions basically are:</p>
  
  <ol>
  <li>How do I calculate the <code>bind_shape_matrix</code> (in this example it's an identity matrix but I've also seen other Collada files in which it's different)?</li>
  <li>How do I calculate inverse bind matrices for every joint?</li>
  </ol>