2
0

biome_renderer.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. #include "biome_renderer.h"
  2. #include "../../game/map/terrain_service.h"
  3. #include "../../game/systems/building_collision_registry.h"
  4. #include "../gl/buffer.h"
  5. #include "../gl/render_constants.h"
  6. #include "../scene_renderer.h"
  7. #include "gl/resources.h"
  8. #include "ground/grass_gpu.h"
  9. #include "ground_utils.h"
  10. #include "map/terrain.h"
  11. #include <QDebug>
  12. #include <QElapsedTimer>
  13. #include <QVector2D>
  14. #include <algorithm>
  15. #include <cmath>
  16. #include <cstddef>
  17. #include <cstdint>
  18. #include <memory>
  19. #include <optional>
  20. #include <qelapsedtimer.h>
  21. #include <qglobal.h>
  22. #include <qvectornd.h>
  23. #include <vector>
  24. namespace {
  25. using std::uint32_t;
  26. using namespace Render::Ground;
  27. using namespace Render::GL::Geometry;
  28. inline auto valueNoise(float x, float z, uint32_t salt = 0U) -> float {
  29. int const x0 = int(std::floor(x));
  30. int const z0 = int(std::floor(z));
  31. int const x1 = x0 + 1;
  32. int const z1 = z0 + 1;
  33. float const tx = x - float(x0);
  34. float const tz = z - float(z0);
  35. float const n00 = hash_to_01(hash_coords(x0, z0, salt));
  36. float const n10 = hash_to_01(hash_coords(x1, z0, salt));
  37. float const n01 = hash_to_01(hash_coords(x0, z1, salt));
  38. float const n11 = hash_to_01(hash_coords(x1, z1, salt));
  39. float const nx0 = n00 * (1 - tx) + n10 * tx;
  40. float const nx1 = n01 * (1 - tx) + n11 * tx;
  41. return nx0 * (1 - tz) + nx1 * tz;
  42. }
  43. inline auto sectionFor(Game::Map::TerrainType type) -> int {
  44. switch (type) {
  45. case Game::Map::TerrainType::Mountain:
  46. return 2;
  47. case Game::Map::TerrainType::Hill:
  48. return 1;
  49. case Game::Map::TerrainType::Flat:
  50. default:
  51. return 0;
  52. }
  53. }
  54. } // namespace
  55. namespace Render::GL {
  56. BiomeRenderer::BiomeRenderer() = default;
  57. BiomeRenderer::~BiomeRenderer() = default;
  58. void BiomeRenderer::configure(const Game::Map::TerrainHeightMap &height_map,
  59. const Game::Map::BiomeSettings &biomeSettings) {
  60. m_width = height_map.getWidth();
  61. m_height = height_map.getHeight();
  62. m_tile_size = height_map.getTileSize();
  63. m_heightData = height_map.getHeightData();
  64. m_terrain_types = height_map.getTerrainTypes();
  65. m_biomeSettings = biomeSettings;
  66. m_noiseSeed = biomeSettings.seed;
  67. m_grassInstances.clear();
  68. m_grassInstanceBuffer.reset();
  69. m_grassInstanceCount = 0;
  70. m_grassInstancesDirty = false;
  71. m_grassParams.soil_color = m_biomeSettings.soil_color;
  72. m_grassParams.wind_strength = m_biomeSettings.sway_strength;
  73. m_grassParams.wind_speed = m_biomeSettings.sway_speed;
  74. m_grassParams.light_direction = QVector3D(0.35F, 0.8F, 0.45F);
  75. m_grassParams.time = 0.0F;
  76. generateGrassInstances();
  77. }
  78. void BiomeRenderer::submit(Renderer &renderer, ResourceManager *resources) {
  79. Q_UNUSED(resources);
  80. if (m_grassInstanceCount > 0) {
  81. if (!m_grassInstanceBuffer) {
  82. m_grassInstanceBuffer = std::make_unique<Buffer>(Buffer::Type::Vertex);
  83. }
  84. if (m_grassInstancesDirty && m_grassInstanceBuffer) {
  85. m_grassInstanceBuffer->setData(m_grassInstances, Buffer::Usage::Static);
  86. m_grassInstancesDirty = false;
  87. }
  88. } else {
  89. m_grassInstanceBuffer.reset();
  90. return;
  91. }
  92. if (m_grassInstanceBuffer && m_grassInstanceCount > 0) {
  93. GrassBatchParams params = m_grassParams;
  94. params.time = renderer.getAnimationTime();
  95. renderer.grassBatch(m_grassInstanceBuffer.get(), m_grassInstanceCount,
  96. params);
  97. }
  98. }
  99. void BiomeRenderer::clear() {
  100. m_grassInstances.clear();
  101. m_grassInstanceBuffer.reset();
  102. m_grassInstanceCount = 0;
  103. m_grassInstancesDirty = false;
  104. }
  105. void BiomeRenderer::refreshGrass() { generateGrassInstances(); }
  106. void BiomeRenderer::generateGrassInstances() {
  107. QElapsedTimer timer;
  108. timer.start();
  109. m_grassInstances.clear();
  110. if (m_width < 2 || m_height < 2 || m_heightData.empty()) {
  111. m_grassInstanceCount = 0;
  112. m_grassInstancesDirty = false;
  113. return;
  114. }
  115. if (m_biomeSettings.patch_density < 0.01F) {
  116. m_grassInstanceCount = 0;
  117. m_grassInstancesDirty = false;
  118. return;
  119. }
  120. const float half_width = m_width * 0.5F - 0.5F;
  121. const float half_height = m_height * 0.5F - 0.5F;
  122. const float tile_safe = std::max(0.001F, m_tile_size);
  123. const float edge_padding =
  124. std::clamp(m_biomeSettings.spawn_edge_padding, 0.0F, 0.5F);
  125. const float edge_margin_x = static_cast<float>(m_width) * edge_padding;
  126. const float edge_margin_z = static_cast<float>(m_height) * edge_padding;
  127. std::vector<QVector3D> normals(static_cast<qsizetype>(m_width * m_height),
  128. QVector3D(0.0F, 1.0F, 0.0F));
  129. auto sample_height_at = [&](float gx, float gz) -> float {
  130. gx = std::clamp(gx, 0.0F, float(m_width - 1));
  131. gz = std::clamp(gz, 0.0F, float(m_height - 1));
  132. int const x0 = int(std::floor(gx));
  133. int const z0 = int(std::floor(gz));
  134. int const x1 = std::min(x0 + 1, m_width - 1);
  135. int const z1 = std::min(z0 + 1, m_height - 1);
  136. float const tx = gx - float(x0);
  137. float const tz = gz - float(z0);
  138. float const h00 = m_heightData[z0 * m_width + x0];
  139. float const h10 = m_heightData[z0 * m_width + x1];
  140. float const h01 = m_heightData[z1 * m_width + x0];
  141. float const h11 = m_heightData[z1 * m_width + x1];
  142. float const h0 = h00 * (1.0F - tx) + h10 * tx;
  143. float const h1 = h01 * (1.0F - tx) + h11 * tx;
  144. return h0 * (1.0F - tz) + h1 * tz;
  145. };
  146. for (int z = 0; z < m_height; ++z) {
  147. for (int x = 0; x < m_width; ++x) {
  148. int const idx = z * m_width + x;
  149. float const gx0 = std::clamp(float(x) - 1.0F, 0.0F, float(m_width - 1));
  150. float const gx1 = std::clamp(float(x) + 1.0F, 0.0F, float(m_width - 1));
  151. float const gz0 = std::clamp(float(z) - 1.0F, 0.0F, float(m_height - 1));
  152. float const gz1 = std::clamp(float(z) + 1.0F, 0.0F, float(m_height - 1));
  153. float const h_l = sample_height_at(gx0, float(z));
  154. float const h_r = sample_height_at(gx1, float(z));
  155. float const h_d = sample_height_at(float(x), gz0);
  156. float const h_u = sample_height_at(float(x), gz1);
  157. QVector3D const dx(2.0F * m_tile_size, h_r - h_l, 0.0F);
  158. QVector3D const dz(0.0F, h_u - h_d, 2.0F * m_tile_size);
  159. QVector3D n = QVector3D::crossProduct(dz, dx);
  160. if (n.lengthSquared() > 0.0F) {
  161. n.normalize();
  162. } else {
  163. n = QVector3D(0, 1, 0);
  164. }
  165. normals[idx] = n;
  166. }
  167. }
  168. auto add_grass_blade = [&](float gx, float gz, uint32_t &state) {
  169. if (gx < edge_margin_x || gx > m_width - 1 - edge_margin_x ||
  170. gz < edge_margin_z || gz > m_height - 1 - edge_margin_z) {
  171. return false;
  172. }
  173. float const sgx = std::clamp(gx, 0.0F, float(m_width - 1));
  174. float const sgz = std::clamp(gz, 0.0F, float(m_height - 1));
  175. int const ix = std::clamp(int(std::floor(sgx + 0.5F)), 0, m_width - 1);
  176. int const iz = std::clamp(int(std::floor(sgz + 0.5F)), 0, m_height - 1);
  177. int const normal_idx = iz * m_width + ix;
  178. if (m_terrain_types[normal_idx] == Game::Map::TerrainType::Mountain ||
  179. m_terrain_types[normal_idx] == Game::Map::TerrainType::Hill) {
  180. return false;
  181. }
  182. if (m_terrain_types[normal_idx] == Game::Map::TerrainType::River) {
  183. return false;
  184. }
  185. constexpr int k_river_margin = 1;
  186. int near_river_count = 0;
  187. for (int dz = -k_river_margin; dz <= k_river_margin; ++dz) {
  188. for (int dx = -k_river_margin; dx <= k_river_margin; ++dx) {
  189. if (dx == 0 && dz == 0) {
  190. continue;
  191. }
  192. int const nx = ix + dx;
  193. int const nz = iz + dz;
  194. if (nx >= 0 && nx < m_width && nz >= 0 && nz < m_height) {
  195. int const n_idx = nz * m_width + nx;
  196. if (m_terrain_types[n_idx] == Game::Map::TerrainType::River) {
  197. near_river_count++;
  198. }
  199. }
  200. }
  201. }
  202. if (near_river_count > 0) {
  203. float const riverbank_density = 0.15F;
  204. if (rand_01(state) > riverbank_density) {
  205. return false;
  206. }
  207. }
  208. QVector3D const normal = normals[normal_idx];
  209. float const slope = 1.0F - std::clamp(normal.y(), 0.0F, 1.0F);
  210. if (slope > 0.92F) {
  211. return false;
  212. }
  213. float const world_x = (gx - half_width) * m_tile_size;
  214. float const world_z = (gz - half_height) * m_tile_size;
  215. float const world_y = sample_height_at(sgx, sgz);
  216. auto &building_registry =
  217. Game::Systems::BuildingCollisionRegistry::instance();
  218. if (building_registry.isPointInBuilding(world_x, world_z)) {
  219. return false;
  220. }
  221. auto &terrain_service = Game::Map::TerrainService::instance();
  222. if (terrain_service.is_point_on_road(world_x, world_z)) {
  223. return false;
  224. }
  225. float const lush_noise =
  226. valueNoise(world_x * 0.06F, world_z * 0.06F, m_noiseSeed ^ 0x9235U);
  227. float const dryness_noise =
  228. valueNoise(world_x * 0.12F, world_z * 0.12F, m_noiseSeed ^ 0x47d2U);
  229. float const dryness =
  230. std::clamp(dryness_noise * 0.6F + slope * 0.4F, 0.0F, 1.0F);
  231. QVector3D const lush_mix =
  232. m_biomeSettings.grass_primary * (1.0F - lush_noise) +
  233. m_biomeSettings.grass_secondary * lush_noise;
  234. QVector3D const color =
  235. lush_mix * (1.0F - dryness) + m_biomeSettings.grass_dry * dryness;
  236. float const height = remap(rand_01(state), m_biomeSettings.blade_height_min,
  237. m_biomeSettings.blade_height_max) *
  238. tile_safe * 0.5F;
  239. float const width = remap(rand_01(state), m_biomeSettings.blade_width_min,
  240. m_biomeSettings.blade_width_max) *
  241. tile_safe;
  242. float const sway_strength = remap(rand_01(state), 0.75F, 1.25F);
  243. float const sway_speed = remap(rand_01(state), 0.85F, 1.15F);
  244. float const sway_phase = rand_01(state) * MathConstants::k_two_pi;
  245. float const orientation = rand_01(state) * MathConstants::k_two_pi;
  246. GrassInstanceGpu instance;
  247. instance.posHeight = QVector4D(world_x, world_y, world_z, height);
  248. instance.colorWidth = QVector4D(color.x(), color.y(), color.z(), width);
  249. instance.swayParams =
  250. QVector4D(sway_strength, sway_speed, sway_phase, orientation);
  251. m_grassInstances.push_back(instance);
  252. return true;
  253. };
  254. auto quad_section = [&](Game::Map::TerrainType a, Game::Map::TerrainType b,
  255. Game::Map::TerrainType c, Game::Map::TerrainType d) {
  256. int const priority_a = sectionFor(a);
  257. int const priority_b = sectionFor(b);
  258. int const priority_c = sectionFor(c);
  259. int const priority_d = sectionFor(d);
  260. int result = priority_a;
  261. result = std::max(result, priority_b);
  262. result = std::max(result, priority_c);
  263. result = std::max(result, priority_d);
  264. return result;
  265. };
  266. const int chunk_size = DefaultChunkSize;
  267. for (int chunk_z = 0; chunk_z < m_height - 1; chunk_z += chunk_size) {
  268. int const chunk_max_z = std::min(chunk_z + chunk_size, m_height - 1);
  269. for (int chunk_x = 0; chunk_x < m_width - 1; chunk_x += chunk_size) {
  270. int const chunk_max_x = std::min(chunk_x + chunk_size, m_width - 1);
  271. int flat_count = 0;
  272. int hill_count = 0;
  273. int mountain_count = 0;
  274. float chunk_height_sum = 0.0F;
  275. float chunk_slope_sum = 0.0F;
  276. int sample_count = 0;
  277. for (int z = chunk_z; z < chunk_max_z && z < m_height - 1; ++z) {
  278. for (int x = chunk_x; x < chunk_max_x && x < m_width - 1; ++x) {
  279. int const idx0 = z * m_width + x;
  280. int const idx1 = idx0 + 1;
  281. int const idx2 = (z + 1) * m_width + x;
  282. int const idx3 = idx2 + 1;
  283. if (m_terrain_types[idx0] == Game::Map::TerrainType::Mountain ||
  284. m_terrain_types[idx1] == Game::Map::TerrainType::Mountain ||
  285. m_terrain_types[idx2] == Game::Map::TerrainType::Mountain ||
  286. m_terrain_types[idx3] == Game::Map::TerrainType::Mountain ||
  287. m_terrain_types[idx0] == Game::Map::TerrainType::River ||
  288. m_terrain_types[idx1] == Game::Map::TerrainType::River ||
  289. m_terrain_types[idx2] == Game::Map::TerrainType::River ||
  290. m_terrain_types[idx3] == Game::Map::TerrainType::River) {
  291. mountain_count++;
  292. } else if (m_terrain_types[idx0] == Game::Map::TerrainType::Hill ||
  293. m_terrain_types[idx1] == Game::Map::TerrainType::Hill ||
  294. m_terrain_types[idx2] == Game::Map::TerrainType::Hill ||
  295. m_terrain_types[idx3] == Game::Map::TerrainType::Hill) {
  296. hill_count++;
  297. } else {
  298. flat_count++;
  299. }
  300. float const quad_height = (m_heightData[idx0] + m_heightData[idx1] +
  301. m_heightData[idx2] + m_heightData[idx3]) *
  302. 0.25F;
  303. chunk_height_sum += quad_height;
  304. float const n_y = (normals[idx0].y() + normals[idx1].y() +
  305. normals[idx2].y() + normals[idx3].y()) *
  306. 0.25F;
  307. chunk_slope_sum += 1.0F - std::clamp(n_y, 0.0F, 1.0F);
  308. sample_count++;
  309. }
  310. }
  311. if (sample_count == 0) {
  312. continue;
  313. }
  314. const float usable_coverage =
  315. sample_count > 0
  316. ? float(flat_count + hill_count) / float(sample_count)
  317. : 0.0F;
  318. if (usable_coverage < 0.05F) {
  319. continue;
  320. }
  321. bool const is_primarily_flat = flat_count >= hill_count;
  322. float const avg_slope = chunk_slope_sum / float(sample_count);
  323. uint32_t state = hash_coords(chunk_x, chunk_z, m_noiseSeed ^ 0xC915872BU);
  324. float const slope_penalty =
  325. 1.0F - std::clamp(avg_slope * 1.35F, 0.0F, 0.75F);
  326. float const type_bias = 1.0F;
  327. constexpr float k_cluster_boost = 1.35F;
  328. float const expected_clusters =
  329. std::max(0.0F, m_biomeSettings.patch_density * k_cluster_boost *
  330. slope_penalty * type_bias * usable_coverage);
  331. int cluster_count = static_cast<int>(std::floor(expected_clusters));
  332. float const frac = expected_clusters - float(cluster_count);
  333. if (rand_01(state) < frac) {
  334. cluster_count += 1;
  335. }
  336. if (cluster_count > 0) {
  337. auto chunk_span_x = float(chunk_max_x - chunk_x + 1);
  338. auto chunk_span_z = float(chunk_max_z - chunk_z + 1);
  339. float const scatter_base =
  340. std::max(0.25F, m_biomeSettings.patch_jitter);
  341. auto pick_cluster_center =
  342. [&](uint32_t &rng) -> std::optional<QVector2D> {
  343. constexpr int k_max_attempts = 8;
  344. for (int attempt = 0; attempt < k_max_attempts; ++attempt) {
  345. float const candidate_gx =
  346. float(chunk_x) + rand_01(rng) * chunk_span_x;
  347. float const candidate_gz =
  348. float(chunk_z) + rand_01(rng) * chunk_span_z;
  349. int const cx =
  350. std::clamp(int(std::round(candidate_gx)), 0, m_width - 1);
  351. int const cz =
  352. std::clamp(int(std::round(candidate_gz)), 0, m_height - 1);
  353. int const center_idx = cz * m_width + cx;
  354. if (m_terrain_types[center_idx] ==
  355. Game::Map::TerrainType::Mountain ||
  356. m_terrain_types[center_idx] == Game::Map::TerrainType::River) {
  357. continue;
  358. }
  359. QVector3D const center_normal = normals[center_idx];
  360. float const center_slope =
  361. 1.0F - std::clamp(center_normal.y(), 0.0F, 1.0F);
  362. if (center_slope > 0.92F) {
  363. continue;
  364. }
  365. return QVector2D(candidate_gx, candidate_gz);
  366. }
  367. return std::nullopt;
  368. };
  369. for (int cluster = 0; cluster < cluster_count; ++cluster) {
  370. auto center = pick_cluster_center(state);
  371. if (!center) {
  372. continue;
  373. }
  374. float const center_gx = center->x();
  375. float const center_gz = center->y();
  376. int blades = 6 + static_cast<int>(rand_01(state) * 6.0F);
  377. blades = std::max(
  378. 4, int(std::round(blades * (0.85F + 0.3F * rand_01(state)))));
  379. float const scatter_radius =
  380. (0.45F + 0.55F * rand_01(state)) * scatter_base * tile_safe;
  381. for (int blade = 0; blade < blades; ++blade) {
  382. float const angle = rand_01(state) * MathConstants::k_two_pi;
  383. float const radius = scatter_radius * std::sqrt(rand_01(state));
  384. float const gx = center_gx + std::cos(angle) * radius / tile_safe;
  385. float const gz = center_gz + std::sin(angle) * radius / tile_safe;
  386. add_grass_blade(gx, gz, state);
  387. }
  388. }
  389. }
  390. }
  391. }
  392. const float background_density =
  393. std::max(0.0F, m_biomeSettings.background_blade_density);
  394. if (background_density > 0.0F) {
  395. for (int z = 0; z < m_height; ++z) {
  396. for (int x = 0; x < m_width; ++x) {
  397. int const idx = z * m_width + x;
  398. if (m_terrain_types[idx] == Game::Map::TerrainType::Mountain ||
  399. m_terrain_types[idx] == Game::Map::TerrainType::Hill ||
  400. m_terrain_types[idx] == Game::Map::TerrainType::River) {
  401. continue;
  402. }
  403. QVector3D const normal = normals[idx];
  404. float const slope = 1.0F - std::clamp(normal.y(), 0.0F, 1.0F);
  405. if (slope > 0.95F) {
  406. continue;
  407. }
  408. uint32_t state = hash_coords(
  409. x, z, m_noiseSeed ^ 0x51bda7U ^ static_cast<uint32_t>(idx));
  410. int base_count = static_cast<int>(std::floor(background_density));
  411. float const frac = background_density - float(base_count);
  412. if (rand_01(state) < frac) {
  413. base_count += 1;
  414. }
  415. for (int i = 0; i < base_count; ++i) {
  416. float const gx = float(x) + rand_01(state);
  417. float const gz = float(z) + rand_01(state);
  418. add_grass_blade(gx, gz, state);
  419. }
  420. }
  421. }
  422. }
  423. m_grassInstanceCount = m_grassInstances.size();
  424. m_grassInstancesDirty = m_grassInstanceCount > 0;
  425. int debug_flat_count = 0;
  426. int debug_hill_count = 0;
  427. int debug_mountain_count = 0;
  428. for (const auto &type : m_terrain_types) {
  429. if (type == Game::Map::TerrainType::Flat) {
  430. debug_flat_count++;
  431. } else if (type == Game::Map::TerrainType::Hill) {
  432. debug_hill_count++;
  433. } else if (type == Game::Map::TerrainType::Mountain) {
  434. debug_mountain_count++;
  435. }
  436. }
  437. }
  438. } // namespace Render::GL