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

680 lines
21 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 "game/system/terrain.hpp"
  20. #include "game/component/terrain.hpp"
  21. #include "game/component/camera.hpp"
  22. #include "geom/meshes/grid.hpp"
  23. #include "geom/mesh-functions.hpp"
  24. #include "geom/morton.hpp"
  25. #include "geom/quadtree.hpp"
  26. #include "geom/primitive/ray.hpp"
  27. #include "gl/vertex-attribute.hpp"
  28. #include "math/quaternion.hpp"
  29. #include "render/vertex-attribute.hpp"
  30. #include "utility/fundamental-types.hpp"
  31. #include <functional>
  32. #include <iostream>
  33. namespace game {
  34. namespace system {
  35. terrain::terrain(entity::registry& registry):
  36. updatable(registry),
  37. patch_side_length(0.0f),
  38. patch_subdivisions(0),
  39. patch_material(nullptr),
  40. elevation_function(nullptr),
  41. scene_collection(nullptr),
  42. patch_base_mesh(nullptr),
  43. patch_vertex_size(0),
  44. patch_vertex_stride(0),
  45. patch_vertex_data(nullptr)
  46. {
  47. // Specify vertex size and stride
  48. // (position + uv + normal + tangent + barycentric + target)
  49. patch_vertex_size = 3 + 2 + 3 + 4 + 3 + 3;
  50. patch_vertex_stride = patch_vertex_size * sizeof(float);
  51. // Init quadtee node sizes at each depth
  52. for (std::size_t i = 0; i <= quadtree_type::max_depth; ++i)
  53. quadtree_node_size[i] = 0.0f;
  54. registry.on_construct<component::terrain>().connect<&terrain::on_terrain_construct>(this);
  55. registry.on_update<component::terrain>().connect<&terrain::on_terrain_update>(this);
  56. registry.on_destroy<component::terrain>().connect<&terrain::on_terrain_destroy>(this);
  57. }
  58. terrain::~terrain()
  59. {
  60. registry.on_construct<component::terrain>().disconnect<&terrain::on_terrain_construct>(this);
  61. registry.on_update<component::terrain>().disconnect<&terrain::on_terrain_update>(this);
  62. registry.on_destroy<component::terrain>().disconnect<&terrain::on_terrain_destroy>(this);
  63. }
  64. void terrain::update(double t, double dt)
  65. {
  66. // Clear quadtree
  67. quadtree.clear();
  68. // For each camera
  69. this->registry.view<component::camera>().each
  70. (
  71. [&](entity::id camera_eid, const auto& camera)
  72. {
  73. if (!camera.object)
  74. return;
  75. const scene::camera& cam = *camera.object;
  76. // for (int i = 0; i < 8; ++i)
  77. // std::cout << "corner " << i << ": " << cam.get_view_frustum().get_corners()[i] << std::endl;
  78. geom::primitive::ray<float, 3> rays[8];
  79. rays[0] = cam.pick({-1, -1});
  80. rays[1] = cam.pick({-1, 1});
  81. rays[2] = cam.pick({ 1, 1});
  82. rays[3] = cam.pick({ 1, -1});
  83. float3 ntl = rays[0].origin;
  84. float3 nbl = rays[1].origin;
  85. float3 nbr = rays[2].origin;
  86. float3 ntr = rays[3].origin;
  87. float3 ftl = rays[0].origin + rays[0].direction * (cam.get_clip_far() - cam.get_clip_near());
  88. float3 fbl = rays[1].origin + rays[1].direction * (cam.get_clip_far() - cam.get_clip_near());
  89. float3 fbr = rays[2].origin + rays[2].direction * (cam.get_clip_far() - cam.get_clip_near());
  90. float3 ftr = rays[3].origin + rays[3].direction * (cam.get_clip_far() - cam.get_clip_near());
  91. // for (int i = 0; i < 8; ++i)
  92. // std::cout << "ray or " << i << ": " << rays[i].origin << std::endl;
  93. geom::convex_hull<float> hull(6);
  94. hull.planes[0] = geom::plane<float>(ftl, fbl, nbl);
  95. hull.planes[1] = geom::plane<float>(ntr, nbr, fbr);
  96. hull.planes[2] = geom::plane<float>(fbl, fbr, nbr);
  97. hull.planes[3] = geom::plane<float>(ftl, ntl, ntr);
  98. hull.planes[4] = geom::plane<float>(ntl, nbl, nbr);
  99. hull.planes[5] = geom::plane<float>(ftr, fbr, fbl);
  100. geom::sphere<float> sphere;
  101. sphere.center = cam.get_translation();
  102. sphere.radius = cam.get_clip_far() * 0.25;
  103. //visit_quadtree(cam.get_view_frustum().get_bounds(), quadtree_type::root);
  104. visit_quadtree(sphere, quadtree_type::root);
  105. }
  106. );
  107. //std::cout << "qsize: " << quadtree.size() << std::endl;
  108. std::size_t qvis = 0;
  109. /// Toggle visibility of terrain scene objects
  110. for (auto it = patches.begin(); it != patches.end(); ++it)
  111. {
  112. bool active = (quadtree.contains(it->first) && quadtree.is_leaf(it->first));
  113. it->second->model_instance->set_active(active);
  114. if (active)
  115. ++qvis;
  116. }
  117. //std::cout << "qvis: " << qvis << std::endl;
  118. }
  119. void terrain::set_patch_side_length(float length)
  120. {
  121. patch_side_length = length;
  122. // Recalculate node sizes at each quadtree depth
  123. for (std::size_t i = 0; i <= quadtree_type::max_depth; ++i)
  124. {
  125. quadtree_node_size[i] = std::exp2(quadtree_type::max_depth - i) * patch_side_length;
  126. //std::cout << quadtree_node_size[i] << std::endl;
  127. }
  128. }
  129. void terrain::set_patch_subdivisions(std::size_t n)
  130. {
  131. patch_subdivisions = n;
  132. // Recalculate patch properties
  133. patch_cell_count = (patch_subdivisions + 1) * (patch_subdivisions + 1);
  134. patch_triangle_count = patch_cell_count * 2;
  135. // Resize patch vertex data buffer
  136. delete[] patch_vertex_data;
  137. patch_vertex_data = new float[patch_triangle_count * 3 * patch_vertex_size];
  138. // Resize patch buffers
  139. std::size_t vertex_buffer_row_size = patch_subdivisions + 4;
  140. std::size_t vertex_buffer_column_size = vertex_buffer_row_size;
  141. patch_vertex_buffer.resize(vertex_buffer_row_size);
  142. for (std::size_t i = 0; i < patch_vertex_buffer.size(); ++i)
  143. patch_vertex_buffer[i].resize(vertex_buffer_column_size);
  144. rebuild_patch_base_mesh();
  145. }
  146. void terrain::set_patch_material(::render::material* material)
  147. {
  148. patch_material = material;
  149. }
  150. void terrain::set_elevation_function(const std::function<float(float, float)>& f)
  151. {
  152. elevation_function = f;
  153. }
  154. void terrain::set_scene_collection(scene::collection* collection)
  155. {
  156. scene_collection = collection;
  157. }
  158. void terrain::on_terrain_construct(entity::registry& registry, entity::id entity_id)
  159. {
  160. }
  161. void terrain::on_terrain_update(entity::registry& registry, entity::id entity_id)
  162. {
  163. }
  164. void terrain::on_terrain_destroy(entity::registry& registry, entity::id entity_id)
  165. {
  166. }
  167. float terrain::get_patch_size(quadtree_node_type node) const
  168. {
  169. return quadtree_node_size[quadtree_type::depth(node)];
  170. }
  171. float3 terrain::get_patch_center(quadtree_node_type node) const
  172. {
  173. const float node_size = get_patch_size(node);
  174. const float node_offset = quadtree_node_size[0] * -0.5f + node_size * 0.5f;
  175. // Extract node location from Morton location code
  176. quadtree_type::node_type node_location = quadtree_type::location(node);
  177. quadtree_type::node_type node_location_x;
  178. quadtree_type::node_type node_location_y;
  179. geom::morton::decode(node_location, node_location_x, node_location_y);
  180. return float3
  181. {
  182. node_offset + static_cast<float>(node_location_x) * node_size,
  183. 0.0f,
  184. node_offset + static_cast<float>(node_location_y) * node_size
  185. };
  186. }
  187. void terrain::rebuild_patch_base_mesh()
  188. {
  189. // Rebuild grid
  190. delete patch_base_mesh;
  191. patch_base_mesh = geom::meshes::grid_xy(1.0f, patch_subdivisions, patch_subdivisions);
  192. // Convert quads to triangle fans
  193. for (std::size_t i = 0; i < patch_base_mesh->get_faces().size(); ++i)
  194. {
  195. geom::mesh::face* face = patch_base_mesh->get_faces()[i];
  196. std::size_t edge_count = 1;
  197. for (geom::mesh::edge* edge = face->edge->next; edge != face->edge; edge = edge->next)
  198. ++edge_count;
  199. if (edge_count > 3)
  200. {
  201. geom::poke_face(*patch_base_mesh, face->index);
  202. --i;
  203. }
  204. }
  205. // Transform patch base mesh coordinates from XY plane to XZ plane
  206. const math::quaternion<float> xy_to_xz = math::quaternion<float>::rotate_x(math::half_pi<float>);
  207. for (geom::mesh::vertex* vertex: patch_base_mesh->get_vertices())
  208. {
  209. vertex->position = xy_to_xz * vertex->position;
  210. }
  211. }
  212. void terrain::visit_quadtree(const geom::bounding_volume<float>& volume, quadtree_node_type node)
  213. {
  214. const float root_offset = quadtree_node_size[0] * -0.5f;
  215. // Extract node depth
  216. quadtree_type::node_type node_depth = quadtree_type::depth(node);
  217. const float node_size = get_patch_size(node);
  218. const float3 node_center = get_patch_center(node);
  219. // Build node bounds AABB
  220. geom::aabb<float> node_bounds;
  221. node_bounds.min_point =
  222. {
  223. node_center.x() - node_size * 0.5f,
  224. quadtree_node_size[quadtree_type::max_depth] * -0.5f,
  225. node_center.z() - node_size * 0.5f
  226. };
  227. node_bounds.max_point =
  228. {
  229. node_bounds.min_point[0] + node_size,
  230. node_bounds.min_point[1] + quadtree_node_size[quadtree_type::max_depth],
  231. node_bounds.min_point[2] + node_size
  232. };
  233. // If volume intersects node
  234. if (volume.intersects(node_bounds))
  235. {
  236. // Subdivide leaf nodes
  237. if (quadtree.is_leaf(node))
  238. {
  239. quadtree.insert(quadtree_type::child(node, 0));
  240. for (quadtree_node_type i = 0; i < quadtree_type::children_per_node; ++i)
  241. {
  242. quadtree_node_type child = quadtree_type::child(node, i);
  243. if (patches.find(child) == patches.end())
  244. {
  245. patch* child_patch = generate_patch(child);
  246. patches[child] = child_patch;
  247. scene_collection->add_object(child_patch->model_instance);
  248. }
  249. }
  250. }
  251. // Visit children
  252. if (node_depth < quadtree_type::max_depth - 1)
  253. {
  254. for (quadtree_node_type i = 0; i < quadtree_type::children_per_node; ++i)
  255. visit_quadtree(volume, quadtree_type::child(node, i));
  256. }
  257. }
  258. }
  259. geom::mesh* terrain::generate_patch_mesh(quadtree_node_type node) const
  260. {
  261. // Extract node depth
  262. const quadtree_type::node_type node_depth = quadtree_type::depth(node);
  263. // Get size of node at depth
  264. const float node_size = quadtree_node_size[node_depth];
  265. // Extract node Morton location code and decode location
  266. const quadtree_type::node_type node_location = quadtree_type::location(node);
  267. quadtree_type::node_type node_location_x;
  268. quadtree_type::node_type node_location_y;
  269. geom::morton::decode(node_location, node_location_x, node_location_y);
  270. // Determine center of node
  271. const float node_offset = quadtree_node_size[0] * -0.5f + node_size * 0.5f;
  272. const float3 node_center =
  273. {
  274. node_offset + static_cast<float>(node_location_x) * node_size,
  275. 0.0f,
  276. node_offset + static_cast<float>(node_location_y) * node_size
  277. };
  278. // Copy patch base mesh
  279. geom::mesh* patch_mesh = new geom::mesh(*patch_base_mesh);
  280. // Modify patch mesh vertex positions
  281. for (geom::mesh::vertex* v: patch_mesh->get_vertices())
  282. {
  283. v->position.x() = node_center.x() + v->position.x() * node_size;
  284. v->position.z() = node_center.z() + v->position.z() * node_size;
  285. v->position.y() = elevation_function(v->position.x(), v->position.z());
  286. }
  287. return patch_mesh;
  288. }
  289. ::render::model* terrain::generate_patch_model(quadtree_node_type node) const
  290. {
  291. // Get size and position of patch
  292. const float patch_size = get_patch_size(node);
  293. const float3 patch_center = get_patch_center(node);
  294. // Calculate size of a patch cell
  295. const float cell_size = patch_size / static_cast<float>(patch_subdivisions + 1);
  296. const float half_cell_size = cell_size * 0.5f;
  297. // Init patch bounds
  298. geom::aabb<float> patch_bounds;
  299. patch_bounds.min_point.x() = patch_center.x() - patch_size * 0.5f;
  300. patch_bounds.min_point.y() = std::numeric_limits<float>::infinity();
  301. patch_bounds.min_point.z() = patch_center.z() - patch_size * 0.5f;
  302. patch_bounds.max_point.x() = patch_center.x() + patch_size * 0.5f;
  303. patch_bounds.max_point.y() = -std::numeric_limits<float>::infinity();
  304. patch_bounds.max_point.z() = patch_center.z() + patch_size * 0.5f;
  305. // Calculate positions and UVs of patch vertices and immediately neighboring vertices
  306. float3 first_vertex_position =
  307. {
  308. patch_bounds.min_point.x() - cell_size,
  309. patch_center.y(),
  310. patch_bounds.min_point.z() - cell_size
  311. };
  312. float3 vertex_position = first_vertex_position;
  313. for (std::size_t i = 0; i < patch_vertex_buffer.size(); ++i)
  314. {
  315. // For each column
  316. for (std::size_t j = 0; j < patch_vertex_buffer[i].size(); ++j)
  317. {
  318. // Calculate vertex elevation
  319. vertex_position.y() = elevation_function(vertex_position.x(), vertex_position.z());
  320. // Update patch bounds
  321. patch_bounds.min_point.y() = std::min(patch_bounds.min_point.y(), vertex_position.y());
  322. patch_bounds.max_point.y() = std::max(patch_bounds.max_point.y(), vertex_position.y());
  323. // Update patch vertex position
  324. patch_vertex_buffer[i][j].position = vertex_position;
  325. // Calculate patch vertex UV
  326. patch_vertex_buffer[i][j].uv.x() = (vertex_position.x() - patch_bounds.min_point.x()) / patch_size;
  327. patch_vertex_buffer[i][j].uv.y() = (vertex_position.z() - patch_bounds.min_point.z()) / patch_size;
  328. // Init patch vertex normal, tangent, and bitangent
  329. patch_vertex_buffer[i][j].normal = {0, 0, 0};
  330. patch_vertex_buffer[i][j].tangent = {0, 0, 0};
  331. patch_vertex_buffer[i][j].bitangent = {0, 0, 0};
  332. vertex_position.x() += cell_size;
  333. }
  334. vertex_position.z() += cell_size;
  335. vertex_position.x() = first_vertex_position.x();
  336. }
  337. // Accumulate normals, tangents, and bitangents
  338. for (std::size_t i = 0; i < patch_vertex_buffer.size() - 1; ++i)
  339. {
  340. for (std::size_t j = 0; j < patch_vertex_buffer[i].size() - 1; ++j)
  341. {
  342. patch_vertex& a = patch_vertex_buffer[i ][j];
  343. patch_vertex& b = patch_vertex_buffer[i+1][j];
  344. patch_vertex& c = patch_vertex_buffer[i ][j+1];
  345. patch_vertex& d = patch_vertex_buffer[i+1][j+1];
  346. auto add_ntb = [](auto& a, auto& b, auto& c)
  347. {
  348. const float3 ba = b.position - a.position;
  349. const float3 ca = c.position - a.position;
  350. const float2 uvba = b.uv - a.uv;
  351. const float2 uvca = c.uv - a.uv;
  352. const float3 normal = math::normalize(math::cross(ba, ca));
  353. const float f = 1.0f / (uvba.x() * uvca.y() - uvca.x() * uvba.y());
  354. const float3 tangent = (ba * uvca.y() - ca * uvba.y()) * f;
  355. const float3 bitangent = (ba * -uvca.x() + ca * uvba.x()) * f;
  356. a.normal += normal;
  357. a.tangent += tangent;
  358. a.bitangent += bitangent;
  359. b.normal += normal;
  360. b.tangent += tangent;
  361. b.bitangent += bitangent;
  362. c.normal += normal;
  363. c.tangent += tangent;
  364. c.bitangent += bitangent;
  365. };
  366. if ((j + i) % 2)
  367. {
  368. add_ntb(a, b, c);
  369. add_ntb(c, b, d);
  370. }
  371. else
  372. {
  373. add_ntb(a, b, d);
  374. add_ntb(a, d, c);
  375. }
  376. }
  377. }
  378. // Finalize normals, tangents, and bitangent signs of patch vertices
  379. for (std::size_t i = 1; i < patch_vertex_buffer.size() - 1; ++i)
  380. {
  381. for (std::size_t j = 1; j < patch_vertex_buffer[i].size() - 1; ++j)
  382. {
  383. auto& vertex = patch_vertex_buffer[i][j];
  384. // Normalize normal
  385. vertex.normal = math::normalize(vertex.normal);
  386. // Gram-Schmidt orthogonalize tangent
  387. vertex.tangent = math::normalize(vertex.tangent - vertex.normal * math::dot(vertex.normal, vertex.tangent));
  388. // Calculate bitangent sign
  389. vertex.bitangent_sign = std::copysign(1.0f, math::dot(math::cross(vertex.normal, vertex.tangent), vertex.bitangent));
  390. }
  391. }
  392. /*
  393. 0 subdivisions:
  394. +---+---+---+
  395. | |
  396. + +---+ +
  397. | | | |
  398. + +---+ +
  399. | |
  400. +---+---+---+
  401. 1 subdivision:
  402. +---+---+---+---+
  403. | |
  404. + +---+---+ +
  405. | | | | |
  406. + +---+---+ +
  407. | | | | |
  408. + +---+---+ +
  409. | |
  410. +---+---+---+---+
  411. 2 subdivisions:
  412. +---+---+---+---+---+
  413. | |
  414. + +---+---+---+ +
  415. | | | | | |
  416. + +---+---+---+ +
  417. | | | | | |
  418. + +---+---+---+ +
  419. | | | | | |
  420. + +---+---+---+ +
  421. | |
  422. +---+---+---+---+---+
  423. */
  424. // For each row
  425. float* v = patch_vertex_data;
  426. for (std::size_t i = 1; i < patch_vertex_buffer.size() - 2; ++i)
  427. {
  428. // For each column
  429. for (std::size_t j = 1; j < patch_vertex_buffer[i].size() - 2; ++j)
  430. {
  431. // a---c
  432. // | |
  433. // b---d
  434. const patch_vertex& a = patch_vertex_buffer[i ][j];
  435. const patch_vertex& b = patch_vertex_buffer[i+1][j];
  436. const patch_vertex& c = patch_vertex_buffer[i ][j+1];
  437. const patch_vertex& d = patch_vertex_buffer[i+1][j+1];
  438. auto add_triangle = [&v](const patch_vertex& a, const patch_vertex& b, const patch_vertex& c)
  439. {
  440. auto add_vertex = [&v](const patch_vertex& vertex, const float3& barycentric)
  441. {
  442. // Position
  443. *(v++) = vertex.position[0];
  444. *(v++) = vertex.position[1];
  445. *(v++) = vertex.position[2];
  446. // UV
  447. *(v++) = vertex.uv[0];
  448. *(v++) = vertex.uv[1];
  449. // Normal
  450. *(v++) = vertex.normal[0];
  451. *(v++) = vertex.normal[1];
  452. *(v++) = vertex.normal[2];
  453. /// Tangent
  454. *(v++) = vertex.tangent[0];
  455. *(v++) = vertex.tangent[1];
  456. *(v++) = vertex.tangent[2];
  457. *(v++) = vertex.bitangent_sign;
  458. // Barycentric
  459. *(v++) = barycentric[0];
  460. *(v++) = barycentric[1];
  461. *(v++) = barycentric[2];
  462. // Morph target (LOD transition)
  463. *(v++) = 0.0f;
  464. *(v++) = 0.0f;
  465. *(v++) = 0.0f;
  466. };
  467. add_vertex(a, float3{1, 0, 0});
  468. add_vertex(b, float3{0, 1, 0});
  469. add_vertex(c, float3{0, 0, 1});
  470. };
  471. if ((j + i) % 2)
  472. {
  473. add_triangle(a, b, c);
  474. add_triangle(c, b, d);
  475. }
  476. else
  477. {
  478. add_triangle(a, b, d);
  479. add_triangle(a, d, c);
  480. }
  481. }
  482. }
  483. // Allocate patch model
  484. ::render::model* patch_model = new ::render::model();
  485. // Get model VBO and VAO
  486. gl::vertex_buffer* vbo = patch_model->get_vertex_buffer();
  487. gl::vertex_array* vao = patch_model->get_vertex_array();
  488. // Resize model VBO and upload vertex data
  489. vbo->resize(patch_triangle_count * 3 * patch_vertex_stride, patch_vertex_data);
  490. std::size_t attribute_offset = 0;
  491. // Define position vertex attribute
  492. gl::vertex_attribute position_attribute;
  493. position_attribute.buffer = vbo;
  494. position_attribute.offset = attribute_offset;
  495. position_attribute.stride = patch_vertex_stride;
  496. position_attribute.type = gl::vertex_attribute_type::float_32;
  497. position_attribute.components = 3;
  498. attribute_offset += position_attribute.components * sizeof(float);
  499. // Define UV vertex attribute
  500. gl::vertex_attribute uv_attribute;
  501. uv_attribute.buffer = vbo;
  502. uv_attribute.offset = attribute_offset;
  503. uv_attribute.stride = patch_vertex_stride;
  504. uv_attribute.type = gl::vertex_attribute_type::float_32;
  505. uv_attribute.components = 2;
  506. attribute_offset += uv_attribute.components * sizeof(float);
  507. // Define normal vertex attribute
  508. gl::vertex_attribute normal_attribute;
  509. normal_attribute.buffer = vbo;
  510. normal_attribute.offset = attribute_offset;
  511. normal_attribute.stride = patch_vertex_stride;
  512. normal_attribute.type = gl::vertex_attribute_type::float_32;
  513. normal_attribute.components = 3;
  514. attribute_offset += normal_attribute.components * sizeof(float);
  515. // Define tangent vertex attribute
  516. gl::vertex_attribute tangent_attribute;
  517. tangent_attribute.buffer = vbo;
  518. tangent_attribute.offset = attribute_offset;
  519. tangent_attribute.stride = patch_vertex_stride;
  520. tangent_attribute.type = gl::vertex_attribute_type::float_32;
  521. tangent_attribute.components = 4;
  522. attribute_offset += tangent_attribute.components * sizeof(float);
  523. // Define barycentric vertex attribute
  524. gl::vertex_attribute barycentric_attribute;
  525. barycentric_attribute.buffer = vbo;
  526. barycentric_attribute.offset = attribute_offset;
  527. barycentric_attribute.stride = patch_vertex_stride;
  528. barycentric_attribute.type = gl::vertex_attribute_type::float_32;
  529. barycentric_attribute.components = 3;
  530. attribute_offset += barycentric_attribute.components * sizeof(float);
  531. // Define target vertex attribute
  532. gl::vertex_attribute target_attribute;
  533. target_attribute.buffer = vbo;
  534. target_attribute.offset = attribute_offset;
  535. target_attribute.stride = patch_vertex_stride;
  536. target_attribute.type = gl::vertex_attribute_type::float_32;
  537. target_attribute.components = 3;
  538. attribute_offset += target_attribute.components * sizeof(float);
  539. // Bind vertex attributes to VAO
  540. vao->bind(::render::vertex_attribute::position, position_attribute);
  541. vao->bind(::render::vertex_attribute::uv, uv_attribute);
  542. vao->bind(::render::vertex_attribute::normal, normal_attribute);
  543. vao->bind(::render::vertex_attribute::tangent, tangent_attribute);
  544. vao->bind(::render::vertex_attribute::barycentric, barycentric_attribute);
  545. vao->bind(::render::vertex_attribute::target, target_attribute);
  546. // Create model group
  547. ::render::model_group* patch_model_group = patch_model->add_group("terrain");
  548. patch_model_group->set_material(patch_material);
  549. patch_model_group->set_drawing_mode(gl::drawing_mode::triangles);
  550. patch_model_group->set_start_index(0);
  551. patch_model_group->set_index_count(patch_triangle_count * 3);
  552. // Set patch model bounds
  553. patch_model->set_bounds(patch_bounds);
  554. return patch_model;
  555. }
  556. terrain::patch* terrain::generate_patch(quadtree_node_type node)
  557. {
  558. patch* node_patch = new patch();
  559. node_patch->mesh = nullptr;//generate_patch_mesh(node);
  560. node_patch->model = generate_patch_model(node);
  561. node_patch->model_instance = new scene::model_instance(node_patch->model);
  562. return node_patch;
  563. }
  564. } // namespace system
  565. } // namespace game