scene_renderer.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #pragma once
  2. #include "draw_queue.h"
  3. #include "gl/backend.h"
  4. #include "gl/camera.h"
  5. #include "gl/mesh.h"
  6. #include "gl/resources.h"
  7. #include "gl/texture.h"
  8. #include "submitter.h"
  9. #include <atomic>
  10. #include <cstdint>
  11. #include <memory>
  12. #include <mutex>
  13. #include <optional>
  14. #include <unordered_set>
  15. #include <vector>
  16. namespace Engine::Core {
  17. class World;
  18. class Entity;
  19. class TransformComponent;
  20. class UnitComponent;
  21. } // namespace Engine::Core
  22. namespace Render::GL {
  23. class EntityRendererRegistry;
  24. }
  25. namespace Game::Systems {
  26. class ArrowSystem;
  27. }
  28. namespace Render::GL {
  29. class Backend;
  30. class Renderer : public ISubmitter {
  31. public:
  32. Renderer();
  33. ~Renderer() override;
  34. auto initialize() -> bool;
  35. void shutdown();
  36. void beginFrame();
  37. void endFrame();
  38. void setViewport(int width, int height);
  39. void setCamera(Camera *camera);
  40. void setClearColor(float r, float g, float b, float a = 1.0F);
  41. void updateAnimationTime(float deltaTime) { m_accumulatedTime += deltaTime; }
  42. auto getAnimationTime() const -> float { return m_accumulatedTime; }
  43. auto resources() const -> ResourceManager * {
  44. return m_backend ? m_backend->resources() : nullptr;
  45. }
  46. void setHoveredEntityId(unsigned int id) { m_hoveredEntityId = id; }
  47. void setLocalOwnerId(int owner_id) { m_localOwnerId = owner_id; }
  48. void setSelectedEntities(const std::vector<unsigned int> &ids) {
  49. m_selectedIds.clear();
  50. m_selectedIds.insert(ids.begin(), ids.end());
  51. }
  52. auto getMeshQuad() const -> Mesh * {
  53. return m_backend && (m_backend->resources() != nullptr)
  54. ? m_backend->resources()->quad()
  55. : nullptr;
  56. }
  57. auto getMeshPlane() const -> Mesh * {
  58. return m_backend && (m_backend->resources() != nullptr)
  59. ? m_backend->resources()->ground()
  60. : nullptr;
  61. }
  62. auto getMeshCube() const -> Mesh * {
  63. return m_backend && (m_backend->resources() != nullptr)
  64. ? m_backend->resources()->unit()
  65. : nullptr;
  66. }
  67. auto getWhiteTexture() const -> Texture * {
  68. return m_backend && (m_backend->resources() != nullptr)
  69. ? m_backend->resources()->white()
  70. : nullptr;
  71. }
  72. auto getShader(const QString &name) const -> Shader * {
  73. return m_backend ? m_backend->shader(name) : nullptr;
  74. }
  75. auto loadShader(const QString &name, const QString &vertPath,
  76. const QString &fragPath) -> Shader * {
  77. return m_backend ? m_backend->getOrLoadShader(name, vertPath, fragPath)
  78. : nullptr;
  79. }
  80. void setCurrentShader(Shader *shader) { m_currentShader = shader; }
  81. auto getCurrentShader() const -> Shader * { return m_currentShader; }
  82. struct GridParams {
  83. float cellSize = 1.0F;
  84. float thickness = 0.06F;
  85. QVector3D gridColor{0.15F, 0.18F, 0.15F};
  86. float extent = 50.0F;
  87. };
  88. void setGridParams(const GridParams &gp) { m_gridParams = gp; }
  89. auto gridParams() const -> const GridParams & { return m_gridParams; }
  90. void pause() { m_paused = true; }
  91. void resume() { m_paused = false; }
  92. auto isPaused() const -> bool { return m_paused; }
  93. void mesh(Mesh *mesh, const QMatrix4x4 &model, const QVector3D &color,
  94. Texture *texture = nullptr, float alpha = 1.0F,
  95. int materialId = 0) override;
  96. void cylinder(const QVector3D &start, const QVector3D &end, float radius,
  97. const QVector3D &color, float alpha = 1.0F) override;
  98. void selectionRing(const QMatrix4x4 &model, float alphaInner,
  99. float alphaOuter, const QVector3D &color) override;
  100. void grid(const QMatrix4x4 &model, const QVector3D &color, float cellSize,
  101. float thickness, float extent) override;
  102. void selectionSmoke(const QMatrix4x4 &model, const QVector3D &color,
  103. float baseAlpha = 0.15F) override;
  104. void terrainChunk(Mesh *mesh, const QMatrix4x4 &model,
  105. const TerrainChunkParams &params,
  106. std::uint16_t sortKey = 0x8000U, bool depthWrite = true,
  107. float depthBias = 0.0F);
  108. void renderWorld(Engine::Core::World *world);
  109. void lockWorldForModification() { m_worldMutex.lock(); }
  110. void unlockWorldForModification() { m_worldMutex.unlock(); }
  111. void fogBatch(const FogInstanceData *instances, std::size_t count);
  112. void grassBatch(Buffer *instanceBuffer, std::size_t instance_count,
  113. const GrassBatchParams &params);
  114. void stoneBatch(Buffer *instanceBuffer, std::size_t instance_count,
  115. const StoneBatchParams &params);
  116. void plantBatch(Buffer *instanceBuffer, std::size_t instance_count,
  117. const PlantBatchParams &params);
  118. void pineBatch(Buffer *instanceBuffer, std::size_t instance_count,
  119. const PineBatchParams &params);
  120. void oliveBatch(Buffer *instanceBuffer, std::size_t instance_count,
  121. const OliveBatchParams &params);
  122. void firecampBatch(Buffer *instanceBuffer, std::size_t instance_count,
  123. const FireCampBatchParams &params);
  124. private:
  125. void enqueueSelectionRing(Engine::Core::Entity *entity,
  126. Engine::Core::TransformComponent *transform,
  127. Engine::Core::UnitComponent *unit_comp,
  128. bool selected, bool hovered);
  129. Camera *m_camera = nullptr;
  130. std::shared_ptr<Backend> m_backend;
  131. DrawQueue m_queues[2];
  132. DrawQueue *m_activeQueue = nullptr;
  133. int m_fillQueueIndex = 0;
  134. int m_render_queueIndex = 1;
  135. std::unique_ptr<EntityRendererRegistry> m_entityRegistry;
  136. unsigned int m_hoveredEntityId = 0;
  137. std::unordered_set<unsigned int> m_selectedIds;
  138. int m_viewportWidth = 0;
  139. int m_viewportHeight = 0;
  140. GridParams m_gridParams;
  141. float m_accumulatedTime = 0.0F;
  142. std::atomic<bool> m_paused{false};
  143. std::mutex m_worldMutex;
  144. int m_localOwnerId = 1;
  145. QMatrix4x4 m_view_proj;
  146. Shader *m_currentShader = nullptr;
  147. };
  148. struct FrameScope {
  149. Renderer &r;
  150. FrameScope(Renderer &renderer) : r(renderer) { r.beginFrame(); }
  151. ~FrameScope() { r.endFrame(); }
  152. };
  153. } // namespace Render::GL