Skip to content

Commit

Permalink
#5584: Introduce a separate colour shader that is applicable to both …
Browse files Browse the repository at this point in the history
…camera and ortho views
  • Loading branch information
codereader committed Dec 4, 2021
1 parent 3e50d82 commit 593ccd1
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 5 deletions.
3 changes: 3 additions & 0 deletions include/ieclass.h
Expand Up @@ -185,6 +185,9 @@ class IEntityClass :
/// Get the shader used for rendering this entity class in filled mode.
virtual const std::string& getFillShader() const = 0;

// Get the shader name used for rendering coloured primitives in both cam and ortho
virtual const std::string& getColourShader() const = 0;


/* ENTITY CLASS ATTRIBUTES */

Expand Down
8 changes: 8 additions & 0 deletions include/irender.h
Expand Up @@ -150,6 +150,14 @@ class IRenderEntity
* Returns the wireframe shader for this entity - child primitives need this for rendering.
*/
virtual const ShaderPtr& getWireShader() const = 0;

/**
* Returns the shader that is used to render coloured primitives like lines and points,
* using the colour of the entity as defined by its entityDef, light colour, etc.
* This shader will be applicable to both camera and ortho views, it can be used for
* visualisations such as target lines and light boxes.
*/
virtual const ShaderPtr& getColourShader() const = 0;
};
typedef std::shared_ptr<IRenderEntity> IRenderEntityPtr;
typedef std::weak_ptr<IRenderEntity> IRenderEntityWeakPtr;
Expand Down
8 changes: 8 additions & 0 deletions radiantcore/eclass/EntityClass.cpp
Expand Up @@ -14,6 +14,7 @@ namespace eclass

const std::string EntityClass::DefaultWireShader("<0.3 0.3 1>");
const std::string EntityClass::DefaultFillShader("(0.3 0.3 1)");
const std::string EntityClass::DefaultColourShader("{0.3 0.3 1}");
const Vector3 EntityClass::DefaultEntityColour(0.3, 0.3, 1);
const EntityClassAttribute EntityClass::_emptyAttribute("", "", "");

Expand Down Expand Up @@ -107,6 +108,7 @@ void EntityClass::setColour(const Vector3& colour)
fmt::format("({0:f} {1:f} {2:f})", _colour[0], _colour[1], _colour[2]);

_wireShader = fmt::format("<{0:f} {1:f} {2:f}>", _colour[0], _colour[1], _colour[2]);
_colourShader = fmt::format("{{{0:f} {1:f} {2:f}}}", _colour[0], _colour[1], _colour[2]);

emitChangedSignal();
}
Expand Down Expand Up @@ -148,6 +150,12 @@ const std::string& EntityClass::getFillShader() const
return !_fillShader.empty() ? _fillShader : DefaultFillShader;
}

const std::string& EntityClass::getColourShader() const
{
// Use a fallback shader colour in case we don't have anything
return !_colourShader.empty() ? _colourShader : DefaultColourShader;
}

/* ATTRIBUTES */

/**
Expand Down
3 changes: 3 additions & 0 deletions radiantcore/eclass/EntityClass.h
Expand Up @@ -52,11 +52,13 @@ class EntityClass
// Default shader names, in case we don't get any from the parent or otherwise
static const std::string DefaultWireShader;
static const std::string DefaultFillShader;
static const std::string DefaultColourShader;
static const Vector3 DefaultEntityColour;

// Shader versions of the colour
std::string _fillShader;
std::string _wireShader;
std::string _colourShader;

// Does this entity have a fixed size?
bool _fixedSize;
Expand Down Expand Up @@ -148,6 +150,7 @@ class EntityClass
void resetColour();
const std::string& getWireShader() const override;
const std::string& getFillShader() const override;
const std::string& getColourShader() const override;
EntityClassAttribute& getAttribute(const std::string&, bool includeInherited = true) override;
const EntityClassAttribute& getAttribute(const std::string&, bool includeInherited = true) const override;
const std::string& getAttributeType(const std::string& name) const override;
Expand Down
7 changes: 7 additions & 0 deletions radiantcore/entity/EntityNode.cpp
Expand Up @@ -410,11 +410,13 @@ void EntityNode::acquireShaders(const RenderSystemPtr& renderSystem)
{
_fillShader = renderSystem->capture(_spawnArgs.getEntityClass()->getFillShader());
_wireShader = renderSystem->capture(_spawnArgs.getEntityClass()->getWireShader());
_colourShader = renderSystem->capture(_spawnArgs.getEntityClass()->getColourShader());
}
else
{
_fillShader.reset();
_wireShader.reset();
_colourShader.reset();
}
}

Expand Down Expand Up @@ -469,6 +471,11 @@ const ShaderPtr& EntityNode::getWireShader() const
return _wireShader;
}

const ShaderPtr& EntityNode::getColourShader() const
{
return _colourShader;
}

const ShaderPtr& EntityNode::getFillShader() const
{
return _fillShader;
Expand Down
9 changes: 6 additions & 3 deletions radiantcore/entity/EntityNode.h
Expand Up @@ -73,9 +73,11 @@ class EntityNode :
// This entity's main direction, usually determined by the angle/rotation keys
Vector3 _direction;

// The wireframe / solid shaders as determined by the entityclass
ShaderPtr _fillShader;
ShaderPtr _wireShader;
// The coloured shaders as determined by the entityclass

ShaderPtr _fillShader; // cam only
ShaderPtr _wireShader; // ortho only
ShaderPtr _colourShader; // cam+ortho view

sigc::connection _eclassChangedConn;

Expand Down Expand Up @@ -160,6 +162,7 @@ class EntityNode :
const ModelKey& getModelKey() const;

const ShaderPtr& getWireShader() const override;
const ShaderPtr& getColourShader() const override;
const ShaderPtr& getFillShader() const;

virtual void onPostUndo() override;
Expand Down
2 changes: 1 addition & 1 deletion radiantcore/entity/target/TargetLineNode.cpp
Expand Up @@ -53,7 +53,7 @@ void TargetLineNode::onPreRender(const VolumeTest& volume)
return;
}

_targetLines.update(_owner.getWireShader(), getOwnerPosition());
_targetLines.update(_owner.getColourShader(), getOwnerPosition());
}

void TargetLineNode::renderSolid(IRenderableCollector& collector, const VolumeTest& volume) const
Expand Down
25 changes: 24 additions & 1 deletion radiantcore/rendersystem/backend/OpenGLShader.cpp
Expand Up @@ -60,7 +60,8 @@ OpenGLShader::OpenGLShader(const std::string& name, OpenGLRenderSystem& renderSy
_name(name),
_renderSystem(renderSystem),
_isVisible(true),
_useCount(0)
_useCount(0),
_enabledViewTypes(0)
{
_windingRenderer.reset(new WindingRenderer<WindingIndexer_Triangles>());
}
Expand All @@ -77,6 +78,7 @@ OpenGLRenderSystem& OpenGLShader::getRenderSystem()

void OpenGLShader::destroy()
{
_enabledViewTypes = 0;
_vertexBuffer.reset();
_materialChanged.disconnect();
_material.reset();
Expand Down Expand Up @@ -875,6 +877,27 @@ void OpenGLShader::construct()
state.m_linewidth = 1;
state.m_pointsize = 1;

enableViewType(RenderViewType::OrthoView);
break;
}

case '{': // cam + wireframe shader
{
OpenGLState& state = appendDefaultPass();
state.setName(_name);

Colour4 colour;
sscanf(_name.c_str(), "{%f %f %f}", &colour[0], &colour[1], &colour[2]);
colour[3] = 1;
state.setColour(colour);

state.setRenderFlags(RENDER_DEPTHTEST | RENDER_DEPTHWRITE);
state.setSortPosition(OpenGLState::SORT_FULLBRIGHT);
state.setDepthFunc(GL_LESS);
state.m_linewidth = 1;
state.m_pointsize = 1;

// Applicable to both views
enableViewType(RenderViewType::OrthoView);
enableViewType(RenderViewType::Camera);
break;
Expand Down

0 comments on commit 593ccd1

Please sign in to comment.