| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- #include "scene_renderer.h"
- #include "../game/map/terrain_service.h"
- #include "../game/map/visibility_service.h"
- #include "../game/units/spawn_type.h"
- #include "../game/units/troop_config.h"
- #include "entity/registry.h"
- #include "game/core/component.h"
- #include "game/core/world.h"
- #include "gl/backend.h"
- #include "gl/camera.h"
- #include "gl/primitives.h"
- #include "gl/resources.h"
- #include <QDebug>
- #include <algorithm>
- #include <cmath>
- namespace Render::GL {
- namespace {
- const QVector3D kAxisX(1.0f, 0.0f, 0.0f);
- const QVector3D kAxisY(0.0f, 1.0f, 0.0f);
- const QVector3D kAxisZ(0.0f, 0.0f, 1.0f);
- } // namespace
- Renderer::Renderer() { m_activeQueue = &m_queues[m_fillQueueIndex]; }
- Renderer::~Renderer() { shutdown(); }
- bool Renderer::initialize() {
- if (!m_backend) {
- m_backend = std::make_shared<Backend>();
- }
- m_backend->initialize();
- m_entityRegistry = std::make_unique<EntityRendererRegistry>();
- registerBuiltInEntityRenderers(*m_entityRegistry);
- return true;
- }
- void Renderer::shutdown() { m_backend.reset(); }
- void Renderer::beginFrame() {
- m_activeQueue = &m_queues[m_fillQueueIndex];
- m_activeQueue->clear();
- if (m_camera) {
- m_viewProj = m_camera->getProjectionMatrix() * m_camera->getViewMatrix();
- }
- if (m_backend) {
- m_backend->beginFrame();
- }
- }
- void Renderer::endFrame() {
- if (m_paused.load()) {
- return;
- }
- if (m_backend && m_camera) {
- std::swap(m_fillQueueIndex, m_renderQueueIndex);
- DrawQueue &renderQueue = m_queues[m_renderQueueIndex];
- renderQueue.sortForBatching();
- m_backend->setAnimationTime(m_accumulatedTime);
- m_backend->execute(renderQueue, *m_camera);
- }
- }
- void Renderer::setCamera(Camera *camera) { m_camera = camera; }
- void Renderer::setClearColor(float r, float g, float b, float a) {
- if (m_backend) {
- m_backend->setClearColor(r, g, b, a);
- }
- }
- void Renderer::setViewport(int width, int height) {
- m_viewportWidth = width;
- m_viewportHeight = height;
- if (m_backend) {
- m_backend->setViewport(width, height);
- }
- if (m_camera && height > 0) {
- float aspect = float(width) / float(height);
- m_camera->setPerspective(m_camera->getFOV(), aspect, m_camera->getNear(),
- m_camera->getFar());
- }
- }
- void Renderer::mesh(Mesh *mesh, const QMatrix4x4 &model, const QVector3D &color,
- Texture *texture, float alpha) {
- if (!mesh) {
- return;
- }
- if (mesh == getUnitCylinder() && (!texture) && (!m_currentShader)) {
- QVector3D start, end;
- float radius = 0.0f;
- if (detail::decomposeUnitCylinder(model, start, end, radius)) {
- cylinder(start, end, radius, color, alpha);
- return;
- }
- }
- MeshCmd cmd;
- cmd.mesh = mesh;
- cmd.texture = texture;
- cmd.model = model;
- cmd.mvp = m_viewProj * model;
- cmd.color = color;
- cmd.alpha = alpha;
- cmd.shader = m_currentShader;
- if (m_activeQueue) {
- m_activeQueue->submit(cmd);
- }
- }
- void Renderer::cylinder(const QVector3D &start, const QVector3D &end,
- float radius, const QVector3D &color, float alpha) {
- CylinderCmd cmd;
- cmd.start = start;
- cmd.end = end;
- cmd.radius = radius;
- cmd.color = color;
- cmd.alpha = alpha;
- if (m_activeQueue) {
- m_activeQueue->submit(cmd);
- }
- }
- void Renderer::fogBatch(const FogInstanceData *instances, std::size_t count) {
- if (!instances || count == 0 || !m_activeQueue) {
- return;
- }
- FogBatchCmd cmd;
- cmd.instances = instances;
- cmd.count = count;
- m_activeQueue->submit(cmd);
- }
- void Renderer::grassBatch(Buffer *instanceBuffer, std::size_t instanceCount,
- const GrassBatchParams ¶ms) {
- if (!instanceBuffer || instanceCount == 0 || !m_activeQueue) {
- return;
- }
- GrassBatchCmd cmd;
- cmd.instanceBuffer = instanceBuffer;
- cmd.instanceCount = instanceCount;
- cmd.params = params;
- cmd.params.time = m_accumulatedTime;
- m_activeQueue->submit(cmd);
- }
- void Renderer::stoneBatch(Buffer *instanceBuffer, std::size_t instanceCount,
- const StoneBatchParams ¶ms) {
- if (!instanceBuffer || instanceCount == 0 || !m_activeQueue) {
- return;
- }
- StoneBatchCmd cmd;
- cmd.instanceBuffer = instanceBuffer;
- cmd.instanceCount = instanceCount;
- cmd.params = params;
- m_activeQueue->submit(cmd);
- }
- void Renderer::plantBatch(Buffer *instanceBuffer, std::size_t instanceCount,
- const PlantBatchParams ¶ms) {
- if (!instanceBuffer || instanceCount == 0 || !m_activeQueue) {
- return;
- }
- PlantBatchCmd cmd;
- cmd.instanceBuffer = instanceBuffer;
- cmd.instanceCount = instanceCount;
- cmd.params = params;
- cmd.params.time = m_accumulatedTime;
- m_activeQueue->submit(cmd);
- }
- void Renderer::pineBatch(Buffer *instanceBuffer, std::size_t instanceCount,
- const PineBatchParams ¶ms) {
- if (!instanceBuffer || instanceCount == 0 || !m_activeQueue) {
- return;
- }
- PineBatchCmd cmd;
- cmd.instanceBuffer = instanceBuffer;
- cmd.instanceCount = instanceCount;
- cmd.params = params;
- cmd.params.time = m_accumulatedTime;
- m_activeQueue->submit(cmd);
- }
- void Renderer::firecampBatch(Buffer *instanceBuffer, std::size_t instanceCount,
- const FireCampBatchParams ¶ms) {
- if (!instanceBuffer || instanceCount == 0 || !m_activeQueue) {
- return;
- }
- FireCampBatchCmd cmd;
- cmd.instanceBuffer = instanceBuffer;
- cmd.instanceCount = instanceCount;
- cmd.params = params;
- cmd.params.time = m_accumulatedTime;
- m_activeQueue->submit(cmd);
- }
- void Renderer::terrainChunk(Mesh *mesh, const QMatrix4x4 &model,
- const TerrainChunkParams ¶ms,
- std::uint16_t sortKey, bool depthWrite,
- float depthBias) {
- if (!mesh || !m_activeQueue) {
- return;
- }
- TerrainChunkCmd cmd;
- cmd.mesh = mesh;
- cmd.model = model;
- cmd.params = params;
- cmd.sortKey = sortKey;
- cmd.depthWrite = depthWrite;
- cmd.depthBias = depthBias;
- m_activeQueue->submit(cmd);
- }
- void Renderer::selectionRing(const QMatrix4x4 &model, float alphaInner,
- float alphaOuter, const QVector3D &color) {
- SelectionRingCmd cmd;
- cmd.model = model;
- cmd.mvp = m_viewProj * model;
- cmd.alphaInner = alphaInner;
- cmd.alphaOuter = alphaOuter;
- cmd.color = color;
- if (m_activeQueue) {
- m_activeQueue->submit(cmd);
- }
- }
- void Renderer::grid(const QMatrix4x4 &model, const QVector3D &color,
- float cellSize, float thickness, float extent) {
- GridCmd cmd;
- cmd.model = model;
- cmd.mvp = m_viewProj * model;
- cmd.color = color;
- cmd.cellSize = cellSize;
- cmd.thickness = thickness;
- cmd.extent = extent;
- if (m_activeQueue) {
- m_activeQueue->submit(cmd);
- }
- }
- void Renderer::selectionSmoke(const QMatrix4x4 &model, const QVector3D &color,
- float baseAlpha) {
- SelectionSmokeCmd cmd;
- cmd.model = model;
- cmd.mvp = m_viewProj * model;
- cmd.color = color;
- cmd.baseAlpha = baseAlpha;
- if (m_activeQueue) {
- m_activeQueue->submit(cmd);
- }
- }
- void Renderer::enqueueSelectionRing(Engine::Core::Entity *,
- Engine::Core::TransformComponent *transform,
- Engine::Core::UnitComponent *unitComp,
- bool selected, bool hovered) {
- if ((!selected && !hovered) || !transform) {
- return;
- }
- float ringSize = 0.5f;
- float ringOffset = 0.05f;
- float groundOffset = 0.0f;
- if (unitComp) {
- auto &config = Game::Units::TroopConfig::instance();
- ringSize = config.getSelectionRingSize(unitComp->spawnType);
- ringOffset += config.getSelectionRingYOffset(unitComp->spawnType);
- groundOffset = config.getSelectionRingGroundOffset(unitComp->spawnType);
- }
- QVector3D pos(transform->position.x, transform->position.y,
- transform->position.z);
- auto &terrainService = Game::Map::TerrainService::instance();
- float terrainY = transform->position.y;
- if (terrainService.isInitialized()) {
- terrainY = terrainService.getTerrainHeight(pos.x(), pos.z());
- } else {
- terrainY -= groundOffset * transform->scale.y;
- }
- pos.setY(terrainY);
- QMatrix4x4 ringModel;
- ringModel.translate(pos.x(), pos.y() + ringOffset, pos.z());
- ringModel.scale(ringSize, 1.0f, ringSize);
- if (selected) {
- selectionRing(ringModel, 0.6f, 0.25f, QVector3D(0.2f, 0.4f, 1.0f));
- } else if (hovered) {
- selectionRing(ringModel, 0.35f, 0.15f, QVector3D(0.90f, 0.90f, 0.25f));
- }
- }
- void Renderer::renderWorld(Engine::Core::World *world) {
- if (m_paused.load()) {
- return;
- }
- if (!world) {
- return;
- }
- std::lock_guard<std::recursive_mutex> guard(world->getEntityMutex());
- auto &vis = Game::Map::VisibilityService::instance();
- const bool visibilityEnabled = vis.isInitialized();
- auto renderableEntities =
- world->getEntitiesWith<Engine::Core::RenderableComponent>();
- for (auto entity : renderableEntities) {
- if (entity->hasComponent<Engine::Core::PendingRemovalComponent>()) {
- continue;
- }
- auto renderable = entity->getComponent<Engine::Core::RenderableComponent>();
- auto transform = entity->getComponent<Engine::Core::TransformComponent>();
- if (!renderable->visible || !transform) {
- continue;
- }
- auto *unitComp = entity->getComponent<Engine::Core::UnitComponent>();
- if (unitComp && unitComp->health <= 0) {
- continue;
- }
- if (m_camera && unitComp) {
- float cullRadius = 3.0f;
- if (unitComp->spawnType == Game::Units::SpawnType::MountedKnight) {
- cullRadius = 4.0f;
- } else if (unitComp->spawnType == Game::Units::SpawnType::Spearman ||
- unitComp->spawnType == Game::Units::SpawnType::Archer ||
- unitComp->spawnType == Game::Units::SpawnType::Knight) {
- cullRadius = 2.5f;
- }
- QVector3D unitPos(transform->position.x, transform->position.y,
- transform->position.z);
- if (!m_camera->isInFrustum(unitPos, cullRadius)) {
- continue;
- }
- }
- if (unitComp && unitComp->ownerId != m_localOwnerId) {
- if (visibilityEnabled) {
- if (!vis.isVisibleWorld(transform->position.x, transform->position.z)) {
- continue;
- }
- }
- }
- bool isSelected =
- (m_selectedIds.find(entity->getId()) != m_selectedIds.end());
- bool isHovered = (entity->getId() == m_hoveredEntityId);
- QMatrix4x4 modelMatrix;
- modelMatrix.translate(transform->position.x, transform->position.y,
- transform->position.z);
- modelMatrix.rotate(transform->rotation.x, kAxisX);
- modelMatrix.rotate(transform->rotation.y, kAxisY);
- modelMatrix.rotate(transform->rotation.z, kAxisZ);
- modelMatrix.scale(transform->scale.x, transform->scale.y,
- transform->scale.z);
- bool drawnByRegistry = false;
- if (unitComp && m_entityRegistry) {
- std::string unitTypeStr =
- Game::Units::spawnTypeToString(unitComp->spawnType);
- auto fn = m_entityRegistry->get(unitTypeStr);
- if (fn) {
- DrawContext ctx{resources(), entity, world, modelMatrix};
- ctx.selected = isSelected;
- ctx.hovered = isHovered;
- ctx.animationTime = m_accumulatedTime;
- ctx.backend = m_backend.get();
- fn(ctx, *this);
- enqueueSelectionRing(entity, transform, unitComp, isSelected,
- isHovered);
- drawnByRegistry = true;
- }
- }
- if (drawnByRegistry) {
- continue;
- }
- Mesh *meshToDraw = nullptr;
- ResourceManager *res = resources();
- switch (renderable->mesh) {
- case Engine::Core::RenderableComponent::MeshKind::Quad:
- meshToDraw = res ? res->quad() : nullptr;
- break;
- case Engine::Core::RenderableComponent::MeshKind::Plane:
- meshToDraw = res ? res->ground() : nullptr;
- break;
- case Engine::Core::RenderableComponent::MeshKind::Cube:
- meshToDraw = res ? res->unit() : nullptr;
- break;
- case Engine::Core::RenderableComponent::MeshKind::Capsule:
- meshToDraw = nullptr;
- break;
- case Engine::Core::RenderableComponent::MeshKind::Ring:
- meshToDraw = nullptr;
- break;
- case Engine::Core::RenderableComponent::MeshKind::None:
- default:
- break;
- }
- if (!meshToDraw && res) {
- meshToDraw = res->unit();
- }
- if (!meshToDraw && res) {
- meshToDraw = res->quad();
- }
- QVector3D color = QVector3D(renderable->color[0], renderable->color[1],
- renderable->color[2]);
- if (res) {
- Mesh *contactQuad = res->quad();
- Texture *white = res->white();
- if (contactQuad && white) {
- QMatrix4x4 contactBase;
- contactBase.translate(transform->position.x,
- transform->position.y + 0.03f,
- transform->position.z);
- contactBase.rotate(-90.0f, 1.0f, 0.0f, 0.0f);
- float footprint =
- std::max({transform->scale.x, transform->scale.z, 0.6f});
- float sizeRatio = 1.0f;
- if (auto *unit = entity->getComponent<Engine::Core::UnitComponent>()) {
- int mh = std::max(1, unit->maxHealth);
- sizeRatio = std::clamp(unit->health / float(mh), 0.0f, 1.0f);
- }
- float eased = 0.25f + 0.75f * sizeRatio;
- float baseScaleX = footprint * 0.55f * eased;
- float baseScaleY = footprint * 0.35f * eased;
- QVector3D col(0.03f, 0.03f, 0.03f);
- float centerAlpha = 0.32f * eased;
- float midAlpha = 0.16f * eased;
- float outerAlpha = 0.07f * eased;
- QMatrix4x4 c0 = contactBase;
- c0.scale(baseScaleX * 0.60f, baseScaleY * 0.60f, 1.0f);
- mesh(contactQuad, c0, col, white, centerAlpha);
- QMatrix4x4 c1 = contactBase;
- c1.scale(baseScaleX * 0.95f, baseScaleY * 0.95f, 1.0f);
- mesh(contactQuad, c1, col, white, midAlpha);
- QMatrix4x4 c2 = contactBase;
- c2.scale(baseScaleX * 1.35f, baseScaleY * 1.35f, 1.0f);
- mesh(contactQuad, c2, col, white, outerAlpha);
- }
- }
- enqueueSelectionRing(entity, transform, unitComp, isSelected, isHovered);
- mesh(meshToDraw, modelMatrix, color, res ? res->white() : nullptr, 1.0f);
- }
- }
- } // namespace Render::GL
|