VertexAttachment.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. #include <spine/VertexAttachment.h>
  31. #include <spine/extension.h>
  32. void _spVertexAttachment_deinit (spVertexAttachment* attachment) {
  33. _spAttachment_deinit(SUPER(attachment));
  34. FREE(attachment->bones);
  35. FREE(attachment->vertices);
  36. }
  37. void spVertexAttachment_computeWorldVertices (spVertexAttachment* self, spSlot* slot, float* worldVertices) {
  38. spVertexAttachment_computeWorldVertices1(self, 0, self->worldVerticesLength, slot, worldVertices, 0);
  39. }
  40. void spVertexAttachment_computeWorldVertices1 (spVertexAttachment* self, int start, int count, spSlot* slot, float* worldVertices, int offset) {
  41. spSkeleton* skeleton;
  42. int deformLength;
  43. float* deform;
  44. float* vertices;
  45. int* bones;
  46. count += offset;
  47. skeleton = slot->bone->skeleton;
  48. deformLength = slot->attachmentVerticesCount;
  49. deform = slot->attachmentVertices;
  50. vertices = self->vertices;
  51. bones = self->bones;
  52. if (!bones) {
  53. spBone* bone;
  54. int v, w;
  55. float x, y;
  56. if (deformLength > 0) vertices = deform;
  57. bone = slot->bone;
  58. x = bone->worldX;
  59. y = bone->worldY;
  60. for (v = start, w = offset; w < count; v += 2, w += 2) {
  61. float vx = vertices[v], vy = vertices[v + 1];
  62. worldVertices[w] = vx * bone->a + vy * bone->b + x;
  63. worldVertices[w + 1] = vx * bone->c + vy * bone->d + y;
  64. }
  65. } else {
  66. int v = 0, skip = 0, i;
  67. spBone** skeletonBones;
  68. for (i = 0; i < start; i += 2) {
  69. int n = bones[v];
  70. v += n + 1;
  71. skip += n;
  72. }
  73. skeletonBones = skeleton->bones;
  74. if (deformLength == 0) {
  75. int w, b;
  76. for (w = offset, b = skip * 3; w < count; w += 2) {
  77. float wx = 0, wy = 0;
  78. int n = bones[v++];
  79. n += v;
  80. for (; v < n; v++, b += 3) {
  81. spBone* bone = skeletonBones[bones[v]];
  82. float vx = vertices[b], vy = vertices[b + 1], weight = vertices[b + 2];
  83. wx += (vx * bone->a + vy * bone->b + bone->worldX) * weight;
  84. wy += (vx * bone->c + vy * bone->d + bone->worldY) * weight;
  85. }
  86. worldVertices[w] = wx;
  87. worldVertices[w + 1] = wy;
  88. }
  89. } else {
  90. int w, b, f;
  91. for (w = offset, b = skip * 3, f = skip << 1; w < count; w += 2) {
  92. float wx = 0, wy = 0;
  93. int n = bones[v++];
  94. n += v;
  95. for (; v < n; v++, b += 3, f += 2) {
  96. spBone* bone = skeletonBones[bones[v]];
  97. float vx = vertices[b] + deform[f], vy = vertices[b + 1] + deform[f + 1], weight = vertices[b + 2];
  98. wx += (vx * bone->a + vy * bone->b + bone->worldX) * weight;
  99. wy += (vx * bone->c + vy * bone->d + bone->worldY) * weight;
  100. }
  101. worldVertices[w] = wx;
  102. worldVertices[w + 1] = wy;
  103. }
  104. }
  105. }
  106. }