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

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