💿🐜 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.

187 lines
5.5 KiB

  1. /*
  2. * Copyright (C) 2021 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. namespace geom {
  23. struct edge_hasher
  24. {
  25. std::size_t operator()(const std::array<std::size_t, 2>& v) const noexcept
  26. {
  27. std::size_t hash = std::hash<std::size_t>()(v[0]);
  28. return hash ^ (std::hash<std::size_t>()(v[1]) + 0x9e3779b9 + (hash << 6) + (hash >> 2));
  29. }
  30. };
  31. void create_triangle_mesh(mesh& mesh, const std::vector<float3>& vertices, const std::vector<std::array<std::uint_fast32_t, 3>>& triangles)
  32. {
  33. for (const auto& vertex: vertices)
  34. mesh.add_vertex(vertex);
  35. std::unordered_map<std::array<std::size_t, 2>, geom::mesh::edge*, edge_hasher> edge_map;
  36. const std::vector<mesh::vertex*>& mesh_vertices = mesh.get_vertices();
  37. std::vector<geom::mesh::edge*> loop(3);
  38. for (const auto& triangle: triangles)
  39. {
  40. geom::mesh::vertex* triangle_vertices[3] =
  41. {
  42. mesh_vertices[triangle[0]],
  43. mesh_vertices[triangle[1]],
  44. mesh_vertices[triangle[2]]
  45. };
  46. for (int j = 0; j < 3; ++j)
  47. {
  48. geom::mesh::vertex* start = triangle_vertices[j];
  49. geom::mesh::vertex* end = triangle_vertices[(j + 1) % 3];
  50. if (auto it = edge_map.find({start->index, end->index}); it != edge_map.end())
  51. {
  52. loop[j] = it->second;
  53. }
  54. else
  55. {
  56. loop[j] = mesh.add_edge(start, end);
  57. edge_map[{start->index, end->index}] = loop[j];
  58. edge_map[{end->index, start->index}] = loop[j]->symmetric;
  59. }
  60. }
  61. mesh.add_face(loop);
  62. }
  63. }
  64. void calculate_face_normals(float3* normals, const mesh& mesh)
  65. {
  66. const std::vector<mesh::face*>& faces = mesh.get_faces();
  67. for (std::size_t i = 0; i < faces.size(); ++i)
  68. {
  69. const mesh::face& face = *(faces[i]);
  70. const float3& a = face.edge->vertex->position;
  71. const float3& b = face.edge->next->vertex->position;
  72. const float3& c = face.edge->previous->vertex->position;
  73. normals[i] = math::normalize(math::cross(b - a, c - a));
  74. }
  75. }
  76. float3 calculate_face_normal(const mesh::face& face)
  77. {
  78. const float3& a = face.edge->vertex->position;
  79. const float3& b = face.edge->next->vertex->position;
  80. const float3& c = face.edge->previous->vertex->position;
  81. return math::normalize(math::cross(b - a, c - a));
  82. }
  83. void calculate_vertex_tangents(float4* tangents, const float2* texcoords, const float3* normals, const mesh& mesh)
  84. {
  85. const std::vector<mesh::face*>& faces = mesh.get_faces();
  86. const std::vector<mesh::vertex*>& vertices = mesh.get_vertices();
  87. // Allocate tangent and bitangent buffers
  88. float3* tangent_buffer = new float3[vertices.size()];
  89. float3* bitangent_buffer = new float3[vertices.size()];
  90. for (std::size_t i = 0; i < vertices.size(); ++i)
  91. {
  92. tangent_buffer[i] = {0.0f, 0.0f, 0.0f};
  93. bitangent_buffer[i] = {0.0f, 0.0f, 0.0f};
  94. }
  95. // Accumulate tangents and bitangents
  96. for (std::size_t i = 0; i < faces.size(); ++i)
  97. {
  98. const mesh::face& face = *(faces[i]);
  99. std::size_t ia = face.edge->vertex->index;
  100. std::size_t ib = face.edge->next->vertex->index;
  101. std::size_t ic = face.edge->previous->vertex->index;
  102. const float3& a = vertices[ia]->position;
  103. const float3& b = vertices[ib]->position;
  104. const float3& c = vertices[ic]->position;
  105. const float2& uva = texcoords[ia];
  106. const float2& uvb = texcoords[ib];
  107. const float2& uvc = texcoords[ic];
  108. float3 ba = b - a;
  109. float3 ca = c - a;
  110. float2 uvba = uvb - uva;
  111. float2 uvca = uvc - uva;
  112. float f = 1.0f / (uvba.x * uvca.y - uvca.x * uvba.y);
  113. float3 tangent = (ba * uvca.y - ca * uvba.y) * f;
  114. float3 bitangent = (ba * -uvca.x + ca * uvba.x) * f;
  115. tangent_buffer[ia] += tangent;
  116. tangent_buffer[ib] += tangent;
  117. tangent_buffer[ic] += tangent;
  118. bitangent_buffer[ia] += bitangent;
  119. bitangent_buffer[ib] += bitangent;
  120. bitangent_buffer[ic] += bitangent;
  121. }
  122. // Orthogonalize tangents
  123. for (std::size_t i = 0; i < vertices.size(); ++i)
  124. {
  125. const float3& n = normals[i];
  126. const float3& t = tangent_buffer[i];
  127. const float3& b = bitangent_buffer[i];
  128. // Gram-Schmidt orthogonalize tangent
  129. float3 tangent = math::normalize(t - n * math::dot(n, t));
  130. // Calculate bitangent sign
  131. float bitangent_sign = (math::dot(math::cross(n, t), b) < 0.0f) ? -1.0f : 1.0f;
  132. tangents[i] = {tangent.x, tangent.y, tangent.z, bitangent_sign};
  133. }
  134. // Free faceted tangents and bitangents
  135. delete[] tangent_buffer;
  136. delete[] bitangent_buffer;
  137. }
  138. aabb<float> calculate_bounds(const mesh& mesh)
  139. {
  140. float3 bounds_min;
  141. float3 bounds_max;
  142. for (int i = 0; i < 3; ++i)
  143. {
  144. bounds_min[i] = std::numeric_limits<float>::infinity();
  145. bounds_max[i] = -std::numeric_limits<float>::infinity();
  146. }
  147. for (const mesh::vertex* vertex: mesh.get_vertices())
  148. {
  149. const auto& position = vertex->position;
  150. for (int i = 0; i < 3; ++i)
  151. {
  152. bounds_min[i] = std::min<float>(bounds_min[i], position[i]);
  153. bounds_max[i] = std::max<float>(bounds_max[i], position[i]);
  154. }
  155. }
  156. return aabb<float>{bounds_min, bounds_max};
  157. }
  158. } // namespace geom