Skip to content

Commit

Permalink
#5893: Migrate the MeasurementTool to use a RenderableGeometry subtype
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Feb 13, 2022
1 parent 34cd699 commit cedc797
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 35 deletions.
104 changes: 104 additions & 0 deletions libs/render/RenderableVertexArray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#pragma once

#include "RenderableGeometry.h"
#include "math/Matrix4.h"

namespace render
{

namespace detail
{

// Generates indices to render N points as line
struct LineIndexer
{
static void GenerateIndices(std::vector<unsigned int>& indices, std::size_t numPoints)
{
for (unsigned int index = 1; index < numPoints; ++index)
{
indices.push_back(index - 1);
indices.push_back(index);
}
}

static constexpr GeometryType GeometryType()
{
return GeometryType::Lines;
}
};

// Generates indices to render N points as separate points
struct PointIndexer
{
static void GenerateIndices(std::vector<unsigned int>& indices, std::size_t numPoints)
{
for (unsigned int index = 0; index < numPoints; ++index)
{
indices.push_back(index);
}
}

static constexpr GeometryType GeometryType()
{
return GeometryType::Points;
}
};

}

// Wraps around a vertex array to render it as Lines or Points
// Coordinates are specified in world space
template<typename Indexer>
class RenderableVertexArray :
public RenderableGeometry
{
protected:
const std::vector<Vertex3f>& _vertices;
bool _needsUpdate;
Vector4 _colour;

public:
void queueUpdate()
{
_needsUpdate = true;
}

void setColour(const Vector4& colour)
{
_colour = colour;
queueUpdate();
}

public:
RenderableVertexArray(const std::vector<Vertex3f>& vertices) :
_vertices(vertices),
_needsUpdate(true)
{}

void updateGeometry() override
{
if (!_needsUpdate) return;

_needsUpdate = false;

std::vector<ArbitraryMeshVertex> vertices;

for (const auto& vertex : _vertices)
{
vertices.push_back(ArbitraryMeshVertex(vertex, { 0,0,0 }, { 0,0 }, _colour));
}

std::vector<unsigned int> indices;
Indexer::GenerateIndices(indices, _vertices.size());

RenderableGeometry::updateGeometry(Indexer::GeometryType(), vertices, indices);
}
};

// Renders the vertex array using GeometryType::Points
using RenderablePoints = RenderableVertexArray<detail::PointIndexer>;

// Renders the vertex array using GeometryType::Lines
using RenderableLine = RenderableVertexArray<detail::LineIndexer>;

}
56 changes: 23 additions & 33 deletions radiant/xyview/tools/MeasurementTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ namespace ui
{

MeasurementTool::MeasurementTool() :
_points(GL_POINTS),
_lines(GL_LINE_STRIP)
_points(_vertices),
_line(_vertices)
{}

const std::string& MeasurementTool::getName()
Expand Down Expand Up @@ -44,31 +44,26 @@ MouseTool::Result MeasurementTool::onMouseDown(Event& ev)
Vector3 clickPos = xyEvent.getWorldPos();
xyEvent.getView().snapToGrid(clickPos);

assert(_lines.size() == _points.size());
Vertex3f clickVertex(clickPos);

VertexCb clickVertex(clickPos, Colour4b());

if (_points.empty())
if (_vertices.empty())
{
// If we just started, allocate two points
_points.push_back(clickVertex);
_points.push_back(clickVertex);

// Copy the vertices to the lines vector
_lines.push_back(clickVertex);
_lines.push_back(clickVertex);
_vertices.push_back(clickVertex);
_vertices.push_back(clickVertex);
}
else
{
// Store the click position and add a new vertex
_points[_points.size() - 1].vertex = clickVertex.vertex;
_lines[_lines.size() - 1].vertex = clickVertex.vertex;
_vertices[_vertices.size() - 1] = clickVertex;

// Add one additional point to the chain
_points.push_back(clickVertex);
_lines.push_back(clickVertex);
_vertices.push_back(clickVertex);
}

_points.queueUpdate();
_line.queueUpdate();

return Result::Activated;
}
catch (std::bad_cast&)
Expand All @@ -88,12 +83,11 @@ MouseTool::Result MeasurementTool::onMouseMove(Event& ev)
Vector3 endPos = xyEvent.getWorldPos();
xyEvent.getView().snapToGrid(endPos);

assert(!_points.empty());
assert(!_vertices.empty());

_points[_points.size() - 1].vertex = endPos;
_points.setColour(Colour4b(0, 0, 0, 0));

_lines[_lines.size() -1] = _points[_points.size() - 1];
_vertices[_vertices.size() - 1] = endPos;
_line.setColour({ 0, 0, 0, 0 });
_points.setColour({ 0, 0, 0, 0 });
}
catch (std::bad_cast&)
{
Expand Down Expand Up @@ -121,8 +115,9 @@ MouseTool::Result MeasurementTool::onMouseUp(Event& ev)

MeasurementTool::Result MeasurementTool::onCancel(IInteractiveView& view)
{
_points.clear();
_lines.clear();
_vertices.clear();
_line.clear();
_points.clear();

return Result::Finished;
}
Expand Down Expand Up @@ -166,19 +161,14 @@ void MeasurementTool::render(RenderSystem& renderSystem, IRenderableCollector& c
{
ensureShaders(renderSystem);

#if 0
// Render lines
collector.addRenderable(*_wireShader, _lines, Matrix4::getIdentity());

// Render points
collector.addRenderable(*_pointShader, _points, Matrix4::getIdentity());
#endif
_points.update(_pointShader);
_line.update(_wireShader);

// Render distance string
for (std::size_t i = 1; i < _points.size(); ++i)
for (std::size_t i = 1; i < _vertices.size(); ++i)
{
const Vector3& a = _points[i-1].vertex;
const Vector3& b = _points[i].vertex;
const auto& a = _vertices[i - 1];
const auto& b = _vertices[i];

glColor4fv(_colour);
glRasterPos3dv((a+b)*0.5);
Expand Down
6 changes: 4 additions & 2 deletions radiant/xyview/tools/MeasurementTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "math/Vector3.h"
#include "render.h"
#include "render/Colour4.h"
#include "render/RenderableVertexArray.h"

namespace ui
{
Expand All @@ -17,8 +18,9 @@ class MeasurementTool :
public MouseTool
{
private:
RenderablePointVector _points;
RenderablePointVector _lines;
std::vector<Vertex3f> _vertices;
render::RenderablePoints _points;
render::RenderableLine _line;

ShaderPtr _pointShader;
ShaderPtr _wireShader;
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/libs.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@
<ClInclude Include="..\..\libs\render\RenderableCollectorBase.h" />
<ClInclude Include="..\..\libs\render\RenderableColouredBoundingBoxes.h" />
<ClInclude Include="..\..\libs\render\RenderableGeometry.h" />
<ClInclude Include="..\..\libs\render\RenderableVertexArray.h" />
<ClInclude Include="..\..\libs\render\RenderablePivot.h" />
<ClInclude Include="..\..\libs\render\RenderableSpacePartition.h" />
<ClInclude Include="..\..\libs\render\RenderableSurface.h" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/libs.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@
<ClInclude Include="..\..\libs\render\ContinuousBuffer.h">
<Filter>render</Filter>
</ClInclude>
<ClInclude Include="..\..\libs\render\RenderableVertexArray.h">
<Filter>render</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Filter Include="util">
Expand Down

0 comments on commit cedc797

Please sign in to comment.