2
0

MeshSkin.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * MeshSkin.cpp
  3. */
  4. #include "Base.h"
  5. #include "MeshSkin.h"
  6. #include "Joint.h"
  7. // The number of rows in each palette matrix.
  8. #define PALETTE_ROWS 3
  9. namespace gameplay
  10. {
  11. MeshSkin::MeshSkin() : _matrixPalette(NULL)
  12. {
  13. }
  14. MeshSkin::~MeshSkin()
  15. {
  16. clearJoints();
  17. SAFE_DELETE_ARRAY(_matrixPalette);
  18. }
  19. const Matrix& MeshSkin::getBindShape() const
  20. {
  21. return _bindShape;
  22. }
  23. void MeshSkin::setBindShape(const float* matrix)
  24. {
  25. _bindShape.set(matrix);
  26. }
  27. Joint* MeshSkin::getJoint(const char* id) const
  28. {
  29. assert(id);
  30. for (unsigned int i = 0, count = _joints.size(); i < count; ++i)
  31. {
  32. Joint* j = _joints[i];
  33. if (j && j->getId() != NULL && strcmp(j->getId(), id) == 0)
  34. {
  35. return j;
  36. }
  37. }
  38. return NULL;
  39. }
  40. void MeshSkin::setJointCount(unsigned int jointCount)
  41. {
  42. // Erase the joints vector and release all joints
  43. clearJoints();
  44. // Resize the joints vector and initialize to NULL
  45. _joints.resize(jointCount);
  46. for (unsigned int i = 0; i < jointCount; i++)
  47. {
  48. _joints[i] = NULL;
  49. }
  50. // Rebuild the matrix palette. Each matrix is 3 rows of Vector4.
  51. SAFE_DELETE_ARRAY(_matrixPalette);
  52. if (jointCount > 0)
  53. {
  54. _matrixPalette = new Vector4[jointCount * PALETTE_ROWS];
  55. for (unsigned int i = 0; i < jointCount * PALETTE_ROWS; i+=PALETTE_ROWS)
  56. {
  57. _matrixPalette[i+0].set(1.0f, 0.0f, 0.0f, 0.0f);
  58. _matrixPalette[i+1].set(0.0f, 1.0f, 0.0f, 0.0f);
  59. _matrixPalette[i+2].set(0.0f, 0.0f, 1.0f, 0.0f);
  60. }
  61. }
  62. }
  63. void MeshSkin::setJoint(Joint* joint, unsigned int index)
  64. {
  65. assert(index < _joints.size());
  66. _joints[index] = joint;
  67. }
  68. Vector4* MeshSkin::getMatrixPalette() const
  69. {
  70. unsigned int count = _joints.size();
  71. for (unsigned int i = 0; i < count; i++)
  72. {
  73. _joints[i]->updateJointMatrix(getBindShape(), &_matrixPalette[i * PALETTE_ROWS]);
  74. }
  75. return _matrixPalette;
  76. }
  77. unsigned int MeshSkin::getMatrixPaletteSize() const
  78. {
  79. return _joints.size() * PALETTE_ROWS;
  80. }
  81. Joint* MeshSkin::getJoint(unsigned int index) const
  82. {
  83. assert(index < _joints.size());
  84. return _joints[index];
  85. }
  86. void MeshSkin::clearJoints()
  87. {
  88. _joints.clear();
  89. }
  90. }