Skip to content

Commit

Permalink
#5584: Model rendering working on camera and ortho views, no highligh…
Browse files Browse the repository at this point in the history
…ting yet
  • Loading branch information
codereader committed Jan 9, 2022
1 parent ef93420 commit 9660303
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
53 changes: 39 additions & 14 deletions radiantcore/model/StaticModelNode.cpp
Expand Up @@ -16,7 +16,8 @@ namespace model

StaticModelNode::StaticModelNode(const StaticModelPtr& picoModel) :
_model(new StaticModel(*picoModel)),
_name(picoModel->getFilename())
_name(picoModel->getFilename()),
_attachedToShaders(false)
{
// Update the skin
skinChanged("");
Expand All @@ -26,14 +27,12 @@ void StaticModelNode::onInsertIntoScene(scene::IMapRootNode& root)
{
_model->connectUndoSystem(root.getUndoSystem());

// Generate renderables
// Renderables will acquire their shaders in onPreRender
_model->foreachSurface([&](const StaticModelSurface& surface)
{
_renderableSurfaces.emplace_back(std::make_shared<RenderableStaticSurface>(surface, localToWorld()));
});

updateShaders();

Node::onInsertIntoScene(root);
}

Expand Down Expand Up @@ -97,6 +96,14 @@ bool StaticModelNode::isOriented() const
return false;
}

void StaticModelNode::onPreRender(const VolumeTest& volume)
{
assert(_renderEntity);

// Attach renderables (or do nothing if everything is up to date)
attachToShaders();
}

void StaticModelNode::renderSolid(IRenderableCollector& collector, const VolumeTest& volume) const
{
assert(_renderEntity);
Expand Down Expand Up @@ -143,25 +150,42 @@ void StaticModelNode::setRenderSystem(const RenderSystemPtr& renderSystem)
Node::setRenderSystem(renderSystem);

_renderSystem = renderSystem;
updateShaders();

// Detach renderables on render system change
detachFromShaders();

_model->setRenderSystem(renderSystem);
}

void StaticModelNode::updateShaders()
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->clear();
}

_attachedToShaders = false;
}

void StaticModelNode::attachToShaders()
{
if (_attachedToShaders) return;

_attachedToShaders = true;

auto renderSystem = _renderSystem.lock();

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

// For orthoview rendering we need the entity's colour shader
if (_renderEntity)
{
surface->clear();
surface->attachToShader(_renderEntity->getColourShader());
}
}
}
Expand All @@ -183,7 +207,8 @@ void StaticModelNode::skinChanged(const std::string& newSkinName)
ModelSkin& skin = GlobalModelSkinCache().capture(_skin);
_model->applySkin(skin);

updateShaders();
// Detach from existing shaders, re-acquire them in onPreRender
detachFromShaders();

// Refresh the scene (TODO: get rid of that)
GlobalSceneGraph().sceneChanged();
Expand Down
6 changes: 5 additions & 1 deletion radiantcore/model/StaticModelNode.h
Expand Up @@ -47,6 +47,8 @@ class StaticModelNode final :
// We need to keep a reference for skin swapping
RenderSystemWeakPtr _renderSystem;

bool _attachedToShaders;

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

Expand Down Expand Up @@ -82,6 +84,7 @@ class StaticModelNode final :

// Renderable implementation
bool isOriented() const override;
void onPreRender(const VolumeTest& volume) override;
void renderSolid(IRenderableCollector& collector, const VolumeTest& volume) const override;
void renderWireframe(IRenderableCollector& collector, const VolumeTest& volume) const override;
void renderHighlights(IRenderableCollector& collector, const VolumeTest& volume) override;
Expand All @@ -100,7 +103,8 @@ class StaticModelNode final :
void _applyTransformation() override;

private:
void updateShaders();
void attachToShaders();
void detachFromShaders();
};

} // namespace model

0 comments on commit 9660303

Please sign in to comment.