Tangent.cpp 4.6 KB

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