2
0

Tangent.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Tangent.h"
  25. #include "Vector4.h"
  26. inline unsigned GetIndex(unsigned index, const void* indexData, unsigned indexSize)
  27. {
  28. if (indexSize == sizeof(unsigned short))
  29. return ((const unsigned short*)indexData)[index];
  30. else
  31. return ((const unsigned*)indexData)[index];
  32. }
  33. void GenerateTangents(void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart,
  34. unsigned indexCount, unsigned normalOffset, unsigned texCoordOffset, unsigned tangentOffset)
  35. {
  36. // Tangent generation from
  37. // http://www.terathon.com/code/tangent.html
  38. unsigned minVertex = M_MAX_UNSIGNED;
  39. unsigned maxVertex = 0;
  40. unsigned char* vertices = (unsigned char*)vertexData;
  41. for (unsigned i = indexStart; i < indexStart + indexCount; ++i)
  42. {
  43. unsigned v = GetIndex(i, indexData, indexSize);
  44. if (v < minVertex)
  45. minVertex = v;
  46. if (v > maxVertex)
  47. maxVertex = v;
  48. }
  49. unsigned vertexCount = maxVertex + 1;
  50. Vector3 *tan1 = new Vector3[vertexCount * 2];
  51. Vector3 *tan2 = tan1 + vertexCount;
  52. memset(tan1, 0, sizeof(Vector3) * vertexCount * 2);
  53. for (unsigned i = indexStart; i < indexStart + indexCount; i += 3)
  54. {
  55. unsigned i1 = GetIndex(i, indexData, indexSize);
  56. unsigned i2 = GetIndex(i + 1, indexData, indexSize);
  57. unsigned i3 = GetIndex(i + 2, indexData, indexSize);
  58. const Vector3& v1 = *((Vector3*)(vertices + i1 * vertexSize));
  59. const Vector3& v2 = *((Vector3*)(vertices + i2 * vertexSize));
  60. const Vector3& v3 = *((Vector3*)(vertices + i3 * vertexSize));
  61. const Vector2& w1 = *((Vector2*)(vertices + i1 * vertexSize + texCoordOffset));
  62. const Vector2& w2 = *((Vector2*)(vertices + i2 * vertexSize + texCoordOffset));
  63. const Vector2& w3 = *((Vector2*)(vertices + i3 * vertexSize + texCoordOffset));
  64. float x1 = v2.x_ - v1.x_;
  65. float x2 = v3.x_ - v1.x_;
  66. float y1 = v2.y_ - v1.y_;
  67. float y2 = v3.y_ - v1.y_;
  68. float z1 = v2.z_ - v1.z_;
  69. float z2 = v3.z_ - v1.z_;
  70. float s1 = w2.x_ - w1.x_;
  71. float s2 = w3.x_ - w1.x_;
  72. float t1 = w2.y_ - w1.y_;
  73. float t2 = w3.y_ - w1.y_;
  74. float r = 1.0f / (s1 * t2 - s2 * t1);
  75. Vector3 sdir((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r,
  76. (t2 * z1 - t1 * z2) * r);
  77. Vector3 tdir((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r,
  78. (s1 * z2 - s2 * z1) * r);
  79. tan1[i1] += sdir;
  80. tan1[i2] += sdir;
  81. tan1[i3] += sdir;
  82. tan2[i1] += tdir;
  83. tan2[i2] += tdir;
  84. tan2[i3] += tdir;
  85. }
  86. for (unsigned i = minVertex; i <= maxVertex; i++)
  87. {
  88. const Vector3& n = *((Vector3*)(vertices + i * vertexSize + normalOffset));
  89. const Vector3& t = tan1[i];
  90. Vector3 xyz;
  91. float w;
  92. // Gram-Schmidt orthogonalize
  93. xyz = (t - n * n.DotProduct(t)).Normalized();
  94. // Calculate handedness
  95. w = n.CrossProduct(t).DotProduct(tan2[i]) < 0.0f ? -1.0f : 1.0f;
  96. Vector4& tangent = *((Vector4*)(vertices + i * vertexSize + tangentOffset));
  97. tangent = Vector4(xyz, w);
  98. }
  99. delete[] tan1;
  100. }