💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.4 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "mesh-functions.hpp"
  20. #include "math/math.hpp"
  21. #include <unordered_map>
  22. struct edge_hasher
  23. {
  24. std::size_t operator()(const std::array<std::size_t, 2>& v) const noexcept
  25. {
  26. std::size_t hash = std::hash<std::size_t>()(v[0]);
  27. return hash ^ (std::hash<std::size_t>()(v[1]) + 0x9e3779b9 + (hash << 6) + (hash >> 2));
  28. }
  29. };
  30. void create_triangle_mesh(mesh& mesh, const std::vector<float3>& vertices, const std::vector<std::array<std::uint_fast32_t, 3>>& triangles)
  31. {
  32. for (const auto& vertex: vertices)
  33. mesh.add_vertex(vertex);
  34. std::unordered_map<std::array<std::size_t, 2>, ::mesh::edge*, edge_hasher> edge_map;
  35. const std::vector<mesh::vertex*>& mesh_vertices = mesh.get_vertices();
  36. std::vector<::mesh::edge*> loop(3);
  37. for (const auto& triangle: triangles)
  38. {
  39. ::mesh::vertex* triangle_vertices[3] =
  40. {
  41. mesh_vertices[triangle[0]],
  42. mesh_vertices[triangle[1]],
  43. mesh_vertices[triangle[2]]
  44. };
  45. for (int j = 0; j < 3; ++j)
  46. {
  47. ::mesh::vertex* start = triangle_vertices[j];
  48. ::mesh::vertex* end = triangle_vertices[(j + 1) % 3];
  49. if (auto it = edge_map.find({start->index, end->index}); it != edge_map.end())
  50. {
  51. loop[j] = it->second;
  52. }
  53. else
  54. {
  55. loop[j] = mesh.add_edge(start, end);
  56. edge_map[{start->index, end->index}] = loop[j];
  57. edge_map[{end->index, start->index}] = loop[j]->symmetric;
  58. }
  59. }
  60. mesh.add_face(loop);
  61. }
  62. }
  63. void calculate_face_normals(float3* normals, const mesh& mesh)
  64. {
  65. const std::vector<mesh::face*>& faces = mesh.get_faces();
  66. for (std::size_t i = 0; i < faces.size(); ++i)
  67. {
  68. const mesh::face& face = *(faces[i]);
  69. const float3& a = face.edge->vertex->position;
  70. const float3& b = face.edge->next->vertex->position;
  71. const float3& c = face.edge->previous->vertex->position;
  72. normals[i] = math::normalize(math::cross(b - a, c - a));
  73. }
  74. }
  75. float3 calculate_face_normal(const mesh::face& face)
  76. {
  77. const float3& a = face.edge->vertex->position;
  78. const float3& b = face.edge->next->vertex->position;
  79. const float3& c = face.edge->previous->vertex->position;
  80. return math::normalize(math::cross(b - a, c - a));
  81. }
  82. void calculate_vertex_tangents(float4* tangents, const float2* texcoords, const float3* normals, const mesh& mesh)
  83. {
  84. const std::vector<mesh::face*>& faces = mesh.get_faces();
  85. const std::vector<mesh::vertex*>& vertices = mesh.get_vertices();
  86. // Allocate tangent and bitangent buffers
  87. float3* tangent_buffer = new float3[vertices.size()];
  88. float3* bitangent_buffer = new float3[vertices.size()];
  89. for (std::size_t i = 0; i < vertices.size(); ++i)
  90. {
  91. tangent_buffer[i] = {0.0f, 0.0f, 0.0f};
  92. bitangent_buffer[i] = {0.0f, 0.0f, 0.0f};
  93. }
  94. // Accumulate tangents and bitangents
  95. for (std::size_t i = 0; i < faces.size(); ++i)
  96. {
  97. const mesh::face& face = *(faces[i]);
  98. std::size_t ia = face.edge->vertex->index;
  99. std::size_t ib = face.edge->next->vertex->index;
  100. std::size_t ic = face.edge->previous->vertex->index;
  101. const float3& a = vertices[ia]->position;
  102. const float3& b = vertices[ib]->position;
  103. const float3& c = vertices[ic]->position;
  104. const float2& uva = texcoords[ia];
  105. const float2& uvb = texcoords[ib];
  106. const float2& uvc = texcoords[ic];
  107. float3 ba = b - a;
  108. float3 ca = c - a;
  109. float2 uvba = uvb - uva;
  110. float2 uvca = uvc - uva;
  111. float f = 1.0f / (uvba.x * uvca.y - uvca.x * uvba.y);
  112. float3 tangent = (ba * uvca.y - ca * uvba.y) * f;
  113. float3 bitangent = (ba * -uvca.x + ca * uvba.x) * f;
  114. tangent_buffer[ia] += tangent;
  115. tangent_buffer[ib] += tangent;
  116. tangent_buffer[ic] += tangent;
  117. bitangent_buffer[ia] += bitangent;
  118. bitangent_buffer[ib] += bitangent;
  119. bitangent_buffer[ic] += bitangent;
  120. }
  121. // Orthogonalize tangents
  122. for (std::size_t i = 0; i < vertices.size(); ++i)
  123. {
  124. const float3& n = normals[i];
  125. const float3& t = tangent_buffer[i];
  126. const float3& b = bitangent_buffer[i];
  127. // Gram-Schmidt orthogonalize tangent
  128. float3 tangent = math::normalize(t - n * math::dot(n, t));
  129. // Calculate bitangent sign
  130. float bitangent_sign = (math::dot(math::cross(n, t), b) < 0.0f) ? -1.0f : 1.0f;
  131. tangents[i] = {tangent.x, tangent.y, tangent.z, bitangent_sign};
  132. }
  133. // Free faceted tangents and bitangents
  134. delete[] tangent_buffer;
  135. delete[] bitangent_buffer;
  136. }
  137. aabb<float> calculate_bounds(const mesh& mesh)
  138. {
  139. float3 bounds_min;
  140. float3 bounds_max;
  141. for (int i = 0; i < 3; ++i)
  142. {
  143. bounds_min[i] = std::numeric_limits<float>::infinity();
  144. bounds_max[i] = -std::numeric_limits<float>::infinity();
  145. }
  146. for (const mesh::vertex* vertex: mesh.get_vertices())
  147. {
  148. const auto& position = vertex->position;
  149. for (int i = 0; i < 3; ++i)
  150. {
  151. bounds_min[i] = std::min<float>(bounds_min[i], position[i]);
  152. bounds_max[i] = std::max<float>(bounds_max[i], position[i]);
  153. }
  154. }
  155. return aabb<float>{bounds_min, bounds_max};
  156. }