plant_renderer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "plant_renderer.h"
  2. #include "../../game/systems/building_collision_registry.h"
  3. #include "../gl/buffer.h"
  4. #include "../scene_renderer.h"
  5. #include <QVector2D>
  6. #include <algorithm>
  7. #include <array>
  8. #include <cmath>
  9. #include <optional>
  10. namespace {
  11. using std::uint32_t;
  12. inline uint32_t hashCoords(int x, int z, uint32_t salt = 0u) {
  13. uint32_t ux = static_cast<uint32_t>(x * 73856093);
  14. uint32_t uz = static_cast<uint32_t>(z * 19349663);
  15. return ux ^ uz ^ (salt * 83492791u);
  16. }
  17. inline float rand01(uint32_t &state) {
  18. state = state * 1664525u + 1013904223u;
  19. return static_cast<float>((state >> 8) & 0xFFFFFF) /
  20. static_cast<float>(0xFFFFFF);
  21. }
  22. inline float remap(float value, float minOut, float maxOut) {
  23. return minOut + (maxOut - minOut) * value;
  24. }
  25. inline float hashTo01(uint32_t h) {
  26. h ^= h >> 17;
  27. h *= 0xed5ad4bbu;
  28. h ^= h >> 11;
  29. h *= 0xac4c1b51u;
  30. h ^= h >> 15;
  31. h *= 0x31848babu;
  32. h ^= h >> 14;
  33. return (h & 0x00FFFFFFu) / float(0x01000000);
  34. }
  35. inline float valueNoise(float x, float z, uint32_t salt = 0u) {
  36. int x0 = int(std::floor(x)), z0 = int(std::floor(z));
  37. int x1 = x0 + 1, z1 = z0 + 1;
  38. float tx = x - float(x0), tz = z - float(z0);
  39. float n00 = hashTo01(hashCoords(x0, z0, salt));
  40. float n10 = hashTo01(hashCoords(x1, z0, salt));
  41. float n01 = hashTo01(hashCoords(x0, z1, salt));
  42. float n11 = hashTo01(hashCoords(x1, z1, salt));
  43. float nx0 = n00 * (1 - tx) + n10 * tx;
  44. float nx1 = n01 * (1 - tx) + n11 * tx;
  45. return nx0 * (1 - tz) + nx1 * tz;
  46. }
  47. } // namespace
  48. namespace Render::GL {
  49. PlantRenderer::PlantRenderer() = default;
  50. PlantRenderer::~PlantRenderer() = default;
  51. void PlantRenderer::configure(const Game::Map::TerrainHeightMap &heightMap,
  52. const Game::Map::BiomeSettings &biomeSettings) {
  53. m_width = heightMap.getWidth();
  54. m_height = heightMap.getHeight();
  55. m_tileSize = heightMap.getTileSize();
  56. m_heightData = heightMap.getHeightData();
  57. m_terrainTypes = heightMap.getTerrainTypes();
  58. m_biomeSettings = biomeSettings;
  59. m_noiseSeed = biomeSettings.seed;
  60. m_plantInstances.clear();
  61. m_plantInstanceBuffer.reset();
  62. m_plantInstanceCount = 0;
  63. m_plantInstancesDirty = false;
  64. m_plantParams.lightDirection = QVector3D(0.35f, 0.8f, 0.45f);
  65. m_plantParams.time = 0.0f;
  66. m_plantParams.windStrength = m_biomeSettings.swayStrength;
  67. m_plantParams.windSpeed = m_biomeSettings.swaySpeed;
  68. generatePlantInstances();
  69. }
  70. void PlantRenderer::submit(Renderer &renderer, ResourceManager *resources) {
  71. (void)resources;
  72. m_plantInstanceCount = static_cast<uint32_t>(m_plantInstances.size());
  73. if (m_plantInstanceCount > 0) {
  74. if (!m_plantInstanceBuffer) {
  75. m_plantInstanceBuffer = std::make_unique<Buffer>(Buffer::Type::Vertex);
  76. }
  77. if (m_plantInstancesDirty && m_plantInstanceBuffer) {
  78. m_plantInstanceBuffer->setData(m_plantInstances, Buffer::Usage::Static);
  79. m_plantInstancesDirty = false;
  80. }
  81. } else {
  82. m_plantInstanceBuffer.reset();
  83. return;
  84. }
  85. if (m_plantInstanceBuffer && m_plantInstanceCount > 0) {
  86. PlantBatchParams params = m_plantParams;
  87. params.time = renderer.getAnimationTime();
  88. renderer.plantBatch(m_plantInstanceBuffer.get(), m_plantInstanceCount,
  89. params);
  90. }
  91. }
  92. void PlantRenderer::clear() {
  93. m_plantInstances.clear();
  94. m_plantInstanceBuffer.reset();
  95. m_plantInstanceCount = 0;
  96. m_plantInstancesDirty = false;
  97. }
  98. void PlantRenderer::generatePlantInstances() {
  99. m_plantInstances.clear();
  100. if (m_width < 2 || m_height < 2 || m_heightData.empty()) {
  101. m_plantInstanceCount = 0;
  102. m_plantInstancesDirty = false;
  103. return;
  104. }
  105. const float plantDensity =
  106. std::clamp(m_biomeSettings.plantDensity, 0.0f, 2.0f);
  107. if (plantDensity < 0.01f) {
  108. m_plantInstanceCount = 0;
  109. m_plantInstancesDirty = false;
  110. return;
  111. }
  112. const float halfWidth = m_width * 0.5f - 0.5f;
  113. const float halfHeight = m_height * 0.5f - 0.5f;
  114. const float tileSafe = std::max(0.001f, m_tileSize);
  115. const float edgePadding =
  116. std::clamp(m_biomeSettings.spawnEdgePadding, 0.0f, 0.5f);
  117. const float edgeMarginX = static_cast<float>(m_width) * edgePadding;
  118. const float edgeMarginZ = static_cast<float>(m_height) * edgePadding;
  119. std::vector<QVector3D> normals(m_width * m_height,
  120. QVector3D(0.0f, 1.0f, 0.0f));
  121. auto sampleHeightAt = [&](float gx, float gz) -> float {
  122. gx = std::clamp(gx, 0.0f, float(m_width - 1));
  123. gz = std::clamp(gz, 0.0f, float(m_height - 1));
  124. int x0 = int(std::floor(gx));
  125. int z0 = int(std::floor(gz));
  126. int x1 = std::min(x0 + 1, m_width - 1);
  127. int z1 = std::min(z0 + 1, m_height - 1);
  128. float tx = gx - float(x0);
  129. float tz = gz - float(z0);
  130. float h00 = m_heightData[z0 * m_width + x0];
  131. float h10 = m_heightData[z0 * m_width + x1];
  132. float h01 = m_heightData[z1 * m_width + x0];
  133. float h11 = m_heightData[z1 * m_width + x1];
  134. float h0 = h00 * (1.0f - tx) + h10 * tx;
  135. float h1 = h01 * (1.0f - tx) + h11 * tx;
  136. return h0 * (1.0f - tz) + h1 * tz;
  137. };
  138. for (int z = 0; z < m_height; ++z) {
  139. for (int x = 0; x < m_width; ++x) {
  140. int idx = z * m_width + x;
  141. float gx0 = std::clamp(float(x) - 1.0f, 0.0f, float(m_width - 1));
  142. float gx1 = std::clamp(float(x) + 1.0f, 0.0f, float(m_width - 1));
  143. float gz0 = std::clamp(float(z) - 1.0f, 0.0f, float(m_height - 1));
  144. float gz1 = std::clamp(float(z) + 1.0f, 0.0f, float(m_height - 1));
  145. float hL = sampleHeightAt(gx0, float(z));
  146. float hR = sampleHeightAt(gx1, float(z));
  147. float hD = sampleHeightAt(float(x), gz0);
  148. float hU = sampleHeightAt(float(x), gz1);
  149. QVector3D dx(2.0f * m_tileSize, hR - hL, 0.0f);
  150. QVector3D dz(0.0f, hU - hD, 2.0f * m_tileSize);
  151. QVector3D n = QVector3D::crossProduct(dz, dx);
  152. if (n.lengthSquared() > 0.0f) {
  153. n.normalize();
  154. } else {
  155. n = QVector3D(0, 1, 0);
  156. }
  157. normals[idx] = n;
  158. }
  159. }
  160. auto addPlant = [&](float gx, float gz, uint32_t &state) -> bool {
  161. if (gx < edgeMarginX || gx > m_width - 1 - edgeMarginX ||
  162. gz < edgeMarginZ || gz > m_height - 1 - edgeMarginZ) {
  163. return false;
  164. }
  165. float sgx = std::clamp(gx, 0.0f, float(m_width - 1));
  166. float sgz = std::clamp(gz, 0.0f, float(m_height - 1));
  167. int ix = std::clamp(int(std::floor(sgx + 0.5f)), 0, m_width - 1);
  168. int iz = std::clamp(int(std::floor(sgz + 0.5f)), 0, m_height - 1);
  169. int normalIdx = iz * m_width + ix;
  170. if (m_terrainTypes[normalIdx] == Game::Map::TerrainType::Mountain)
  171. return false;
  172. if (m_terrainTypes[normalIdx] == Game::Map::TerrainType::River)
  173. return false;
  174. constexpr int kRiverMargin = 1;
  175. for (int dz = -kRiverMargin; dz <= kRiverMargin; ++dz) {
  176. for (int dx = -kRiverMargin; dx <= kRiverMargin; ++dx) {
  177. if (dx == 0 && dz == 0)
  178. continue;
  179. int nx = ix + dx;
  180. int nz = iz + dz;
  181. if (nx >= 0 && nx < m_width && nz >= 0 && nz < m_height) {
  182. int nIdx = nz * m_width + nx;
  183. if (m_terrainTypes[nIdx] == Game::Map::TerrainType::River)
  184. return false;
  185. }
  186. }
  187. }
  188. QVector3D normal = normals[normalIdx];
  189. float slope = 1.0f - std::clamp(normal.y(), 0.0f, 1.0f);
  190. if (slope > 0.65f)
  191. return false;
  192. float worldX = (gx - halfWidth) * m_tileSize;
  193. float worldZ = (gz - halfHeight) * m_tileSize;
  194. float worldY = sampleHeightAt(sgx, sgz);
  195. auto &buildingRegistry =
  196. Game::Systems::BuildingCollisionRegistry::instance();
  197. if (buildingRegistry.isPointInBuilding(worldX, worldZ)) {
  198. return false;
  199. }
  200. float scale = remap(rand01(state), 0.30f, 0.80f) * tileSafe;
  201. float plantType = std::floor(rand01(state) * 4.0f);
  202. float colorVar = remap(rand01(state), 0.0f, 1.0f);
  203. QVector3D baseColor = m_biomeSettings.grassPrimary * 0.7f;
  204. QVector3D varColor = m_biomeSettings.grassSecondary * 0.8f;
  205. QVector3D tintColor = baseColor * (1.0f - colorVar) + varColor * colorVar;
  206. float brownMix = remap(rand01(state), 0.15f, 0.35f);
  207. QVector3D brownTint(0.55f, 0.50f, 0.35f);
  208. tintColor = tintColor * (1.0f - brownMix) + brownTint * brownMix;
  209. float swayPhase = rand01(state) * 6.2831853f;
  210. float swayStrength = remap(rand01(state), 0.6f, 1.2f);
  211. float swaySpeed = remap(rand01(state), 0.8f, 1.3f);
  212. float rotation = rand01(state) * 6.2831853f;
  213. PlantInstanceGpu instance;
  214. instance.posScale = QVector4D(worldX, worldY + 0.05f, worldZ, scale);
  215. instance.colorSway =
  216. QVector4D(tintColor.x(), tintColor.y(), tintColor.z(), swayPhase);
  217. instance.typeParams =
  218. QVector4D(plantType, rotation, swayStrength, swaySpeed);
  219. m_plantInstances.push_back(instance);
  220. return true;
  221. };
  222. int cellsChecked = 0;
  223. int cellsPassed = 0;
  224. int plantsAdded = 0;
  225. for (int z = 0; z < m_height; z += 3) {
  226. for (int x = 0; x < m_width; x += 3) {
  227. cellsChecked++;
  228. int idx = z * m_width + x;
  229. if (m_terrainTypes[idx] == Game::Map::TerrainType::Mountain ||
  230. m_terrainTypes[idx] == Game::Map::TerrainType::River)
  231. continue;
  232. QVector3D normal = normals[idx];
  233. float slope = 1.0f - std::clamp(normal.y(), 0.0f, 1.0f);
  234. if (slope > 0.65f)
  235. continue;
  236. uint32_t state = hashCoords(
  237. x, z, m_noiseSeed ^ 0x8F3C5A7Eu ^ static_cast<uint32_t>(idx));
  238. float worldX = (x - halfWidth) * m_tileSize;
  239. float worldZ = (z - halfHeight) * m_tileSize;
  240. float clusterNoise =
  241. valueNoise(worldX * 0.05f, worldZ * 0.05f, m_noiseSeed ^ 0x4B9D2F1Au);
  242. if (clusterNoise < 0.45f)
  243. continue;
  244. cellsPassed++;
  245. float densityMult = 1.0f;
  246. if (m_terrainTypes[idx] == Game::Map::TerrainType::Hill) {
  247. densityMult = 0.6f;
  248. }
  249. float effectiveDensity = plantDensity * densityMult * 2.0f;
  250. int plantCount = static_cast<int>(std::floor(effectiveDensity));
  251. float frac = effectiveDensity - float(plantCount);
  252. if (rand01(state) < frac)
  253. plantCount += 1;
  254. for (int i = 0; i < plantCount; ++i) {
  255. float gx = float(x) + rand01(state) * 3.0f;
  256. float gz = float(z) + rand01(state) * 3.0f;
  257. if (addPlant(gx, gz, state)) {
  258. plantsAdded++;
  259. }
  260. }
  261. }
  262. }
  263. m_plantInstanceCount = m_plantInstances.size();
  264. m_plantInstancesDirty = m_plantInstanceCount > 0;
  265. }
  266. } // namespace Render::GL