scene_renderer.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. } // namespace Engine::Core
  20. namespace Render::GL {
  21. class EntityRendererRegistry;
  22. }
  23. namespace Game {
  24. namespace Systems {
  25. class ArrowSystem;
  26. }
  27. } // namespace Game
  28. namespace Render::GL {
  29. class Backend;
  30. class Renderer : public ISubmitter {
  31. public:
  32. Renderer();
  33. ~Renderer();
  34. bool initialize();
  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. float getAnimationTime() const { return m_accumulatedTime; }
  43. ResourceManager *resources() const {
  44. return m_backend ? m_backend->resources() : nullptr;
  45. }
  46. void setHoveredEntityId(unsigned int id) { m_hoveredEntityId = id; }
  47. void setLocalOwnerId(int ownerId) { m_localOwnerId = ownerId; }
  48. void setSelectedEntities(const std::vector<unsigned int> &ids) {
  49. m_selectedIds.clear();
  50. m_selectedIds.insert(ids.begin(), ids.end());
  51. }
  52. Mesh *getMeshQuad() const {
  53. return m_backend && m_backend->resources() ? m_backend->resources()->quad()
  54. : nullptr;
  55. }
  56. Mesh *getMeshPlane() const {
  57. return m_backend && m_backend->resources()
  58. ? m_backend->resources()->ground()
  59. : nullptr;
  60. }
  61. Mesh *getMeshCube() const {
  62. return m_backend && m_backend->resources() ? m_backend->resources()->unit()
  63. : nullptr;
  64. }
  65. Texture *getWhiteTexture() const {
  66. return m_backend && m_backend->resources() ? m_backend->resources()->white()
  67. : nullptr;
  68. }
  69. Shader *getShader(const QString &name) const {
  70. return m_backend ? m_backend->shader(name) : nullptr;
  71. }
  72. Shader *loadShader(const QString &name, const QString &vertPath,
  73. const QString &fragPath) {
  74. return m_backend ? m_backend->getOrLoadShader(name, vertPath, fragPath)
  75. : nullptr;
  76. }
  77. void setCurrentShader(Shader *shader) { m_currentShader = shader; }
  78. Shader *getCurrentShader() const { return m_currentShader; }
  79. struct GridParams {
  80. float cellSize = 1.0f;
  81. float thickness = 0.06f;
  82. QVector3D gridColor{0.15f, 0.18f, 0.15f};
  83. float extent = 50.0f;
  84. };
  85. void setGridParams(const GridParams &gp) { m_gridParams = gp; }
  86. const GridParams &gridParams() const { return m_gridParams; }
  87. void pause() { m_paused = true; }
  88. void resume() { m_paused = false; }
  89. bool isPaused() const { return m_paused; }
  90. void mesh(Mesh *mesh, const QMatrix4x4 &model, const QVector3D &color,
  91. Texture *texture = nullptr, float alpha = 1.0f) override;
  92. void cylinder(const QVector3D &start, const QVector3D &end, float radius,
  93. const QVector3D &color, float alpha = 1.0f) override;
  94. void selectionRing(const QMatrix4x4 &model, float alphaInner,
  95. float alphaOuter, const QVector3D &color) override;
  96. void grid(const QMatrix4x4 &model, const QVector3D &color, float cellSize,
  97. float thickness, float extent) override;
  98. void selectionSmoke(const QMatrix4x4 &model, const QVector3D &color,
  99. float baseAlpha = 0.15f) override;
  100. void terrainChunk(Mesh *mesh, const QMatrix4x4 &model,
  101. const TerrainChunkParams &params,
  102. std::uint16_t sortKey = 0x8000u, bool depthWrite = true,
  103. float depthBias = 0.0f);
  104. void renderWorld(Engine::Core::World *world);
  105. void lockWorldForModification() { m_worldMutex.lock(); }
  106. void unlockWorldForModification() { m_worldMutex.unlock(); }
  107. void fogBatch(const FogInstanceData *instances, std::size_t count);
  108. void grassBatch(Buffer *instanceBuffer, std::size_t instanceCount,
  109. const GrassBatchParams &params);
  110. void stoneBatch(Buffer *instanceBuffer, std::size_t instanceCount,
  111. const StoneBatchParams &params);
  112. void plantBatch(Buffer *instanceBuffer, std::size_t instanceCount,
  113. const PlantBatchParams &params);
  114. void pineBatch(Buffer *instanceBuffer, std::size_t instanceCount,
  115. const PineBatchParams &params);
  116. private:
  117. Camera *m_camera = nullptr;
  118. std::shared_ptr<Backend> m_backend;
  119. DrawQueue m_queues[2];
  120. DrawQueue *m_activeQueue = nullptr;
  121. int m_fillQueueIndex = 0;
  122. int m_renderQueueIndex = 1;
  123. std::unique_ptr<EntityRendererRegistry> m_entityRegistry;
  124. unsigned int m_hoveredEntityId = 0;
  125. std::unordered_set<unsigned int> m_selectedIds;
  126. int m_viewportWidth = 0;
  127. int m_viewportHeight = 0;
  128. GridParams m_gridParams;
  129. float m_accumulatedTime = 0.0f;
  130. std::atomic<bool> m_paused{false};
  131. std::mutex m_worldMutex;
  132. int m_localOwnerId = 1;
  133. QMatrix4x4 m_viewProj;
  134. Shader *m_currentShader = nullptr;
  135. };
  136. struct FrameScope {
  137. Renderer &r;
  138. FrameScope(Renderer &renderer) : r(renderer) { r.beginFrame(); }
  139. ~FrameScope() { r.endFrame(); }
  140. };
  141. } // namespace Render::GL