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

302 lines
9.9 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 "terrain.hpp"
  20. #include "entity/components/model.hpp"
  21. #include "entity/components/collision.hpp"
  22. #include "entity/components/transform.hpp"
  23. #include "cart/relief-map.hpp"
  24. #include "renderer/model.hpp"
  25. #include "geom/mesh.hpp"
  26. #include "geom/mesh-functions.hpp"
  27. #include "renderer/vertex-attributes.hpp"
  28. #include "gl/vertex-attribute-type.hpp"
  29. #include "gl/drawing-mode.hpp"
  30. #include "gl/vertex-buffer.hpp"
  31. #include "resources/resource-manager.hpp"
  32. #include "resources/image.hpp"
  33. #include "utility/fundamental-types.hpp"
  34. #include <limits>
  35. namespace entity {
  36. namespace system {
  37. terrain::terrain(entity::registry& registry, ::resource_manager* resource_manager):
  38. updatable(registry),
  39. resource_manager(resource_manager)
  40. {
  41. registry.on_construct<component::terrain>().connect<&terrain::on_terrain_construct>(this);
  42. registry.on_destroy<component::terrain>().connect<&terrain::on_terrain_destroy>(this);
  43. heightmap = resource_manager->load<image>("grassland-heightmap.png");
  44. heightmap_size = 2000.0f;
  45. heightmap_scale = 150.0f;
  46. }
  47. terrain::~terrain()
  48. {}
  49. void terrain::update(double t, double dt)
  50. {
  51. registry.view<component::terrain, component::transform>().each(
  52. [this](entity::id entity_id, auto& terrain, auto& transform)
  53. {
  54. transform.local.translation = float3{(float)terrain.x * patch_size, 0.0f, (float)terrain.z * patch_size};
  55. transform.warp = true;
  56. });
  57. }
  58. void terrain::set_patch_size(float size)
  59. {
  60. patch_size = size;
  61. }
  62. geom::mesh* terrain::generate_terrain_mesh(float size, int subdivisions)
  63. {
  64. auto elevation = [](float u, float v) -> float
  65. {
  66. return 0.0f;
  67. };
  68. return cart::map_elevation(elevation, size, subdivisions);
  69. }
  70. model* terrain::generate_terrain_model(geom::mesh* terrain_mesh)
  71. {
  72. // Allocate model
  73. model* terrain_model = new model();
  74. // Get model's VAO and VBO
  75. gl::vertex_buffer* vbo = terrain_model->get_vertex_buffer();
  76. gl::vertex_array* vao = terrain_model->get_vertex_array();
  77. // Resize VBO
  78. int vertex_size = 3 + 2 + 3 + 4 + 3;
  79. int vertex_stride = vertex_size * sizeof(float);
  80. vbo->resize(terrain_mesh->get_faces().size() * 3 * vertex_stride, nullptr);
  81. // Bind vertex attributes
  82. std::size_t offset = 0;
  83. vao->bind_attribute(VERTEX_POSITION_LOCATION, *vbo, 3, gl::vertex_attribute_type::float_32, vertex_stride, 0);
  84. offset += 3;
  85. vao->bind_attribute(VERTEX_TEXCOORD_LOCATION, *vbo, 2, gl::vertex_attribute_type::float_32, vertex_stride, sizeof(float) * offset);
  86. offset += 2;
  87. vao->bind_attribute(VERTEX_NORMAL_LOCATION, *vbo, 3, gl::vertex_attribute_type::float_32, vertex_stride, sizeof(float) * offset);
  88. offset += 3;
  89. vao->bind_attribute(VERTEX_TANGENT_LOCATION, *vbo, 4, gl::vertex_attribute_type::float_32, vertex_stride, sizeof(float) * offset);
  90. offset += 4;
  91. vao->bind_attribute(VERTEX_BARYCENTRIC_LOCATION, *vbo, 3, gl::vertex_attribute_type::float_32, vertex_stride, sizeof(float) * offset);
  92. offset += 3;
  93. // Create model group
  94. model_group* model_group = terrain_model->add_group("terrain");
  95. model_group->set_material(resource_manager->load<material>("forest-terrain.mtl"));
  96. model_group->set_drawing_mode(gl::drawing_mode::triangles);
  97. model_group->set_start_index(0);
  98. model_group->set_index_count(terrain_mesh->get_faces().size() * 3);
  99. return terrain_model;
  100. }
  101. void terrain::project_terrain_mesh(geom::mesh* terrain_mesh, const component::terrain& component)
  102. {
  103. float offset_x = (float)component.x * patch_size;
  104. float offset_z = (float)component.z * patch_size;
  105. for (geom::mesh::vertex* vertex: terrain_mesh->get_vertices())
  106. {
  107. int pixel_x = (vertex->position[0] + offset_x + heightmap_size * 0.5f) / heightmap_size * (float)(heightmap->get_width() - 1);
  108. int pixel_y = (vertex->position[2] + offset_z + heightmap_size * 0.5f) / heightmap_size * (float)(heightmap->get_height() - 1);
  109. pixel_x = std::max<int>(0, std::min<int>(heightmap->get_width() - 1, pixel_x));
  110. pixel_y = std::max<int>(0, std::min<int>(heightmap->get_height() - 1, pixel_y));
  111. int pixel_index = (pixel_y * heightmap->get_width() + pixel_x) * heightmap->get_channels();
  112. const unsigned char* pixel = static_cast<const unsigned char*>(heightmap->get_pixels()) + pixel_index;
  113. float elevation = (static_cast<float>(*pixel) / 255.0f - 0.5) * heightmap_scale;
  114. vertex->position[1] = elevation;
  115. }
  116. }
  117. void terrain::update_terrain_model(model* terrain_model, geom::mesh* terrain_mesh)
  118. {
  119. const std::vector<geom::mesh::face*>& faces = terrain_mesh->get_faces();
  120. const std::vector<geom::mesh::vertex*>& vertices = terrain_mesh->get_vertices();
  121. geom::aabb<float> bounds = calculate_bounds(*terrain_mesh);
  122. float bounds_width = bounds.max_point.x - bounds.min_point.x;
  123. float bounds_height = bounds.max_point.y - bounds.min_point.y;
  124. float bounds_depth = bounds.max_point.z - bounds.min_point.z;
  125. static const float3 barycentric_coords[3] =
  126. {
  127. float3{1, 0, 0},
  128. float3{0, 1, 0},
  129. float3{0, 0, 1}
  130. };
  131. int triangle_count = faces.size();
  132. int vertex_count = triangle_count * 3;
  133. int vertex_size = 3 + 2 + 3 + 4 + 3;
  134. // Allocate vertex data
  135. float* vertex_data = new float[vertex_size * vertex_count];
  136. // Allocate and calculate face normals
  137. float3* face_normals = new float3[faces.size()];
  138. calculate_face_normals(face_normals, *terrain_mesh);
  139. // Allocate and calculate vertex normals
  140. float3* vertex_normals = new float3[vertices.size()];
  141. for (std::size_t i = 0; i < vertices.size(); ++i)
  142. {
  143. const geom::mesh::vertex* vertex = vertices[i];
  144. float3 n = {0, 0, 0};
  145. geom::mesh::edge* start = vertex->edge;
  146. geom::mesh::edge* edge = start;
  147. do
  148. {
  149. if (edge->face)
  150. {
  151. n += face_normals[edge->face->index];
  152. }
  153. edge = edge->previous->symmetric;
  154. }
  155. while (edge != start);
  156. n = math::normalize(n);
  157. vertex_normals[i] = n;
  158. }
  159. // Allocate and generate vertex texture coordinates
  160. float2* vertex_texcoords = new float2[vertices.size()];
  161. for (std::size_t i = 0; i < vertices.size(); ++i)
  162. {
  163. const geom::mesh::vertex* vertex = vertices[i];
  164. vertex_texcoords[i].x = (vertex->position.x - bounds.min_point.x) / bounds_width;
  165. vertex_texcoords[i].y = (vertex->position.z - bounds.min_point.z) / bounds_depth;
  166. }
  167. // Allocate and calculate vertex tangents
  168. float4* vertex_tangents = new float4[vertices.size()];
  169. calculate_vertex_tangents(vertex_tangents, vertex_texcoords, vertex_normals, *terrain_mesh);
  170. // Generate vertex data
  171. float* v = vertex_data;
  172. for (int i = 0; i < triangle_count; ++i)
  173. {
  174. const geom::mesh::face* triangle = faces[i];
  175. const geom::mesh::vertex* a = triangle->edge->vertex;
  176. const geom::mesh::vertex* b = triangle->edge->next->vertex;
  177. const geom::mesh::vertex* c = triangle->edge->previous->vertex;
  178. const geom::mesh::vertex* abc[] = {a, b, c};
  179. for (int j = 0; j < 3; ++j)
  180. {
  181. const geom::mesh::vertex* vertex = abc[j];
  182. const float3& position = vertex->position;
  183. const float2& texcoord = vertex_texcoords[vertex->index];
  184. const float3& normal = vertex_normals[vertex->index];
  185. const float4& tangent = vertex_tangents[vertex->index];
  186. const float3& barycentric = barycentric_coords[j];
  187. *(v++) = position.x;
  188. *(v++) = position.y;
  189. *(v++) = position.z;
  190. *(v++) = texcoord.x;
  191. *(v++) = texcoord.y;
  192. *(v++) = normal.x;
  193. *(v++) = normal.y;
  194. *(v++) = normal.z;
  195. *(v++) = tangent.x;
  196. *(v++) = tangent.y;
  197. *(v++) = tangent.z;
  198. *(v++) = tangent.w;
  199. *(v++) = barycentric.x;
  200. *(v++) = barycentric.y;
  201. *(v++) = barycentric.z;
  202. }
  203. }
  204. // Update bounds
  205. terrain_model->set_bounds(bounds);
  206. // Update VBO
  207. terrain_model->get_vertex_buffer()->update(0, vertex_count * vertex_size * sizeof(float), vertex_data);
  208. // Free vertex data
  209. delete[] face_normals;
  210. delete[] vertex_normals;
  211. delete[] vertex_texcoords;
  212. delete[] vertex_tangents;
  213. delete[] vertex_data;
  214. }
  215. void terrain::on_terrain_construct(entity::registry& registry, entity::id entity_id, component::terrain& component)
  216. {
  217. geom::mesh* terrain_mesh = generate_terrain_mesh(patch_size, component.subdivisions);
  218. model* terrain_model = generate_terrain_model(terrain_mesh);
  219. project_terrain_mesh(terrain_mesh, component);
  220. update_terrain_model(terrain_model, terrain_mesh);
  221. // Assign the entity a collision component with the terrain mesh
  222. component::collision collision;
  223. collision.mesh = terrain_mesh;
  224. collision.bounds = calculate_bounds(*terrain_mesh);
  225. collision.mesh_accelerator.build(*collision.mesh);
  226. registry.assign_or_replace<component::collision>(entity_id, collision);
  227. // Assign the entity a model component with the terrain model
  228. component::model model;
  229. model.render_model = terrain_model;
  230. model.instance_count = 0;
  231. model.layers = 1;
  232. registry.assign_or_replace<component::model>(entity_id, model);
  233. // Assign the entity a transform component
  234. component::transform transform;
  235. transform.local = math::identity_transform<float>;
  236. transform.local.translation = float3{(float)component.x * patch_size, 0.0f, (float)component.z * patch_size};
  237. transform.warp = true;
  238. registry.assign_or_replace<component::transform>(entity_id, transform);
  239. }
  240. void terrain::on_terrain_destroy(entity::registry& registry, entity::id entity_id)
  241. {
  242. /*
  243. if (auto it = terrain_map.find(entity_id); it != terrain_map.end())
  244. {
  245. delete std::get<0>(it->second);
  246. delete std::get<1>(it->second);
  247. terrain_map.erase(it);
  248. }
  249. */
  250. }
  251. } // namespace system
  252. } // namespace entity