/
segment.cpp
398 lines (327 loc) · 10.4 KB
/
segment.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/** @file segment.cpp World map line segment.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program 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. This program 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 this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include <QMap>
#include <QtAlgorithms>
#include <de/binangle.h>
#include "dd_main.h" // App_World()
#include "BspLeaf"
#include "HEdge"
#include "Line"
#include "Sector"
#ifdef __CLIENT__
# include "world/map.h"
# include "BiasIllum"
# include "BiasSource"
# include "BiasTracker"
#endif
#include "world/segment.h"
using namespace de;
#ifdef __CLIENT__
struct GeometryGroup
{
typedef QList<BiasIllum> BiasIllums;
uint biasLastUpdateFrame;
BiasIllums biasIllums;
BiasTracker biasTracker;
};
/// Geometry group identifier => group data.
typedef QMap<int, GeometryGroup> GeometryGroups;
#endif
DENG2_PIMPL(Segment)
{
/// Segment on the back side of this (if any). @todo remove me
Segment *back;
/// Segment flags.
Flags flags;
/// Map Line::Side attributed to the line segment (not owned).
/// Can be @c 0 (signifying a partition line segment).
Line::Side *lineSide;
/// Distance along the attributed map line at which the half-edge vertex occurs.
coord_t lineSideOffset;
/// Half-edge attributed to the line segment (not owned).
HEdge *hedge;
/// World angle.
angle_t angle;
/// Accurate length of the segment.
coord_t length;
#ifdef __CLIENT__
/// Bias lighting data for each geometry group (i.e., each Line::Side section).
GeometryGroups geomGroups;
#endif
Instance(Public *i)
: Base(i),
back(0),
flags(0),
lineSide(0),
lineSideOffset(0),
hedge(0),
angle(0),
length(0)
{}
#ifdef __CLIENT__
/**
* Retrieve geometry data by it's associated unique @a group identifier.
*
* @param group Geometry group identifier.
* @param canAlloc @c true= to allocate if no data exists.
*/
GeometryGroup *geometryGroup(int group, bool canAlloc = true)
{
DENG_ASSERT(group >= 0 && group < 3); // sanity check
DENG_ASSERT(self.hasLineSide()); // sanity check
GeometryGroups::iterator foundAt = geomGroups.find(group);
if(foundAt != geomGroups.end())
{
return &*foundAt;
}
if(!canAlloc) return 0;
// Number of bias illumination points for this geometry. Presently we
// define a 1:1 mapping to strip geometry vertices.
int const numBiasIllums = 4;
GeometryGroup &newGeomGroup = *geomGroups.insert(group, GeometryGroup());
newGeomGroup.biasIllums.reserve(numBiasIllums);
for(int i = 0; i < numBiasIllums; ++i)
{
newGeomGroup.biasIllums.append(BiasIllum(&newGeomGroup.biasTracker));
}
return &newGeomGroup;
}
/**
* @todo This could be enhanced so that only the lights on the right
* side of the surface are taken into consideration.
*/
void updateBiasContributors(GeometryGroup &geomGroup, int sectionIndex)
{
DENG_UNUSED(sectionIndex);
// If the data is already up to date, nothing needs to be done.
uint lastChangeFrame = self.map().biasLastChangeOnFrame();
if(geomGroup.biasLastUpdateFrame == lastChangeFrame)
return;
geomGroup.biasLastUpdateFrame = lastChangeFrame;
geomGroup.biasTracker.clearContributors();
Surface const &surface = lineSide->middle();
Vector2d const &from = self.from().origin();
Vector2d const &to = self.to().origin();
foreach(BiasSource *source, self.map().biasSources())
{
// If the source is too weak we will ignore it completely.
if(source->intensity() <= 0)
continue;
Vector3d sourceToSurface = (source->origin() - self.center()).normalize();
// Calculate minimum 2D distance to the segment.
coord_t distance = 0;
for(int k = 0; k < 2; ++k)
{
coord_t len = (Vector2d(source->origin()) - (!k? from : to)).length();
if(k == 0 || len < distance)
distance = len;
}
if(sourceToSurface.dot(surface.normal()) < 0)
continue;
geomGroup.biasTracker.addContributor(source, source->evaluateIntensity() / de::max(distance, 1.0));
}
}
#endif
};
Segment::Segment(Line::Side *lineSide, HEdge *hedge)
: MapElement(DMU_SEGMENT),
d(new Instance(this))
{
d->lineSide = lineSide;
d->hedge = hedge;
}
HEdge &Segment::hedge() const
{
if(d->hedge)
{
return *d->hedge;
}
/// @throw MissingHEdgeError Attempted with no half-edge attributed.
throw MissingHEdgeError("Segment::hedge", "No half-edge is attributed");
}
bool Segment::hasBack() const
{
return d->back != 0;
}
Segment &Segment::back() const
{
DENG_ASSERT(d->back != 0);
return *d->back;
}
void Segment::setBack(Segment *newBack)
{
d->back = newBack;
}
Sector &Segment::sector() const
{
return bspLeaf().sector();
}
Sector *Segment::sectorPtr() const
{
return hasBspLeaf()? bspLeaf().sectorPtr() : 0;
}
bool Segment::hasLineSide() const
{
return d->lineSide != 0;
}
Line::Side &Segment::lineSide() const
{
if(d->lineSide)
{
return *d->lineSide;
}
/// @throw MissingLineError Attempted with no line attributed.
throw MissingLineSideError("Segment::lineSide", "No line.side is attributed");
}
coord_t Segment::lineSideOffset() const
{
return d->lineSideOffset;
}
void Segment::setLineSideOffset(coord_t newOffset)
{
d->lineSideOffset = newOffset;
}
angle_t Segment::angle() const
{
return d->angle;
}
void Segment::setAngle(angle_t newAngle)
{
d->angle = newAngle;
}
coord_t Segment::length() const
{
return d->length;
}
void Segment::setLength(coord_t newLength)
{
d->length = newLength;
/// @todo Is this still necessary?
if(d->length == 0)
d->length = 0.01f; // Hmm...
}
Segment::Flags Segment::flags() const
{
return d->flags;
}
void Segment::setFlags(Flags flagsToChange, FlagOp operation)
{
applyFlagOperation(d->flags, flagsToChange, operation);
}
#ifdef __CLIENT__
void Segment::updateBiasAfterGeometryMove(int group)
{
if(GeometryGroup *geomGroup = d->geometryGroup(group, false /*don't allocate*/))
{
geomGroup->biasTracker.updateAllContributors();
}
}
void Segment::applyBiasDigest(BiasDigest &changes)
{
for(GeometryGroups::iterator it = d->geomGroups.begin();
it != d->geomGroups.end(); ++it)
{
it.value().biasTracker.applyChanges(changes);
}
}
void Segment::lightBiasPoly(int group, rvertex_t const *positions, ColorRawf *colors)
{
int const sectionIndex = group;
DENG_ASSERT(hasLineSide()); // sanity check
DENG_ASSERT(positions != 0 && colors != 0);
GeometryGroup *geomGroup = d->geometryGroup(sectionIndex);
// Should we update?
if(devUpdateBiasContributors)
{
d->updateBiasContributors(*geomGroup, sectionIndex);
}
Surface const &surface = d->lineSide->surface(sectionIndex);
uint biasTime = map().biasCurrentTime();
rvertex_t const *vtx = positions;
ColorRawf *color = colors;
for(int i = 0; i < geomGroup->biasIllums.count(); ++i, vtx++, color++)
{
BiasIllum *illum = &geomGroup->biasIllums[i];
Vector3d surfacePoint(vtx->pos[VX], vtx->pos[VY], vtx->pos[VZ]);
Vector3f tmp;
illum->evaluate(tmp, surfacePoint, surface.normal(), biasTime);
for(int c = 0; c < 3; ++c)
color->rgba[c] += tmp[c];
}
// Any changes from contributors will have now been applied.
geomGroup->biasTracker.markIllumUpdateCompleted();
}
#endif // __CLIENT__
coord_t Segment::pointDistance(const_pvec2d_t point, coord_t *offset) const
{
DENG_ASSERT(point != 0);
/// @todo Why are we calculating this every time?
Vector2d direction = to().origin() - from().origin();
coord_t fromOriginV1[2] = { from().origin().x, from().origin().y };
coord_t directionV1[2] = { direction.x, direction.y };
return V2d_PointLineDistance(point, fromOriginV1, directionV1, offset);
}
coord_t Segment::pointOnSide(const_pvec2d_t point) const
{
DENG_ASSERT(point != 0);
/// @todo Why are we calculating this every time?
Vector2d direction = to().origin() - from().origin();
coord_t fromOriginV1[2] = { from().origin().x, from().origin().y };
coord_t directionV1[2] = { direction.x, direction.y };
return V2d_PointOnLineSide(point, fromOriginV1, directionV1);
}
int Segment::property(DmuArgs &args) const
{
switch(args.prop)
{
case DMU_VERTEX0: {
Vertex *vtx = &from();
args.setValue(DMT_SEGMENT_V, &vtx, 0);
break; }
case DMU_VERTEX1: {
Vertex *vtx = &to();
args.setValue(DMT_SEGMENT_V, &vtx, 0);
break; }
case DMU_LENGTH:
args.setValue(DMT_SEGMENT_LENGTH, &d->length, 0);
break;
case DMU_OFFSET: {
coord_t offset = d->lineSide? d->lineSideOffset : 0;
args.setValue(DMT_SEGMENT_OFFSET, &offset, 0);
break; }
case DMU_SIDE:
args.setValue(DMT_SEGMENT_SIDE, &d->lineSide, 0);
break;
case DMU_LINE: {
Line *lineAdr = d->lineSide? &d->lineSide->line() : 0;
args.setValue(DMT_SEGMENT_LINE, &lineAdr, 0);
break; }
case DMU_SECTOR: {
Sector *sector = sectorPtr();
args.setValue(DMT_SEGMENT_SECTOR, §or, 0);
break; }
case DMU_ANGLE:
args.setValue(DMT_SEGMENT_ANGLE, &d->angle, 0);
break;
default:
return MapElement::property(args);
}
return false; // Continue iteration.
}