PolyBone.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "PolyBone.h"
  20. #include "PolyCoreServices.h"
  21. #include "PolyLabel.h"
  22. #include "PolyMesh.h"
  23. #include "PolyRenderer.h"
  24. #include "PolySceneLabel.h"
  25. using namespace Polycode;
  26. Bone::Bone(const String& boneName) : Entity() {
  27. this->boneName = boneName;
  28. parentBone = NULL;
  29. boneMatrix.identity();
  30. }
  31. Bone::~Bone() {
  32. }
  33. void Bone::setParentBone(Bone *bone) {
  34. parentBone = bone;
  35. }
  36. void Bone::addChildBone(Bone *bone) {
  37. childBones.push_back(bone);
  38. }
  39. Bone* Bone::getParentBone() {
  40. return parentBone;
  41. }
  42. int Bone::getNumChildBones() {
  43. return childBones.size();
  44. }
  45. Bone *Bone::getChildBone(unsigned int index) {
  46. if(index > childBones.size()-1)
  47. index = childBones.size()-1;
  48. return childBones[index];
  49. }
  50. Matrix4 Bone::getBoneMatrix() const {
  51. if(parentBone)
  52. return boneMatrix * parentBone->getBoneMatrix();
  53. else {
  54. return boneMatrix;
  55. }
  56. }
  57. Matrix4 Bone::getFinalMatrix() const {
  58. return finalMatrix;
  59. }
  60. Matrix4 Bone::buildFinalMatrix() const {
  61. if(parentBone) {
  62. return boneMatrix * parentBone->buildFinalMatrix();
  63. } else {
  64. return boneMatrix;
  65. }
  66. }
  67. void Bone::rebuildFinalMatrix() {
  68. finalMatrix = buildFinalMatrix();
  69. }
  70. void Bone::setBoneMatrix(const Matrix4& matrix) {
  71. boneMatrix = matrix;
  72. }
  73. void Bone::setBaseMatrix(const Matrix4& matrix) {
  74. baseMatrix = matrix;
  75. }
  76. Matrix4 Bone::getRestMatrix() const {
  77. return restMatrix;
  78. }
  79. Matrix4 Bone::getParentRestMatrix() const {
  80. if(parentBone)
  81. return parentBone->getFullRestMatrix();
  82. else {
  83. Matrix4 ident;
  84. ident.identity();
  85. return ident;
  86. }
  87. }
  88. Matrix4 Bone::getFullBaseMatrix() const {
  89. if(parentBone)
  90. return baseMatrix * parentBone->getFullBaseMatrix();
  91. else {
  92. return baseMatrix;
  93. }
  94. }
  95. Matrix4 Bone::getFullRestMatrix() const {
  96. if(parentBone)
  97. return restMatrix * parentBone->getFullRestMatrix();
  98. else {
  99. return restMatrix;
  100. }
  101. }
  102. void Bone::setRestMatrix(const Matrix4& matrix) {
  103. restMatrix = matrix;
  104. }
  105. const String& Bone::getName() const {
  106. return boneName;
  107. }