ground_renderer.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "ground_renderer.h"
  2. #include "../draw_queue.h"
  3. #include "../gl/resources.h"
  4. #include "../scene_renderer.h"
  5. #include <algorithm>
  6. #include <cmath>
  7. namespace Render {
  8. namespace GL {
  9. namespace {
  10. const QMatrix4x4 kIdentityMatrix;
  11. inline QVector3D saturate(const QVector3D &c) {
  12. return QVector3D(std::clamp(c.x(), 0.0f, 1.0f), std::clamp(c.y(), 0.0f, 1.0f),
  13. std::clamp(c.z(), 0.0f, 1.0f));
  14. }
  15. } // namespace
  16. static QVector3D clamp01(const QVector3D &c) { return saturate(c); }
  17. void GroundRenderer::recomputeModel() {
  18. QMatrix4x4 newModel = kIdentityMatrix;
  19. newModel.translate(0.0f, -0.5f, 0.0f);
  20. if (m_width > 0 && m_height > 0) {
  21. const float scaleX = std::sqrt(float(m_width)) * m_tileSize;
  22. const float scaleZ = std::sqrt(float(m_height)) * m_tileSize;
  23. newModel.scale(scaleX, 1.0f, scaleZ);
  24. } else {
  25. newModel.scale(m_extent, 1.0f, m_extent);
  26. }
  27. if (newModel != m_model) {
  28. m_model = newModel;
  29. m_modelDirty = true;
  30. }
  31. }
  32. void GroundRenderer::updateNoiseOffset() {
  33. const float spanX = (m_width > 0 ? float(m_width) * m_tileSize : m_extent);
  34. const float spanZ = (m_height > 0 ? float(m_height) * m_tileSize : m_extent);
  35. const float seed = static_cast<float>(m_biomeSettings.seed % 1024u);
  36. QVector2D newOffset;
  37. newOffset.setX(spanX * 0.37f + seed * 0.21f);
  38. newOffset.setY(spanZ * 0.43f + seed * 0.17f);
  39. m_noiseAngle = std::fmod(seed * 0.6180339887f, 1.0f) * 6.28318530718f;
  40. if (newOffset != m_noiseOffset) {
  41. m_noiseOffset = newOffset;
  42. invalidateParamsCache();
  43. }
  44. }
  45. TerrainChunkParams GroundRenderer::buildParams() const {
  46. if (m_cachedParamsValid) {
  47. return m_cachedParams;
  48. }
  49. TerrainChunkParams params;
  50. const QVector3D primary = m_biomeSettings.grassPrimary * 0.97f;
  51. const QVector3D secondary = m_biomeSettings.grassSecondary * 0.93f;
  52. const QVector3D dry = m_biomeSettings.grassDry * 0.90f;
  53. const QVector3D soil = m_biomeSettings.soilColor * 0.68f;
  54. params.grassPrimary = saturate(primary);
  55. params.grassSecondary = saturate(secondary);
  56. params.grassDry = saturate(dry);
  57. params.soilColor = saturate(soil);
  58. params.rockLow = saturate(m_biomeSettings.rockLow);
  59. params.rockHigh = saturate(m_biomeSettings.rockHigh);
  60. params.tint = QVector3D(0.96f, 0.98f, 0.96f);
  61. params.tileSize = std::max(0.25f, m_tileSize);
  62. params.macroNoiseScale =
  63. std::max(0.012f, m_biomeSettings.terrainMacroNoiseScale * 0.60f);
  64. params.detailNoiseScale =
  65. std::max(0.045f, m_biomeSettings.terrainDetailNoiseScale * 0.75f);
  66. params.slopeRockThreshold = 0.72f;
  67. params.slopeRockSharpness = 4.5f;
  68. params.soilBlendHeight = -0.65f;
  69. params.soilBlendSharpness = 2.6f;
  70. params.noiseOffset = m_noiseOffset;
  71. params.noiseAngle = m_noiseAngle;
  72. const float targetAmp =
  73. std::clamp(m_biomeSettings.heightNoiseAmplitude * 0.22f, 0.10f, 0.20f);
  74. params.heightNoiseStrength = targetAmp;
  75. params.heightNoiseFrequency =
  76. std::max(0.6f, m_biomeSettings.heightNoiseFrequency * 1.05f);
  77. params.microBumpAmp = 0.07f;
  78. params.microBumpFreq = 2.2f;
  79. params.microNormalWeight = 0.65f;
  80. params.albedoJitter = 0.05f;
  81. params.ambientBoost = m_biomeSettings.terrainAmbientBoost * 0.85f;
  82. params.rockDetailStrength = m_biomeSettings.terrainRockDetailStrength * 0.18f;
  83. QVector3D L(0.35f, 0.85f, 0.42f);
  84. params.lightDirection = L.normalized();
  85. params.isGroundPlane = true;
  86. m_cachedParams = params;
  87. m_cachedParamsValid = true;
  88. return params;
  89. }
  90. void GroundRenderer::submit(Renderer &renderer, ResourceManager *resources) {
  91. if (!resources) {
  92. return;
  93. }
  94. if (m_hasBiome) {
  95. Mesh *plane = resources->ground();
  96. if (plane) {
  97. const TerrainChunkParams params = buildParams();
  98. const bool modelChanged =
  99. m_modelDirty || (m_lastSubmittedModel != m_model);
  100. const bool stateChanged = (m_lastSubmittedStateVersion != m_stateVersion);
  101. (void)modelChanged;
  102. (void)stateChanged;
  103. renderer.terrainChunk(plane, m_model, params, 0x0040u, true, +0.0008f);
  104. m_lastSubmittedModel = m_model;
  105. m_modelDirty = false;
  106. m_lastSubmittedStateVersion = m_stateVersion;
  107. return;
  108. }
  109. }
  110. const float cell = (m_tileSize > 0.0f ? m_tileSize : 1.0f);
  111. const float extent = (m_width > 0 && m_height > 0)
  112. ? std::max(m_width, m_height) * m_tileSize * 0.5f
  113. : m_extent;
  114. renderer.grid(m_model, m_color, cell, 0.06f, extent);
  115. }
  116. } // namespace GL
  117. } // namespace Render