extract render-related code from Entities to EntitiesRenderer

This commit is contained in:
MihailRis 2026-11-01 03:34:16 +03:00
parent 5afe1e2f37
commit 1da6ef9747
6 changed files with 219 additions and 185 deletions

View file

@ -0,0 +1,177 @@
#include "EntitiesRenderer.hpp"
#include "animation/rigging.hpp"
#include "assets/Assets.hpp"
#include "graphics/core/DrawContext.hpp"
#include "graphics/core/LineBatch.hpp"
#include "maths/FrustumCulling.hpp"
#include "objects/Entities.hpp"
#include "objects/Entity.hpp"
#include "objects/EntityDef.hpp"
#include "ModelBatch.hpp"
static void debug_render_skeleton(
LineBatch& batch,
const rigging::Bone* bone,
const rigging::Skeleton& skeleton
) {
size_t pindex = bone->getIndex();
for (auto& sub : bone->getBones()) {
size_t sindex = sub->getIndex();
const auto& matrices = skeleton.calculated.matrices;
batch.line(
glm::vec3(matrices[pindex] * glm::vec4(0, 0, 0, 1)),
glm::vec3(matrices[sindex] * glm::vec4(0, 0, 0, 1)),
glm::vec4(0, 0.5f, 0, 1)
);
debug_render_skeleton(batch, sub.get(), skeleton);
}
}
static glm::vec3 calc_extent(
const Transform& transform,
const Rigidbody& rigidbody,
const rigging::Skeleton& skeleton
) {
const auto& size = transform.size;
float scale = glm::max(
glm::abs(size.x), glm::max(glm::abs(size.y), glm::abs(size.z))
);
return glm::max(
rigidbody.hitbox.getHalfSize(), glm::vec3(skeleton.boundsRadius * scale)
);
}
static bool check_frustum(
const Frustum& frustum,
const Transform& transform,
const Rigidbody& rigidbody,
const rigging::Skeleton& skeleton
) {
glm::vec3 ext = calc_extent(transform, rigidbody, skeleton);
return frustum.isBoxVisible(transform.pos - ext, transform.pos + ext);
}
EntitiesRenderer::EntitiesRenderer(const Assets& assets, Entities& entities)
: assets(assets), entities(entities) {
}
void EntitiesRenderer::render(
ModelBatch& batch, const Frustum* frustum, entityid_t fpsEntity
) {
for (const auto& [uid, entity] : entities.getEntities()) {
if (!entity->getSkeleton() || uid == fpsEntity) {
continue;
}
const auto& transform = entity->getTransform();
const auto& rigidbody = entity->getRigidbody();
auto& skeleton = *entity->getSkeleton();
const auto& def = entity->getID().def;
const auto& pos = transform.pos;
const auto& size = transform.size;
const auto& rigConfig = skeleton.config;
if (rigConfig == nullptr) {
continue;
}
if (frustum && !check_frustum(*frustum, transform, rigidbody, skeleton)) {
if (!skeleton.boundsDirty || !skeleton.visible) {
continue;
}
rigConfig->updateBounds(assets, skeleton, transform.rot, pos, size);
if (!check_frustum(*frustum, transform, rigidbody, skeleton)) {
continue;
}
}
rigConfig->render(
assets,
batch,
skeleton,
def.lightingMode,
transform.rot,
pos,
size,
def.translucent
);
}
}
void EntitiesRenderer::renderDebug(
LineBatch& batch,
const Frustum* frustum,
const DrawContext& pctx,
entityid_t fpsEntity
) {
const auto& all = entities.getEntities();
{
auto ctx = pctx.sub(&batch);
ctx.setLineWidth(1);
for (const auto& [uid, entity] : all) {
if (!entity->getSkeleton()) {
continue;
}
const auto& transform = entity->getTransform();
const auto& rigidbody = entity->getRigidbody();
const auto& skeleton = *entity->getSkeleton();
const auto& hitbox = rigidbody.hitbox;
if (frustum && !check_frustum(*frustum, transform, rigidbody, skeleton)) {
continue;
}
batch.box(
hitbox.position, hitbox.getHalfSize() * 2.0f, glm::vec4(1.0f)
);
glm::vec3 ext = calc_extent(transform, rigidbody, skeleton);
batch.box(
transform.pos, ext * 2.0f, glm::vec4(0.0f, 0.0f, 1.0f, 1.0f)
);
for (auto& sensor : rigidbody.sensors) {
if (sensor.type != SensorType::AABB) continue;
batch.box(
sensor.calculated.aabb.center(),
sensor.calculated.aabb.size(),
glm::vec4(1.0f, 1.0f, 0.0f, 1.0f)
);
}
}
}
{
auto ctx = pctx.sub(&batch);
ctx.setDepthTest(false);
ctx.setDepthMask(false);
ctx.setLineWidth(2);
for (const auto& [uid, entity] : all) {
if (!entity->getSkeleton() || uid == fpsEntity) {
continue;
}
const auto& transform = entity->getTransform();
const auto& rigidbody = entity->getRigidbody();
const auto& skeleton = *entity->getSkeleton();
auto config = skeleton.config;
if (config == nullptr) {
continue;
}
if (frustum && !check_frustum(*frustum, transform, rigidbody, skeleton)) {
continue;
}
auto bone = config->getRoot();
debug_render_skeleton(batch, bone, skeleton);
}
}
}
void EntitiesRenderer::updateCulling(
const Frustum* frustum, entityid_t fpsEntity
) {
for (const auto& [uid, entity] : entities.getEntities()) {
if (!entity->getSkeleton()) {
continue;
}
auto& skeleton = *entity->getSkeleton();
if (frustum == nullptr || uid == fpsEntity) {
skeleton.culled = false;
continue;
}
skeleton.culled = !check_frustum(
*frustum, entity->getTransform(), entity->getRigidbody(), skeleton
);
}
}

View file

@ -0,0 +1,30 @@
#pragma once
#include "typedefs.hpp"
class Assets;
class DrawContext;
class Entities;
class Frustum;
class LineBatch;
class ModelBatch;
class EntitiesRenderer {
const Assets& assets;
Entities& entities;
public:
EntitiesRenderer(const Assets& assets, Entities& entities);
void render(
ModelBatch& batch, const Frustum* frustum, entityid_t fpsEntity
);
void renderDebug(
LineBatch& batch,
const Frustum* frustum,
const DrawContext& ctx,
entityid_t fpsEntity
);
void updateCulling(const Frustum* frustum, entityid_t fpsEntity);
};

View file

@ -6,6 +6,7 @@
#include "CloudsRenderer.hpp"
#include "DebugLinesRenderer.hpp"
#include "Emitter.hpp"
#include "EntitiesRenderer.hpp"
#include "HandsRenderer.hpp"
#include "ModelBatch.hpp"
#include "NamedSkeletons.hpp"
@ -161,6 +162,7 @@ WorldRenderer::WorldRenderer(
shadowMapping = std::make_unique<Shadows>(level);
debugLines = std::make_unique<DebugLinesRenderer>(level);
cloudsRenderer = std::make_unique<CloudsRenderer>();
entitiesRenderer = std::make_unique<EntitiesRenderer>(assets, *level.entities);
keepAlive(observe_setting(settings.graphics.enableFog, dirtySettings));
keepAlive(observe_setting(settings.graphics.advancedRender, dirtySettings));
@ -279,8 +281,7 @@ void WorldRenderer::renderOpaque(
auto& modelShader = assets.require<Shader>("model");
setupWorldShader(modelShader, camera, settings, fogFactor);
setup_entity_pass(modelShader, {0.5f, true, false, false, 1.0f});
level.entities->render(
assets,
entitiesRenderer->render(
*modelBatch,
culling ? frustumCulling.get() : nullptr,
player.currentCamera.get() == player.fpCamera.get() ? player.getEntity()
@ -354,7 +355,7 @@ void WorldRenderer::renderInWorldLines(
if (debug && showEntitiesDebug) {
auto ctx = pctx.sub(lineBatch.get());
bool culling = engine.getSettings().graphics.frustumCulling.get();
level.entities->renderDebug(
entitiesRenderer->renderDebug(
*lineBatch, culling ? frustumCulling.get() : nullptr, ctx,
player.currentCamera.get() == player.fpCamera.get() ? player.getEntity() : 0
);
@ -366,7 +367,7 @@ void WorldRenderer::updateEntitiesCulling(const Camera& camera) {
if (culling) {
frustumCulling->update(camera.getProjView());
}
level.entities->updateCulling(
entitiesRenderer->updateCulling(
culling ? frustumCulling.get() : nullptr,
player.currentCamera.get() == player.fpCamera.get() ? player.getEntity()
: 0

View file

@ -18,6 +18,7 @@ class ChunksRenderer;
class DebugLinesRenderer;
class DrawContext;
class Engine;
class EntitiesRenderer;
class Frustum;
class HandsRenderer;
class Level;
@ -80,6 +81,7 @@ private:
std::unique_ptr<DebugLinesRenderer> debugLines;
std::unique_ptr<PrecipitationRenderer> precipitation;
std::unique_ptr<CloudsRenderer> cloudsRenderer;
std::unique_ptr<EntitiesRenderer> entitiesRenderer;
std::unique_ptr<TranslucentLayers> translucentLayers;
float timer = 0.0f;

View file

@ -9,12 +9,7 @@
#include "engine/Engine.hpp"
#include "Entity.hpp"
#include "EntityDef.hpp"
#include "graphics/commons/Model.hpp"
#include "graphics/core/DrawContext.hpp"
#include "graphics/core/LineBatch.hpp"
#include "graphics/render/ModelBatch.hpp"
#include "logic/scripting/scripting.hpp"
#include "maths/FrustumCulling.hpp"
#include "maths/rays.hpp"
#include "maths/util.hpp"
#include "physics/PhysicsSolver.hpp"
@ -410,165 +405,6 @@ void Entities::update(float delta) {
}
}
static void debug_render_skeleton(
LineBatch& batch,
const rigging::Bone* bone,
const rigging::Skeleton& skeleton
) {
size_t pindex = bone->getIndex();
for (auto& sub : bone->getBones()) {
size_t sindex = sub->getIndex();
const auto& matrices = skeleton.calculated.matrices;
batch.line(
glm::vec3(matrices[pindex] * glm::vec4(0, 0, 0, 1)),
glm::vec3(matrices[sindex] * glm::vec4(0, 0, 0, 1)),
glm::vec4(0, 0.5f, 0, 1)
);
debug_render_skeleton(batch, sub.get(), skeleton);
}
}
static glm::vec3 calc_extent(
const Transform& transform,
const Rigidbody& rigidbody,
const rigging::Skeleton& skeleton
) {
const auto& size = transform.size;
float scale = glm::max(
glm::abs(size.x), glm::max(glm::abs(size.y), glm::abs(size.z))
);
return glm::max(
rigidbody.hitbox.getHalfSize(), glm::vec3(skeleton.boundsRadius * scale)
);
}
static bool check_frustum(
const Frustum& frustum,
const Transform& transform,
const Rigidbody& rigidbody,
const rigging::Skeleton& skeleton
) {
glm::vec3 ext = calc_extent(transform, rigidbody, skeleton);
return frustum.isBoxVisible(transform.pos - ext, transform.pos + ext);
}
void Entities::renderDebug(
LineBatch& batch, const Frustum* frustum, const DrawContext& pctx, entityid_t fpsEntity
) {
{
auto ctx = pctx.sub(&batch);
ctx.setLineWidth(1);
for (const auto& [uid, entity] : entities) {
if (!entity->getSkeleton()) {
continue;
}
const auto& transform = entity->getTransform();
const auto& rigidbody = entity->getRigidbody();
const auto& skeleton = *entity->getSkeleton();
const auto& hitbox = rigidbody.hitbox;
if (frustum && !check_frustum(*frustum, transform, rigidbody, skeleton)) {
continue;
}
batch.box(
hitbox.position, hitbox.getHalfSize() * 2.0f, glm::vec4(1.0f)
);
glm::vec3 ext = calc_extent(transform, rigidbody, skeleton);
batch.box(
transform.pos, ext * 2.0f, glm::vec4(0.0f, 0.0f, 1.0f, 1.0f)
);
for (auto& sensor : rigidbody.sensors) {
if (sensor.type != SensorType::AABB) continue;
batch.box(
sensor.calculated.aabb.center(),
sensor.calculated.aabb.size(),
glm::vec4(1.0f, 1.0f, 0.0f, 1.0f)
);
}
}
}
{
auto ctx = pctx.sub(&batch);
ctx.setDepthTest(false);
ctx.setDepthMask(false);
ctx.setLineWidth(2);
for (const auto& [uid, entity] : entities) {
if (!entity->getSkeleton() || uid == fpsEntity) {
continue;
}
const auto& transform = entity->getTransform();
const auto& rigidbody = entity->getRigidbody();
const auto& skeleton = *entity->getSkeleton();
auto config = skeleton.config;
if (config == nullptr) {
continue;
}
if (frustum && !check_frustum(*frustum, transform, rigidbody, skeleton)) {
continue;
}
auto bone = config->getRoot();
debug_render_skeleton(batch, bone, skeleton);
}
}
}
void Entities::render(
const Assets& assets,
ModelBatch& batch,
const Frustum* frustum,
entityid_t fpsEntity
) {
for (const auto& [uid, entity] : entities) {
if (!entity->getSkeleton() || uid == fpsEntity) {
continue;
}
const auto& transform = entity->getTransform();
const auto& rigidbody = entity->getRigidbody();
auto& skeleton = *entity->getSkeleton();
const auto& def = entity->getID().def;
const auto& pos = transform.pos;
const auto& size = transform.size;
const auto& rigConfig = skeleton.config;
if (rigConfig == nullptr) {
continue;
}
if (frustum && !check_frustum(*frustum, transform, rigidbody, skeleton)) {
if (!skeleton.boundsDirty || !skeleton.visible) {
continue;
}
rigConfig->updateBounds(assets, skeleton, transform.rot, pos, size);
if (!check_frustum(*frustum, transform, rigidbody, skeleton)) {
continue;
}
}
rigConfig->render(
assets,
batch,
skeleton,
def.lightingMode,
transform.rot,
pos,
size,
def.translucent
);
}
}
void Entities::updateCulling(const Frustum* frustum, entityid_t fpsEntity) {
for (const auto& [uid, entity] : entities) {
if (!entity->getSkeleton()) {
continue;
}
auto& skeleton = *entity->getSkeleton();
if (frustum == nullptr || uid == fpsEntity) {
skeleton.culled = false;
continue;
}
skeleton.culled = !check_frustum(
*frustum, entity->getTransform(), entity->getRigidbody(), skeleton
);
}
}
bool Entities::hasBlockingInside(AABB aabb) {
constexpr float eps = 0.05f;
for (const auto& [uid, entity] : entities) {

View file

@ -21,11 +21,6 @@ struct EntityDef;
class Level;
class Assets;
class Entity;
class LineBatch;
class ModelBatch;
class Frustum;
class Entities;
class DrawContext;
namespace rigging {
struct Skeleton;
@ -55,18 +50,6 @@ public:
void updatePhysics(float delta);
void update(float delta);
void renderDebug(
LineBatch& batch, const Frustum* frustum, const DrawContext& ctx, entityid_t fpsEntity
);
void render(
const Assets& assets,
ModelBatch& batch,
const Frustum* frustum,
entityid_t fpsEntity
);
void updateCulling(const Frustum* frustum, entityid_t fpsEntity);
entityid_t spawn(
const EntityDef& def,
glm::vec3 position,
@ -122,6 +105,11 @@ public:
nextID = id;
}
inline const std::map<entityid_t, std::unique_ptr<Entity>>& getEntities(
) const {
return entities;
}
inline size_t size() const {
return entities.size();
}