AnimationChannel.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include "Base.h"
  2. #include "AnimationChannel.h"
  3. #include "Transform.h"
  4. namespace gameplay
  5. {
  6. AnimationChannel::AnimationChannel(void) :
  7. _targetAttrib(0)
  8. {
  9. }
  10. AnimationChannel::~AnimationChannel(void)
  11. {
  12. }
  13. unsigned int AnimationChannel::getTypeId(void) const
  14. {
  15. return ANIMATIONCHANNEL_ID;
  16. }
  17. const char* AnimationChannel::getElementName(void) const
  18. {
  19. return "AnimationChannel";
  20. }
  21. void AnimationChannel::writeBinary(FILE* file)
  22. {
  23. Object::writeBinary(file);
  24. write(_targetId, file);
  25. write(_targetAttrib, file);
  26. write(_keytimes.size(), file);
  27. for (std::vector<float>::const_iterator i = _keytimes.begin(); i != _keytimes.end(); ++i)
  28. {
  29. write((unsigned long)*i, file);
  30. }
  31. write(_keyValues, file);
  32. write(_tangentsIn, file);
  33. write(_tangentsOut, file);
  34. write(_interpolations, file);
  35. }
  36. void AnimationChannel::writeText(FILE* file)
  37. {
  38. fprintElementStart(file);
  39. fprintfElement(file, "targetId", _targetId);
  40. fprintf(file, "<%s>%u %s</%s>\n", "targetAttrib", _targetAttrib, Transform::getPropertyString(_targetAttrib), "targetAttrib");
  41. fprintfElement(file, "%f ", "keytimes", _keytimes);
  42. fprintfElement(file, "%f ", "values", _keyValues);
  43. fprintfElement(file, "%f ", "tangentsIn", _tangentsIn);
  44. fprintfElement(file, "%f ", "tangentsOut", _tangentsOut);
  45. fprintfElement(file, "%u ", "interpolations", _interpolations);
  46. fprintElementEnd(file);
  47. }
  48. void AnimationChannel::setInterpolation(unsigned int interpolation)
  49. {
  50. _interpolations.clear();
  51. _interpolations.push_back(interpolation);
  52. }
  53. const std::string& AnimationChannel::getTargetId() const
  54. {
  55. return _targetId;
  56. }
  57. unsigned int AnimationChannel::getTargetAttribute() const
  58. {
  59. return _targetAttrib;
  60. }
  61. const std::vector<float>& AnimationChannel::getKeyValues() const
  62. {
  63. return _keyValues;
  64. }
  65. const std::vector<float>& AnimationChannel::getKeyTimes() const
  66. {
  67. return _keytimes;
  68. }
  69. const std::vector<float>& AnimationChannel::getTangentsIn() const
  70. {
  71. return _tangentsIn;
  72. }
  73. const std::vector<float>& AnimationChannel::getTangentsOut() const
  74. {
  75. return _tangentsOut;
  76. }
  77. const std::vector<unsigned int>& AnimationChannel::getInterpolationTypes() const
  78. {
  79. return _interpolations;
  80. }
  81. void AnimationChannel::setTargetId(const std::string& str)
  82. {
  83. _targetId = str;
  84. }
  85. void AnimationChannel::setTargetAttribute(unsigned int attrib)
  86. {
  87. _targetAttrib = attrib;
  88. }
  89. void AnimationChannel::setKeyTimes(const std::vector<float>& values)
  90. {
  91. _keytimes = values;
  92. }
  93. void AnimationChannel::setKeyValues(const std::vector<float>& values)
  94. {
  95. _keyValues = values;
  96. }
  97. void AnimationChannel::setTangentsIn(const std::vector<float>& values)
  98. {
  99. _tangentsIn = values;
  100. }
  101. void AnimationChannel::setTangentsOut(const std::vector<float>& values)
  102. {
  103. _tangentsOut = values;
  104. }
  105. void AnimationChannel::setInterpolations(const std::vector<unsigned int>& values)
  106. {
  107. _interpolations = values;
  108. }
  109. void AnimationChannel::removeDuplicates()
  110. {
  111. size_t propSize = Transform::getPropertySize(_targetAttrib);
  112. if (propSize > 1 && !_interpolations.empty() && _interpolations[0] == LINEAR)
  113. {
  114. size_t prevIndex = 0;
  115. std::vector<float>::iterator prevStart = _keyValues.begin();
  116. std::vector<float>::iterator prevEnd = prevStart + propSize - 1;
  117. size_t i = 1;
  118. for (i = 1; i < _keytimes.size(); ++i)
  119. {
  120. std::vector<float>::iterator start = _keyValues.begin() + i * propSize;
  121. std::vector<float>::iterator end = start + propSize - 1;
  122. if (!equal(prevStart, prevEnd, start) || i == _keytimes.size() - 1)
  123. {
  124. if (i - prevIndex > 2)
  125. {
  126. deleteRange(prevIndex+1, i, propSize);
  127. i = prevIndex;
  128. prevStart = _keyValues.begin() + i * propSize;
  129. prevEnd = prevStart + propSize - 1;
  130. }
  131. else
  132. {
  133. prevStart = start;
  134. prevEnd = end;
  135. prevIndex = i;
  136. }
  137. }
  138. }
  139. if (i - 1 - prevIndex >= 2)
  140. {
  141. deleteRange(prevIndex+1, i, propSize);
  142. }
  143. }
  144. }
  145. void AnimationChannel::convertToQuaternion()
  146. {
  147. if (_targetAttrib == Transform::ANIMATE_ROTATE_X ||
  148. _targetAttrib == Transform::ANIMATE_ROTATE_Y ||
  149. _targetAttrib == Transform::ANIMATE_ROTATE_Z)
  150. {
  151. std::vector<float> newKeyValues;
  152. newKeyValues.resize(_keyValues.size() * 4);
  153. const size_t count = _keyValues.size();
  154. float x = _targetAttrib == Transform::ANIMATE_ROTATE_X ? 1.0f : 0.0f;
  155. float y = _targetAttrib == Transform::ANIMATE_ROTATE_Y ? 1.0f : 0.0f;
  156. float z = _targetAttrib == Transform::ANIMATE_ROTATE_Z ? 1.0f : 0.0f;
  157. for (size_t i = 0; i < count; ++i)
  158. {
  159. size_t j = i << 2;
  160. newKeyValues[j] = x;
  161. newKeyValues[j+1] = y;
  162. newKeyValues[j+2] = z;
  163. newKeyValues[j+3] = _keyValues[i];
  164. }
  165. setKeyValues(newKeyValues);
  166. setTargetAttribute(Transform::ANIMATE_ROTATE);
  167. }
  168. }
  169. void AnimationChannel::convertToTransform()
  170. {
  171. if (_targetAttrib == Transform::ANIMATE_ROTATE_X ||
  172. _targetAttrib == Transform::ANIMATE_ROTATE_Y ||
  173. _targetAttrib == Transform::ANIMATE_ROTATE_Z)
  174. {
  175. std::vector<float> newKeyValues;
  176. newKeyValues.resize(_keyValues.size() * 10);
  177. const size_t count = _keyValues.size();
  178. float x = _targetAttrib == Transform::ANIMATE_ROTATE_X ? 1.0f : 0.0f;
  179. float y = _targetAttrib == Transform::ANIMATE_ROTATE_Y ? 1.0f : 0.0f;
  180. float z = _targetAttrib == Transform::ANIMATE_ROTATE_Z ? 1.0f : 0.0f;
  181. for (size_t i = 0; i < count; ++i)
  182. {
  183. size_t j = i << 2;
  184. newKeyValues[j+0] = 1.0f;
  185. newKeyValues[j+1] = 1.0f;
  186. newKeyValues[j+2] = 1.0f;
  187. newKeyValues[j+3] = x;
  188. newKeyValues[j+4] = y;
  189. newKeyValues[j+5] = z;
  190. newKeyValues[j+6] = _keyValues[i];
  191. newKeyValues[j+7] = 0.0f;
  192. newKeyValues[j+8] = 0.0f;
  193. newKeyValues[j+9] = 0.0f;
  194. }
  195. setKeyValues(newKeyValues);
  196. setTargetAttribute(Transform::ANIMATE_SCALE_ROTATE_TRANSLATE);
  197. }
  198. }
  199. unsigned int AnimationChannel::getInterpolationType(const char* str)
  200. {
  201. unsigned int value = 0;
  202. switch (str[0])
  203. {
  204. case 'L':
  205. if (strcmp(str, "LINEAR") == 0)
  206. {
  207. value = AnimationChannel::LINEAR;
  208. }
  209. break;
  210. case 'B':
  211. if (strcmp(str, "BEZIER") == 0)
  212. {
  213. value = AnimationChannel::BEZIER;
  214. }
  215. else if (strcmp(str, "BSPLINE") == 0)
  216. {
  217. value = AnimationChannel::BSPLINE;
  218. }
  219. break;
  220. case 'C':
  221. if (strcmp(str, "CARDINAL") == 0)
  222. {
  223. value = AnimationChannel::CARDINAL;
  224. }
  225. break;
  226. case 'H':
  227. if (strcmp(str, "HERMITE") == 0)
  228. {
  229. value = AnimationChannel::HERMITE;
  230. }
  231. break;
  232. case 'S':
  233. if (strcmp(str, "STEP") == 0)
  234. {
  235. value = AnimationChannel::STEP;
  236. }
  237. break;
  238. default:
  239. break;
  240. }
  241. return value;
  242. }
  243. void AnimationChannel::deleteRange(size_t begin, size_t end, size_t propSize)
  244. {
  245. assert(end > begin);
  246. // delete range
  247. printf("delete %lu to %lu\n", begin, end - 1);
  248. std::vector<float>::iterator a = _keyValues.begin() + begin * propSize;
  249. std::vector<float>::iterator b = _keyValues.begin() + end * propSize;
  250. _keyValues.erase(a, b);
  251. a = _keytimes.begin() + begin;
  252. b = _keytimes.begin() + end;
  253. _keytimes.erase(a, b);
  254. if (_interpolations.size() > 1)
  255. {
  256. std::vector<unsigned int>::iterator a = _interpolations.begin() + begin;
  257. std::vector<unsigned int>::iterator b = _interpolations.begin() + end * propSize;
  258. _interpolations.erase(a, b);
  259. }
  260. // TODO: also remove key frames from _tangentsIn and _tangentsOut once other curve types are supported.
  261. }
  262. }