stone_renderer.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "stone_renderer.h"
  2. #include "../gl/buffer.h"
  3. #include "../scene_renderer.h"
  4. #include "gl/render_constants.h"
  5. #include "gl/resources.h"
  6. #include "ground/stone_gpu.h"
  7. #include "ground_utils.h"
  8. #include "map/terrain.h"
  9. #include "spawn_validator.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 <qelapsedtimer.h>
  19. #include <qglobal.h>
  20. #include <vector>
  21. namespace {
  22. using std::uint32_t;
  23. using namespace Render::Ground;
  24. inline auto value_noise(float x, float z, uint32_t salt = 0U) -> float {
  25. int const x0 = int(std::floor(x));
  26. int const z0 = int(std::floor(z));
  27. int const x1 = x0 + 1;
  28. int const z1 = z0 + 1;
  29. float const tx = x - float(x0);
  30. float const tz = z - float(z0);
  31. float const n00 = hash_to_01(hash_coords(x0, z0, salt));
  32. float const n10 = hash_to_01(hash_coords(x1, z0, salt));
  33. float const n01 = hash_to_01(hash_coords(x0, z1, salt));
  34. float const n11 = hash_to_01(hash_coords(x1, z1, salt));
  35. float const nx0 = n00 * (1 - tx) + n10 * tx;
  36. float const nx1 = n01 * (1 - tx) + n11 * tx;
  37. return nx0 * (1 - tz) + nx1 * tz;
  38. }
  39. } // namespace
  40. namespace Render::GL {
  41. StoneRenderer::StoneRenderer() = default;
  42. StoneRenderer::~StoneRenderer() = default;
  43. void StoneRenderer::configure(const Game::Map::TerrainHeightMap &height_map,
  44. const Game::Map::BiomeSettings &biome_settings) {
  45. m_width = height_map.getWidth();
  46. m_height = height_map.getHeight();
  47. m_tile_size = height_map.getTileSize();
  48. m_heightData = height_map.getHeightData();
  49. m_terrain_types = height_map.getTerrainTypes();
  50. m_biome_settings = biome_settings;
  51. m_noiseSeed = biome_settings.seed;
  52. m_stoneInstances.clear();
  53. m_stoneInstanceBuffer.reset();
  54. m_stoneInstanceCount = 0;
  55. m_stoneInstancesDirty = false;
  56. m_stoneParams.light_direction = QVector3D(0.35F, 0.8F, 0.45F);
  57. m_stoneParams.time = 0.0F;
  58. generate_stone_instances();
  59. }
  60. void StoneRenderer::submit(Renderer &renderer, ResourceManager *resources) {
  61. Q_UNUSED(resources);
  62. if (m_stoneInstanceCount > 0) {
  63. if (!m_stoneInstanceBuffer) {
  64. m_stoneInstanceBuffer = std::make_unique<Buffer>(Buffer::Type::Vertex);
  65. }
  66. if (m_stoneInstancesDirty && m_stoneInstanceBuffer) {
  67. m_stoneInstanceBuffer->set_data(m_stoneInstances, Buffer::Usage::Static);
  68. m_stoneInstancesDirty = false;
  69. }
  70. } else {
  71. m_stoneInstanceBuffer.reset();
  72. return;
  73. }
  74. renderer.stone_batch(m_stoneInstanceBuffer.get(), m_stoneInstanceCount,
  75. m_stoneParams);
  76. }
  77. void StoneRenderer::clear() {
  78. m_stoneInstances.clear();
  79. m_stoneInstanceBuffer.reset();
  80. m_stoneInstanceCount = 0;
  81. m_stoneInstancesDirty = false;
  82. }
  83. void StoneRenderer::generate_stone_instances() {
  84. QElapsedTimer timer;
  85. timer.start();
  86. m_stoneInstances.clear();
  87. if (m_width < 2 || m_height < 2 || m_heightData.empty()) {
  88. m_stoneInstanceCount = 0;
  89. m_stoneInstancesDirty = false;
  90. return;
  91. }
  92. const float tile_safe = std::max(0.001F, m_tile_size);
  93. SpawnTerrainCache terrain_cache;
  94. terrain_cache.build_from_height_map(m_heightData, m_terrain_types, m_width,
  95. m_height, m_tile_size);
  96. SpawnValidationConfig config = make_stone_spawn_config();
  97. config.grid_width = m_width;
  98. config.grid_height = m_height;
  99. config.tile_size = m_tile_size;
  100. config.edge_padding = m_biome_settings.spawn_edge_padding;
  101. SpawnValidator validator(terrain_cache, config);
  102. auto add_stone = [&](float gx, float gz, uint32_t &state) -> bool {
  103. if (!validator.can_spawn_at_grid(gx, gz)) {
  104. return false;
  105. }
  106. float const sgx = std::clamp(gx, 0.0F, float(m_width - 1));
  107. float const sgz = std::clamp(gz, 0.0F, float(m_height - 1));
  108. float world_x = 0.0F;
  109. float world_z = 0.0F;
  110. validator.grid_to_world(gx, gz, world_x, world_z);
  111. float const world_y = terrain_cache.sample_height_at(sgx, sgz);
  112. float const scale = remap(rand_01(state), 0.08F, 0.25F) * tile_safe;
  113. float const color_var = remap(rand_01(state), 0.0F, 1.0F);
  114. QVector3D const base_rock = m_biome_settings.rock_low;
  115. QVector3D const high_rock = m_biome_settings.rock_high;
  116. QVector3D color = base_rock * (1.0F - color_var) + high_rock * color_var;
  117. float const brown_mix = remap(rand_01(state), 0.0F, 0.4F);
  118. QVector3D const brown_tint(0.45F, 0.38F, 0.30F);
  119. color = color * (1.0F - brown_mix) + brown_tint * brown_mix;
  120. float const rotation = rand_01(state) * MathConstants::k_two_pi;
  121. StoneInstanceGpu instance;
  122. instance.pos_scale = QVector4D(world_x, world_y + 0.01F, world_z, scale);
  123. instance.color_rot = QVector4D(color.x(), color.y(), color.z(), rotation);
  124. m_stoneInstances.push_back(instance);
  125. return true;
  126. };
  127. const float stone_density = 0.15F;
  128. for (int z = 0; z < m_height; z += 2) {
  129. for (int x = 0; x < m_width; x += 2) {
  130. int const idx = z * m_width + x;
  131. Game::Map::TerrainType const terrain_type =
  132. terrain_cache.get_terrain_type_at(x, z);
  133. if (terrain_type != Game::Map::TerrainType::Flat) {
  134. continue;
  135. }
  136. float const slope = terrain_cache.get_slope_at(x, z);
  137. if (slope > 0.15F) {
  138. continue;
  139. }
  140. uint32_t state = hash_coords(
  141. x, z, m_noiseSeed ^ 0xABCDEF12U ^ static_cast<uint32_t>(idx));
  142. float world_x = 0.0F;
  143. float world_z = 0.0F;
  144. validator.grid_to_world(static_cast<float>(x), static_cast<float>(z),
  145. world_x, world_z);
  146. float const cluster_noise = value_noise(world_x * 0.03F, world_z * 0.03F,
  147. m_noiseSeed ^ 0x7F3A9B2CU);
  148. if (cluster_noise < 0.6F) {
  149. continue;
  150. }
  151. int stone_count = static_cast<int>(std::floor(stone_density));
  152. float const frac = stone_density - float(stone_count);
  153. if (rand_01(state) < frac) {
  154. stone_count += 1;
  155. }
  156. for (int i = 0; i < stone_count; ++i) {
  157. float const gx = float(x) + rand_01(state) * 2.0F;
  158. float const gz = float(z) + rand_01(state) * 2.0F;
  159. add_stone(gx, gz, state);
  160. }
  161. }
  162. }
  163. m_stoneInstanceCount = m_stoneInstances.size();
  164. m_stoneInstancesDirty = m_stoneInstanceCount > 0;
  165. }
  166. } // namespace Render::GL