1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 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 GetGeometryType()
{
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 GetGeometryType()
{
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<Vertex3>& _vertices;
bool _needsUpdate;
Vector4 _colour;
public:
void queueUpdate()
{
_needsUpdate = true;
}
void setColour(const Vector4& colour)
{
_colour = colour;
queueUpdate();
}
public:
RenderableVertexArray(const std::vector<Vertex3>& vertices) :
_vertices(vertices),
_needsUpdate(true)
{}
void updateGeometry() override
{
if (!_needsUpdate) return;
_needsUpdate = false;
std::vector<RenderVertex> vertices;
for (const auto& vertex : _vertices)
{
vertices.push_back(RenderVertex(vertex, { 0,0,0 }, { 0,0 }, _colour));
}
std::vector<unsigned int> indices;
Indexer::GenerateIndices(indices, _vertices.size());
updateGeometryWithData(Indexer::GetGeometryType(), 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>;
}
|