PolySkeleton.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolySkeleton.h"
  20. #include "PolyBezierCurve.h"
  21. #include "PolyBone.h"
  22. #include "PolyLabel.h"
  23. #include "PolySceneLabel.h"
  24. #include "PolySceneLine.h"
  25. #include "PolyTween.h"
  26. #include "PolyTweenManager.h"
  27. #include "OSBasics.h"
  28. using namespace Polycode;
  29. Skeleton *Skeleton::BlankSkeleton() {
  30. return new Skeleton();
  31. }
  32. Skeleton::Skeleton(const String& fileName) : Entity() {
  33. baseAnimation = NULL;
  34. loadSkeleton(fileName);
  35. }
  36. Skeleton::Skeleton() {
  37. }
  38. Skeleton::~Skeleton() {
  39. }
  40. int Skeleton::getNumBones() const {
  41. return bones.size();
  42. }
  43. Bone *Skeleton::getBoneByName(const String& name) const {
  44. for(int i=0; i < bones.size(); i++) {
  45. if(bones[i]->getName() == name)
  46. return bones[i];
  47. }
  48. return NULL;
  49. }
  50. Bone *Skeleton::getBone(int index) const {
  51. return bones[index];
  52. }
  53. void Skeleton::setBaseAnimationByName(const String &animName) {
  54. SkeletonAnimation *anim = getAnimation(animName);
  55. if(anim) {
  56. setBaseAnimation(anim);
  57. }
  58. }
  59. void Skeleton::setBaseAnimation(SkeletonAnimation *animation){
  60. baseAnimation = animation;
  61. baseAnimation->setWeight(1.0);
  62. animation->Play(false);
  63. }
  64. void Skeleton::playAnimationByName(const String& animName, Number weight, bool once, bool restartIfPlaying) {
  65. SkeletonAnimation *anim = getAnimation(animName);
  66. if(anim) {
  67. playAnimation(anim, weight, once, restartIfPlaying);
  68. }
  69. }
  70. void Skeleton::playAnimation(SkeletonAnimation *animation, Number weight, bool once, bool restartIfPlaying) {
  71. if(weight > 1.0) {
  72. weight = 1.0;
  73. }
  74. if(weight < 0.0) {
  75. weight = 0.0;
  76. }
  77. animation->setWeight(weight);
  78. if(animation->isPlaying()) {
  79. if(restartIfPlaying) {
  80. animation->Reset();
  81. }
  82. return;
  83. }
  84. animation->Reset();
  85. playingAnimations.push_back(animation);
  86. animation->Play(once);
  87. }
  88. SkeletonAnimation *Skeleton::getAnimation(const String& name) const {
  89. for(int i=0; i < animations.size(); i++) {
  90. if(animations[i]->getName() == name)
  91. return animations[i];
  92. }
  93. return NULL;
  94. }
  95. void Skeleton::Update() {
  96. for(int i=0; i < bones.size(); i++) {
  97. bones[i]->setRotationByQuaternion(bones[i]->baseRotation);
  98. bones[i]->setPosition(bones[i]->basePosition);
  99. bones[i]->setScale(bones[i]->baseScale);
  100. }
  101. if(baseAnimation) {
  102. baseAnimation->Update();
  103. }
  104. for(int i=0; i < playingAnimations.size(); i++) {
  105. playingAnimations[i]->Update();
  106. }
  107. for(int i=0; i < bones.size(); i++) {
  108. bones[i]->rebuildTransformMatrix();
  109. bones[i]->setBoneMatrix(bones[i]->getTransformMatrix());
  110. }
  111. }
  112. void Skeleton::loadSkeleton(const String& fileName) {
  113. OSFILE *inFile = OSBasics::open(fileName.c_str(), "rb");
  114. if(!inFile) {
  115. return;
  116. }
  117. bonesEntity = new Entity();
  118. bonesEntity->visible = false;
  119. addChild(bonesEntity);
  120. unsigned int numBones;
  121. float t[3],rq[4],s[3];
  122. OSBasics::read(&numBones, sizeof(unsigned int), 1, inFile);
  123. unsigned int namelen;
  124. char buffer[1024];
  125. Matrix4 mat;
  126. unsigned int hasParent, boneID;
  127. for(int i=0; i < numBones; i++) {
  128. OSBasics::read(&namelen, sizeof(unsigned int), 1, inFile);
  129. memset(buffer, 0, 1024);
  130. OSBasics::read(buffer, 1, namelen, inFile);
  131. Bone *newBone = new Bone(String(buffer));
  132. OSBasics::read(&hasParent, sizeof(unsigned int), 1, inFile);
  133. if(hasParent == 1) {
  134. OSBasics::read(&boneID, sizeof(unsigned int), 1, inFile);
  135. newBone->parentBoneId = boneID;
  136. } else {
  137. newBone->parentBoneId = -1;
  138. }
  139. OSBasics::read(t, sizeof(float), 3, inFile);
  140. OSBasics::read(s, sizeof(float), 3, inFile);
  141. OSBasics::read(rq, sizeof(float), 4, inFile);
  142. bones.push_back(newBone);
  143. Quaternion bq;
  144. bq.set(rq[0], rq[1], rq[2], rq[3]);
  145. newBone->baseRotation = bq;
  146. newBone->baseScale = Vector3(s[0], s[1], s[2]);
  147. newBone->basePosition = Vector3(t[0], t[1], t[2]);
  148. newBone->setPosition(t[0], t[1], t[2]);
  149. newBone->setRotationQuat(rq[0], rq[1], rq[2], rq[3]);
  150. newBone->setScale(s[0], s[1], s[2]);
  151. newBone->rebuildTransformMatrix();
  152. newBone->setBaseMatrix(newBone->getTransformMatrix());
  153. newBone->setBoneMatrix(newBone->getTransformMatrix());
  154. OSBasics::read(t, sizeof(float), 3, inFile);
  155. OSBasics::read(s, sizeof(float), 3, inFile);
  156. OSBasics::read(rq, sizeof(float), 4, inFile);
  157. Quaternion q;
  158. q.set(rq[0], rq[1], rq[2], rq[3]);
  159. Matrix4 m = q.createMatrix();
  160. m.setPosition(t[0], t[1], t[2]);
  161. newBone->setRestMatrix(m);
  162. }
  163. Bone *parentBone;
  164. for(int i=0; i < bones.size(); i++) {
  165. if(bones[i]->parentBoneId != -1) {
  166. parentBone = bones[bones[i]->parentBoneId];
  167. parentBone->addChildBone(bones[i]);
  168. bones[i]->setParentBone(parentBone);
  169. parentBone->addChild(bones[i]);
  170. } else {
  171. bonesEntity->addChild(bones[i]);
  172. }
  173. }
  174. OSBasics::close(inFile);
  175. }
  176. SkeletonAnimation *Skeleton::getBaseAnimation() {
  177. return baseAnimation;
  178. }
  179. void Skeleton::stopAllAnimations() {
  180. for(int i=0; i < playingAnimations.size(); i++) {
  181. playingAnimations[i]->Stop();
  182. }
  183. playingAnimations.clear();
  184. }
  185. void Skeleton::stopAnimationByName(const String &name) {
  186. SkeletonAnimation *anim = getAnimation(name);
  187. if(anim) {
  188. stopAnimation(anim);
  189. }
  190. }
  191. void Skeleton::stopAnimation(SkeletonAnimation *animation) {
  192. for(int i=0; i < playingAnimations.size(); i++) {
  193. if(playingAnimations[i] == animation) {
  194. playingAnimations[i]->Stop();
  195. playingAnimations.erase(playingAnimations.begin()+i);
  196. return;
  197. }
  198. }
  199. }
  200. void Skeleton::addAnimation(const String& name, const String& fileName) {
  201. OSFILE *inFile = OSBasics::open(fileName.c_str(), "rb");
  202. if(!inFile) {
  203. return;
  204. }
  205. unsigned int activeBones,numPoints,numCurves, curveType;
  206. float length;
  207. OSBasics::read(&length, 1, sizeof(float), inFile);
  208. SkeletonAnimation *newAnimation = new SkeletonAnimation(name, length);
  209. OSBasics::read(&activeBones, sizeof(unsigned int), 1, inFile);
  210. unsigned short boneNameLen;
  211. char boneNameBuffer[1024];
  212. for(int j=0; j < activeBones; j++) {
  213. OSBasics::read(&boneNameLen, sizeof(unsigned short), 1, inFile);
  214. OSBasics::read(boneNameBuffer, 1, boneNameLen, inFile);
  215. boneNameBuffer[boneNameLen] = '\0';
  216. Bone *trackBone = getBoneByName(boneNameBuffer);
  217. if(!trackBone) {
  218. printf("WARNING, INVALID BONE NAME: %s\n", boneNameBuffer);
  219. continue;
  220. }
  221. BoneTrack *newTrack = new BoneTrack(trackBone, length);
  222. BezierCurve *curve;
  223. float vec1[2];
  224. OSBasics::read(&numCurves, sizeof(unsigned int), 1, inFile);
  225. for(int l=0; l < numCurves; l++) {
  226. curve = new BezierCurve();
  227. OSBasics::read(&curveType, sizeof(unsigned int), 1, inFile);
  228. OSBasics::read(&numPoints, sizeof(unsigned int), 1, inFile);
  229. for(int k=0; k < numPoints; k++) {
  230. OSBasics::read(vec1, sizeof(float), 2, inFile);
  231. curve->addControlPoint2d(vec1[1], vec1[0]);
  232. }
  233. switch(curveType) {
  234. case 0:
  235. newTrack->scaleX = curve;
  236. break;
  237. case 1:
  238. newTrack->scaleY = curve;
  239. break;
  240. case 2:
  241. newTrack->scaleZ = curve;
  242. break;
  243. case 3:
  244. newTrack->QuatW = curve;
  245. break;
  246. case 4:
  247. newTrack->QuatX = curve;
  248. break;
  249. case 5:
  250. newTrack->QuatY = curve;
  251. break;
  252. case 6:
  253. newTrack->QuatZ = curve;
  254. break;
  255. case 7:;
  256. newTrack->LocX = curve;
  257. break;
  258. case 8:
  259. newTrack->LocY = curve;
  260. break;
  261. case 9:
  262. newTrack->LocZ = curve;
  263. break;
  264. }
  265. }
  266. newAnimation->addBoneTrack(newTrack);
  267. }
  268. animations.push_back(newAnimation);
  269. OSBasics::close(inFile);
  270. }
  271. void Skeleton::bonesVisible(bool val) {
  272. bonesEntity->visible = val;
  273. }
  274. BoneTrack::BoneTrack(Bone *bone, Number length) {
  275. weight = 0.0;
  276. this->length = length;
  277. targetBone = bone;
  278. paused = false;
  279. time = 0.0;
  280. speed = 1.0;
  281. playOnce = false;
  282. scaleX = NULL;
  283. scaleY = NULL;
  284. scaleZ = NULL;
  285. QuatW = NULL;
  286. QuatX = NULL;
  287. QuatY = NULL;
  288. QuatZ = NULL;
  289. LocX = NULL;
  290. LocY = NULL;
  291. LocZ = NULL;
  292. quatCurve = NULL;
  293. }
  294. BoneTrack::~BoneTrack() {
  295. delete scaleX;
  296. delete scaleY;
  297. delete scaleZ;
  298. delete QuatW;
  299. delete QuatX;
  300. delete QuatY;
  301. delete QuatZ;
  302. delete LocX;
  303. delete LocY;
  304. delete LocZ;
  305. }
  306. void BoneTrack::Reset() {
  307. time = 0.0;
  308. }
  309. void BoneTrack::Stop() {
  310. paused = true;
  311. }
  312. void BoneTrack::Play(bool once) {
  313. paused = true;
  314. playOnce = once;
  315. }
  316. void BoneTrack::Update(Number elapsed) {
  317. if(!targetBone)
  318. return;
  319. // if(!paused) {
  320. time += elapsed * speed;
  321. // }
  322. if(time > length) {
  323. if(playOnce) {
  324. time = length;
  325. return;
  326. } else {
  327. time = time - length;
  328. }
  329. }
  330. if(LocX) {
  331. position.x = LocX->getYValueAtX(time);
  332. }
  333. if(LocY) {
  334. position.y = LocY->getYValueAtX(time);
  335. }
  336. if(LocZ) {
  337. position.z = LocZ->getYValueAtX(time);
  338. }
  339. if(scaleX) {
  340. scale.x = scaleX->getYValueAtX(time);
  341. }
  342. if(scaleY) {
  343. scale.y = scaleY->getYValueAtX(time);
  344. }
  345. if(scaleZ) {
  346. scale.z = scaleZ->getYValueAtX(time);
  347. }
  348. if(!quatCurve) {
  349. if(QuatW) {
  350. quatCurve = new QuaternionCurve(QuatW, QuatX, QuatY, QuatZ);
  351. }
  352. }
  353. if(quatCurve) {
  354. boneQuat = quatCurve->interpolate(time/length, true);
  355. }
  356. Quaternion rotationQuat = targetBone->getRotationQuat();
  357. rotationQuat = Quaternion::Slerp(weight, rotationQuat, boneQuat, true);
  358. targetBone->setRotationByQuaternion(rotationQuat);
  359. targetBone->setPosition((position * weight) + (targetBone->getPosition() * (1.0 - weight)));
  360. targetBone->setScale(scale);
  361. Vector3 newScale = ((scale - Vector3(1.0, 1.0, 1.0)) * weight) + Vector3(1.0, 1.0, 1.0);
  362. }
  363. void BoneTrack::setSpeed(Number speed) {
  364. this->speed = speed;
  365. }
  366. SkeletonAnimation::SkeletonAnimation(const String& name, Number duration) {
  367. this->name = name;
  368. this->duration = duration;
  369. this->weight = 1.0;
  370. this->playing = false;
  371. }
  372. bool SkeletonAnimation::isPlaying() const {
  373. return playing;
  374. }
  375. void SkeletonAnimation::setWeight(Number newWeight) {
  376. weight = newWeight;
  377. }
  378. Number SkeletonAnimation::getWeight() const {
  379. return weight;
  380. }
  381. void SkeletonAnimation::setSpeed(Number speed) {
  382. for(int i=0; i < boneTracks.size(); i++) {
  383. boneTracks[i]->setSpeed(speed);
  384. }
  385. }
  386. void SkeletonAnimation::Update() {
  387. Number elapsed = CoreServices::getInstance()->getCore()->getElapsed();
  388. for(int i=0; i < boneTracks.size(); i++) {
  389. boneTracks[i]->weight = weight;
  390. boneTracks[i]->Update(elapsed);
  391. }
  392. }
  393. void SkeletonAnimation::Reset() {
  394. for(int i=0; i < boneTracks.size(); i++) {
  395. boneTracks[i]->Reset();
  396. }
  397. }
  398. void SkeletonAnimation::Stop() {
  399. playing = false;
  400. for(int i=0; i < boneTracks.size(); i++) {
  401. boneTracks[i]->Stop();
  402. }
  403. }
  404. void SkeletonAnimation::Play(bool once) {
  405. playing = true;
  406. for(int i=0; i < boneTracks.size(); i++) {
  407. boneTracks[i]->Play(once);
  408. }
  409. }
  410. SkeletonAnimation::~SkeletonAnimation() {
  411. for(int i=0; i < boneTracks.size(); i++) {
  412. delete boneTracks[i];
  413. }
  414. }
  415. const String& SkeletonAnimation::getName() const {
  416. return name;
  417. }
  418. void SkeletonAnimation::addBoneTrack(BoneTrack *boneTrack) {
  419. boneTracks.push_back(boneTrack);
  420. }