scene_renderer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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->setAnimationTime(m_accumulatedTime);
  47. m_backend->execute(renderQueue, *m_camera);
  48. }
  49. }
  50. void Renderer::setCamera(Camera *camera) { m_camera = camera; }
  51. void Renderer::setClearColor(float r, float g, float b, float a) {
  52. if (m_backend)
  53. m_backend->setClearColor(r, g, b, a);
  54. }
  55. void Renderer::setViewport(int width, int height) {
  56. m_viewportWidth = width;
  57. m_viewportHeight = height;
  58. if (m_backend)
  59. m_backend->setViewport(width, height);
  60. if (m_camera && height > 0) {
  61. float aspect = float(width) / float(height);
  62. m_camera->setPerspective(m_camera->getFOV(), aspect, m_camera->getNear(),
  63. m_camera->getFar());
  64. }
  65. }
  66. void Renderer::mesh(Mesh *mesh, const QMatrix4x4 &model, const QVector3D &color,
  67. Texture *texture, float alpha) {
  68. if (!mesh)
  69. return;
  70. if (mesh == getUnitCylinder() && (!texture) && (!m_currentShader)) {
  71. QVector3D start, end;
  72. float radius = 0.0f;
  73. if (detail::decomposeUnitCylinder(model, start, end, radius)) {
  74. cylinder(start, end, radius, color, alpha);
  75. return;
  76. }
  77. }
  78. MeshCmd cmd;
  79. cmd.mesh = mesh;
  80. cmd.texture = texture;
  81. cmd.model = model;
  82. cmd.mvp = m_viewProj * model;
  83. cmd.color = color;
  84. cmd.alpha = alpha;
  85. cmd.shader = m_currentShader;
  86. if (m_activeQueue)
  87. m_activeQueue->submit(cmd);
  88. }
  89. void Renderer::cylinder(const QVector3D &start, const QVector3D &end,
  90. float radius, const QVector3D &color, float alpha) {
  91. CylinderCmd cmd;
  92. cmd.start = start;
  93. cmd.end = end;
  94. cmd.radius = radius;
  95. cmd.color = color;
  96. cmd.alpha = alpha;
  97. if (m_activeQueue)
  98. m_activeQueue->submit(cmd);
  99. }
  100. void Renderer::fogBatch(const FogInstanceData *instances, std::size_t count) {
  101. if (!instances || count == 0 || !m_activeQueue)
  102. return;
  103. FogBatchCmd cmd;
  104. cmd.instances = instances;
  105. cmd.count = count;
  106. m_activeQueue->submit(cmd);
  107. }
  108. void Renderer::grassBatch(Buffer *instanceBuffer, std::size_t instanceCount,
  109. const GrassBatchParams &params) {
  110. if (!instanceBuffer || instanceCount == 0 || !m_activeQueue)
  111. return;
  112. GrassBatchCmd cmd;
  113. cmd.instanceBuffer = instanceBuffer;
  114. cmd.instanceCount = instanceCount;
  115. cmd.params = params;
  116. cmd.params.time = m_accumulatedTime;
  117. m_activeQueue->submit(cmd);
  118. }
  119. void Renderer::stoneBatch(Buffer *instanceBuffer, std::size_t instanceCount,
  120. const StoneBatchParams &params) {
  121. if (!instanceBuffer || instanceCount == 0 || !m_activeQueue)
  122. return;
  123. StoneBatchCmd cmd;
  124. cmd.instanceBuffer = instanceBuffer;
  125. cmd.instanceCount = instanceCount;
  126. cmd.params = params;
  127. m_activeQueue->submit(cmd);
  128. }
  129. void Renderer::plantBatch(Buffer *instanceBuffer, std::size_t instanceCount,
  130. const PlantBatchParams &params) {
  131. if (!instanceBuffer || instanceCount == 0 || !m_activeQueue)
  132. return;
  133. PlantBatchCmd cmd;
  134. cmd.instanceBuffer = instanceBuffer;
  135. cmd.instanceCount = instanceCount;
  136. cmd.params = params;
  137. cmd.params.time = m_accumulatedTime;
  138. m_activeQueue->submit(cmd);
  139. }
  140. void Renderer::pineBatch(Buffer *instanceBuffer, std::size_t instanceCount,
  141. const PineBatchParams &params) {
  142. if (!instanceBuffer || instanceCount == 0 || !m_activeQueue)
  143. return;
  144. PineBatchCmd cmd;
  145. cmd.instanceBuffer = instanceBuffer;
  146. cmd.instanceCount = instanceCount;
  147. cmd.params = params;
  148. cmd.params.time = m_accumulatedTime;
  149. m_activeQueue->submit(cmd);
  150. }
  151. void Renderer::terrainChunk(Mesh *mesh, const QMatrix4x4 &model,
  152. const TerrainChunkParams &params,
  153. std::uint16_t sortKey, bool depthWrite,
  154. float depthBias) {
  155. if (!mesh || !m_activeQueue)
  156. return;
  157. TerrainChunkCmd cmd;
  158. cmd.mesh = mesh;
  159. cmd.model = model;
  160. cmd.params = params;
  161. cmd.sortKey = sortKey;
  162. cmd.depthWrite = depthWrite;
  163. cmd.depthBias = depthBias;
  164. m_activeQueue->submit(cmd);
  165. }
  166. void Renderer::selectionRing(const QMatrix4x4 &model, float alphaInner,
  167. float alphaOuter, const QVector3D &color) {
  168. SelectionRingCmd cmd;
  169. cmd.model = model;
  170. cmd.mvp = m_viewProj * model;
  171. cmd.alphaInner = alphaInner;
  172. cmd.alphaOuter = alphaOuter;
  173. cmd.color = color;
  174. if (m_activeQueue)
  175. m_activeQueue->submit(cmd);
  176. }
  177. void Renderer::grid(const QMatrix4x4 &model, const QVector3D &color,
  178. float cellSize, float thickness, float extent) {
  179. GridCmd cmd;
  180. cmd.model = model;
  181. cmd.mvp = m_viewProj * model;
  182. cmd.color = color;
  183. cmd.cellSize = cellSize;
  184. cmd.thickness = thickness;
  185. cmd.extent = extent;
  186. if (m_activeQueue)
  187. m_activeQueue->submit(cmd);
  188. }
  189. void Renderer::selectionSmoke(const QMatrix4x4 &model, const QVector3D &color,
  190. float baseAlpha) {
  191. SelectionSmokeCmd cmd;
  192. cmd.model = model;
  193. cmd.mvp = m_viewProj * model;
  194. cmd.color = color;
  195. cmd.baseAlpha = baseAlpha;
  196. if (m_activeQueue)
  197. m_activeQueue->submit(cmd);
  198. }
  199. void Renderer::renderWorld(Engine::Core::World *world) {
  200. if (m_paused.load())
  201. return;
  202. if (!world)
  203. return;
  204. std::lock_guard<std::recursive_mutex> guard(world->getEntityMutex());
  205. auto &vis = Game::Map::VisibilityService::instance();
  206. const bool visibilityEnabled = vis.isInitialized();
  207. auto renderableEntities =
  208. world->getEntitiesWith<Engine::Core::RenderableComponent>();
  209. for (auto entity : renderableEntities) {
  210. if (entity->hasComponent<Engine::Core::PendingRemovalComponent>()) {
  211. continue;
  212. }
  213. auto renderable = entity->getComponent<Engine::Core::RenderableComponent>();
  214. auto transform = entity->getComponent<Engine::Core::TransformComponent>();
  215. if (!renderable->visible || !transform) {
  216. continue;
  217. }
  218. auto *unitComp = entity->getComponent<Engine::Core::UnitComponent>();
  219. if (unitComp && unitComp->health <= 0) {
  220. continue;
  221. }
  222. if (unitComp && unitComp->ownerId != m_localOwnerId) {
  223. if (visibilityEnabled) {
  224. if (!vis.isVisibleWorld(transform->position.x, transform->position.z)) {
  225. continue;
  226. }
  227. }
  228. }
  229. QMatrix4x4 modelMatrix;
  230. modelMatrix.translate(transform->position.x, transform->position.y,
  231. transform->position.z);
  232. modelMatrix.rotate(transform->rotation.x, kAxisX);
  233. modelMatrix.rotate(transform->rotation.y, kAxisY);
  234. modelMatrix.rotate(transform->rotation.z, kAxisZ);
  235. modelMatrix.scale(transform->scale.x, transform->scale.y,
  236. transform->scale.z);
  237. bool drawnByRegistry = false;
  238. if (unitComp && !unitComp->unitType.empty() && m_entityRegistry) {
  239. auto fn = m_entityRegistry->get(unitComp->unitType);
  240. if (fn) {
  241. DrawContext ctx{resources(), entity, world, modelMatrix};
  242. ctx.selected =
  243. (m_selectedIds.find(entity->getId()) != m_selectedIds.end());
  244. ctx.hovered = (entity->getId() == m_hoveredEntityId);
  245. ctx.animationTime = m_accumulatedTime;
  246. ctx.backend = m_backend.get();
  247. fn(ctx, *this);
  248. drawnByRegistry = true;
  249. }
  250. }
  251. if (drawnByRegistry)
  252. continue;
  253. Mesh *meshToDraw = nullptr;
  254. ResourceManager *res = resources();
  255. switch (renderable->mesh) {
  256. case Engine::Core::RenderableComponent::MeshKind::Quad:
  257. meshToDraw = res ? res->quad() : nullptr;
  258. break;
  259. case Engine::Core::RenderableComponent::MeshKind::Plane:
  260. meshToDraw = res ? res->ground() : nullptr;
  261. break;
  262. case Engine::Core::RenderableComponent::MeshKind::Cube:
  263. meshToDraw = res ? res->unit() : nullptr;
  264. break;
  265. case Engine::Core::RenderableComponent::MeshKind::Capsule:
  266. meshToDraw = nullptr;
  267. break;
  268. case Engine::Core::RenderableComponent::MeshKind::Ring:
  269. meshToDraw = nullptr;
  270. break;
  271. case Engine::Core::RenderableComponent::MeshKind::None:
  272. default:
  273. break;
  274. }
  275. if (!meshToDraw && res)
  276. meshToDraw = res->unit();
  277. if (!meshToDraw && res)
  278. meshToDraw = res->quad();
  279. QVector3D color = QVector3D(renderable->color[0], renderable->color[1],
  280. renderable->color[2]);
  281. if (res) {
  282. Mesh *contactQuad = res->quad();
  283. Texture *white = res->white();
  284. if (contactQuad && white) {
  285. QMatrix4x4 contactBase;
  286. contactBase.translate(transform->position.x,
  287. transform->position.y + 0.03f,
  288. transform->position.z);
  289. contactBase.rotate(-90.0f, 1.0f, 0.0f, 0.0f);
  290. float footprint =
  291. std::max({transform->scale.x, transform->scale.z, 0.6f});
  292. float sizeRatio = 1.0f;
  293. if (auto *unit = entity->getComponent<Engine::Core::UnitComponent>()) {
  294. int mh = std::max(1, unit->maxHealth);
  295. sizeRatio = std::clamp(unit->health / float(mh), 0.0f, 1.0f);
  296. }
  297. float eased = 0.25f + 0.75f * sizeRatio;
  298. float baseScaleX = footprint * 0.55f * eased;
  299. float baseScaleY = footprint * 0.35f * eased;
  300. QVector3D col(0.03f, 0.03f, 0.03f);
  301. float centerAlpha = 0.32f * eased;
  302. float midAlpha = 0.16f * eased;
  303. float outerAlpha = 0.07f * eased;
  304. QMatrix4x4 c0 = contactBase;
  305. c0.scale(baseScaleX * 0.60f, baseScaleY * 0.60f, 1.0f);
  306. mesh(contactQuad, c0, col, white, centerAlpha);
  307. QMatrix4x4 c1 = contactBase;
  308. c1.scale(baseScaleX * 0.95f, baseScaleY * 0.95f, 1.0f);
  309. mesh(contactQuad, c1, col, white, midAlpha);
  310. QMatrix4x4 c2 = contactBase;
  311. c2.scale(baseScaleX * 1.35f, baseScaleY * 1.35f, 1.0f);
  312. mesh(contactQuad, c2, col, white, outerAlpha);
  313. }
  314. }
  315. mesh(meshToDraw, modelMatrix, color, res ? res->white() : nullptr, 1.0f);
  316. }
  317. }
  318. } // namespace Render::GL