scene_renderer.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #include "scene_renderer.h"
  2. #include "../game/map/visibility_service.h"
  3. #include "entity/registry.h"
  4. #include "game/core/component.h"
  5. #include "game/core/world.h"
  6. #include "gl/backend.h"
  7. #include "gl/camera.h"
  8. #include "gl/primitives.h"
  9. #include "gl/resources.h"
  10. #include <QDebug>
  11. #include <algorithm>
  12. #include <cmath>
  13. namespace Render::GL {
  14. namespace {
  15. const QVector3D kAxisX(1.0f, 0.0f, 0.0f);
  16. const QVector3D kAxisY(0.0f, 1.0f, 0.0f);
  17. const QVector3D kAxisZ(0.0f, 0.0f, 1.0f);
  18. } // namespace
  19. Renderer::Renderer() { m_activeQueue = &m_queues[m_fillQueueIndex]; }
  20. Renderer::~Renderer() { shutdown(); }
  21. bool Renderer::initialize() {
  22. if (!m_backend)
  23. m_backend = std::make_shared<Backend>();
  24. m_backend->initialize();
  25. m_entityRegistry = std::make_unique<EntityRendererRegistry>();
  26. registerBuiltInEntityRenderers(*m_entityRegistry);
  27. return true;
  28. }
  29. void Renderer::shutdown() { m_backend.reset(); }
  30. void Renderer::beginFrame() {
  31. m_activeQueue = &m_queues[m_fillQueueIndex];
  32. m_activeQueue->clear();
  33. if (m_camera) {
  34. m_viewProj = m_camera->getProjectionMatrix() * m_camera->getViewMatrix();
  35. }
  36. if (m_backend)
  37. m_backend->beginFrame();
  38. }
  39. void Renderer::endFrame() {
  40. if (m_paused.load())
  41. return;
  42. if (m_backend && m_camera) {
  43. std::swap(m_fillQueueIndex, m_renderQueueIndex);
  44. DrawQueue &renderQueue = m_queues[m_renderQueueIndex];
  45. renderQueue.sortForBatching();
  46. m_backend->execute(renderQueue, *m_camera);
  47. }
  48. }
  49. void Renderer::setCamera(Camera *camera) { m_camera = camera; }
  50. void Renderer::setClearColor(float r, float g, float b, float a) {
  51. if (m_backend)
  52. m_backend->setClearColor(r, g, b, a);
  53. }
  54. void Renderer::setViewport(int width, int height) {
  55. m_viewportWidth = width;
  56. m_viewportHeight = height;
  57. if (m_backend)
  58. m_backend->setViewport(width, height);
  59. if (m_camera && height > 0) {
  60. float aspect = float(width) / float(height);
  61. m_camera->setPerspective(m_camera->getFOV(), aspect, m_camera->getNear(),
  62. m_camera->getFar());
  63. }
  64. }
  65. void Renderer::mesh(Mesh *mesh, const QMatrix4x4 &model, const QVector3D &color,
  66. Texture *texture, float alpha) {
  67. if (!mesh)
  68. return;
  69. if (mesh == getUnitCylinder() && (!texture)) {
  70. QVector3D start, end;
  71. float radius = 0.0f;
  72. if (detail::decomposeUnitCylinder(model, start, end, radius)) {
  73. cylinder(start, end, radius, color, alpha);
  74. return;
  75. }
  76. }
  77. MeshCmd cmd;
  78. cmd.mesh = mesh;
  79. cmd.texture = texture;
  80. cmd.model = model;
  81. cmd.mvp = m_viewProj * model;
  82. cmd.color = color;
  83. cmd.alpha = alpha;
  84. if (m_activeQueue)
  85. m_activeQueue->submit(cmd);
  86. }
  87. void Renderer::cylinder(const QVector3D &start, const QVector3D &end,
  88. float radius, const QVector3D &color, float alpha) {
  89. CylinderCmd cmd;
  90. cmd.start = start;
  91. cmd.end = end;
  92. cmd.radius = radius;
  93. cmd.color = color;
  94. cmd.alpha = alpha;
  95. if (m_activeQueue)
  96. m_activeQueue->submit(cmd);
  97. }
  98. void Renderer::fogBatch(const FogInstanceData *instances, std::size_t count) {
  99. if (!instances || count == 0 || !m_activeQueue)
  100. return;
  101. FogBatchCmd cmd;
  102. cmd.instances = instances;
  103. cmd.count = count;
  104. m_activeQueue->submit(cmd);
  105. }
  106. void Renderer::grassBatch(Buffer *instanceBuffer, std::size_t instanceCount,
  107. const GrassBatchParams &params) {
  108. if (!instanceBuffer || instanceCount == 0 || !m_activeQueue)
  109. return;
  110. GrassBatchCmd cmd;
  111. cmd.instanceBuffer = instanceBuffer;
  112. cmd.instanceCount = instanceCount;
  113. cmd.params = params;
  114. cmd.params.time = m_accumulatedTime;
  115. m_activeQueue->submit(cmd);
  116. }
  117. void Renderer::terrainChunk(Mesh *mesh, const QMatrix4x4 &model,
  118. const TerrainChunkParams &params,
  119. std::uint16_t sortKey, bool depthWrite,
  120. float depthBias) {
  121. if (!mesh || !m_activeQueue)
  122. return;
  123. TerrainChunkCmd cmd;
  124. cmd.mesh = mesh;
  125. cmd.model = model;
  126. cmd.params = params;
  127. cmd.sortKey = sortKey;
  128. cmd.depthWrite = depthWrite;
  129. cmd.depthBias = depthBias;
  130. m_activeQueue->submit(cmd);
  131. }
  132. void Renderer::selectionRing(const QMatrix4x4 &model, float alphaInner,
  133. float alphaOuter, const QVector3D &color) {
  134. SelectionRingCmd cmd;
  135. cmd.model = model;
  136. cmd.mvp = m_viewProj * model;
  137. cmd.alphaInner = alphaInner;
  138. cmd.alphaOuter = alphaOuter;
  139. cmd.color = color;
  140. if (m_activeQueue)
  141. m_activeQueue->submit(cmd);
  142. }
  143. void Renderer::grid(const QMatrix4x4 &model, const QVector3D &color,
  144. float cellSize, float thickness, float extent) {
  145. GridCmd cmd;
  146. cmd.model = model;
  147. cmd.mvp = m_viewProj * model;
  148. cmd.color = color;
  149. cmd.cellSize = cellSize;
  150. cmd.thickness = thickness;
  151. cmd.extent = extent;
  152. if (m_activeQueue)
  153. m_activeQueue->submit(cmd);
  154. }
  155. void Renderer::selectionSmoke(const QMatrix4x4 &model, const QVector3D &color,
  156. float baseAlpha) {
  157. SelectionSmokeCmd cmd;
  158. cmd.model = model;
  159. cmd.mvp = m_viewProj * model;
  160. cmd.color = color;
  161. cmd.baseAlpha = baseAlpha;
  162. if (m_activeQueue)
  163. m_activeQueue->submit(cmd);
  164. }
  165. void Renderer::renderWorld(Engine::Core::World *world) {
  166. if (m_paused.load())
  167. return;
  168. if (!world)
  169. return;
  170. std::lock_guard<std::mutex> guard(m_worldMutex);
  171. auto renderableEntities =
  172. world->getEntitiesWith<Engine::Core::RenderableComponent>();
  173. for (auto entity : renderableEntities) {
  174. auto renderable = entity->getComponent<Engine::Core::RenderableComponent>();
  175. auto transform = entity->getComponent<Engine::Core::TransformComponent>();
  176. if (!renderable->visible || !transform) {
  177. continue;
  178. }
  179. auto *unitComp = entity->getComponent<Engine::Core::UnitComponent>();
  180. if (unitComp && unitComp->ownerId != m_localOwnerId) {
  181. auto &vis = Game::Map::VisibilityService::instance();
  182. if (vis.isInitialized()) {
  183. if (!vis.isVisibleWorld(transform->position.x, transform->position.z)) {
  184. continue;
  185. }
  186. }
  187. }
  188. QMatrix4x4 modelMatrix;
  189. modelMatrix.translate(transform->position.x, transform->position.y,
  190. transform->position.z);
  191. modelMatrix.rotate(transform->rotation.x, kAxisX);
  192. modelMatrix.rotate(transform->rotation.y, kAxisY);
  193. modelMatrix.rotate(transform->rotation.z, kAxisZ);
  194. modelMatrix.scale(transform->scale.x, transform->scale.y,
  195. transform->scale.z);
  196. bool drawnByRegistry = false;
  197. if (auto *unit = entity->getComponent<Engine::Core::UnitComponent>()) {
  198. if (!unit->unitType.empty() && m_entityRegistry) {
  199. auto fn = m_entityRegistry->get(unit->unitType);
  200. if (fn) {
  201. DrawContext ctx{resources(), entity, world, modelMatrix};
  202. ctx.selected =
  203. (m_selectedIds.find(entity->getId()) != m_selectedIds.end());
  204. ctx.hovered = (entity->getId() == m_hoveredEntityId);
  205. ctx.animationTime = m_accumulatedTime;
  206. fn(ctx, *this);
  207. drawnByRegistry = true;
  208. }
  209. }
  210. }
  211. if (drawnByRegistry)
  212. continue;
  213. Mesh *meshToDraw = nullptr;
  214. ResourceManager *res = resources();
  215. switch (renderable->mesh) {
  216. case Engine::Core::RenderableComponent::MeshKind::Quad:
  217. meshToDraw = res ? res->quad() : nullptr;
  218. break;
  219. case Engine::Core::RenderableComponent::MeshKind::Plane:
  220. meshToDraw = res ? res->ground() : nullptr;
  221. break;
  222. case Engine::Core::RenderableComponent::MeshKind::Cube:
  223. meshToDraw = res ? res->unit() : nullptr;
  224. break;
  225. case Engine::Core::RenderableComponent::MeshKind::Capsule:
  226. meshToDraw = nullptr;
  227. break;
  228. case Engine::Core::RenderableComponent::MeshKind::Ring:
  229. meshToDraw = nullptr;
  230. break;
  231. case Engine::Core::RenderableComponent::MeshKind::None:
  232. default:
  233. break;
  234. }
  235. if (!meshToDraw && res)
  236. meshToDraw = res->unit();
  237. if (!meshToDraw && res)
  238. meshToDraw = res->quad();
  239. QVector3D color = QVector3D(renderable->color[0], renderable->color[1],
  240. renderable->color[2]);
  241. if (res) {
  242. Mesh *contactQuad = res->quad();
  243. Texture *white = res->white();
  244. if (contactQuad && white) {
  245. QMatrix4x4 contactBase;
  246. contactBase.translate(transform->position.x,
  247. transform->position.y + 0.03f,
  248. transform->position.z);
  249. contactBase.rotate(-90.0f, 1.0f, 0.0f, 0.0f);
  250. float footprint =
  251. std::max({transform->scale.x, transform->scale.z, 0.6f});
  252. float sizeRatio = 1.0f;
  253. if (auto *unit = entity->getComponent<Engine::Core::UnitComponent>()) {
  254. int mh = std::max(1, unit->maxHealth);
  255. sizeRatio = std::clamp(unit->health / float(mh), 0.0f, 1.0f);
  256. }
  257. float eased = 0.25f + 0.75f * sizeRatio;
  258. float baseScaleX = footprint * 0.55f * eased;
  259. float baseScaleY = footprint * 0.35f * eased;
  260. QVector3D col(0.03f, 0.03f, 0.03f);
  261. float centerAlpha = 0.32f * eased;
  262. float midAlpha = 0.16f * eased;
  263. float outerAlpha = 0.07f * eased;
  264. QMatrix4x4 c0 = contactBase;
  265. c0.scale(baseScaleX * 0.60f, baseScaleY * 0.60f, 1.0f);
  266. mesh(contactQuad, c0, col, white, centerAlpha);
  267. QMatrix4x4 c1 = contactBase;
  268. c1.scale(baseScaleX * 0.95f, baseScaleY * 0.95f, 1.0f);
  269. mesh(contactQuad, c1, col, white, midAlpha);
  270. QMatrix4x4 c2 = contactBase;
  271. c2.scale(baseScaleX * 1.35f, baseScaleY * 1.35f, 1.0f);
  272. mesh(contactQuad, c2, col, white, outerAlpha);
  273. }
  274. }
  275. mesh(meshToDraw, modelMatrix, color, res ? res->white() : nullptr, 1.0f);
  276. }
  277. }
  278. } // namespace Render::GL