Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

314 řádky
9.4 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file FBXAnimation.cpp
  34. * @brief Assimp::FBX::AnimationCurve, Assimp::FBX::AnimationCurveNode,
  35. * Assimp::FBX::AnimationLayer, Assimp::FBX::AnimationStack
  36. */
  37. #include "AssimpPCH.h"
  38. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  39. #include "FBXParser.h"
  40. #include "FBXDocument.h"
  41. #include "FBXImporter.h"
  42. #include "FBXImportSettings.h"
  43. #include "FBXDocumentUtil.h"
  44. #include "FBXProperties.h"
  45. namespace Assimp {
  46. namespace FBX {
  47. using namespace Util;
  48. // ------------------------------------------------------------------------------------------------
  49. AnimationCurve::AnimationCurve(uint64_t id, const Element& element, const std::string& name, const Document& doc)
  50. : Object(id, element, name)
  51. {
  52. const Scope& sc = GetRequiredScope(element);
  53. const Element& KeyTime = GetRequiredElement(sc,"KeyTime");
  54. const Element& KeyValueFloat = GetRequiredElement(sc,"KeyValueFloat");
  55. ParseVectorDataArray(keys, KeyTime);
  56. ParseVectorDataArray(values, KeyValueFloat);
  57. if(keys.size() != values.size()) {
  58. DOMError("the number of key times does not match the number of keyframe values",&KeyTime);
  59. }
  60. // check if the key times are well-ordered
  61. if(!std::equal(keys.begin(), keys.end() - 1, keys.begin() + 1, std::less<KeyTimeList::value_type>())) {
  62. DOMError("the keyframes are not in ascending order",&KeyTime);
  63. }
  64. const Element* KeyAttrDataFloat = sc["KeyAttrDataFloat"];
  65. if(KeyAttrDataFloat) {
  66. ParseVectorDataArray(attributes, *KeyAttrDataFloat);
  67. }
  68. const Element* KeyAttrFlags = sc["KeyAttrFlags"];
  69. if(KeyAttrFlags) {
  70. ParseVectorDataArray(flags, *KeyAttrFlags);
  71. }
  72. }
  73. // ------------------------------------------------------------------------------------------------
  74. AnimationCurve::~AnimationCurve()
  75. {
  76. }
  77. // ------------------------------------------------------------------------------------------------
  78. AnimationCurveNode::AnimationCurveNode(uint64_t id, const Element& element, const std::string& name, const Document& doc,
  79. const char* const * target_prop_whitelist /*= NULL*/, size_t whitelist_size /*= 0*/)
  80. : Object(id, element, name)
  81. , target()
  82. , doc(doc)
  83. {
  84. const Scope& sc = GetRequiredScope(element);
  85. // find target node
  86. const char* whitelist[] = {"Model","NodeAttribute"};
  87. const std::vector<const Connection*>& conns = doc.GetConnectionsBySourceSequenced(ID(),whitelist,2);
  88. BOOST_FOREACH(const Connection* con, conns) {
  89. // link should go for a property
  90. if (!con->PropertyName().length()) {
  91. continue;
  92. }
  93. if(target_prop_whitelist) {
  94. const char* const s = con->PropertyName().c_str();
  95. bool ok = false;
  96. for (size_t i = 0; i < whitelist_size; ++i) {
  97. if (!strcmp(s, target_prop_whitelist[i])) {
  98. ok = true;
  99. break;
  100. }
  101. }
  102. if (!ok) {
  103. throw std::range_error("AnimationCurveNode target property is not in whitelist");
  104. }
  105. }
  106. const Object* const ob = con->DestinationObject();
  107. if(!ob) {
  108. DOMWarning("failed to read destination object for AnimationCurveNode->Model link, ignoring",&element);
  109. continue;
  110. }
  111. // XXX support constraints as DOM class
  112. //ai_assert(dynamic_cast<const Model*>(ob) || dynamic_cast<const NodeAttribute*>(ob));
  113. target = ob;
  114. if(!target) {
  115. continue;
  116. }
  117. prop = con->PropertyName();
  118. break;
  119. }
  120. if(!target) {
  121. DOMWarning("failed to resolve target Model/NodeAttribute/Constraint for AnimationCurveNode",&element);
  122. }
  123. props = GetPropertyTable(doc,"AnimationCurveNode.FbxAnimCurveNode",element,sc,false);
  124. }
  125. // ------------------------------------------------------------------------------------------------
  126. AnimationCurveNode::~AnimationCurveNode()
  127. {
  128. }
  129. // ------------------------------------------------------------------------------------------------
  130. const AnimationCurveMap& AnimationCurveNode::Curves() const
  131. {
  132. if(curves.empty()) {
  133. // resolve attached animation curves
  134. const std::vector<const Connection*>& conns = doc.GetConnectionsByDestinationSequenced(ID(),"AnimationCurve");
  135. BOOST_FOREACH(const Connection* con, conns) {
  136. // link should go for a property
  137. if (!con->PropertyName().length()) {
  138. continue;
  139. }
  140. const Object* const ob = con->SourceObject();
  141. if(!ob) {
  142. DOMWarning("failed to read source object for AnimationCurve->AnimationCurveNode link, ignoring",&element);
  143. continue;
  144. }
  145. const AnimationCurve* const anim = dynamic_cast<const AnimationCurve*>(ob);
  146. if(!anim) {
  147. DOMWarning("source object for ->AnimationCurveNode link is not an AnimationCurve",&element);
  148. continue;
  149. }
  150. curves[con->PropertyName()] = anim;
  151. }
  152. }
  153. return curves;
  154. }
  155. // ------------------------------------------------------------------------------------------------
  156. AnimationLayer::AnimationLayer(uint64_t id, const Element& element, const std::string& name, const Document& doc)
  157. : Object(id, element, name)
  158. , doc(doc)
  159. {
  160. const Scope& sc = GetRequiredScope(element);
  161. // note: the props table here bears little importance and is usually absent
  162. props = GetPropertyTable(doc,"AnimationLayer.FbxAnimLayer",element,sc, true);
  163. }
  164. // ------------------------------------------------------------------------------------------------
  165. AnimationLayer::~AnimationLayer()
  166. {
  167. }
  168. // ------------------------------------------------------------------------------------------------
  169. AnimationCurveNodeList AnimationLayer::Nodes(const char* const * target_prop_whitelist /*= NULL*/,
  170. size_t whitelist_size /*= 0*/) const
  171. {
  172. AnimationCurveNodeList nodes;
  173. // resolve attached animation nodes
  174. const std::vector<const Connection*>& conns = doc.GetConnectionsByDestinationSequenced(ID(),"AnimationCurveNode");
  175. nodes.reserve(conns.size());
  176. BOOST_FOREACH(const Connection* con, conns) {
  177. // link should not go to a property
  178. if (con->PropertyName().length()) {
  179. continue;
  180. }
  181. const Object* const ob = con->SourceObject();
  182. if(!ob) {
  183. DOMWarning("failed to read source object for AnimationCurveNode->AnimationLayer link, ignoring",&element);
  184. continue;
  185. }
  186. const AnimationCurveNode* const anim = dynamic_cast<const AnimationCurveNode*>(ob);
  187. if(!anim) {
  188. DOMWarning("source object for ->AnimationLayer link is not an AnimationCurveNode",&element);
  189. continue;
  190. }
  191. if(target_prop_whitelist) {
  192. const char* s = anim->TargetProperty().c_str();
  193. bool ok = false;
  194. for (size_t i = 0; i < whitelist_size; ++i) {
  195. if (!strcmp(s, target_prop_whitelist[i])) {
  196. ok = true;
  197. break;
  198. }
  199. }
  200. if(!ok) {
  201. continue;
  202. }
  203. }
  204. nodes.push_back(anim);
  205. }
  206. return nodes; // pray for NRVO
  207. }
  208. // ------------------------------------------------------------------------------------------------
  209. AnimationStack::AnimationStack(uint64_t id, const Element& element, const std::string& name, const Document& doc)
  210. : Object(id, element, name)
  211. {
  212. const Scope& sc = GetRequiredScope(element);
  213. // note: we don't currently use any of these properties so we shouldn't bother if it is missing
  214. props = GetPropertyTable(doc,"AnimationStack.FbxAnimStack",element,sc, true);
  215. // resolve attached animation layers
  216. const std::vector<const Connection*>& conns = doc.GetConnectionsByDestinationSequenced(ID(),"AnimationLayer");
  217. layers.reserve(conns.size());
  218. BOOST_FOREACH(const Connection* con, conns) {
  219. // link should not go to a property
  220. if (con->PropertyName().length()) {
  221. continue;
  222. }
  223. const Object* const ob = con->SourceObject();
  224. if(!ob) {
  225. DOMWarning("failed to read source object for AnimationLayer->AnimationStack link, ignoring",&element);
  226. continue;
  227. }
  228. const AnimationLayer* const anim = dynamic_cast<const AnimationLayer*>(ob);
  229. if(!anim) {
  230. DOMWarning("source object for ->AnimationStack link is not an AnimationLayer",&element);
  231. continue;
  232. }
  233. layers.push_back(anim);
  234. }
  235. }
  236. // ------------------------------------------------------------------------------------------------
  237. AnimationStack::~AnimationStack()
  238. {
  239. }
  240. } //!FBX
  241. } //!Assimp
  242. #endif