/
PatchRenderables.h
215 lines (170 loc) · 5.98 KB
/
PatchRenderables.h
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* \file
* These are the renderables that are used in the PatchNode/Patch class to draw
* the patch onto the screen.
*/
#pragma once
#include <iterator>
#include "igl.h"
#include "imodelsurface.h"
#include "PatchTesselation.h"
#include "render/VertexBuffer.h"
#include "render/IndexedVertexBuffer.h"
#include "render/RenderableGeometry.h"
#if 0
/// Helper class to render a PatchTesselation in solid mode
class RenderablePatchSolid :
public OpenGLRenderable
#ifdef RENDERABLE_GEOMETRY
, public RenderableGeometry
#endif
{
// Geometry source
PatchTesselation& _tess;
// VertexBuffer for rendering
typedef render::IndexedVertexBuffer<ArbitraryMeshVertex> VertexBuffer_T;
mutable VertexBuffer_T _vertexBuf;
mutable bool _needsUpdate;
// The render indices to render the mesh vertices as QUADS
std::vector<unsigned int> _indices;
public:
RenderablePatchSolid(PatchTesselation& tess);
void render(const RenderInfo& info) const;
void queueUpdate();
#ifdef RENDERABLE_GEOMETRY
Type getType() const override;
const Vector3& getFirstVertex() override;
std::size_t getVertexStride() override;
const unsigned int& getFirstIndex() override;
std::size_t getNumIndices() override;
private:
void updateIndices();
#endif
};
#endif
// Renders a vertex' normal/tangent/bitangent vector (for debugging purposes)
class RenderablePatchVectorsNTB :
public OpenGLRenderable
{
private:
std::vector<VertexCb> _vertices;
const PatchTesselation& _tess;
ShaderPtr _shader;
public:
const ShaderPtr& getShader() const;
RenderablePatchVectorsNTB(const PatchTesselation& tess);
void setRenderSystem(const RenderSystemPtr& renderSystem);
void render(const RenderInfo& info) const;
void render(IRenderableCollector& collector, const VolumeTest& volume, const Matrix4& localToWorld) const;
};
class ITesselationIndexer
{
public:
virtual ~ITesselationIndexer() {}
virtual render::GeometryType getType() const = 0;
// The number of indices generated by this indexer for the given tesselation
virtual std::size_t getNumIndices(const PatchTesselation& tess) const = 0;
// Generate the indices for the given tesselation, assigning them to the given insert iterator
virtual void generateIndices(const PatchTesselation& tess, std::back_insert_iterator<std::vector<unsigned int>> outputIt) const = 0;
};
class TesselationIndexer_Triangles :
public ITesselationIndexer
{
public:
render::GeometryType getType() const override
{
return render::GeometryType::Triangles;
}
std::size_t getNumIndices(const PatchTesselation& tess) const override
{
return (tess.height - 1) * (tess.width - 1) * 6; // 6 => 2 triangles per quad
}
void generateIndices(const PatchTesselation& tess, std::back_insert_iterator<std::vector<unsigned int>> outputIt) const override
{
// Generate the indices to define the triangles in clockwise order
for (std::size_t h = 0; h < tess.height - 1; ++h)
{
auto rowOffset = h * tess.width;
for (std::size_t w = 0; w < tess.width - 1; ++w)
{
outputIt = static_cast<unsigned int>(rowOffset + w + tess.width);
outputIt = static_cast<unsigned int>(rowOffset + w + 1);
outputIt = static_cast<unsigned int>(rowOffset + w);
outputIt = static_cast<unsigned int>(rowOffset + w + tess.width);
outputIt = static_cast<unsigned int>(rowOffset + w + tess.width + 1);
outputIt = static_cast<unsigned int>(rowOffset + w + 1);
}
}
}
};
class TesselationIndexer_Quads :
public ITesselationIndexer
{
public:
render::GeometryType getType() const override
{
return render::GeometryType::Quads;
}
std::size_t getNumIndices(const PatchTesselation& tess) const override
{
return (tess.height - 1) * (tess.width - 1) * 4; // 4 indices per quad
}
void generateIndices(const PatchTesselation& tess, std::back_insert_iterator<std::vector<unsigned int>> outputIt) const override
{
for (std::size_t h = 0; h < tess.height - 1; ++h)
{
auto rowOffset = h * tess.width;
for (std::size_t w = 0; w < tess.width - 1; ++w)
{
outputIt = static_cast<unsigned int>(rowOffset + w);
outputIt = static_cast<unsigned int>(rowOffset + w + tess.width);
outputIt = static_cast<unsigned int>(rowOffset + w + tess.width + 1);
outputIt = static_cast<unsigned int>(rowOffset + w + 1);
}
}
}
};
template<typename TesselationIndexerT>
class RenderablePatchTesselation :
public render::RenderableGeometry
{
private:
static_assert(std::is_base_of_v<ITesselationIndexer, TesselationIndexerT>, "Indexer must implement ITesselationIndexer");
TesselationIndexerT _indexer;
const PatchTesselation& _tess;
bool _needsUpdate;
std::size_t _size;
public:
RenderablePatchTesselation(const PatchTesselation& tess) :
_tess(tess),
_needsUpdate(true),
_size(0)
{}
void clear() override
{
RenderableGeometry::clear();
_size = 0;
}
void queueUpdate()
{
_needsUpdate = true;
}
void update(const ShaderPtr& shader)
{
bool shaderChanged = _shader != shader;
if (!_needsUpdate && !shaderChanged) return;
_needsUpdate = false;
auto sizeChanged = _tess.vertices.size() != _size;
if (_shader && _surfaceSlot != render::IGeometryRenderer::InvalidSlot && (shaderChanged || sizeChanged))
{
clear();
}
_shader = shader;
_size = _tess.vertices.size();
// Generate the index array
std::vector<unsigned int> indices;
indices.reserve(_indexer.getNumIndices(_tess));
_indexer.generateIndices(_tess, std::back_inserter(indices));
addOrUpdateGeometry(shader, _indexer.getType(), _tess.vertices, indices);
}
};