scene_renderer.h 8.0 KB

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