BsMeshUtility.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsMeshUtility.h"
  4. #include "BsVector3.h"
  5. #include "BsVector2.h"
  6. namespace BansheeEngine
  7. {
  8. struct VertexFaces
  9. {
  10. UINT32* faces;
  11. UINT32 numFaces = 0;
  12. };
  13. struct VertexConnectivity
  14. {
  15. VertexConnectivity(UINT8* indices, UINT32 numVertices, UINT32 numFaces, UINT32 indexSize)
  16. :vertexFaces(nullptr), mMaxFacesPerVertex(0), mNumVertices(numVertices), mFaces(nullptr)
  17. {
  18. vertexFaces = bs_newN<VertexFaces>(numVertices);
  19. resizeFaceArray(10);
  20. for (UINT32 i = 0; i < numFaces; i++)
  21. {
  22. for (UINT32 j = 0; j < 3; j++)
  23. {
  24. UINT32 idx = i * 3 + j;
  25. UINT32 vertexIdx = 0;
  26. memcpy(&vertexIdx, indices + idx * indexSize, indexSize);
  27. assert(vertexIdx < mNumVertices);
  28. VertexFaces& faces = vertexFaces[vertexIdx];
  29. if (faces.numFaces >= mMaxFacesPerVertex)
  30. resizeFaceArray(mMaxFacesPerVertex * 2);
  31. faces.faces[faces.numFaces] = i;
  32. faces.numFaces++;
  33. }
  34. }
  35. }
  36. ~VertexConnectivity()
  37. {
  38. if (vertexFaces != nullptr)
  39. bs_deleteN(vertexFaces, mNumVertices);
  40. if (mFaces != nullptr)
  41. bs_free(mFaces);
  42. }
  43. VertexFaces* vertexFaces;
  44. private:
  45. void resizeFaceArray(UINT32 numFaces)
  46. {
  47. UINT32* newFaces = (UINT32*)bs_alloc(numFaces * mNumVertices * sizeof(UINT32));
  48. if (mFaces != nullptr)
  49. {
  50. for (UINT32 i = 0; i < mNumVertices; i++)
  51. memcpy(newFaces + (i * numFaces), mFaces + (i * mMaxFacesPerVertex), mMaxFacesPerVertex * sizeof(UINT32));
  52. bs_free(mFaces);
  53. }
  54. for (UINT32 i = 0; i < mNumVertices; i++)
  55. vertexFaces[i].faces = newFaces + (i * numFaces);
  56. mFaces = newFaces;
  57. mMaxFacesPerVertex = numFaces;
  58. }
  59. UINT32 mMaxFacesPerVertex;
  60. UINT32 mNumVertices;
  61. UINT32* mFaces;
  62. };
  63. void MeshUtility::calculateNormals(Vector3* vertices, UINT8* indices, UINT32 numVertices,
  64. UINT32 numIndices, Vector3* normals, UINT32 indexSize)
  65. {
  66. UINT32 numFaces = numIndices / 3;
  67. Vector3* faceNormals = bs_newN<Vector3>(numFaces);
  68. for (UINT32 i = 0; i < numFaces; i++)
  69. {
  70. UINT32 triangle[3];
  71. memcpy(&triangle[0], indices + (i * 3 + 0) * indexSize, indexSize);
  72. memcpy(&triangle[1], indices + (i * 3 + 1) * indexSize, indexSize);
  73. memcpy(&triangle[2], indices + (i * 3 + 2) * indexSize, indexSize);
  74. Vector3 edgeA = vertices[triangle[1]] - vertices[triangle[0]];
  75. Vector3 edgeB = vertices[triangle[2]] - vertices[triangle[0]];
  76. faceNormals[i] = Vector3::normalize(Vector3::cross(edgeA, edgeB));
  77. // Note: Potentially don't normalize here in order to weigh the normals
  78. // by triangle size
  79. }
  80. VertexConnectivity connectivity(indices, numVertices, numFaces, indexSize);
  81. for (UINT32 i = 0; i < numVertices; i++)
  82. {
  83. VertexFaces& faces = connectivity.vertexFaces[i];
  84. for (UINT32 j = 0; j < faces.numFaces; j++)
  85. {
  86. UINT32 faceIdx = faces.faces[j];
  87. normals[i] += faceNormals[faceIdx];
  88. }
  89. normals[i].normalize();
  90. }
  91. bs_deleteN(faceNormals, numFaces);
  92. }
  93. void MeshUtility::calculateTangents(Vector3* vertices, Vector3* normals, Vector2* uv, UINT8* indices, UINT32 numVertices,
  94. UINT32 numIndices, Vector3* tangents, Vector3* bitangents, UINT32 indexSize)
  95. {
  96. UINT32 numFaces = numIndices / 3;
  97. Vector3* faceTangents = bs_newN<Vector3>(numFaces);
  98. Vector3* faceBitangents = bs_newN<Vector3>(numFaces);
  99. for (UINT32 i = 0; i < numFaces; i++)
  100. {
  101. UINT32 triangle[3];
  102. memcpy(&triangle[0], indices + (i * 3 + 0) * indexSize, indexSize);
  103. memcpy(&triangle[1], indices + (i * 3 + 1) * indexSize, indexSize);
  104. memcpy(&triangle[2], indices + (i * 3 + 2) * indexSize, indexSize);
  105. Vector3 p0 = vertices[triangle[0]];
  106. Vector3 p1 = vertices[triangle[1]];
  107. Vector3 p2 = vertices[triangle[2]];
  108. Vector2 uv0 = uv[triangle[0]];
  109. Vector2 uv1 = uv[triangle[1]];
  110. Vector2 uv2 = uv[triangle[2]];
  111. Vector3 q0 = p1 - p0;
  112. Vector3 q1 = p2 - p0;
  113. Vector2 s;
  114. s.x = uv1.x - uv0.x;
  115. s.y = uv2.x - uv0.x;
  116. Vector2 t;
  117. t.x = uv1.y - uv0.y;
  118. t.y = uv2.y - uv0.y;
  119. float denom = s.x*t.y - s.y * t.x;
  120. if (fabs(denom) >= 0e-8f)
  121. {
  122. float r = 1.0f / denom;
  123. s *= r;
  124. t *= r;
  125. faceTangents[i] = t.y * q0 - t.x * q1;
  126. faceBitangents[i] = s.x * q0 - s.y * q1;
  127. faceTangents[i].normalize();
  128. faceBitangents[i].normalize();
  129. }
  130. // Note: Potentially don't normalize here in order to weigh the normals
  131. // by triangle size
  132. }
  133. VertexConnectivity connectivity(indices, numVertices, numFaces, indexSize);
  134. for (UINT32 i = 0; i < numVertices; i++)
  135. {
  136. VertexFaces& faces = connectivity.vertexFaces[i];
  137. for (UINT32 j = 0; j < faces.numFaces; j++)
  138. {
  139. UINT32 faceIdx = faces.faces[j];
  140. tangents[i] += faceTangents[faceIdx];
  141. bitangents[i] += faceBitangents[faceIdx];
  142. }
  143. tangents[i].normalize();
  144. bitangents[i].normalize();
  145. // Orthonormalize
  146. float dot0 = normals[i].dot(tangents[i]);
  147. tangents[i] -= dot0*normals[i];
  148. tangents[i].normalize();
  149. float dot1 = tangents[i].dot(bitangents[i]);
  150. dot0 = normals[i].dot(bitangents[i]);
  151. bitangents[i] -= dot0*normals[i] + dot1*tangents[i];
  152. bitangents[i].normalize();
  153. }
  154. bs_deleteN(faceTangents, numFaces);
  155. bs_deleteN(faceBitangents, numFaces);
  156. // TODO - Consider weighing tangents by triangle size and/or edge angles
  157. }
  158. void MeshUtility::calculateTangentSpace(Vector3* vertices, Vector2* uv, UINT8* indices, UINT32 numVertices,
  159. UINT32 numIndices, Vector3* normals, Vector3* tangents, Vector3* bitangents, UINT32 indexSize)
  160. {
  161. calculateNormals(vertices, indices, numVertices, numIndices, normals, indexSize);
  162. calculateTangents(vertices, normals, uv, indices, numVertices, numIndices, tangents, bitangents, indexSize);
  163. }
  164. }