/
r_fakeradio.cpp
287 lines (241 loc) · 8.67 KB
/
r_fakeradio.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
/** @file r_fakeradio.cpp Faked Radiosity Lighting.
*
* @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 "de_base.h"
#include "de_render.h"
#include <de/Error>
#include <de/Log>
#include <de/memoryzone.h>
#include <de/vector1.h>
#include "map/gamemap.h"
#include "map/vertex.h"
using namespace de;
static zblockset_t *shadowLinksBlockSet;
bool Rend_RadioLineCastsShadow(LineDef const &line)
{
if(line.isFromPolyobj()) return false;
if(line.isSelfReferencing()) return false;
// Lines with no other neighbor do not qualify for shadowing.
if(&line.v1Owner()->next().line() == &line ||
&line.v2Owner()->next().line() == &line) return false;
return true;
}
bool Rend_RadioPlaneCastsShadow(Plane const &plane)
{
if(plane.surface().hasMaterial())
{
Material const &surfaceMaterial = plane.surface().material();
if(!surfaceMaterial.isDrawable()) return false;
if(surfaceMaterial.hasGlow()) return false;
if(surfaceMaterial.isSkyMasked()) return false;
}
return true;
}
/**
* Given two lines "connected" by shared origin coordinates (0, 0) at a "corner"
* vertex, calculate the point which lies @a dist1 away from @a line1 and also
* @a dist2 from @a line2. The point should also be the nearest point to the
* origin (in case of parallel lines).
*
* @param line1 Direction vector for the "left" line.
* @param line2 Direction vector for the "right" line.
* @param dist1 Distance from @a line1 to offset the corner point.
* @param dist2 Distance from @a line2 to offset the corner point.
*
* Return values:
* @param point Coordinates for the corner point are written here. Can be @c 0.
* @param lp Coordinates for the "extended" point are written here. Can be @c 0.
*/
static void cornerNormalPoint(const_pvec2d_t line1, double dist1, const_pvec2d_t line2,
double dist2, pvec2d_t point, pvec2d_t lp)
{
if(!point && !lp) return;
// Length of both lines.
double len1 = V2d_Length(line1);
double len2 = V2d_Length(line2);
// Calculate normals for both lines.
vec2d_t norm1; V2d_Set(norm1, -line1[VY] / len1 * dist1, line1[VX] / len1 * dist1);
vec2d_t norm2; V2d_Set(norm2, line2[VY] / len2 * dist2, -line2[VX] / len2 * dist2);
// Do we need to calculate the extended points, too? Check that
// the extension does not bleed too badly outside the legal shadow
// area.
if(lp)
{
V2d_Set(lp, line2[VX] / len2 * dist2, line2[VY] / len2 * dist2);
}
// Are the lines parallel? If so, they won't connect at any
// point, and it will be impossible to determine a corner point.
if(V2d_IsParallel(line1, line2))
{
// Just use a normal as the point.
if(point)
V2d_Copy(point, norm1);
return;
}
// Find the intersection of normal-shifted lines. That'll be our
// corner point.
if(point)
V2d_Intersection(norm1, line1, norm2, line2, point);
}
/**
* @return The width (world units) of the shadow edge. It is scaled depending on
* the length of @a edge.
*/
static double shadowEdgeWidth(pvec2d_t const edge)
{
double const normalWidth = 20; //16;
double const maxWidth = 60;
// A long edge?
double length = V2d_Length(edge);
if(length > 600)
{
double w = length - 600;
if(w > 1000)
w = 1000;
return normalWidth + w / 1000 * maxWidth;
}
return normalWidth;
}
void Rend_RadioUpdateVertexShadowOffsets(Vertex &vtx)
{
if(!vtx.lineOwnerCount()) return;
vec2d_t leftDir, rightDir;
LineOwner *base = vtx.firstLineOwner();
LineOwner *own = base;
do
{
LineDef const &lineB = own->line();
LineDef const &lineA = own->next().line();
if(&lineB.v1() == &vtx)
{
rightDir[VX] = lineB.direction()[VX];
rightDir[VY] = lineB.direction()[VY];
}
else
{
rightDir[VX] = -lineB.direction()[VX];
rightDir[VY] = -lineB.direction()[VY];
}
if(&lineA.v1() == &vtx)
{
leftDir[VX] = -lineA.direction()[VX];
leftDir[VY] = -lineA.direction()[VY];
}
else
{
leftDir[VX] = lineA.direction()[VX];
leftDir[VY] = lineA.direction()[VY];
}
// The left side is always flipped.
V2d_Scale(leftDir, -1);
cornerNormalPoint(leftDir, shadowEdgeWidth(leftDir),
rightDir, shadowEdgeWidth(rightDir),
own->_shadowOffsets.inner,
own->_shadowOffsets.extended);
own = &own->next();
} while(own != base);
}
/**
* Link @a line to @a bspLeaf for the purposes of shadowing.
*/
static void linkShadowLineDefToSSec(LineDef *line, byte side, BspLeaf *bspLeaf)
{
DENG_ASSERT(line && bspLeaf);
#ifdef DENG_DEBUG
// Check the links for dupes!
for(ShadowLink *i = bspLeaf->firstShadowLink(); i; i = i->next)
{
if(i->line == line && i->side == side)
throw Error("R_LinkShadow", "Already here!!");
}
#endif
// We'll need to allocate a new link.
ShadowLink *link = (ShadowLink *) ZBlockSet_Allocate(shadowLinksBlockSet);
link->line = line;
link->side = side;
// The links are stored into a linked list.
link->next = bspLeaf->_shadows;
bspLeaf->_shadows = link;
}
struct ShadowLinkerParms
{
LineDef *line;
byte side;
};
static int RIT_ShadowBspLeafLinker(BspLeaf *bspLeaf, void *context)
{
ShadowLinkerParms *parms = static_cast<ShadowLinkerParms *>(context);
linkShadowLineDefToSSec(parms->line, parms->side, bspLeaf);
return false; // Continue iteration.
}
void Rend_RadioInitForMap()
{
DENG_ASSERT(theMap);
Time begunAt;
foreach(Vertex *vertex, theMap->vertexes())
{
Rend_RadioUpdateVertexShadowOffsets(*vertex);
}
/**
* The algorithm:
*
* 1. Use the BSP leaf blockmap to look for all the blocks that are
* within the linedef's shadow bounding box.
*
* 2. Check the BspLeafs whose sector is the same as the linedef.
*
* 3. If any of the shadow points are in the BSP leaf, or any of the
* shadow edges cross one of the BSP leaf's edges (not parallel),
* link the linedef to the BspLeaf.
*/
shadowLinksBlockSet = ZBlockSet_New(sizeof(ShadowLink), 1024, PU_MAP);
ShadowLinkerParms parms;
AABoxd bounds;
vec2d_t point;
foreach(LineDef *line, theMap->lines())
{
if(!Rend_RadioLineCastsShadow(*line)) continue;
// For each side of the line.
for(uint j = 0; j < 2; ++j)
{
LineDef::Side &side = line->side(j);
if(!side.hasSector() || !side.hasSideDef()) continue;
Vertex &vtx0 = line->vertex(j);
Vertex &vtx1 = line->vertex(j^1);
LineOwner &vo0 = line->vertexOwner(j)->next();
LineOwner &vo1 = line->vertexOwner(j^1)->prev();
// Use the extended points, they are wider than inoffsets.
V2d_Copy(point, vtx0.origin());
V2d_InitBox(bounds.arvec2, point);
V2d_Sum(point, point, vo0.extendedShadowOffset());
V2d_AddToBox(bounds.arvec2, point);
V2d_Copy(point, vtx1.origin());
V2d_AddToBox(bounds.arvec2, point);
V2d_Sum(point, point, vo1.extendedShadowOffset());
V2d_AddToBox(bounds.arvec2, point);
parms.line = line;
parms.side = j;
// Link the shadowing line to all the BspLeafs whose axis-aligned
// bounding box intersects 'bounds'.
GameMap_BspLeafsBoxIterator(theMap, &bounds, side.sectorPtr(),
RIT_ShadowBspLeafLinker, &parms);
}
}
LOG_INFO(String("Rend_RadioInitForMap: Done in %1 seconds.").arg(begunAt.since(), 0, 'g', 2));
}