biome_renderer.cpp 18 KB

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