/
Winding.cpp
346 lines (284 loc) · 8.33 KB
/
Winding.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
Copyright (C) 1999-2006 Id Software, Inc. and contributors.
For a list of contributors, see the accompanying CONTRIBUTORS file.
This file is part of GtkRadiant.
GtkRadiant is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GtkRadiant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "Winding.h"
#include "igl.h"
#include "itextstream.h"
#include <algorithm>
#include "FixedWinding.h"
#include "math/Ray.h"
#include "math/Plane3.h"
#include "texturelib.h"
#include "Brush.h"
#include "GLProgramAttributes.h"
#include "debugging/render.h"
namespace {
struct indexremap_t {
indexremap_t(std::size_t _x, std::size_t _y, std::size_t _z) :
x(_x), y(_y), z(_z)
{}
std::size_t x, y, z;
};
inline indexremap_t indexremap_for_projectionaxis(const ProjectionAxis axis) {
switch (axis) {
case eProjectionAxisX:
return indexremap_t(1, 2, 0);
case eProjectionAxisY:
return indexremap_t(2, 0, 1);
default:
return indexremap_t(0, 1, 2);
}
}
}
void Winding::drawWireframe() const
{
if (!empty())
{
glVertexPointer(3, GL_DOUBLE, sizeof(WindingVertex), &front().vertex);
glDrawArrays(GL_LINE_LOOP, 0, GLsizei(size()));
}
}
void Winding::render(const RenderInfo& info) const
{
// Do not render if there are no points
if (empty())
{
return;
}
// Our vertex colours are always white, if requested
glDisableClientState(GL_COLOR_ARRAY);
if (info.checkFlag(RENDER_VERTEX_COLOUR))
{
glColor3f(1, 1, 1);
}
// A shortcut pointer to the first array element to avoid
// massive calls to std::vector<>::begin()
const WindingVertex& firstElement = front();
// Set the vertex pointer first
glVertexPointer(3, GL_DOUBLE, sizeof(WindingVertex), &firstElement.vertex);
// Check render flags. Multiple flags may be set, so the order matters.
if (info.checkFlag(RENDER_TEXTURE_CUBEMAP))
{
// In cube-map mode, we submit the vertex coordinate as the texture
// coordinate. The RenderSystem will set the appropriate texture matrix
// etc.
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(
3, GL_DOUBLE, sizeof(WindingVertex), &firstElement.vertex
);
}
else if (info.checkFlag(RENDER_BUMP))
{
// Lighting mode, submit normals, tangents and texcoords to the shader
// program.
glVertexAttribPointer(
ATTR_NORMAL, 3, GL_DOUBLE, 0, sizeof(WindingVertex), &firstElement.normal
);
glVertexAttribPointer(
ATTR_TEXCOORD, 2, GL_DOUBLE, 0, sizeof(WindingVertex), &firstElement.texcoord
);
glVertexAttribPointer(
ATTR_TANGENT, 3, GL_DOUBLE, 0, sizeof(WindingVertex), &firstElement.tangent
);
glVertexAttribPointer(
ATTR_BITANGENT, 3, GL_DOUBLE, 0, sizeof(WindingVertex), &firstElement.bitangent
);
}
else
{
// Submit normals in lighting mode
if (info.checkFlag(RENDER_LIGHTING))
{
glNormalPointer(GL_DOUBLE, sizeof(WindingVertex), &firstElement.normal);
}
// Set texture coordinates in 2D texture mode
if (info.checkFlag(RENDER_TEXTURE_2D))
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(
2, GL_DOUBLE, sizeof(WindingVertex), &firstElement.texcoord
);
}
}
// Submit all data to OpenGL
glDrawArrays(GL_POLYGON, 0, GLsizei(size()));
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
void Winding::testSelect(SelectionTest& test, SelectionIntersection& best)
{
if (empty()) return;
test.TestPolygon(VertexPointer(&front().vertex, sizeof(WindingVertex)), size(), best);
}
void Winding::updateNormals(const Vector3& normal)
{
// Copy all normals into the winding vertices
for (iterator i = begin(); i != end(); ++i)
{
i->normal = normal;
}
}
AABB Winding::aabb() const
{
AABB returnValue;
for (const_iterator i = begin(); i != end(); ++i)
{
returnValue.includePoint(i->vertex);
}
return returnValue;
}
bool Winding::testPlane(const Plane3& plane, bool flipped) const
{
const int test = (flipped) ? ePlaneBack : ePlaneFront;
for (const_iterator i = begin(); i != end(); ++i)
{
if (test == classifyDistance(plane.distanceToPoint(i->vertex), ON_EPSILON))
{
return false;
}
}
return true;
}
BrushSplitType Winding::classifyPlane(const Plane3& plane) const
{
BrushSplitType split;
for (const_iterator i = begin(); i != end(); ++i)
{
++split.counts[classifyDistance(plane.distanceToPoint(i->vertex), ON_EPSILON)];
}
return split;
}
PlaneClassification Winding::classifyDistance(const double distance, const double epsilon)
{
if (distance > epsilon) {
return ePlaneFront;
}
if (distance < -epsilon) {
return ePlaneBack;
}
return ePlaneOn;
}
bool Winding::planesConcave(const Winding& w1, const Winding& w2, const Plane3& plane1, const Plane3& plane2)
{
return !w1.testPlane(plane2, false) || !w2.testPlane(plane1, false);
}
std::size_t Winding::findAdjacent(std::size_t face) const
{
for (std::size_t i = 0; i < size(); ++i)
{
ASSERT_MESSAGE((*this)[i].adjacent != brush::c_brush_maxFaces, "edge connectivity data is invalid");
if ((*this)[i].adjacent == face)
{
return i;
}
}
return brush::c_brush_maxFaces;
}
std::size_t Winding::opposite(const std::size_t index, const std::size_t other) const
{
ASSERT_MESSAGE(index < size() && other < size(), "Winding::opposite: index out of range");
double dist_best = 0;
std::size_t index_best = brush::c_brush_maxFaces;
Ray edge = Ray::createForPoints((*this)[index].vertex, (*this)[other].vertex);
for (std::size_t i=0; i < size(); ++i)
{
if (i == index || i == other) {
continue;
}
auto dist_squared = edge.getSquaredDistance((*this)[i].vertex);
if (dist_squared > dist_best) {
dist_best = dist_squared;
index_best = i;
}
}
return index_best;
}
std::size_t Winding::opposite(std::size_t index) const
{
return opposite(index, next(index));
}
Vector3 Winding::centroid(const Plane3& plane) const
{
Vector3 centroid(0,0,0);
double area2 = 0, x_sum = 0, y_sum = 0;
const ProjectionAxis axis = projectionaxis_for_normal(plane.normal());
const indexremap_t remap = indexremap_for_projectionaxis(axis);
for (std::size_t i = size() - 1, j = 0; j < size(); i = j, ++j)
{
const auto ai = (*this)[i].vertex[remap.x]
* (*this)[j].vertex[remap.y] - (*this)[j].vertex[remap.x]
* (*this)[i].vertex[remap.y];
area2 += ai;
x_sum += ((*this)[j].vertex[remap.x] + (*this)[i].vertex[remap.x]) * ai;
y_sum += ((*this)[j].vertex[remap.y] + (*this)[i].vertex[remap.y]) * ai;
}
centroid[remap.x] = x_sum / (3 * area2);
centroid[remap.y] = y_sum / (3 * area2);
{
Ray ray(Vector3(0, 0, 0), Vector3(0, 0, 0));
ray.origin[remap.x] = centroid[remap.x];
ray.origin[remap.y] = centroid[remap.y];
ray.direction[remap.z] = 1;
centroid[remap.z] = ray.getDistance(plane);
}
return centroid;
}
void Winding::printConnectivity()
{
for (iterator i = begin(); i != end(); ++i)
{
std::size_t vertexIndex = std::distance(begin(), i);
rMessage() << "vertex: " << vertexIndex
<< " adjacent: " << i->adjacent << std::endl;
}
}
#ifdef RENDERABLE_GEOMETRY
Winding::Type Winding::getType() const
{
return Type::Polygons;
}
const Vector3& Winding::getFirstVertex()
{
return front().vertex;
}
std::size_t Winding::getVertexStride()
{
return sizeof(WindingVertex);
}
const unsigned int& Winding::getFirstIndex()
{
updateIndices();
return _indices.front();
}
std::size_t Winding::getNumIndices()
{
updateIndices();
return _indices.size();
}
void Winding::updateIndices()
{
auto windingSize = size();
if (_indices.size() == windingSize) return;
if (_indices.size() > windingSize)
{
_indices.resize(windingSize);
return;
}
while (_indices.size() < windingSize)
{
_indices.push_back(static_cast<unsigned int>(_indices.size()));
}
}
#endif