Skip to content

Commit

Permalink
Add test for the "overrideLightColour" registry key
Browse files Browse the repository at this point in the history
Confirm that a light wireframe is rendered with a default shader (based on the
entity class) if the registry key is set, then reverts to using the _color key
once the registry key is cleared.
  • Loading branch information
Matthew Mott committed Feb 13, 2021
1 parent 226163a commit b4dc4cb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
3 changes: 1 addition & 2 deletions libs/registry/CachedKey.h
Expand Up @@ -6,8 +6,7 @@ namespace registry
{

/**
* \brief
* Simple value caching class for a registry key.
* \brief Simple value caching class for a registry key.
*
* This can be used when a registry key needs to be read frequently but only
* written rarely. It stores a copy of the value converted to its given type,
Expand Down
9 changes: 4 additions & 5 deletions radiantcore/entity/light/LightNode.cpp
Expand Up @@ -26,7 +26,8 @@ LightNode::LightNode(const IEntityClassPtr& eclass) :
_lightEndInstance(_light.endTransformed(), std::bind(&LightNode::selectedChangedComponent, this, std::placeholders::_1)),
_dragPlanes(std::bind(&LightNode::selectedChangedComponent, this, std::placeholders::_1)),
_renderableRadius(_light._lightBox.origin),
_renderableFrustum(_light._lightBox.origin, _light._lightStartTransformed, _light._frustum)
_renderableFrustum(_light._lightBox.origin, _light._lightStartTransformed, _light._frustum),
_overrideColKey(colours::RKEY_OVERRIDE_LIGHTCOL)
{}

LightNode::LightNode(const LightNode& other) :
Expand All @@ -46,7 +47,8 @@ LightNode::LightNode(const LightNode& other) :
_lightEndInstance(_light.endTransformed(), std::bind(&LightNode::selectedChangedComponent, this,std::placeholders:: _1)),
_dragPlanes(std::bind(&LightNode::selectedChangedComponent, this, std::placeholders::_1)),
_renderableRadius(_light._lightBox.origin),
_renderableFrustum(_light._lightBox.origin, _light._lightStartTransformed, _light._frustum)
_renderableFrustum(_light._lightBox.origin, _light._lightStartTransformed, _light._frustum),
_overrideColKey(colours::RKEY_OVERRIDE_LIGHTCOL)
{}

LightNodePtr LightNode::Create(const IEntityClassPtr& eclass)
Expand Down Expand Up @@ -303,9 +305,6 @@ void LightNode::renderLightVolume(RenderableCollector& collector,
bool selected) const
{
// Obtain the appropriate Shader for the light volume colour
static registry::CachedKey<bool> _overrideColKey(
colours::RKEY_OVERRIDE_LIGHTCOL
);
Shader* colourShader = _overrideColKey.get() ? EntityNode::_wireShader.get()
: _colourKey.getWireShader();
if (!colourShader)
Expand Down
4 changes: 4 additions & 0 deletions radiantcore/entity/light/LightNode.h
@@ -1,6 +1,7 @@
#pragma once

#include "ilightnode.h"
#include "registry/CachedKey.h"

#include "Light.h"
#include "dragplanes.h"
Expand Down Expand Up @@ -45,6 +46,9 @@ class LightNode :
// a temporary variable for calculating the AABB of all (selected) components
mutable AABB m_aabb_component;

// Cached rkey to override light volume colour
registry::CachedKey<bool> _overrideColKey;

public:
LightNode(const IEntityClassPtr& eclass);

Expand Down
42 changes: 42 additions & 0 deletions test/Entity.cpp
Expand Up @@ -6,10 +6,12 @@
#include "iselectable.h"
#include "iselection.h"
#include "ishaders.h"
#include "icolourscheme.h"

#include "render/NopVolumeTest.h"
#include "string/convert.h"
#include "transformlib.h"
#include "registry/registry.h"

namespace test
{
Expand Down Expand Up @@ -465,6 +467,46 @@ TEST_F(EntityTest, LightVolumeColorFromColorKey)
}
}

TEST_F(EntityTest, OverrideLightVolumeColour)
{
// Create a light with an arbitrary colour
auto light = createByClassName("light");
light->getEntity().setKeyValue("_color", "0.25 0.55 0.9");

// Set the "override light volume colour" key
registry::setValue(colours::RKEY_OVERRIDE_LIGHTCOL, true);

{
RenderFixture rf;
rf.renderSubGraph(light);

// The shader should ignore the _color key and render based on the entity
// class colour
EXPECT_EQ(rf.collector.renderables, 1);
const Shader* shader = rf.collector.renderablePtrs.at(0).first;
EXPECT_EQ(shader->getMaterial()->getName(), "<0.000000 1.000000 0.000000>");
}

// Unset the override key
registry::setValue(colours::RKEY_OVERRIDE_LIGHTCOL, false);

{
RenderFixture rf;
rf.renderSubGraph(light);

// Light should be rendered with its original _color key again
EXPECT_EQ(rf.collector.renderables, 1);
const Shader* shader = rf.collector.renderablePtrs.at(0).first;
EXPECT_EQ(shader->getMaterial()->getName(), "<0.250000 0.550000 0.900000>");
}

// Changing the override key after deleting the light must not crash
// (because the LightNode's CachedKey is sigc::trackable)
light.reset();
registry::setValue(colours::RKEY_OVERRIDE_LIGHTCOL, true);
registry::setValue(colours::RKEY_OVERRIDE_LIGHTCOL, false);
}

TEST_F(EntityTest, FuncStaticLocalToWorld)
{
auto funcStatic = createByClassName("func_static");
Expand Down

0 comments on commit b4dc4cb

Please sign in to comment.