Skip to content

Commit

Permalink
#5893: Coarse implementation of the surface collection used by IRende…
Browse files Browse the repository at this point in the history
…rEntities.
  • Loading branch information
codereader committed Jan 27, 2022
1 parent 8f56350 commit 6bc219f
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 5 deletions.
6 changes: 3 additions & 3 deletions radiantcore/entity/EntityNode.cpp
Expand Up @@ -202,18 +202,18 @@ const Vector3& EntityNode::getDirection() const

void EntityNode::addSurface(const render::IRenderableSurface::Ptr& surface)
{
// TODO
_renderSurfaces.addSurface(surface);
}

void EntityNode::removeSurface(const render::IRenderableSurface::Ptr& surface)
{
// TODO
_renderSurfaces.removeSurface(surface);
}

void EntityNode::foreachSurfaceTouchingBounds(const AABB& bounds,
const std::function<void(const render::IRenderableSurface::Ptr&)>& functor)
{
// TODO
_renderSurfaces.foreachSurfaceTouchingBounds(bounds, functor);
}

std::string EntityNode::getFingerprint()
Expand Down
5 changes: 5 additions & 0 deletions radiantcore/entity/EntityNode.h
Expand Up @@ -19,6 +19,7 @@

#include "KeyObserverMap.h"
#include "RenderableEntityName.h"
#include "RenderableSurfaceCollection.h"

namespace entity
{
Expand Down Expand Up @@ -105,6 +106,10 @@ class EntityNode :
// Whether this entity has registered itself to a render system
bool _isAttachedToRenderSystem;

// The list of surfaces attached to this IRenderEntity
// Used in lighting render mode to enumerate surfaces by entity
RenderableSurfaceCollection _renderSurfaces;

protected:
// The Constructor needs the eclass
EntityNode(const IEntityClassPtr& eclass);
Expand Down
74 changes: 74 additions & 0 deletions radiantcore/entity/RenderableSurfaceCollection.h
@@ -0,0 +1,74 @@
#pragma once

#include <set>
#include "irender.h"

namespace entity
{

class RenderableSurfaceCollection
{
private:
AABB _collectionBounds;
bool _collectionBoundsNeedUpdate;

std::set<render::IRenderableSurface::Ptr> _surfaces;

public:
RenderableSurfaceCollection() :
_collectionBoundsNeedUpdate(true)
{}

void addSurface(const render::IRenderableSurface::Ptr& surface)
{
_surfaces.insert(surface);
_collectionBoundsNeedUpdate = true;
}

void removeSurface(const render::IRenderableSurface::Ptr& surface)
{
_surfaces.erase(surface);
_collectionBoundsNeedUpdate = true;
}

void foreachSurfaceTouchingBounds(const AABB& bounds,
const std::function<void(const render::IRenderableSurface::Ptr&)>& functor)
{
if (_surfaces.empty()) return;

ensureBoundsUpToDate();

// If the whole collection doesn't intersect, quit early
// TODO: bounds not updated when surface changes
//if (!_collectionBounds.intersects(bounds)) return;

for (const auto& surface : _surfaces)
{
auto orientedBounds = AABB::createFromOrientedAABBSafe(
surface->getSurfaceBounds(), surface->getSurfaceTransform());

if (bounds.intersects(orientedBounds))
{
functor(surface);
}
}
}

private:
void ensureBoundsUpToDate()
{
if (!_collectionBoundsNeedUpdate) return;

_collectionBoundsNeedUpdate = false;

_collectionBounds = AABB();

for (const auto& surface : _surfaces)
{
_collectionBounds.includeAABB(AABB::createFromOrientedAABBSafe(
surface->getSurfaceBounds(), surface->getSurfaceTransform()));
}
}
};

}
6 changes: 4 additions & 2 deletions radiantcore/rendersystem/OpenGLRenderSystem.cpp
Expand Up @@ -302,7 +302,9 @@ IRenderResult::Ptr OpenGLRenderSystem::renderLitScene(RenderStateFlags globalFla
// Gather all visible lights and render the surfaces touched by them
for (const auto& light : _lights)
{
if (view.TestAABB(light->lightAABB()) == VOLUME_OUTSIDE)
auto lightBounds = light->lightAABB();

if (view.TestAABB(lightBounds) == VOLUME_OUTSIDE)
{
result->skippedLights++;
continue;
Expand All @@ -315,7 +317,7 @@ IRenderResult::Ptr OpenGLRenderSystem::renderLitScene(RenderStateFlags globalFla
{
auto entitySurfaceCount = 0;

entity->foreachSurfaceTouchingBounds(light->lightAABB(),
entity->foreachSurfaceTouchingBounds(lightBounds,
[&](const render::IRenderableSurface::Ptr& surface)
{
entitySurfaceCount++;
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/DarkRadiantCore.vcxproj
Expand Up @@ -790,6 +790,7 @@
<ClInclude Include="..\..\radiantcore\entity\RenderableArrow.h" />
<ClInclude Include="..\..\radiantcore\entity\RenderableEntityBox.h" />
<ClInclude Include="..\..\radiantcore\entity\RenderableEntityName.h" />
<ClInclude Include="..\..\radiantcore\entity\RenderableSurfaceCollection.h" />
<ClInclude Include="..\..\radiantcore\entity\RotationKey.h" />
<ClInclude Include="..\..\radiantcore\entity\RotationMatrix.h" />
<ClInclude Include="..\..\radiantcore\entity\ShaderParms.h" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/DarkRadiantCore.vcxproj.filters
Expand Up @@ -2295,5 +2295,8 @@
<ClInclude Include="..\..\radiantcore\rendersystem\LightingModeRenderResult.h">
<Filter>src\rendersystem</Filter>
</ClInclude>
<ClInclude Include="..\..\radiantcore\entity\RenderableSurfaceCollection.h">
<Filter>src\entity</Filter>
</ClInclude>
</ItemGroup>
</Project>

0 comments on commit 6bc219f

Please sign in to comment.