AnimationTarget.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. #include "Base.h"
  2. #include "AnimationTarget.h"
  3. #include "Animation.h"
  4. #include "Game.h"
  5. #include "Node.h"
  6. namespace gameplay
  7. {
  8. AnimationTarget::AnimationTarget()
  9. : _targetType(SCALAR), _animationChannels(NULL)
  10. {
  11. }
  12. AnimationTarget::~AnimationTarget()
  13. {
  14. if (_animationChannels)
  15. {
  16. std::vector<Animation::Channel*>::iterator itr = _animationChannels->begin();
  17. while (itr != _animationChannels->end())
  18. {
  19. Animation::Channel* channel = (*itr);
  20. GP_ASSERT(channel);
  21. GP_ASSERT(channel->_animation);
  22. channel->_animation->removeChannel(channel);
  23. SAFE_DELETE(channel);
  24. itr++;
  25. }
  26. _animationChannels->clear();
  27. SAFE_DELETE(_animationChannels);
  28. }
  29. }
  30. Animation* AnimationTarget::createAnimation(const char* id, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, Curve::InterpolationType type)
  31. {
  32. GP_ASSERT(type != Curve::BEZIER && type != Curve::HERMITE);
  33. GP_ASSERT(keyCount >= 1 && keyTimes && keyValues);
  34. Animation* animation = new Animation(id, this, propertyId, keyCount, keyTimes, keyValues, type);
  35. return animation;
  36. }
  37. Animation* AnimationTarget::createAnimation(const char* id, int propertyId, unsigned int keyCount, unsigned long* keyTimes, float* keyValues, float* keyInValue, float* keyOutValue, Curve::InterpolationType type)
  38. {
  39. GP_ASSERT(keyCount >= 1 && keyTimes && keyValues && keyInValue && keyOutValue);
  40. Animation* animation = new Animation(id, this, propertyId, keyCount, keyTimes, keyValues, keyInValue, keyOutValue, type);
  41. return animation;
  42. }
  43. Animation* AnimationTarget::createAnimation(const char* id, const char* url)
  44. {
  45. Properties* p = Properties::create(url);
  46. GP_ASSERT(p);
  47. Animation* animation = createAnimation(id, (strlen(p->getNamespace()) > 0) ? p : p->getNextNamespace());
  48. SAFE_DELETE(p);
  49. return animation;
  50. }
  51. Animation* AnimationTarget::createAnimationFromTo(const char* id, int propertyId, float* from, float* to, Curve::InterpolationType type, unsigned long duration)
  52. {
  53. GP_ASSERT(from);
  54. GP_ASSERT(to);
  55. const unsigned int propertyComponentCount = getAnimationPropertyComponentCount(propertyId);
  56. GP_ASSERT(propertyComponentCount > 0);
  57. float* keyValues = new float[2 * propertyComponentCount];
  58. memcpy(keyValues, from, sizeof(float) * propertyComponentCount);
  59. memcpy(keyValues + propertyComponentCount, to, sizeof(float) * propertyComponentCount);
  60. unsigned long* keyTimes = new unsigned long[2];
  61. keyTimes[0] = 0;
  62. keyTimes[1] = duration;
  63. Animation* animation = createAnimation(id, propertyId, 2, keyTimes, keyValues, type);
  64. SAFE_DELETE_ARRAY(keyValues);
  65. SAFE_DELETE_ARRAY(keyTimes);
  66. return animation;
  67. }
  68. Animation* AnimationTarget::createAnimationFromBy(const char* id, int propertyId, float* from, float* by, Curve::InterpolationType type, unsigned long duration)
  69. {
  70. GP_ASSERT(from);
  71. GP_ASSERT(by);
  72. const unsigned int propertyComponentCount = getAnimationPropertyComponentCount(propertyId);
  73. GP_ASSERT(propertyComponentCount > 0);
  74. float* keyValues = new float[2 * propertyComponentCount];
  75. memcpy(keyValues, from, sizeof(float) * propertyComponentCount);
  76. convertByValues(propertyId, propertyComponentCount, from, by);
  77. memcpy(keyValues + propertyComponentCount, by, sizeof(float) * propertyComponentCount);
  78. unsigned long* keyTimes = new unsigned long[2];
  79. keyTimes[0] = 0;
  80. keyTimes[1] = duration;
  81. Animation* animation = createAnimation(id, propertyId, 2, keyTimes, keyValues, type);
  82. SAFE_DELETE_ARRAY(keyValues);
  83. SAFE_DELETE_ARRAY(keyTimes);
  84. return animation;
  85. }
  86. Animation* AnimationTarget::createAnimation(const char* id, Properties* animationProperties)
  87. {
  88. GP_ASSERT(animationProperties);
  89. if (std::strcmp(animationProperties->getNamespace(), "animation") != 0)
  90. {
  91. GP_ERROR("Invalid animation namespace '%s'.", animationProperties->getNamespace());
  92. return NULL;
  93. }
  94. const char* propertyIdStr = animationProperties->getString("property");
  95. if (propertyIdStr == NULL)
  96. {
  97. GP_ERROR("Attribute 'property' must be specified for an animation.");
  98. return NULL;
  99. }
  100. // Get animation target property id
  101. int propertyId = AnimationTarget::getPropertyId(_targetType, propertyIdStr);
  102. if (propertyId == -1)
  103. {
  104. GP_ERROR("Property ID is invalid.");
  105. return NULL;
  106. }
  107. unsigned int keyCount = animationProperties->getInt("keyCount");
  108. if (keyCount == 0)
  109. {
  110. GP_ERROR("Attribute 'keyCount' must be specified for an animation.");
  111. return NULL;
  112. }
  113. const char* keyTimesStr = animationProperties->getString("keyTimes");
  114. if (keyTimesStr == NULL)
  115. {
  116. GP_ERROR("Attribute 'keyTimes' must be specified for an animation.");
  117. return NULL;
  118. }
  119. const char* keyValuesStr = animationProperties->getString("keyValues");
  120. if (keyValuesStr == NULL)
  121. {
  122. GP_ERROR("Attribute 'keyValues' must be specified for an animation.");
  123. return NULL;
  124. }
  125. const char* curveStr = animationProperties->getString("curve");
  126. if (curveStr == NULL)
  127. {
  128. GP_ERROR("Attribute 'curve' must be specified for an animation.");
  129. return NULL;
  130. }
  131. char delimeter = ' ';
  132. unsigned int startOffset = 0;
  133. unsigned int endOffset = (unsigned int)std::string::npos;
  134. unsigned long* keyTimes = new unsigned long[keyCount];
  135. for (unsigned int i = 0; i < keyCount; i++)
  136. {
  137. endOffset = static_cast<std::string>(keyTimesStr).find_first_of(delimeter, startOffset);
  138. if (endOffset != std::string::npos)
  139. {
  140. keyTimes[i] = std::strtoul(static_cast<std::string>(keyTimesStr).substr(startOffset, endOffset - startOffset).c_str(), NULL, 0);
  141. }
  142. else
  143. {
  144. keyTimes[i] = std::strtoul(static_cast<std::string>(keyTimesStr).substr(startOffset, static_cast<std::string>(keyTimesStr).length()).c_str(), NULL, 0);
  145. }
  146. startOffset = endOffset + 1;
  147. }
  148. startOffset = 0;
  149. endOffset = (unsigned int)std::string::npos;
  150. int componentCount = getAnimationPropertyComponentCount(propertyId);
  151. GP_ASSERT(componentCount > 0);
  152. unsigned int components = keyCount * componentCount;
  153. float* keyValues = new float[components];
  154. for (unsigned int i = 0; i < components; i++)
  155. {
  156. endOffset = static_cast<std::string>(keyValuesStr).find_first_of(delimeter, startOffset);
  157. if (endOffset != std::string::npos)
  158. {
  159. keyValues[i] = std::atof(static_cast<std::string>(keyValuesStr).substr(startOffset, endOffset - startOffset).c_str());
  160. }
  161. else
  162. {
  163. keyValues[i] = std::atof(static_cast<std::string>(keyValuesStr).substr(startOffset, static_cast<std::string>(keyValuesStr).length()).c_str());
  164. }
  165. startOffset = endOffset + 1;
  166. }
  167. const char* keyInStr = animationProperties->getString("keyIn");
  168. float* keyIn = NULL;
  169. if (keyInStr)
  170. {
  171. keyIn = new float[components];
  172. startOffset = 0;
  173. endOffset = (unsigned int)std::string::npos;
  174. for (unsigned int i = 0; i < components; i++)
  175. {
  176. endOffset = static_cast<std::string>(keyInStr).find_first_of(delimeter, startOffset);
  177. if (endOffset != std::string::npos)
  178. {
  179. keyIn[i] = std::atof(static_cast<std::string>(keyInStr).substr(startOffset, endOffset - startOffset).c_str());
  180. }
  181. else
  182. {
  183. keyIn[i] = std::atof(static_cast<std::string>(keyInStr).substr(startOffset, static_cast<std::string>(keyInStr).length()).c_str());
  184. }
  185. startOffset = endOffset + 1;
  186. }
  187. }
  188. const char* keyOutStr = animationProperties->getString("keyOut");
  189. float* keyOut = NULL;
  190. if (keyOutStr)
  191. {
  192. keyOut = new float[components];
  193. startOffset = 0;
  194. endOffset = (unsigned int)std::string::npos;
  195. for (unsigned int i = 0; i < components; i++)
  196. {
  197. endOffset = static_cast<std::string>(keyOutStr).find_first_of(delimeter, startOffset);
  198. if (endOffset != std::string::npos)
  199. {
  200. keyOut[i] = std::atof(static_cast<std::string>(keyOutStr).substr(startOffset, endOffset - startOffset).c_str());
  201. }
  202. else
  203. {
  204. keyOut[i] = std::atof(static_cast<std::string>(keyOutStr).substr(startOffset, static_cast<std::string>(keyOutStr).length()).c_str());
  205. }
  206. startOffset = endOffset + 1;
  207. }
  208. }
  209. int curve = Curve::getInterpolationType(curveStr);
  210. Animation* animation = NULL;
  211. if (keyIn && keyOut)
  212. {
  213. animation = createAnimation(id, propertyId, keyCount, keyTimes, keyValues, keyIn, keyOut, (Curve::InterpolationType)curve);
  214. }
  215. else
  216. {
  217. animation = createAnimation(id, propertyId, keyCount, keyTimes, keyValues, (Curve::InterpolationType) curve);
  218. }
  219. SAFE_DELETE_ARRAY(keyOut);
  220. SAFE_DELETE_ARRAY(keyIn);
  221. SAFE_DELETE_ARRAY(keyValues);
  222. SAFE_DELETE_ARRAY(keyTimes);
  223. Properties* pClip = animationProperties->getNextNamespace();
  224. if (pClip && std::strcmp(pClip->getNamespace(), "clip") == 0)
  225. {
  226. int frameCount = animationProperties->getInt("frameCount");
  227. if (frameCount <= 0)
  228. {
  229. GP_ERROR("Frame count must be greater than zero for a clip.");
  230. return animation;
  231. }
  232. animation->createClips(animationProperties, (unsigned int) frameCount);
  233. }
  234. return animation;
  235. }
  236. void AnimationTarget::destroyAnimation(const char* id)
  237. {
  238. // Find the animation with the specified ID.
  239. Animation::Channel* channel = getChannel(id);
  240. if (channel == NULL)
  241. return;
  242. // Remove this target's channel from animation, and from the target's list of channels.
  243. GP_ASSERT(channel->_animation);
  244. channel->_animation->removeChannel(channel);
  245. removeChannel(channel);
  246. SAFE_DELETE(channel);
  247. }
  248. Animation* AnimationTarget::getAnimation(const char* id) const
  249. {
  250. if (_animationChannels)
  251. {
  252. std::vector<Animation::Channel*>::iterator itr = _animationChannels->begin();
  253. GP_ASSERT(*itr);
  254. if (id == NULL)
  255. return (*itr)->_animation;
  256. Animation::Channel* channel = NULL;
  257. for (; itr != _animationChannels->end(); itr++)
  258. {
  259. channel = (Animation::Channel*)(*itr);
  260. GP_ASSERT(channel);
  261. GP_ASSERT(channel->_animation);
  262. if (channel->_animation->_id.compare(id) == 0)
  263. {
  264. return channel->_animation;
  265. }
  266. }
  267. }
  268. return NULL;
  269. }
  270. int AnimationTarget::getPropertyId(TargetType type, const char* propertyIdStr)
  271. {
  272. GP_ASSERT(propertyIdStr);
  273. if (type == AnimationTarget::TRANSFORM)
  274. {
  275. if (strcmp(propertyIdStr, "ANIMATE_SCALE") == 0)
  276. {
  277. return Transform::ANIMATE_SCALE;
  278. }
  279. else if (strcmp(propertyIdStr, "ANIMATE_SCALE_X") == 0)
  280. {
  281. return Transform::ANIMATE_SCALE_X;
  282. }
  283. else if (strcmp(propertyIdStr, "ANIMATE_SCALE_Y") == 0)
  284. {
  285. return Transform::ANIMATE_SCALE_Y;
  286. }
  287. else if (strcmp(propertyIdStr, "ANIMATE_SCALE_Z") == 0)
  288. {
  289. return Transform::ANIMATE_SCALE_Z;
  290. }
  291. else if (strcmp(propertyIdStr, "ANIMATE_ROTATE") == 0)
  292. {
  293. return Transform::ANIMATE_ROTATE;
  294. }
  295. else if (strcmp(propertyIdStr, "ANIMATE_TRANSLATE") == 0)
  296. {
  297. return Transform::ANIMATE_TRANSLATE;
  298. }
  299. else if (strcmp(propertyIdStr, "ANIMATE_TRANSLATE_X") == 0)
  300. {
  301. return Transform::ANIMATE_TRANSLATE_X;
  302. }
  303. else if (strcmp(propertyIdStr, "ANIMATE_TRANSLATE_Y") == 0)
  304. {
  305. return Transform::ANIMATE_TRANSLATE_Y;
  306. }
  307. else if (strcmp(propertyIdStr, "ANIMATE_TRANSLATE_Z") == 0)
  308. {
  309. return Transform::ANIMATE_TRANSLATE_Z;
  310. }
  311. else if (strcmp(propertyIdStr, "ANIMATE_ROTATE_TRANSLATE") == 0)
  312. {
  313. return Transform::ANIMATE_ROTATE_TRANSLATE;
  314. }
  315. else if (strcmp(propertyIdStr, "ANIMATE_SCALE_ROTATE_TRANSLATE") == 0)
  316. {
  317. return Transform::ANIMATE_SCALE_ROTATE_TRANSLATE;
  318. }
  319. }
  320. else
  321. {
  322. if (strcmp(propertyIdStr, "ANIMATE_UNIFORM") == 0)
  323. {
  324. return MaterialParameter::ANIMATE_UNIFORM;
  325. }
  326. }
  327. return -1;
  328. }
  329. void AnimationTarget::addChannel(Animation::Channel* channel)
  330. {
  331. if (_animationChannels == NULL)
  332. _animationChannels = new std::vector<Animation::Channel*>;
  333. GP_ASSERT(channel);
  334. _animationChannels->push_back(channel);
  335. }
  336. void AnimationTarget::removeChannel(Animation::Channel* channel)
  337. {
  338. if (_animationChannels)
  339. {
  340. std::vector<Animation::Channel*>::iterator itr = _animationChannels->begin();
  341. for ( ; itr != _animationChannels->end(); itr++)
  342. {
  343. Animation::Channel* temp = *itr;
  344. if (channel == temp)
  345. {
  346. _animationChannels->erase(itr);
  347. if (_animationChannels->empty())
  348. SAFE_DELETE(_animationChannels);
  349. return;
  350. }
  351. }
  352. }
  353. }
  354. Animation::Channel* AnimationTarget::getChannel(const char* id) const
  355. {
  356. if (_animationChannels)
  357. {
  358. std::vector<Animation::Channel*>::iterator itr = _animationChannels->begin();
  359. if (id == NULL)
  360. return (*itr);
  361. Animation::Channel* channel = NULL;
  362. for (; itr != _animationChannels->end(); itr++)
  363. {
  364. channel = (Animation::Channel*)(*itr);
  365. GP_ASSERT(channel);
  366. if (channel->_animation->_id.compare(id) == 0)
  367. {
  368. return channel;
  369. }
  370. }
  371. }
  372. return NULL;
  373. }
  374. void AnimationTarget::cloneInto(AnimationTarget* target, NodeCloneContext &context) const
  375. {
  376. if (_animationChannels)
  377. {
  378. for (std::vector<Animation::Channel*>::const_iterator it = _animationChannels->begin(); it != _animationChannels->end(); ++it)
  379. {
  380. Animation::Channel* channel = *it;
  381. GP_ASSERT(channel);
  382. GP_ASSERT(channel->_animation);
  383. Animation* animation = context.findClonedAnimation(channel->_animation);
  384. if (animation != NULL)
  385. {
  386. Animation::Channel* channelCopy = new Animation::Channel(*channel, animation, target);
  387. animation->addChannel(channelCopy);
  388. }
  389. else
  390. {
  391. // Clone the animation and register it with the context so that it only gets cloned once.
  392. animation = channel->_animation->clone(channel, target);
  393. context.registerClonedAnimation(channel->_animation, animation);
  394. }
  395. }
  396. }
  397. }
  398. void AnimationTarget::convertByValues(unsigned int propertyId, unsigned int componentCount, float* from, float* by)
  399. {
  400. if (_targetType == AnimationTarget::TRANSFORM)
  401. {
  402. switch(propertyId)
  403. {
  404. case Transform::ANIMATE_SCALE:
  405. case Transform::ANIMATE_SCALE_UNIT:
  406. case Transform::ANIMATE_SCALE_X:
  407. case Transform::ANIMATE_SCALE_Y:
  408. case Transform::ANIMATE_SCALE_Z:
  409. {
  410. convertScaleByValues(from, by, componentCount);
  411. break;
  412. }
  413. case Transform::ANIMATE_TRANSLATE:
  414. case Transform::ANIMATE_TRANSLATE_X:
  415. case Transform::ANIMATE_TRANSLATE_Y:
  416. case Transform::ANIMATE_TRANSLATE_Z:
  417. {
  418. convertByValues(from, by, componentCount);
  419. break;
  420. }
  421. case Transform::ANIMATE_ROTATE:
  422. {
  423. convertQuaternionByValues(from, by);
  424. break;
  425. }
  426. case Transform::ANIMATE_ROTATE_TRANSLATE:
  427. {
  428. convertQuaternionByValues(from, by);
  429. convertByValues(from + 4, by + 4, 3);
  430. break;
  431. }
  432. case Transform::ANIMATE_SCALE_ROTATE_TRANSLATE:
  433. {
  434. convertScaleByValues(from, by, 3);
  435. convertQuaternionByValues(from + 3, by + 3);
  436. convertByValues(from + 7, by + 7, 3);
  437. break;
  438. }
  439. }
  440. }
  441. else
  442. {
  443. convertByValues(from, by, componentCount);
  444. }
  445. }
  446. void AnimationTarget::convertQuaternionByValues(float* from, float* by)
  447. {
  448. Quaternion qFrom(from);
  449. Quaternion qBy(by);
  450. qBy.multiply(qFrom);
  451. memcpy(by, (float*)&qBy, sizeof(float) * 4);
  452. }
  453. void AnimationTarget::convertScaleByValues(float* from, float* by, unsigned int componentCount)
  454. {
  455. for (unsigned int i = 0; i < componentCount; i++)
  456. {
  457. by[i] *= from[i];
  458. }
  459. }
  460. void AnimationTarget::convertByValues(float* from, float* by, unsigned int componentCount)
  461. {
  462. for (unsigned int i = 0; i < componentCount; i++)
  463. {
  464. by[i] += from[i];
  465. }
  466. }
  467. }