Skip to content

Commit

Permalink
#5584: Make the client code slimmer, the size changes are detected by…
Browse files Browse the repository at this point in the history
… the base class now
  • Loading branch information
codereader committed Dec 4, 2021
1 parent 35132bb commit 6244508
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 41 deletions.
23 changes: 19 additions & 4 deletions libs/render/RenderableGeometry.h
Expand Up @@ -21,9 +21,14 @@ class RenderableGeometry :
ShaderPtr _shader;
IGeometryRenderer::Slot _surfaceSlot;

std::size_t _lastVertexSize; // To detect size changes when updating geometry
std::size_t _lastIndexSize; // To detect size changes when updating geometry

protected:
RenderableGeometry() :
_surfaceSlot(IGeometryRenderer::InvalidSlot)
_surfaceSlot(IGeometryRenderer::InvalidSlot),
_lastVertexSize(0),
_lastIndexSize(0)
{}

public:
Expand Down Expand Up @@ -54,14 +59,15 @@ class RenderableGeometry :
}

// Removes the geometry and clears the shader reference
virtual void clear()
void clear()
{
removeGeometry();

_shader.reset();
}

virtual void render(const RenderInfo& info) const override
// Renders the geometry stored in our single slot
void render(const RenderInfo& info) const override
{
if (_surfaceSlot != IGeometryRenderer::InvalidSlot && _shader)
{
Expand Down Expand Up @@ -89,10 +95,19 @@ class RenderableGeometry :
// Submits the given geometry to the known _shader reference
// This method is supposed to be called from within updateGeometry()
// to ensure that the _shader reference is already up to date.
virtual void addOrUpdateGeometry(GeometryType type,
void updateGeometry(GeometryType type,
const std::vector<ArbitraryMeshVertex>& vertices,
const std::vector<unsigned int>& indices)
{
// Size changes require removal of the geometry before update
if (_lastVertexSize != vertices.size() || _lastIndexSize != indices.size())
{
removeGeometry();

_lastVertexSize = vertices.size();
_lastIndexSize = indices.size();
}

if (_surfaceSlot == IGeometryRenderer::InvalidSlot)
{
_surfaceSlot = _shader->addGeometry(type, vertices, indices);
Expand Down
22 changes: 2 additions & 20 deletions radiantcore/entity/target/RenderableTargetLines.h
Expand Up @@ -31,26 +31,17 @@ class RenderableTargetLines :
const TargetKeyCollection& _targetKeys;

Vector3 _worldPosition;
std::size_t _numVisibleLines;

public:
RenderableTargetLines(const TargetKeyCollection& targetKeys) :
_targetKeys(targetKeys),
_numVisibleLines(0)
_targetKeys(targetKeys)
{}

bool hasTargets() const
{
return !_targetKeys.empty();
}

void clear() override
{
RenderableGeometry::clear();

_numVisibleLines = 0;
}

void update(const ShaderPtr& shader, const Vector3& worldPosition)
{
// Store the new world position for use in updateGeometry()
Expand All @@ -71,7 +62,6 @@ class RenderableTargetLines :
std::vector<ArbitraryMeshVertex> vertices;
std::vector<unsigned int> indices;
auto maxTargets = _targetKeys.getNumTargets();
auto numVisibleLines = 0;

vertices.reserve(6 * maxTargets);
indices.reserve(6 * maxTargets);
Expand All @@ -83,20 +73,12 @@ class RenderableTargetLines :
return;
}

numVisibleLines++;
auto targetPosition = target->getPosition();

addTargetLine(_worldPosition, targetPosition, vertices, indices);
});

// Size changes requires detaching our geometry first
if (numVisibleLines != _numVisibleLines)
{
removeGeometry();
_numVisibleLines = numVisibleLines;
}

addOrUpdateGeometry(render::GeometryType::Lines, vertices, indices);
RenderableGeometry::updateGeometry(render::GeometryType::Lines, vertices, indices);
}

private:
Expand Down
19 changes: 2 additions & 17 deletions radiantcore/patch/PatchRenderables.h
Expand Up @@ -167,21 +167,13 @@ class RenderablePatchTesselation :

const PatchTesselation& _tess;
bool _needsUpdate;
std::size_t _size;

public:
RenderablePatchTesselation(const PatchTesselation& tess) :
_tess(tess),
_needsUpdate(true),
_size(0)
_needsUpdate(true)
{}

void clear() override
{
RenderableGeometry::clear();
_size = 0;
}

void queueUpdate()
{
_needsUpdate = true;
Expand All @@ -194,19 +186,12 @@ class RenderablePatchTesselation :

_needsUpdate = false;

// Tesselation size change requires to remove the geometry first
if (_tess.vertices.size() != _size)
{
removeGeometry();
_size = _tess.vertices.size();
}

// Generate the new index array
std::vector<unsigned int> indices;
indices.reserve(_indexer.getNumIndices(_tess));

_indexer.generateIndices(_tess, std::back_inserter(indices));

addOrUpdateGeometry(_indexer.getType(), _tess.vertices, indices);
RenderableGeometry::updateGeometry(_indexer.getType(), _tess.vertices, indices);
}
};

0 comments on commit 6244508

Please sign in to comment.