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

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