CpuAnimatedVertexBufferHelper.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2015-2016 Kastellanos Nikolaos
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "pch.h"
  15. #include <malloc.h>
  16. #include "CpuAnimatedVertexBufferHelper.h"
  17. #include <vector>
  18. #include <collection.h>
  19. //using namespace DirectX;
  20. using namespace Platform;
  21. using namespace Platform::Collections;
  22. using namespace nkast::Aether::Native::Animation;
  23. using namespace nkast::Aether::Native::Animation::Data;
  24. nkast::Aether::Native::Animation::CpuAnimatedVertexBufferHelper::CpuAnimatedVertexBufferHelper(void)
  25. {
  26. _cpuVertices = nullptr;
  27. _cpuVerticesLength = 0;
  28. }
  29. nkast::Aether::Native::Animation::CpuAnimatedVertexBufferHelper::~CpuAnimatedVertexBufferHelper(void)
  30. {
  31. delete _cpuVertices;
  32. _cpuVerticesLength = 0;
  33. }
  34. void nkast::Aether::Native::Animation::CpuAnimatedVertexBufferHelper::SetCpuVertices(const Platform::Array<VertexIndicesWeightsPositionNormal>^ cpuVertices)
  35. {
  36. _cpuVerticesLength = cpuVertices->Length;
  37. _cpuVertices = new VertexIndicesWeightsPositionNormal[_cpuVerticesLength = cpuVertices->Length];
  38. for (int i = 0;i<_cpuVerticesLength;i++)
  39. {
  40. _cpuVertices[i] = cpuVertices[i];
  41. }
  42. return;
  43. }
  44. void nkast::Aether::Native::Animation::CpuAnimatedVertexBufferHelper::UpdateVertices(
  45. int64 pBoneTransforms, int64 pGpuVertices, int startIndex, int elementCount)
  46. {
  47. MatrixData* pBone = (MatrixData*)pBoneTransforms;
  48. VertexPositionNormalTextureData* pGpuVertex = (VertexPositionNormalTextureData*)pGpuVertices;
  49. UpdateVertices(pBone, pGpuVertex, startIndex, elementCount);
  50. }
  51. void nkast::Aether::Native::Animation::CpuAnimatedVertexBufferHelper::UpdateVertices(
  52. MatrixData* pBoneTransforms, VertexPositionNormalTextureData* pGpuVertices, int startIndex, int elementCount)
  53. {
  54. VertexPositionNormalTextureData* vout = pGpuVertices;
  55. MatrixData transformSum;
  56. transformSum.M14 = 0;
  57. transformSum.M24 = 0;
  58. transformSum.M34 = 0;
  59. transformSum.M44 = 1;
  60. // skin all of the vertices
  61. int endIndex = (startIndex + elementCount - 1);
  62. for (int i = startIndex; i <= endIndex; i++)
  63. {
  64. int b0 = _cpuVertices[i].BlendIndices.X;
  65. int b1 = _cpuVertices[i].BlendIndices.Y;
  66. int b2 = _cpuVertices[i].BlendIndices.Z;
  67. int b3 = _cpuVertices[i].BlendIndices.W;
  68. MatrixData* m1 = &pBoneTransforms[b0];
  69. MatrixData* m2 = &pBoneTransforms[b1];
  70. MatrixData* m3 = &pBoneTransforms[b2];
  71. MatrixData* m4 = &pBoneTransforms[b3];
  72. float w1 = _cpuVertices[i].BlendWeights.X;
  73. float w2 = _cpuVertices[i].BlendWeights.Y;
  74. float w3 = _cpuVertices[i].BlendWeights.Z;
  75. float w4 = _cpuVertices[i].BlendWeights.W;
  76. transformSum.M11 = (m1->M11 * w1) + (m2->M11 * w2) + (m3->M11 * w3) + (m4->M11 * w4);
  77. transformSum.M12 = (m1->M12 * w1) + (m2->M12 * w2) + (m3->M12 * w3) + (m4->M12 * w4);
  78. transformSum.M13 = (m1->M13 * w1) + (m2->M13 * w2) + (m3->M13 * w3) + (m4->M13 * w4);
  79. transformSum.M21 = (m1->M21 * w1) + (m2->M21 * w2) + (m3->M21 * w3) + (m4->M21 * w4);
  80. transformSum.M22 = (m1->M22 * w1) + (m2->M22 * w2) + (m3->M22 * w3) + (m4->M22 * w4);
  81. transformSum.M23 = (m1->M23 * w1) + (m2->M23 * w2) + (m3->M23 * w3) + (m4->M23 * w4);
  82. transformSum.M31 = (m1->M31 * w1) + (m2->M31 * w2) + (m3->M31 * w3) + (m4->M31 * w4);
  83. transformSum.M32 = (m1->M32 * w1) + (m2->M32 * w2) + (m3->M32 * w3) + (m4->M32 * w4);
  84. transformSum.M33 = (m1->M33 * w1) + (m2->M33 * w2) + (m3->M33 * w3) + (m4->M33 * w4);
  85. transformSum.M41 = (m1->M41 * w1) + (m2->M41 * w2) + (m3->M41 * w3) + (m4->M41 * w4);
  86. transformSum.M42 = (m1->M42 * w1) + (m2->M42 * w2) + (m3->M42 * w3) + (m4->M42 * w4);
  87. transformSum.M43 = (m1->M43 * w1) + (m2->M43 * w2) + (m3->M43 * w3) + (m4->M43 * w4);
  88. // Support the 4 Bone Influences - Position then Normal
  89. Vector3Data position = _cpuVertices[i].Position;
  90. vout[i].Position.X = position.X * transformSum.M11 + position.Y * transformSum.M21 + position.Z * transformSum.M31 + transformSum.M41;
  91. vout[i].Position.Y = position.X * transformSum.M12 + position.Y * transformSum.M22 + position.Z * transformSum.M32 + transformSum.M42;
  92. vout[i].Position.Z = position.X * transformSum.M13 + position.Y * transformSum.M23 + position.Z * transformSum.M33 + transformSum.M43;
  93. Vector3Data normal = _cpuVertices[i].Normal;
  94. vout[i].Normal.X = normal.X * transformSum.M11 + normal.Y * transformSum.M21 + normal.Z * transformSum.M31;
  95. vout[i].Normal.Y = normal.X * transformSum.M12 + normal.Y * transformSum.M22 + normal.Z * transformSum.M32;
  96. vout[i].Normal.Z = normal.X * transformSum.M13 + normal.Y * transformSum.M23 + normal.Z * transformSum.M33;
  97. }
  98. }