Skip to content

Commit

Permalink
#6009: Move surface attach/detach code to base
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Oct 29, 2022
1 parent 9beffa7 commit 84248c4
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 113 deletions.
53 changes: 53 additions & 0 deletions radiantcore/model/ModelNodeBase.cpp
Expand Up @@ -3,6 +3,10 @@
namespace model
{

ModelNodeBase::ModelNodeBase() :
_attachedToShaders(false)
{}

void ModelNodeBase::renderHighlights(IRenderableCollector& collector, const VolumeTest& volume)
{
auto identity = Matrix4::getIdentity();
Expand All @@ -13,4 +17,53 @@ void ModelNodeBase::renderHighlights(IRenderableCollector& collector, const Volu
}
}

void ModelNodeBase::detachFromShaders()
{
// Detach any existing surfaces. In case we need them again,
// the node will re-attach in the next pre-render phase
for (auto& surface : _renderableSurfaces)
{
surface->detach();
}

_attachedToShaders = false;
}

void ModelNodeBase::attachToShaders()
{
// Refuse to attach without a render entity
if (_attachedToShaders || !_renderEntity) return;

auto renderSystem = _renderSystem.lock();

if (!renderSystem) return;

for (auto& surface : _renderableSurfaces)
{
auto shader = renderSystem->capture(surface->getSurface().getActiveMaterial());

// Skip filtered materials
//TODO if (!shader->isVisible()) continue;

// Solid mode
surface->attachToShader(shader);

// For orthoview rendering we need the entity's wireframe shader
surface->attachToShader(_renderEntity->getWireShader());

// Attach to the render entity for lighting mode rendering
surface->attachToEntity(_renderEntity, shader);
}

_attachedToShaders = true;
}

void ModelNodeBase::queueRenderableUpdate()
{
for (auto& surface : _renderableSurfaces)
{
surface->queueUpdate();
}
}

}
10 changes: 8 additions & 2 deletions radiantcore/model/ModelNodeBase.h
Expand Up @@ -17,12 +17,18 @@ class ModelNodeBase :
// The renderable surfaces attached to the shaders
std::vector<RenderableModelSurface::Ptr> _renderableSurfaces;

bool _attachedToShaders;

protected:
ModelNodeBase()
{}
ModelNodeBase();

public:
void renderHighlights(IRenderableCollector& collector, const VolumeTest& volume) override;

protected:
void attachToShaders();
void detachFromShaders();
void queueRenderableUpdate();
};

}
57 changes: 1 addition & 56 deletions radiantcore/model/StaticModelNode.cpp
Expand Up @@ -4,20 +4,14 @@
#include "ivolumetest.h"
#include "ishaders.h"
#include "iscenegraph.h"
#include "ifilter.h"
#include "imap.h"
#include "imodelcache.h"
#include "math/Frustum.h"
#include "generic/callback.h"
#include <functional>

namespace model
{

StaticModelNode::StaticModelNode(const StaticModelPtr& picoModel) :
_model(new StaticModel(*picoModel)),
_name(picoModel->getFilename()),
_attachedToShaders(false)
_name(picoModel->getFilename())
{
_model->signal_ShadersChanged().connect(sigc::mem_fun(*this, &StaticModelNode::onModelShadersChanged));
_model->signal_SurfaceScaleApplied().connect(sigc::mem_fun(*this, &StaticModelNode::onModelScaleApplied));
Expand Down Expand Up @@ -120,55 +114,6 @@ void StaticModelNode::setRenderSystem(const RenderSystemPtr& renderSystem)
_model->setRenderSystem(renderSystem);
}

void StaticModelNode::detachFromShaders()
{
// Detach any existing surfaces. In case we need them again,
// the node will re-attach in the next pre-render phase
for (auto& surface : _renderableSurfaces)
{
surface->detach();
}

_attachedToShaders = false;
}

void StaticModelNode::attachToShaders()
{
// Refuse to attach without a render entity
if (_attachedToShaders || !_renderEntity) return;

auto renderSystem = _renderSystem.lock();

if (!renderSystem) return;

for (auto& surface : _renderableSurfaces)
{
auto shader = renderSystem->capture(surface->getSurface().getActiveMaterial());

// Skip filtered materials
//TODO if (!shader->isVisible()) continue;

// Solid mode
surface->attachToShader(shader);

// For orthoview rendering we need the entity's wireframe shader
surface->attachToShader(_renderEntity->getWireShader());

// Attach to the render entity for lighting mode rendering
surface->attachToEntity(_renderEntity, shader);
}

_attachedToShaders = true;
}

void StaticModelNode::queueRenderableUpdate()
{
for (auto& surface : _renderableSurfaces)
{
surface->queueUpdate();
}
}

void StaticModelNode::onModelShadersChanged()
{
// Detach renderables on model shader change,
Expand Down
9 changes: 0 additions & 9 deletions radiantcore/model/StaticModelNode.h
Expand Up @@ -8,7 +8,6 @@
#include "ModelNodeBase.h"
#include "Transformable.h"
#include "StaticModel.h"
#include "RenderableModelSurface.h"

namespace model
{
Expand Down Expand Up @@ -39,11 +38,6 @@ class StaticModelNode final :
// The name of this model's skin
std::string _skin;

// The renderable surfaces attached to the shaders
std::vector<RenderableModelSurface::Ptr> _renderableSurfaces;

bool _attachedToShaders;

public:
typedef std::shared_ptr<StaticModelNode> Ptr;

Expand Down Expand Up @@ -101,9 +95,6 @@ class StaticModelNode final :
void onVisibilityChanged(bool isVisibleNow) override;

private:
void attachToShaders();
void detachFromShaders();
void queueRenderableUpdate();
void onModelShadersChanged();
};

Expand Down
38 changes: 0 additions & 38 deletions radiantcore/model/md5/MD5ModelNode.cpp
@@ -1,17 +1,14 @@
#include "MD5ModelNode.h"

#include "ivolumetest.h"
#include "imodelcache.h"
#include "ishaders.h"
#include "iscenegraph.h"
#include <functional>

namespace md5
{

MD5ModelNode::MD5ModelNode(const MD5ModelPtr& model) :
_model(new MD5Model(*model)), // create a copy of the incoming model, we need our own instance
_attachedToShaders(false),
_showSkeleton(RKEY_RENDER_SKELETON),
_renderableSkeleton(_model->getSkeleton(), localToWorld())
{
Expand Down Expand Up @@ -133,41 +130,6 @@ void MD5ModelNode::onPreRender(const VolumeTest& volume)
}
}

void MD5ModelNode::detachFromShaders()
{
// Detach any existing surfaces. In case we need them again,
// the node will re-attach in the next pre-render phase
for (auto& surface : _renderableSurfaces)
{
surface->detach();
}

_attachedToShaders = false;
}

void MD5ModelNode::attachToShaders()
{
if (_attachedToShaders || !_renderEntity) return;

auto renderSystem = _renderSystem.lock();

if (!renderSystem) return;

for (auto& surface : _renderableSurfaces)
{
auto shader = renderSystem->capture(surface->getSurface().getActiveMaterial());
surface->attachToShader(shader);

// For orthoview rendering we need the entity's wireframe shader
surface->attachToShader(_renderEntity->getWireShader());

// Attach to the render entity for lighting mode rendering
surface->attachToEntity(_renderEntity, shader);
}

_attachedToShaders = true;
}

std::string MD5ModelNode::getSkin() const
{
return _skin;
Expand Down
8 changes: 0 additions & 8 deletions radiantcore/model/md5/MD5ModelNode.h
Expand Up @@ -6,7 +6,6 @@
#include "model/ModelNodeBase.h"
#include "MD5Model.h"
#include "registry/CachedKey.h"
#include "../RenderableModelSurface.h"
#include "RenderableMD5Skeleton.h"

#include <sigc++/connection.h>
Expand All @@ -28,11 +27,6 @@ class MD5ModelNode :
// The name of this model's skin
std::string _skin;

// The renderable surfaces attached to the shaders
std::vector<model::RenderableModelSurface::Ptr> _renderableSurfaces;

bool _attachedToShaders;

sigc::connection _animationUpdateConnection;
sigc::connection _modelShadersChangedConnection;

Expand Down Expand Up @@ -88,8 +82,6 @@ class MD5ModelNode :

private:
void onModelAnimationUpdated();
void attachToShaders();
void detachFromShaders();
void onModelShadersChanged();
};

Expand Down

0 comments on commit 84248c4

Please sign in to comment.