PolyBone.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. using namespace Polycode;
  21. Bone::Bone(String boneName) : SceneEntity() {
  22. this->boneName = boneName;
  23. // boneMesh = new ScenePrimitive(ScenePrimitive::TYPE_BOX, 0.1, 0.1, 0.1);
  24. this->depthTest = false;
  25. parentBone = NULL;
  26. boneMatrix.identity();
  27. // addChild(boneMesh);
  28. boneMesh = new Mesh(Mesh::QUAD_MESH);
  29. boneMesh->createBox(0.2,0.2,0.2);
  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() {
  51. if(parentBone)
  52. return boneMatrix * parentBone->getBoneMatrix();
  53. else {
  54. return boneMatrix;
  55. }
  56. }
  57. Matrix4 Bone::getFinalMatrix() {
  58. Matrix4 final = boneMatrix;
  59. if(parentBone) {
  60. final = final * parentBone->getFinalMatrix();
  61. }
  62. return final;
  63. }
  64. void Bone::setBoneMatrix(Matrix4 matrix) {
  65. boneMatrix = matrix;
  66. }
  67. void Bone::setBaseMatrix(Matrix4 matrix) {
  68. baseMatrix = matrix;
  69. }
  70. Matrix4 Bone::getRestMatrix() {
  71. return restMatrix;
  72. }
  73. Matrix4 Bone::getParentRestMatrix() {
  74. if(parentBone)
  75. return parentBone->getFullRestMatrix();
  76. else {
  77. Matrix4 ident;
  78. ident.identity();
  79. return ident;
  80. }
  81. }
  82. Matrix4 Bone::getFullBaseMatrix() {
  83. if(parentBone)
  84. return baseMatrix * parentBone->getFullBaseMatrix();
  85. else {
  86. return baseMatrix;
  87. }
  88. }
  89. Matrix4 Bone::getFullRestMatrix() {
  90. if(parentBone)
  91. return restMatrix * parentBone->getFullRestMatrix();
  92. else {
  93. return restMatrix;
  94. }
  95. }
  96. void Bone::setRestMatrix(Matrix4 matrix) {
  97. restMatrix = matrix;
  98. }
  99. String Bone::getName() {
  100. return boneName;
  101. }
  102. void Bone::enableBoneLabel(String fontLabel, Number size, Number scale, Color labelColor) {
  103. SceneLabel *label = new SceneLabel(fontLabel, boneName, size, scale, Label::ANTIALIAS_FULL);
  104. label->setColor(labelColor);
  105. label->billboardMode = true;
  106. label->depthTest = false;
  107. addEntity(label);
  108. }
  109. void Bone::Render() {
  110. CoreServices::getInstance()->getRenderer()->setTexture(NULL);
  111. // renderer->pushDataArrayForMesh(boneMesh, RenderDataArray::COLOR_DATA_ARRAY);
  112. renderer->pushDataArrayForMesh(boneMesh, RenderDataArray::VERTEX_DATA_ARRAY);
  113. renderer->pushDataArrayForMesh(boneMesh, RenderDataArray::TEXCOORD_DATA_ARRAY);
  114. renderer->pushDataArrayForMesh(boneMesh, RenderDataArray::NORMAL_DATA_ARRAY);
  115. renderer->drawArrays(boneMesh->getMeshType());
  116. }