terrain_renderer.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. #include "../../game/map/terrain.h"
  3. #include "../i_render_pass.h"
  4. #include "terrain_gpu.h"
  5. #include <QMatrix4x4>
  6. #include <QVector3D>
  7. #include <cmath>
  8. #include <cstdint>
  9. #include <limits>
  10. #include <memory>
  11. #include <vector>
  12. namespace Render::GL {
  13. class Buffer;
  14. class Renderer;
  15. class ResourceManager;
  16. class Mesh;
  17. class Texture;
  18. class TerrainRenderer : public IRenderPass {
  19. public:
  20. TerrainRenderer();
  21. ~TerrainRenderer() override;
  22. void configure(const Game::Map::TerrainHeightMap &height_map,
  23. const Game::Map::BiomeSettings &biome_settings);
  24. void submit(Renderer &renderer, ResourceManager *resources) override;
  25. void set_wireframe(bool enable) { m_wireframe = enable; }
  26. private:
  27. void build_meshes();
  28. [[nodiscard]] static auto section_for(Game::Map::TerrainType type) -> int;
  29. [[nodiscard]] auto get_terrain_color(Game::Map::TerrainType type,
  30. float height) const -> QVector3D;
  31. struct ChunkMesh {
  32. std::unique_ptr<Mesh> mesh;
  33. int min_x = 0;
  34. int max_x = 0;
  35. int min_z = 0;
  36. int max_z = 0;
  37. Game::Map::TerrainType type = Game::Map::TerrainType::Flat;
  38. static constexpr float kDefaultColorR = 0.3F;
  39. static constexpr float kDefaultColorG = 0.5F;
  40. static constexpr float kDefaultColorB = 0.3F;
  41. static auto default_color() -> QVector3D {
  42. return {kDefaultColorR, kDefaultColorG, kDefaultColorB};
  43. }
  44. QVector3D color = default_color();
  45. float average_height = 0.0F;
  46. float tint = 1.0F;
  47. TerrainChunkParams params;
  48. };
  49. struct ChunkVisibilityCacheEntry {
  50. std::uint64_t visibility_version =
  51. std::numeric_limits<std::uint64_t>::max();
  52. bool any_visible = true;
  53. };
  54. int m_width = 0;
  55. int m_height = 0;
  56. float m_tile_size = 1.0F;
  57. bool m_wireframe = false;
  58. std::vector<float> m_height_data;
  59. std::vector<Game::Map::TerrainType> m_terrain_types;
  60. std::vector<bool> m_hill_entrances;
  61. std::vector<ChunkMesh> m_chunks;
  62. std::vector<ChunkVisibilityCacheEntry> m_chunk_visibility_cache;
  63. Game::Map::BiomeSettings m_biome_settings;
  64. std::uint32_t m_noise_seed = 0U;
  65. };
  66. } // namespace Render::GL