-
Notifications
You must be signed in to change notification settings - Fork 47
/
PatchRenderables.cpp
195 lines (154 loc) · 4.87 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
#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;
}
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::setShader(const ShaderPtr& shader)
{
_shader = shader;
}
void RenderablePatchTesselation::update()
{
if (!_shader) return;
if (!_needsUpdate) return;
_needsUpdate = false;
// Load all the vertices into the target vector
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));
}
}
_shader->addSurface(_tess.vertices, indices);
}