home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 5036 / source.7z / x_bone.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2012-02-05  |  5.6 KB  |  180 lines

  1. #include "xentax.h"
  2.  
  3. JOINTTREE::JOINTTREE()
  4. {
  5.  root = INVALID_JOINT;
  6. }
  7.  
  8. bool JOINTTREE::InsertJoint(uint32 index, const JOINT& joint)
  9. {
  10.  // check if already inserted
  11.  tree_t::iterator temp = tree.find(index);
  12.  if(temp != tree.end()) return error("InsertJoint: Joint already exists.");
  13.  
  14.  // insert root
  15.  if(joint.parent == INVALID_JOINT) {
  16.     node_t node;
  17.     node.joint = joint;
  18.     tree.insert(tree_t::value_type(index, node));
  19.     root = index;
  20.     return true;
  21.    }
  22.  
  23.  // find parent
  24.  tree_t::iterator parent = tree.find(joint.parent);
  25.  if(parent == tree.end()) return error("InsertJoint: Can't find parent joint.");
  26.  
  27.  // insert node
  28.  node_t node;
  29.  node.joint = joint;
  30.  tree.insert(tree_t::value_type(index, node));
  31.  
  32.  // insert into parent's adjacency list
  33.  parent->second.children.push_back(index);
  34.  return true;
  35. }
  36.  
  37. bool JOINTTREE::RemoveJoint(uint32 index)
  38. {
  39.  // TODO
  40.  return true;
  41. }
  42.  
  43. void JOINTTREE::PrintJointTree(std::stringstream& ss)const
  44. {
  45.  PrintJointTree(ss, root, 0);
  46. }
  47.  
  48. void JOINTTREE::PrintJointTree(std::stringstream& ss, uint32 index, uint32 level)const
  49. {
  50.  // find node
  51.  const_tree_iterator iter = tree.find(index);
  52.  if(iter == tree.end()) return;
  53.  
  54.  // build string
  55.  const node_t& node = iter->second;
  56.  for(size_t i = 0; i < level; i++) ss << " ";
  57.  ss << node.joint.name << std::endl;
  58.  
  59.  // visit children of joint tree node
  60.  for(size_t i = 0; i < node.children.size(); i++) PrintJointTree(ss, node.children[i], level + 1);
  61. }
  62.  
  63. void JOINTTREE::PrintColladaNodeHeirarchy(const std::string& id, std::stringstream& ss)const
  64. {
  65.  PrintColladaNodeHeirarchy(id, ss, root, 0);
  66. }
  67.  
  68. void JOINTTREE::PrintColladaNodeHeirarchy(const std::string& id, std::stringstream& ss, uint32 index, uint32 level)const
  69. {
  70.  // NOTES:
  71.  // Unlike in Collada specification, you must have id attributes for all joint nodes if you want to see the
  72.  // names in 3DS MAX. Solution is to just define id and sid together at the same time.
  73.  
  74.  // find node
  75.  const_tree_iterator iter = tree.find(index);
  76.  if(iter == tree.end()) return;
  77.  
  78.  // determine spacing
  79.  std::string spacing = "  ";
  80.  for(size_t i = 0; i < level; i++) spacing += ' ';
  81.  
  82.  // print node
  83.  const node_t& node = iter->second;
  84.  if(index == root) ss << spacing << "<node id=\"" << id << "\" sid=\"" << node.joint.name << "\">" << std::endl;
  85.  else ss << spacing << "<node id=\"" << node.joint.name << "\" sid=\"" << node.joint.name << "\" type=\"JOINT\">" << std::endl;
  86.  
  87.  // position node
  88.  ss << spacing << " <matrix>" << endl;
  89.  ss << spacing << "  " << "1 0 0 " << node.joint.rel_x << endl;
  90.  ss << spacing << "  " << "0 1 0 " << node.joint.rel_y << endl;
  91.  ss << spacing << "  " << "0 0 1 " << node.joint.rel_z << endl;
  92.  ss << spacing << "  " << "0 0 0 1" << endl;
  93.  ss << spacing << " </matrix>" << endl;
  94.  
  95.  // print children
  96.  for(size_t i = 0; i < node.children.size(); i++)
  97.      PrintColladaNodeHeirarchy(id, ss, node.children[i], level + 1);
  98.  
  99.  // finish node
  100.  ss << spacing << "</node>" << endl;
  101. }
  102.  
  103. void JOINTTREE::PrintColladaJoints(const std::string& id, std::stringstream& ss)const
  104. {
  105.  ss << "    <source id=\"" << id << "_joints\">" << endl;
  106.  
  107.  ss << "     <Name_array count=\"" << tree.size() << "\">" << endl;
  108.  PrintColladaJoints(ss, root);
  109.  ss << "     </Name_array>" << endl;
  110.  
  111.  ss << "     <technique_common>" << endl;
  112.  ss << "      <acessor source=\"#" << id << "_joints\" count=\"" << tree.size() << "\" stride=\"1\">" << endl;
  113.  ss << "       <param name=\"JOINT\" type=\"name\" />" << endl;
  114.  ss << "      </acessor>" << endl;
  115.  ss << "     </technique_common>" << endl;
  116.  
  117.  ss << "    </source>" << endl;
  118. }
  119.  
  120. void JOINTTREE::PrintColladaJoints(std::stringstream& ss, uint32 index)const
  121. {
  122.  // find node
  123.  const_tree_iterator iter = tree.find(index);
  124.  if(iter == tree.end()) return;
  125.  
  126.  // print node
  127.  const node_t& node = iter->second;
  128.  ss << "      " << node.joint.name << std::endl;
  129.  
  130.  // print children
  131.  for(size_t i = 0; i < node.children.size(); i++)
  132.      PrintColladaJoints(ss, node.children[i]);
  133. }
  134.  
  135. void JOINTTREE::PrintColladaBindMatrices(const std::string& id, std::stringstream& ss)const
  136. {
  137.  ss << "    <source id=\"" << id << "_matrices\">" << endl;
  138.  ss << "     <float_array count=\"" << 16*tree.size() << "\">" << endl;
  139.  PrintColladaBindMatrices(ss, root);
  140.  ss << "     </float_array>" << endl;
  141.  
  142.  ss << "     <technique_common>" << endl;
  143.  ss << "      <acessor source=\"#" << id << "_matrices\" count=\"" << tree.size() << "\" stride=\"16\">" << endl;
  144.  ss << "       <param name=\"INV_BIND_MATRIX\" type=\"float4x4\" />" << endl;
  145.  ss << "      </acessor>" << endl;
  146.  ss << "     </technique_common>" << endl;
  147.  
  148.  ss << "    </source>" << endl;
  149. }
  150.  
  151. void JOINTTREE::PrintColladaBindMatrices(std::stringstream& ss, uint32 index)const
  152. {
  153.  // find node
  154.  const_tree_iterator iter = tree.find(index);
  155.  if(iter == tree.end()) return;
  156.  
  157.  // print node
  158.  const node_t& node = iter->second;
  159.  ss << "      ";
  160.  ss << node.joint.matrix[ 0] << " ";
  161.  ss << node.joint.matrix[ 1] << " ";
  162.  ss << node.joint.matrix[ 2] << " ";
  163.  ss << node.joint.matrix[ 3] << " ";
  164.  ss << node.joint.matrix[ 4] << " ";
  165.  ss << node.joint.matrix[ 5] << " ";
  166.  ss << node.joint.matrix[ 6] << " ";
  167.  ss << node.joint.matrix[ 7] << " ";
  168.  ss << node.joint.matrix[ 8] << " ";
  169.  ss << node.joint.matrix[ 9] << " ";
  170.  ss << node.joint.matrix[10] << " ";
  171.  ss << node.joint.matrix[11] << " ";
  172.  ss << node.joint.matrix[12] << " ";
  173.  ss << node.joint.matrix[13] << " ";
  174.  ss << node.joint.matrix[14] << " ";
  175.  ss << node.joint.matrix[14] << " " << endl;
  176.  
  177.  // print children
  178.  for(size_t i = 0; i < node.children.size(); i++)
  179.      PrintColladaBindMatrices(ss, node.children[i]);
  180. }