Skin.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <boost/property_tree/ptree.hpp>
  2. #include <boost/property_tree/xml_parser.hpp>
  3. #include <boost/foreach.hpp>
  4. #include "anki/resource/Skin.h"
  5. #include "anki/resource/Model.h"
  6. #include "anki/resource/Skeleton.h"
  7. #include "anki/resource/SkelAnim.h"
  8. #include "anki/resource/Mesh.h"
  9. #include "anki/resource/ModelPatch.h"
  10. namespace anki {
  11. //==============================================================================
  12. // Constructors & Destructors =
  13. //==============================================================================
  14. Skin::Skin()
  15. {}
  16. Skin::~Skin()
  17. {}
  18. //==============================================================================
  19. // load =
  20. //==============================================================================
  21. void Skin::load(const char* filename)
  22. {
  23. try
  24. {
  25. //
  26. // Load
  27. //
  28. using namespace boost::property_tree;
  29. ptree pt_;
  30. read_xml(filename, pt_);
  31. const ptree& pt = pt_.get_child("skin");
  32. // model
  33. model.load(pt.get<std::string>("model").c_str());
  34. // skeleton
  35. skeleton.load(pt.get<std::string>("skeleton").c_str());
  36. // Anims
  37. boost::optional<const ptree&> skelAnimsTree =
  38. pt.get_child_optional("skelAnims");
  39. if(skelAnimsTree)
  40. {
  41. BOOST_FOREACH(const ptree::value_type& v, skelAnimsTree.get())
  42. {
  43. if(v.first != "skelAnim")
  44. {
  45. throw ANKI_EXCEPTION("Expected skelAnim and no " + v.first);
  46. }
  47. const std::string& name = v.second.data();
  48. skelAnims.push_back(SkelAnimResourcePointer());
  49. skelAnims.back().load(name.c_str());
  50. }
  51. }
  52. //
  53. // Sanity checks
  54. //
  55. // Anims and skel bones num check
  56. BOOST_FOREACH(const SkelAnimResourcePointer& skelAnim, skelAnims)
  57. {
  58. // Bone number problem
  59. if(skelAnim->getBoneAnimations().size() !=
  60. skeleton->getBones().size())
  61. {
  62. throw ANKI_EXCEPTION("Skeleton animation \"" +
  63. skelAnim.getResourceName() + "\" and skeleton \"" +
  64. skeleton.getResourceName() +
  65. "\" dont have equal bone count");
  66. }
  67. }
  68. // All meshes should have vert weights
  69. BOOST_FOREACH(const ModelPatch& patch, model->getModelPatches())
  70. {
  71. if(!patch.supportsHwSkinning())
  72. {
  73. throw ANKI_EXCEPTION("Mesh does not support HW skinning");
  74. }
  75. }
  76. }
  77. catch(std::exception& e)
  78. {
  79. throw ANKI_EXCEPTION_R("Skin \"" + filename + "\"", e);
  80. }
  81. }
  82. } // end namespace