stone_renderer.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include "stone_renderer.h"
  2. #include "../../game/systems/building_collision_registry.h"
  3. #include "../gl/buffer.h"
  4. #include "../scene_renderer.h"
  5. #include <QDebug>
  6. #include <QElapsedTimer>
  7. #include <QVector2D>
  8. #include <algorithm>
  9. #include <array>
  10. #include <cmath>
  11. #include <optional>
  12. namespace {
  13. using std::uint32_t;
  14. inline uint32_t hashCoords(int x, int z, uint32_t salt = 0u) {
  15. uint32_t ux = static_cast<uint32_t>(x * 73856093);
  16. uint32_t uz = static_cast<uint32_t>(z * 19349663);
  17. return ux ^ uz ^ (salt * 83492791u);
  18. }
  19. inline float rand01(uint32_t &state) {
  20. state = state * 1664525u + 1013904223u;
  21. return static_cast<float>((state >> 8) & 0xFFFFFF) /
  22. static_cast<float>(0xFFFFFF);
  23. }
  24. inline float remap(float value, float minOut, float maxOut) {
  25. return minOut + (maxOut - minOut) * value;
  26. }
  27. inline float hashTo01(uint32_t h) {
  28. h ^= h >> 17;
  29. h *= 0xed5ad4bbu;
  30. h ^= h >> 11;
  31. h *= 0xac4c1b51u;
  32. h ^= h >> 15;
  33. h *= 0x31848babu;
  34. h ^= h >> 14;
  35. return (h & 0x00FFFFFFu) / float(0x01000000);
  36. }
  37. inline float valueNoise(float x, float z, uint32_t salt = 0u) {
  38. int x0 = int(std::floor(x)), z0 = int(std::floor(z));
  39. int x1 = x0 + 1, z1 = z0 + 1;
  40. float tx = x - float(x0), tz = z - float(z0);
  41. float n00 = hashTo01(hashCoords(x0, z0, salt));
  42. float n10 = hashTo01(hashCoords(x1, z0, salt));
  43. float n01 = hashTo01(hashCoords(x0, z1, salt));
  44. float n11 = hashTo01(hashCoords(x1, z1, salt));
  45. float nx0 = n00 * (1 - tx) + n10 * tx;
  46. float nx1 = n01 * (1 - tx) + n11 * tx;
  47. return nx0 * (1 - tz) + nx1 * tz;
  48. }
  49. } // namespace
  50. namespace Render::GL {
  51. StoneRenderer::StoneRenderer() = default;
  52. StoneRenderer::~StoneRenderer() = default;
  53. void StoneRenderer::configure(const Game::Map::TerrainHeightMap &heightMap,
  54. const Game::Map::BiomeSettings &biomeSettings) {
  55. m_width = heightMap.getWidth();
  56. m_height = heightMap.getHeight();
  57. m_tileSize = heightMap.getTileSize();
  58. m_heightData = heightMap.getHeightData();
  59. m_terrainTypes = heightMap.getTerrainTypes();
  60. m_biomeSettings = biomeSettings;
  61. m_noiseSeed = biomeSettings.seed;
  62. m_stoneInstances.clear();
  63. m_stoneInstanceBuffer.reset();
  64. m_stoneInstanceCount = 0;
  65. m_stoneInstancesDirty = false;
  66. m_stoneParams.lightDirection = QVector3D(0.35f, 0.8f, 0.45f);
  67. m_stoneParams.time = 0.0f;
  68. generateStoneInstances();
  69. }
  70. void StoneRenderer::submit(Renderer &renderer, ResourceManager *resources) {
  71. Q_UNUSED(resources);
  72. if (m_stoneInstanceCount > 0) {
  73. if (!m_stoneInstanceBuffer) {
  74. m_stoneInstanceBuffer = std::make_unique<Buffer>(Buffer::Type::Vertex);
  75. }
  76. if (m_stoneInstancesDirty && m_stoneInstanceBuffer) {
  77. m_stoneInstanceBuffer->setData(m_stoneInstances, Buffer::Usage::Static);
  78. m_stoneInstancesDirty = false;
  79. }
  80. } else {
  81. m_stoneInstanceBuffer.reset();
  82. return;
  83. }
  84. renderer.stoneBatch(m_stoneInstanceBuffer.get(), m_stoneInstanceCount,
  85. m_stoneParams);
  86. }
  87. void StoneRenderer::clear() {
  88. m_stoneInstances.clear();
  89. m_stoneInstanceBuffer.reset();
  90. m_stoneInstanceCount = 0;
  91. m_stoneInstancesDirty = false;
  92. }
  93. void StoneRenderer::generateStoneInstances() {
  94. QElapsedTimer timer;
  95. timer.start();
  96. m_stoneInstances.clear();
  97. if (m_width < 2 || m_height < 2 || m_heightData.empty()) {
  98. m_stoneInstanceCount = 0;
  99. m_stoneInstancesDirty = false;
  100. return;
  101. }
  102. const float halfWidth = m_width * 0.5f - 0.5f;
  103. const float halfHeight = m_height * 0.5f - 0.5f;
  104. const float tileSafe = std::max(0.001f, m_tileSize);
  105. const float edgePadding =
  106. std::clamp(m_biomeSettings.spawnEdgePadding, 0.0f, 0.5f);
  107. const float edgeMarginX = static_cast<float>(m_width) * edgePadding;
  108. const float edgeMarginZ = static_cast<float>(m_height) * edgePadding;
  109. std::vector<QVector3D> normals(m_width * m_height,
  110. QVector3D(0.0f, 1.0f, 0.0f));
  111. auto sampleHeightAt = [&](float gx, float gz) -> float {
  112. gx = std::clamp(gx, 0.0f, float(m_width - 1));
  113. gz = std::clamp(gz, 0.0f, float(m_height - 1));
  114. int x0 = int(std::floor(gx));
  115. int z0 = int(std::floor(gz));
  116. int x1 = std::min(x0 + 1, m_width - 1);
  117. int z1 = std::min(z0 + 1, m_height - 1);
  118. float tx = gx - float(x0);
  119. float tz = gz - float(z0);
  120. float h00 = m_heightData[z0 * m_width + x0];
  121. float h10 = m_heightData[z0 * m_width + x1];
  122. float h01 = m_heightData[z1 * m_width + x0];
  123. float h11 = m_heightData[z1 * m_width + x1];
  124. float h0 = h00 * (1.0f - tx) + h10 * tx;
  125. float h1 = h01 * (1.0f - tx) + h11 * tx;
  126. return h0 * (1.0f - tz) + h1 * tz;
  127. };
  128. for (int z = 0; z < m_height; ++z) {
  129. for (int x = 0; x < m_width; ++x) {
  130. int idx = z * m_width + x;
  131. float gx0 = std::clamp(float(x) - 1.0f, 0.0f, float(m_width - 1));
  132. float gx1 = std::clamp(float(x) + 1.0f, 0.0f, float(m_width - 1));
  133. float gz0 = std::clamp(float(z) - 1.0f, 0.0f, float(m_height - 1));
  134. float gz1 = std::clamp(float(z) + 1.0f, 0.0f, float(m_height - 1));
  135. float hL = sampleHeightAt(gx0, float(z));
  136. float hR = sampleHeightAt(gx1, float(z));
  137. float hD = sampleHeightAt(float(x), gz0);
  138. float hU = sampleHeightAt(float(x), gz1);
  139. QVector3D dx(2.0f * m_tileSize, hR - hL, 0.0f);
  140. QVector3D dz(0.0f, hU - hD, 2.0f * m_tileSize);
  141. QVector3D n = QVector3D::crossProduct(dz, dx);
  142. if (n.lengthSquared() > 0.0f) {
  143. n.normalize();
  144. } else {
  145. n = QVector3D(0, 1, 0);
  146. }
  147. normals[idx] = n;
  148. }
  149. }
  150. auto addStone = [&](float gx, float gz, uint32_t &state) -> bool {
  151. if (gx < edgeMarginX || gx > m_width - 1 - edgeMarginX ||
  152. gz < edgeMarginZ || gz > m_height - 1 - edgeMarginZ) {
  153. return false;
  154. }
  155. float sgx = std::clamp(gx, 0.0f, float(m_width - 1));
  156. float sgz = std::clamp(gz, 0.0f, float(m_height - 1));
  157. int ix = std::clamp(int(std::floor(sgx + 0.5f)), 0, m_width - 1);
  158. int iz = std::clamp(int(std::floor(sgz + 0.5f)), 0, m_height - 1);
  159. int normalIdx = iz * m_width + ix;
  160. if (m_terrainTypes[normalIdx] != Game::Map::TerrainType::Flat)
  161. return false;
  162. constexpr int kRiverMargin = 1;
  163. for (int dz = -kRiverMargin; dz <= kRiverMargin; ++dz) {
  164. for (int dx = -kRiverMargin; dx <= kRiverMargin; ++dx) {
  165. int nx = ix + dx;
  166. int nz = iz + dz;
  167. if (nx >= 0 && nx < m_width && nz >= 0 && nz < m_height) {
  168. int nIdx = nz * m_width + nx;
  169. if (m_terrainTypes[nIdx] == Game::Map::TerrainType::River)
  170. return false;
  171. }
  172. }
  173. }
  174. QVector3D normal = normals[normalIdx];
  175. float slope = 1.0f - std::clamp(normal.y(), 0.0f, 1.0f);
  176. if (slope > 0.15f)
  177. return false;
  178. float worldX = (gx - halfWidth) * m_tileSize;
  179. float worldZ = (gz - halfHeight) * m_tileSize;
  180. float worldY = sampleHeightAt(sgx, sgz);
  181. auto &buildingRegistry =
  182. Game::Systems::BuildingCollisionRegistry::instance();
  183. if (buildingRegistry.isPointInBuilding(worldX, worldZ)) {
  184. return false;
  185. }
  186. float scale = remap(rand01(state), 0.08f, 0.25f) * tileSafe;
  187. float colorVar = remap(rand01(state), 0.0f, 1.0f);
  188. QVector3D baseRock = m_biomeSettings.rockLow;
  189. QVector3D highRock = m_biomeSettings.rockHigh;
  190. QVector3D color = baseRock * (1.0f - colorVar) + highRock * colorVar;
  191. float brownMix = remap(rand01(state), 0.0f, 0.4f);
  192. QVector3D brownTint(0.45f, 0.38f, 0.30f);
  193. color = color * (1.0f - brownMix) + brownTint * brownMix;
  194. float rotation = rand01(state) * 6.2831853f;
  195. StoneInstanceGpu instance;
  196. instance.posScale = QVector4D(worldX, worldY + 0.01f, worldZ, scale);
  197. instance.colorRot = QVector4D(color.x(), color.y(), color.z(), rotation);
  198. m_stoneInstances.push_back(instance);
  199. return true;
  200. };
  201. const float stoneDensity = 0.15f;
  202. for (int z = 0; z < m_height; z += 2) {
  203. for (int x = 0; x < m_width; x += 2) {
  204. int idx = z * m_width + x;
  205. if (m_terrainTypes[idx] != Game::Map::TerrainType::Flat)
  206. continue;
  207. QVector3D normal = normals[idx];
  208. float slope = 1.0f - std::clamp(normal.y(), 0.0f, 1.0f);
  209. if (slope > 0.15f)
  210. continue;
  211. uint32_t state = hashCoords(
  212. x, z, m_noiseSeed ^ 0xABCDEF12u ^ static_cast<uint32_t>(idx));
  213. float worldX = (x - halfWidth) * m_tileSize;
  214. float worldZ = (z - halfHeight) * m_tileSize;
  215. float clusterNoise =
  216. valueNoise(worldX * 0.03f, worldZ * 0.03f, m_noiseSeed ^ 0x7F3A9B2Cu);
  217. if (clusterNoise < 0.6f)
  218. continue;
  219. int stoneCount = static_cast<int>(std::floor(stoneDensity));
  220. float frac = stoneDensity - float(stoneCount);
  221. if (rand01(state) < frac)
  222. stoneCount += 1;
  223. for (int i = 0; i < stoneCount; ++i) {
  224. float gx = float(x) + rand01(state) * 2.0f;
  225. float gz = float(z) + rand01(state) * 2.0f;
  226. addStone(gx, gz, state);
  227. }
  228. }
  229. }
  230. m_stoneInstanceCount = m_stoneInstances.size();
  231. m_stoneInstancesDirty = m_stoneInstanceCount > 0;
  232. }
  233. } // namespace Render::GL