AnimationChannel.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. fprintfElement(file, "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. if (_targetAttrib == Transform::ANIMATE_SCALE_ROTATE_TRANSLATE)
  112. {
  113. size_t prevIndex = 0;
  114. std::vector<float>::iterator prevStart = _keyValues.begin();
  115. std::vector<float>::iterator prevEnd = _keyValues.begin() + 9;
  116. size_t i = 1;
  117. for (i = 1; i < _keytimes.size(); ++i)
  118. {
  119. std::vector<float>::iterator start = _keyValues.begin() + i * 10;
  120. std::vector<float>::iterator end = _keyValues.begin() + (i * 10 + 9);
  121. if (!equal(prevStart, prevEnd, start))
  122. {
  123. if (i - prevIndex > 2)
  124. {
  125. deleteRange(prevIndex+1, i);
  126. i = prevIndex;
  127. prevStart = _keyValues.begin() + i * 10;
  128. prevEnd = _keyValues.begin() + (i * 10 + 9);
  129. }
  130. else
  131. {
  132. prevStart = start;
  133. prevEnd = end;
  134. prevIndex = i;
  135. }
  136. }
  137. }
  138. if (i - 1 - prevIndex >= 2)
  139. {
  140. deleteRange(prevIndex+1, i);
  141. }
  142. }
  143. }
  144. void AnimationChannel::convertToQuaternion()
  145. {
  146. if (_targetAttrib == Transform::ANIMATE_ROTATE_X ||
  147. _targetAttrib == Transform::ANIMATE_ROTATE_Y ||
  148. _targetAttrib == Transform::ANIMATE_ROTATE_Z)
  149. {
  150. std::vector<float> newKeyValues;
  151. newKeyValues.resize(_keyValues.size() * 4);
  152. const size_t count = _keyValues.size();
  153. float x = _targetAttrib == Transform::ANIMATE_ROTATE_X ? 1.0f : 0.0f;
  154. float y = _targetAttrib == Transform::ANIMATE_ROTATE_Y ? 1.0f : 0.0f;
  155. float z = _targetAttrib == Transform::ANIMATE_ROTATE_Z ? 1.0f : 0.0f;
  156. for (size_t i = 0; i < count; ++i)
  157. {
  158. size_t j = i << 2;
  159. newKeyValues[j] = x;
  160. newKeyValues[j+1] = y;
  161. newKeyValues[j+2] = z;
  162. newKeyValues[j+3] = _keyValues[i];
  163. }
  164. setKeyValues(newKeyValues);
  165. setTargetAttribute(Transform::ANIMATE_ROTATE);
  166. }
  167. }
  168. void AnimationChannel::convertToTransform()
  169. {
  170. if (_targetAttrib == Transform::ANIMATE_ROTATE_X ||
  171. _targetAttrib == Transform::ANIMATE_ROTATE_Y ||
  172. _targetAttrib == Transform::ANIMATE_ROTATE_Z)
  173. {
  174. std::vector<float> newKeyValues;
  175. newKeyValues.resize(_keyValues.size() * 10);
  176. const size_t count = _keyValues.size();
  177. float x = _targetAttrib == Transform::ANIMATE_ROTATE_X ? 1.0f : 0.0f;
  178. float y = _targetAttrib == Transform::ANIMATE_ROTATE_Y ? 1.0f : 0.0f;
  179. float z = _targetAttrib == Transform::ANIMATE_ROTATE_Z ? 1.0f : 0.0f;
  180. for (size_t i = 0; i < count; ++i)
  181. {
  182. size_t j = i << 2;
  183. newKeyValues[j+0] = 1.0f;
  184. newKeyValues[j+1] = 1.0f;
  185. newKeyValues[j+2] = 1.0f;
  186. newKeyValues[j+3] = x;
  187. newKeyValues[j+4] = y;
  188. newKeyValues[j+5] = z;
  189. newKeyValues[j+6] = _keyValues[i];
  190. newKeyValues[j+7] = 0.0f;
  191. newKeyValues[j+8] = 0.0f;
  192. newKeyValues[j+9] = 0.0f;
  193. }
  194. setKeyValues(newKeyValues);
  195. setTargetAttribute(Transform::ANIMATE_SCALE_ROTATE_TRANSLATE);
  196. }
  197. }
  198. unsigned int AnimationChannel::getInterpolationType(const char* str)
  199. {
  200. unsigned int value = 0;
  201. switch (str[0])
  202. {
  203. case 'L':
  204. if (strcmp(str, "LINEAR") == 0)
  205. {
  206. value = AnimationChannel::LINEAR;
  207. }
  208. break;
  209. case 'B':
  210. if (strcmp(str, "BEZIER") == 0)
  211. {
  212. value = AnimationChannel::BEZIER;
  213. }
  214. else if (strcmp(str, "BSPLINE") == 0)
  215. {
  216. value = AnimationChannel::BSPLINE;
  217. }
  218. break;
  219. case 'C':
  220. if (strcmp(str, "CARDINAL") == 0)
  221. {
  222. value = AnimationChannel::CARDINAL;
  223. }
  224. break;
  225. case 'H':
  226. if (strcmp(str, "HERMITE") == 0)
  227. {
  228. value = AnimationChannel::HERMITE;
  229. }
  230. break;
  231. case 'S':
  232. if (strcmp(str, "STEP") == 0)
  233. {
  234. value = AnimationChannel::STEP;
  235. }
  236. break;
  237. default:
  238. break;
  239. }
  240. return value;
  241. }
  242. void AnimationChannel::deleteRange(size_t begin, size_t end)
  243. {
  244. // delete range
  245. printf("delete %lu to %lu\n", begin, end - 1);
  246. std::vector<float>::iterator a = _keyValues.begin() + begin * 10;
  247. std::vector<float>::iterator b = _keyValues.begin() + end * 10;
  248. _keyValues.erase(a, b);
  249. a = _keytimes.begin() + begin;
  250. b = _keytimes.begin() + end;
  251. _keytimes.erase(a, b);
  252. if (_interpolations.size() > 1)
  253. {
  254. std::vector<unsigned int>::iterator a = _interpolations.begin() + begin;
  255. std::vector<unsigned int>::iterator b = _interpolations.begin() + end * 10;
  256. _interpolations.erase(a, b);
  257. }
  258. }
  259. }