/
PatchRenderables.cpp
281 lines (224 loc) · 6.98 KB
/
PatchRenderables.cpp
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "PatchRenderables.h"
void RenderablePatchWireframe::render(const RenderInfo& info) const
{
// No colour changing
glDisableClientState(GL_COLOR_ARRAY);
if (info.checkFlag(RENDER_VERTEX_COLOUR))
{
glColor3f(1, 1, 1);
}
if (_tess.vertices.empty()) return;
if (_needsUpdate)
{
_needsUpdate = false;
// Create a VBO and add the vertex data
VertexBuffer_T currentVBuf;
currentVBuf.addVertices(_tess.vertices.begin(), _tess.vertices.end());
// Submit index batches
const RenderIndex* strip_indices = &_tess.indices.front();
for (std::size_t i = 0;
i < _tess.numStrips;
i++, strip_indices += _tess.lenStrips)
{
currentVBuf.addIndexBatch(strip_indices, _tess.lenStrips);
}
// Render all index batches
_vertexBuf.replaceData(currentVBuf);
}
_vertexBuf.renderAllBatches(GL_QUAD_STRIP);
}
void RenderablePatchWireframe::queueUpdate()
{
_needsUpdate = true;
}
RenderablePatchSolid::RenderablePatchSolid(PatchTesselation& tess) :
_tess(tess),
_needsUpdate(true)
{}
void RenderablePatchSolid::render(const RenderInfo& info) const
{
if (_tess.vertices.empty() || _tess.indices.empty()) return;
if (!info.checkFlag(RENDER_BUMP))
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
}
// No colour changing
glDisableClientState(GL_COLOR_ARRAY);
if (info.checkFlag(RENDER_VERTEX_COLOUR))
{
glColor3f(1, 1, 1);
}
if (_needsUpdate)
{
_needsUpdate = false;
// Add vertex geometry to vertex buffer
VertexBuffer_T currentVBuf;
currentVBuf.addVertices(_tess.vertices.begin(), _tess.vertices.end());
// Submit indices
const RenderIndex* strip_indices = &_tess.indices.front();
for (std::size_t i = 0;
i < _tess.numStrips;
i++, strip_indices += _tess.lenStrips)
{
currentVBuf.addIndexBatch(strip_indices, _tess.lenStrips);
}
// Render all batches
_vertexBuf.replaceData(currentVBuf);
}
_vertexBuf.renderAllBatches(GL_QUAD_STRIP, info.checkFlag(RENDER_BUMP));
if (!info.checkFlag(RENDER_BUMP))
{
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
}
void RenderablePatchSolid::queueUpdate()
{
_needsUpdate = true;
}
RenderableGeometry::Type RenderablePatchSolid::getType() const
{
return RenderableGeometry::Type::Quads;
}
const Vector3& RenderablePatchSolid::getFirstVertex()
{
return _tess.vertices.front().vertex;
}
std::size_t RenderablePatchSolid::getVertexStride()
{
return sizeof(ArbitraryMeshVertex);
}
const unsigned int& RenderablePatchSolid::getFirstIndex()
{
updateIndices();
return _indices.front();
}
std::size_t RenderablePatchSolid::getNumIndices()
{
updateIndices();
return _indices.size();
}
void RenderablePatchSolid::updateIndices()
{
// To render the patch mesh as quads, we need 4 indices per quad
auto numRequiredIndices = (_tess.height - 1) * (_tess.width - 1) * 4;
if (_indices.size() == numRequiredIndices)
{
return;
}
if (_tess.height == 0 || _tess.width == 0)
{
_indices.clear();
return;
}
_indices.resize(numRequiredIndices);
auto index = 0;
for (auto h = 0; h < _tess.height - 1; ++h)
{
for (auto w = 0; w < _tess.width - 1; ++w)
{
_indices[index++] = static_cast<unsigned int>(h * _tess.width + w + 0);
_indices[index++] = static_cast<unsigned int>((h + 1) * _tess.width + w + 0);
_indices[index++] = static_cast<unsigned int>((h + 1) * _tess.width + w + 1);
_indices[index++] = static_cast<unsigned int>(h * _tess.width + w + 1);
}
}
}
const ShaderPtr& RenderablePatchVectorsNTB::getShader() const
{
return _shader;
}
RenderablePatchVectorsNTB::RenderablePatchVectorsNTB(const PatchTesselation& tess) :
_tess(tess)
{}
void RenderablePatchVectorsNTB::setRenderSystem(const RenderSystemPtr& renderSystem)
{
if (renderSystem)
{
_shader = renderSystem->capture("$PIVOT");
}
else
{
_shader.reset();
}
}
#define VectorMA( v, s, b, o ) ((o)[0]=(v)[0]+(b)[0]*(s),(o)[1]=(v)[1]+(b)[1]*(s),(o)[2]=(v)[2]+(b)[2]*(s))
void RenderablePatchVectorsNTB::render(const RenderInfo& info) const
{
if (_tess.vertices.empty()) return;
glBegin(GL_LINES);
for (const ArbitraryMeshVertex& v : _tess.vertices)
{
Vector3 end;
glColor3f(0, 0, 1);
glVertex3dv(v.vertex);
VectorMA(v.vertex, 5, v.normal, end);
glVertex3dv(end);
glColor3f(1, 0, 0);
glVertex3dv(v.vertex);
VectorMA(v.vertex, 5, v.tangent, end);
glVertex3dv(end);
glColor3f(0, 1, 0);
glVertex3dv(v.vertex);
VectorMA(v.vertex, 5, v.bitangent, end);
glVertex3dv(end);
glColor3f(1, 1, 1);
glVertex3dv(v.vertex);
glVertex3dv(v.vertex);
}
glEnd();
}
void RenderablePatchVectorsNTB::render(RenderableCollector& collector, const VolumeTest& volume, const Matrix4& localToWorld) const
{
collector.setHighlightFlag(RenderableCollector::Highlight::Primitives, false);
collector.addRenderable(*_shader, *this, localToWorld);
}
void RenderablePatchTesselation::clear()
{
if (!_shader || _surfaceSlot == render::ISurfaceRenderer::InvalidSlot) return;
_shader->removeSurface(_surfaceSlot);
_shader.reset();
_surfaceSlot = render::ISurfaceRenderer::InvalidSlot;
_size = 0;
}
void RenderablePatchTesselation::queueUpdate()
{
_needsUpdate = true;
}
void RenderablePatchTesselation::update(const ShaderPtr& shader)
{
bool shaderChanged = _shader != shader;
if (!_needsUpdate && !shaderChanged) return;
_needsUpdate = false;
auto sizeChanged = _tess.vertices.size() != _size;
if (_shader && _surfaceSlot != render::ISurfaceRenderer::InvalidSlot && (shaderChanged || sizeChanged))
{
clear();
}
_shader = shader;
_size = _tess.vertices.size();
// Generate the index array
std::vector<unsigned int> indices;
indices.reserve((_tess.height - 1) * (_tess.width - 1) * 6); // 6 => 2 triangles per quad
// 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)
{
indices.push_back(static_cast<unsigned int>(rowOffset + w + _tess.width));
indices.push_back(static_cast<unsigned int>(rowOffset + w + 1));
indices.push_back(static_cast<unsigned int>(rowOffset + w));
indices.push_back(static_cast<unsigned int>(rowOffset + w + _tess.width));
indices.push_back(static_cast<unsigned int>(rowOffset + w + _tess.width + 1));
indices.push_back(static_cast<unsigned int>(rowOffset + w + 1));
}
}
if (_surfaceSlot == render::ISurfaceRenderer::InvalidSlot)
{
_surfaceSlot = shader->addSurface(_tess.vertices, indices);
}
else
{
shader->updateSurface(_surfaceSlot, _tess.vertices, indices);
}
}