DAEOptimizer.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "Base.h"
  2. #include "DAEOptimizer.h"
  3. namespace gameplay
  4. {
  5. DAEOptimizer::DAEOptimizer(domCOLLADA* dom)
  6. {
  7. _dom = dom;
  8. }
  9. DAEOptimizer::~DAEOptimizer()
  10. {
  11. }
  12. void DAEOptimizer::combineAnimations(const std::string& nodeId, const std::string& animationId)
  13. {
  14. std::list<domChannelRef> channels;
  15. daeSIDResolver resolver(_dom, nodeId.c_str());
  16. daeElement* element = resolver.getElement();
  17. if (element && element->typeID() == COLLADA_TYPE::NODE)
  18. {
  19. domNodeRef node = daeSafeCast<domNode>(resolver.getElement());
  20. getAnimationChannels(node, channels);
  21. }
  22. // Get the <library_animations>
  23. domLibrary_animations_Array& animationsLibraryArray = _dom->getLibrary_animations_array();
  24. assert(animationsLibraryArray.getCount() > 0);
  25. domLibrary_animationsRef& animationsLibrary = animationsLibraryArray.get(0);
  26. // Add a new animation
  27. domAnimationRef animation = daeSafeCast<domAnimation>(animationsLibrary->createAndPlace("animation"));
  28. assert(animation);
  29. animation->setId(animationId.c_str());
  30. // TODO: Make sure that there doesn't already exist an animation with this ID.
  31. // Move each of the channels to this animation
  32. for (std::list<domChannelRef>::iterator i = channels.begin(); i != channels.end(); ++i)
  33. {
  34. moveChannelAndSouresToAnimation(*i, animation);
  35. }
  36. // Clean up the empty animations
  37. deleteEmptyAnimations();
  38. }
  39. void DAEOptimizer::deleteEmptyAnimations()
  40. {
  41. std::list<domAnimationRef> animations;
  42. // Get the list of empty animations
  43. domLibrary_animations_Array& animationLibrary = _dom->getLibrary_animations_array();
  44. size_t animationLibraryCount = animationLibrary.getCount();
  45. for (size_t i = 0; i < animationLibraryCount; ++i)
  46. {
  47. domLibrary_animationsRef& animationsRef = animationLibrary.get(i);
  48. domAnimation_Array& animationArray = animationsRef->getAnimation_array();
  49. size_t animationCount = animationArray.getCount();
  50. for (size_t j = 0; j < animationCount; ++j)
  51. {
  52. domAnimationRef& animation = animationArray.get(j);
  53. if (isEmptyAnimation(animation))
  54. {
  55. animations.push_back(animation);
  56. }
  57. }
  58. }
  59. // Delete all of the empty animations
  60. for (std::list<domAnimationRef>::iterator i = animations.begin(); i != animations.end(); ++i)
  61. {
  62. daeElement::removeFromParent(*i);
  63. }
  64. }
  65. }