Skip to content

Commit

Permalink
Improve portal culling
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzi71 committed Jul 12, 2021
1 parent 384bd7b commit 9b7a2e5
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 59 deletions.
21 changes: 3 additions & 18 deletions owGame/WMO/WMO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,6 @@ void CWMO::CreateInsances(const std::shared_ptr<CWMO_Base_Instance>& Parent) con

m_BaseManager.GetManager<ILoader>()->AddToLoadQueue(groupInstance);
}

#ifdef USE_WMO_PORTALS_CULLING
for (const auto& groupPtr : Parent->getGroupInstances())
{
if (auto group = groupPtr.lock())
{
group->CreatePortals(Parent);
}
else _ASSERT(false);
}
#endif

for (auto& it : m_Lights)
{
//Parent->AddLight(it);
}
}


Expand Down Expand Up @@ -232,19 +216,20 @@ bool CWMO::Load()
}
}

/*
// Create portal controller
if (portals.size() > 0)
{
#ifdef USE_WMO_PORTALS_CULLING
m_PortalController = MakeShared(CWMO_PortalsController);
m_PortalController = MakeShared(CWMO_PortalsComponent);
for (const auto& it : portalsReferences)
{
_ASSERT(it.portalIndex < portals.size());
_ASSERT(it.groupIndex < m_Groups.size());
}
#endif
}
}*/

return true;
}
Expand Down
13 changes: 3 additions & 10 deletions owGame/WMO/WMO.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#include "WMO_Part_Material.h"
#include "WMO_Part_Portal.h"

#include "WMO_PortalsController.h"

// FORWARD BEGIN
class CWMO_Base_Instance;
// FORWARD END
Expand All @@ -42,9 +40,6 @@ class ZN_API CWMO
std::string GetFilename() const { return m_FileName; }
BoundingBox GetBounds() const { return m_Bounds; }
const SWMO_MOHD& GetHeader() const { return m_Header; }
#ifdef USE_WMO_PORTALS_CULLING
const std::shared_ptr<CWMO_PortalsController>& GetPortalController() const { return m_PortalController; }
#endif

std::string GetTextureName(size_t Offset) const { return std::string(m_TexturesNames.get() + Offset); }
const std::shared_ptr<WMO_Part_Material>& GetMaterial(size_t Index) const { return m_Materials.at(Index); }
Expand All @@ -64,18 +59,16 @@ class ZN_API CWMO
std::unique_ptr<char[]> m_TexturesNames; // MOTX chunk
std::vector<std::shared_ptr<WMO_Part_Material>> m_Materials; // MOMT chunk


//-- Groups --//
std::unique_ptr<char[]> m_GroupNames; // MOGN chunk
std::vector<std::shared_ptr<CWMOGroup>> m_Groups; // MOGI chunk


//-- Skybox --//
std::shared_ptr<CM2> m_Skybox;

//-- Portals --//
#ifdef USE_WMO_PORTALS_CULLING
std::shared_ptr<CWMO_PortalsController> m_PortalController;
#endif


//-- Visible block
std::vector<glm::vec3> m_VisibleBlockVertices; // MOVV chunk
std::vector<SWMO_MOVB> m_VisibleBlockList; // MOVB chunk
Expand Down
8 changes: 1 addition & 7 deletions owGame/WMO/WMO_Base_Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,6 @@ void CWMO_Base_Instance::Update(const UpdateEventArgs& e)
{
if (GetState() != ILoadable::ELoadableState::Loaded)
return;

#ifdef USE_WMO_PORTALS_CULLING
if (auto portalsController = GetWMO().GetPortalController())
{
portalsController->Update(this, e.CameraForCulling);
}
#endif
}

void CWMO_Base_Instance::Accept(IVisitor* visitor)
Expand All @@ -98,6 +91,7 @@ bool CWMO_Base_Instance::Load()
GetWMO().CreateInsances(std::dynamic_pointer_cast<CWMO_Base_Instance>(shared_from_this()));

//m_LightComponent = AddComponentT(MakeShared(CWMOLightComponent, *this));
m_PortalsComponent = AddComponentT(MakeShared(CWMO_PortalsComponent, *this));

return true;
}
Expand Down
2 changes: 2 additions & 0 deletions owGame/WMO/WMO_Base_Instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "WMO_Liquid_Instance.h"

#include "WMO_LightComponent.h"
#include "WMO_PortalsComponent.h"

class ZN_API CWMO_Base_Instance
: public CSceneNode
Expand Down Expand Up @@ -46,6 +47,7 @@ class ZN_API CWMO_Base_Instance
//GroupInstances m_OutdoorGroupInstances;

std::shared_ptr<CWMOLightComponent> m_LightComponent;
std::shared_ptr<CWMO_PortalsComponent> m_PortalsComponent;

private:
std::shared_ptr<CWMO> m_WMOObject;
Expand Down
10 changes: 10 additions & 0 deletions owGame/WMO/WMO_Group_Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ bool CWMO_Group_Instance::Load()

GetWMOGroup().CreateInsances(std::dynamic_pointer_cast<CWMO_Group_Instance>(shared_from_this()));

#ifdef USE_WMO_PORTALS_CULLING
CreatePortals(std::dynamic_pointer_cast<CWMO_Base_Instance>(GetParent()));
#endif

return true;
}

Expand Down Expand Up @@ -159,6 +163,12 @@ const CWMOGroup& CWMO_Group_Instance::GetWMOGroup() const

void CWMO_Group_Instance::CreatePortals(const std::shared_ptr<CWMO_Base_Instance>& BaseInstance)
{
if (BaseInstance == nullptr)
{
Log::Error("CWMO_Group_Instance::CreatePortals: BaseInstance is nullptr.");
return;
}

for (const auto& p : GetWMOGroup().GetPortals())
{
std::shared_ptr<CWMO_Group_Instance> roomInnerInstance = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion owGame/WMO/WMO_Group_Instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifdef USE_WMO_MODELS

#include "WMOGroup.h"
#include "WMO_Portal_Instance.h"
#include "WMO_Portal.h"

// FORWARD BEGIN
class CWMO_Base_Instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifdef USE_WMO_MODELS

// General
#include "WMO_Portal_Instance.h"
#include "WMO_Portal.h"

CWMOPortalInstance::CWMOPortalInstance(const std::weak_ptr<IPortalRoom>& RoomInner, const std::weak_ptr<IPortalRoom>& RoomOuter, const std::vector<glm::vec3>& PortalVertices, const Plane& PortalPlane)
: m_RoomInner(RoomInner)
Expand Down
2 changes: 2 additions & 0 deletions owGame/WMO/WMO_Portal_Instance.h → owGame/WMO/WMO_Portal.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#ifdef USE_WMO_MODELS
#ifdef USE_WMO_PORTALS_CULLING

class CWMOPortalInstance
: public IPortal
Expand All @@ -27,4 +28,5 @@ class CWMOPortalInstance
Plane m_Plane;
};

#endif
#endif
Original file line number Diff line number Diff line change
@@ -1,44 +1,50 @@
#include "stdafx.h"

#ifdef USE_WMO_MODELS
#ifdef USE_WMO_PORTALS_CULLING

// Include
#include "WMO.h"
#include "WMO_Base_Instance.h"

// General
#include "WMO_PortalsController.h"
#include "WMO_PortalsComponent.h"

// Additional
#include "WMO_Group_Instance.h"

#ifdef USE_WMO_PORTALS_CULLING

CWMO_PortalsController::CWMO_PortalsController()
CWMO_PortalsComponent::CWMO_PortalsComponent(const CWMO_Base_Instance& OwnerNode)
: CSceneNodeComponentBase(OwnerNode)
{}

CWMO_PortalsController::~CWMO_PortalsController()
CWMO_PortalsComponent::~CWMO_PortalsComponent()
{}

void CWMO_PortalsController::Update(const CWMO_Base_Instance* WMOBaseInstance, const ICameraComponent3D* _camera)


//
// ISceneNodeComponent
//
void CWMO_PortalsComponent::Update(const UpdateEventArgs& e)
{
if (false == WMOBaseInstance->IsLoaded())
if (false == GetWMOOwnerNode().IsLoaded())
return;

// Reset all flags
for (const auto& groupPtr : WMOBaseInstance->getGroupInstances())
for (const auto& groupPtr : GetWMOOwnerNode().getGroupInstances())
if (auto group = groupPtr.lock())
group->Reset();

glm::vec3 cameraTranslate = _camera->GetPosition();
Frustum cameraFrustum = _camera->GetFrustum();
const ICameraComponent3D * camera = e.CameraForCulling;
glm::vec3 cameraTranslate = camera->GetPosition();
Frustum cameraFrustum = camera->GetFrustum();
bool insideIndoor = false;

BoundingBox wmoBaseInstanceBounds = WMOBaseInstance->GetComponentT<IColliderComponent>()->GetWorldBounds();
BoundingBox wmoBaseInstanceBounds = GetWMOOwnerNode().GetComponentT<IColliderComponent>()->GetWorldBounds();

if (wmoBaseInstanceBounds.isPointInside(cameraTranslate))
{
for (const auto& groupPtr : WMOBaseInstance->getGroupInstances())
for (const auto& groupPtr : GetWMOOwnerNode().getGroupInstances())
{
std::shared_ptr<IPortalRoom> portalRoom = groupPtr.lock();
if (portalRoom == nullptr)
Expand Down Expand Up @@ -72,11 +78,11 @@ void CWMO_PortalsController::Update(const CWMO_Base_Instance* WMOBaseInstance, c
// If we outside WMO, then get outdorr group
//if (false == insideIndoor)
{
for (const auto& groupPtr : WMOBaseInstance->getGroupInstances())
for (const auto& groupPtr : GetWMOOwnerNode().getGroupInstances())
{
if (auto group = groupPtr.lock())
{
if (group->IsOutdoor())
if (group->IsLoaded() && group->IsOutdoor())
{
Recur(group, cameraFrustum, cameraTranslate, cameraFrustum, true);
}
Expand All @@ -87,10 +93,20 @@ void CWMO_PortalsController::Update(const CWMO_Base_Instance* WMOBaseInstance, c



//
// Protected
//
const CWMO_Base_Instance& CWMO_PortalsComponent::GetWMOOwnerNode() const
{
return dynamic_cast<const CWMO_Base_Instance&>(GetOwnerNode());
}



//
// Private
//
bool CWMO_PortalsController::Recur(const std::shared_ptr<IPortalRoom>& Room, const Frustum& CameraFrustum, const glm::vec3& CameraPosition, const Frustum& PortalFrustum, bool _isFirstIteration)
bool CWMO_PortalsComponent::Recur(const std::shared_ptr<IPortalRoom>& Room, const Frustum& CameraFrustum, const glm::vec3& CameraPosition, const Frustum& PortalFrustum, bool _isFirstIteration)
{
if (Room->IsCalculated())
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
#pragma once

#ifdef USE_WMO_MODELS
#ifdef USE_WMO_PORTALS_CULLING

#include "WMO_Headers.h"

// FORWARD BEGIN
class CWMO_Base_Instance;
// FORWARD END

#ifdef USE_WMO_PORTALS_CULLING

class CWMO_PortalsController
class ZN_API CWMO_PortalsComponent
: public CSceneNodeComponentBase
{
public:
CWMO_PortalsController();
virtual ~CWMO_PortalsController();
ZN_OBJECTCLASS(cSceneNodeComponentWMOPortalsComponent);

CWMO_PortalsComponent(const CWMO_Base_Instance& OwnerNode);
virtual ~CWMO_PortalsComponent();

void Update(const CWMO_Base_Instance* WMOBaseInstance, const ICameraComponent3D* _camera);
// ISceneNodeComponent
void Update(const UpdateEventArgs& e) override;

protected:
const CWMO_Base_Instance& GetWMOOwnerNode() const;

private:
bool Recur(const std::shared_ptr<IPortalRoom>& Room, const Frustum& CameraFrustum, const glm::vec3& CameraPosition, const Frustum& _frustum, bool _isFirstIteration);
};

#endif

#endif

0 comments on commit 9b7a2e5

Please sign in to comment.