Tangent.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. namespace Urho3D
  27. {
  28. inline unsigned GetIndex(unsigned index, const void* indexData, unsigned indexSize)
  29. {
  30. if (indexSize == sizeof(unsigned short))
  31. return ((const unsigned short*)indexData)[index];
  32. else
  33. return ((const unsigned*)indexData)[index];
  34. }
  35. void GenerateTangents(void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart,
  36. unsigned indexCount, unsigned normalOffset, unsigned texCoordOffset, unsigned tangentOffset)
  37. {
  38. // Tangent generation from
  39. // http://www.terathon.com/code/tangent.html
  40. unsigned minVertex = M_MAX_UNSIGNED;
  41. unsigned maxVertex = 0;
  42. unsigned char* vertices = (unsigned char*)vertexData;
  43. for (unsigned i = indexStart; i < indexStart + indexCount; ++i)
  44. {
  45. unsigned v = GetIndex(i, indexData, indexSize);
  46. if (v < minVertex)
  47. minVertex = v;
  48. if (v > maxVertex)
  49. maxVertex = v;
  50. }
  51. unsigned vertexCount = maxVertex + 1;
  52. Vector3 *tan1 = new Vector3[vertexCount * 2];
  53. Vector3 *tan2 = tan1 + vertexCount;
  54. memset(tan1, 0, sizeof(Vector3) * vertexCount * 2);
  55. for (unsigned i = indexStart; i < indexStart + indexCount; i += 3)
  56. {
  57. unsigned i1 = GetIndex(i, indexData, indexSize);
  58. unsigned i2 = GetIndex(i + 1, indexData, indexSize);
  59. unsigned i3 = GetIndex(i + 2, indexData, indexSize);
  60. const Vector3& v1 = *((Vector3*)(vertices + i1 * vertexSize));
  61. const Vector3& v2 = *((Vector3*)(vertices + i2 * vertexSize));
  62. const Vector3& v3 = *((Vector3*)(vertices + i3 * vertexSize));
  63. const Vector2& w1 = *((Vector2*)(vertices + i1 * vertexSize + texCoordOffset));
  64. const Vector2& w2 = *((Vector2*)(vertices + i2 * vertexSize + texCoordOffset));
  65. const Vector2& w3 = *((Vector2*)(vertices + i3 * vertexSize + texCoordOffset));
  66. float x1 = v2.x_ - v1.x_;
  67. float x2 = v3.x_ - v1.x_;
  68. float y1 = v2.y_ - v1.y_;
  69. float y2 = v3.y_ - v1.y_;
  70. float z1 = v2.z_ - v1.z_;
  71. float z2 = v3.z_ - v1.z_;
  72. float s1 = w2.x_ - w1.x_;
  73. float s2 = w3.x_ - w1.x_;
  74. float t1 = w2.y_ - w1.y_;
  75. float t2 = w3.y_ - w1.y_;
  76. float r = 1.0f / (s1 * t2 - s2 * t1);
  77. Vector3 sdir((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r,
  78. (t2 * z1 - t1 * z2) * r);
  79. Vector3 tdir((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r,
  80. (s1 * z2 - s2 * z1) * r);
  81. tan1[i1] += sdir;
  82. tan1[i2] += sdir;
  83. tan1[i3] += sdir;
  84. tan2[i1] += tdir;
  85. tan2[i2] += tdir;
  86. tan2[i3] += tdir;
  87. }
  88. for (unsigned i = minVertex; i <= maxVertex; i++)
  89. {
  90. const Vector3& n = *((Vector3*)(vertices + i * vertexSize + normalOffset));
  91. const Vector3& t = tan1[i];
  92. Vector3 xyz;
  93. float w;
  94. // Gram-Schmidt orthogonalize
  95. xyz = (t - n * n.DotProduct(t)).Normalized();
  96. // Calculate handedness
  97. w = n.CrossProduct(t).DotProduct(tan2[i]) < 0.0f ? -1.0f : 1.0f;
  98. Vector4& tangent = *((Vector4*)(vertices + i * vertexSize + tangentOffset));
  99. tangent = Vector4(xyz, w);
  100. }
  101. delete[] tan1;
  102. }
  103. }