scene_renderer.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 begin_frame();
  37. void end_frame();
  38. void set_viewport(int width, int height);
  39. void set_camera(Camera *camera);
  40. void set_clear_color(float r, float g, float b, float a = 1.0F);
  41. auto camera() const -> Camera * { return m_camera; }
  42. auto backend() -> Backend * { return m_backend.get(); }
  43. void update_animation_time(float delta_time) {
  44. m_accumulated_time += delta_time;
  45. }
  46. auto get_animation_time() const -> float { return m_accumulated_time; }
  47. auto resources() const -> ResourceManager * {
  48. return m_backend ? m_backend->resources() : nullptr;
  49. }
  50. void set_hovered_entity_id(unsigned int id) { m_hovered_entity_id = id; }
  51. void set_local_owner_id(int owner_id) { m_local_owner_id = owner_id; }
  52. void set_selected_entities(const std::vector<unsigned int> &ids) {
  53. m_selected_ids.clear();
  54. m_selected_ids.insert(ids.begin(), ids.end());
  55. }
  56. auto get_mesh_quad() const -> Mesh * {
  57. return m_backend && (m_backend->resources() != nullptr)
  58. ? m_backend->resources()->quad()
  59. : nullptr;
  60. }
  61. auto get_mesh_plane() const -> Mesh * {
  62. return m_backend && (m_backend->resources() != nullptr)
  63. ? m_backend->resources()->ground()
  64. : nullptr;
  65. }
  66. auto get_mesh_cube() const -> Mesh * {
  67. return m_backend && (m_backend->resources() != nullptr)
  68. ? m_backend->resources()->unit()
  69. : nullptr;
  70. }
  71. auto get_white_texture() const -> Texture * {
  72. return m_backend && (m_backend->resources() != nullptr)
  73. ? m_backend->resources()->white()
  74. : nullptr;
  75. }
  76. auto get_shader(const QString &name) const -> Shader * {
  77. return m_backend ? m_backend->shader(name) : nullptr;
  78. }
  79. auto load_shader(const QString &name, const QString &vert_path,
  80. const QString &frag_path) -> Shader * {
  81. return m_backend ? m_backend->get_or_load_shader(name, vert_path, frag_path)
  82. : nullptr;
  83. }
  84. void set_current_shader(Shader *shader) { m_current_shader = shader; }
  85. auto get_current_shader() const -> Shader * { return m_current_shader; }
  86. struct GridParams {
  87. float cell_size = 1.0F;
  88. float thickness = 0.06F;
  89. QVector3D grid_color{0.15F, 0.18F, 0.15F};
  90. float extent = 50.0F;
  91. };
  92. void set_grid_params(const GridParams &gp) { m_grid_params = gp; }
  93. auto grid_params() const -> const GridParams & { return m_grid_params; }
  94. void pause() { m_paused = true; }
  95. void resume() { m_paused = false; }
  96. auto is_paused() const -> bool { return m_paused; }
  97. void mesh(Mesh *mesh, const QMatrix4x4 &model, const QVector3D &color,
  98. Texture *texture = nullptr, float alpha = 1.0F,
  99. int material_id = 0) override;
  100. void cylinder(const QVector3D &start, const QVector3D &end, float radius,
  101. const QVector3D &color, float alpha = 1.0F) override;
  102. void selection_ring(const QMatrix4x4 &model, float alpha_inner,
  103. float alpha_outer, const QVector3D &color) override;
  104. void grid(const QMatrix4x4 &model, const QVector3D &color, float cell_size,
  105. float thickness, float extent) override;
  106. void selection_smoke(const QMatrix4x4 &model, const QVector3D &color,
  107. float base_alpha = 0.15F) override;
  108. void healing_beam(const QVector3D &start, const QVector3D &end,
  109. const QVector3D &color, float progress, float beam_width,
  110. float intensity, float time) override;
  111. void healer_aura(const QVector3D &position, const QVector3D &color,
  112. float radius, float intensity, float time) override;
  113. void combat_dust(const QVector3D &position, const QVector3D &color,
  114. float radius, float intensity, float time) override;
  115. void mode_indicator(const QMatrix4x4 &model, int mode_type,
  116. const QVector3D &color, float alpha = 1.0F) override;
  117. void terrain_chunk(Mesh *mesh, const QMatrix4x4 &model,
  118. const TerrainChunkParams &params,
  119. std::uint16_t sort_key = 0x8000U, bool depth_write = true,
  120. float depth_bias = 0.0F);
  121. void render_world(Engine::Core::World *world);
  122. void lock_world_for_modification() { m_world_mutex.lock(); }
  123. void unlock_world_for_modification() { m_world_mutex.unlock(); }
  124. void fog_batch(const FogInstanceData *instances, std::size_t count);
  125. void grass_batch(Buffer *instance_buffer, std::size_t instance_count,
  126. const GrassBatchParams &params);
  127. void stone_batch(Buffer *instance_buffer, std::size_t instance_count,
  128. const StoneBatchParams &params);
  129. void plant_batch(Buffer *instance_buffer, std::size_t instance_count,
  130. const PlantBatchParams &params);
  131. void pine_batch(Buffer *instance_buffer, std::size_t instance_count,
  132. const PineBatchParams &params);
  133. void olive_batch(Buffer *instance_buffer, std::size_t instance_count,
  134. const OliveBatchParams &params);
  135. void firecamp_batch(Buffer *instance_buffer, std::size_t instance_count,
  136. const FireCampBatchParams &params);
  137. void rain_batch(Buffer *instance_buffer, std::size_t instance_count,
  138. const RainBatchParams &params);
  139. private:
  140. void enqueue_selection_ring(Engine::Core::Entity *entity,
  141. Engine::Core::TransformComponent *transform,
  142. Engine::Core::UnitComponent *unit_comp,
  143. bool selected, bool hovered);
  144. void enqueue_mode_indicator(Engine::Core::Entity *entity,
  145. Engine::Core::TransformComponent *transform,
  146. Engine::Core::UnitComponent *unit_comp);
  147. Camera *m_camera = nullptr;
  148. std::shared_ptr<Backend> m_backend;
  149. DrawQueue m_queues[2];
  150. DrawQueue *m_active_queue = nullptr;
  151. int m_fill_queue_index = 0;
  152. int m_render_queue_index = 1;
  153. std::unique_ptr<EntityRendererRegistry> m_entity_registry;
  154. unsigned int m_hovered_entity_id = 0;
  155. std::unordered_set<unsigned int> m_selected_ids;
  156. int m_viewport_width = 0;
  157. int m_viewport_height = 0;
  158. GridParams m_grid_params;
  159. float m_accumulated_time = 0.0F;
  160. std::atomic<bool> m_paused{false};
  161. std::mutex m_world_mutex;
  162. int m_local_owner_id = 1;
  163. QMatrix4x4 m_view_proj;
  164. Shader *m_current_shader = nullptr;
  165. };
  166. struct FrameScope {
  167. Renderer &r;
  168. FrameScope(Renderer &renderer) : r(renderer) { r.begin_frame(); }
  169. ~FrameScope() { r.end_frame(); }
  170. };
  171. } // namespace Render::GL