-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
biasillum.cpp
307 lines (257 loc) · 9.28 KB
/
biasillum.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
/** @file biasillum.cpp Shadow Bias map point illumination.
*
* @authors Copyright © 2005-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-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, see:
* http://www.gnu.org/licenses</small>
*/
#include "de_base.h"
#include "de_console.h"
#include "world/map.h"
#include "world/linesighttest.h"
#include "BspLeaf"
#include "BiasTracker"
#include "render/biasillum.h"
using namespace de;
static int lightSpeed = 130; //cvar
static int devUseSightCheck = true; //cvar
DENG2_PIMPL_NOREF(BiasIllum)
{
BiasTracker *tracker; ///< Controlling tracker.
Vector3f color; ///< Current light color.
Vector3f dest; ///< Destination light color (interpolated to).
uint updateTime; ///< When the value was calculated.
bool interpolating; ///< Set to @c true during interpolation.
/**
* Cast lighting contributions from each source that affects the map point.
* Order is the same as that in the tracker.
*/
Vector3f casted[MAX_CONTRIBUTORS];
Instance(BiasTracker *tracker)
: tracker(tracker),
updateTime(0),
interpolating(false)
{}
Instance(Instance const &other)
: tracker(other.tracker),
color(other.color),
dest(other.dest),
updateTime(other.updateTime),
interpolating(other.interpolating)
{}
/**
* Returns a previous light contribution by unique contributor @a index.
*/
inline Vector3f &contribution(int index)
{
DENG_ASSERT(index >= 0 && index < MAX_CONTRIBUTORS);
return casted[index];
}
/**
* Update any changed lighting contributions.
*
* @param activeContributors Bit field denoting the active contributors.
* @param biasTime Time in milliseconds of the last bias frame update.
*/
void applyLightingChanges(byte activeContributors, uint biasTime)
{
#define COLOR_CHANGE_THRESHOLD 0.1f // Ignore small variations for perf
DENG_ASSERT(tracker != 0);
// Determine the new color (initially, black).
Vector3f newColor;
// Do we need to re-accumulate light contributions?
if(activeContributors)
{
/// Maximum accumulated color strength.
static Vector3f const saturated(1, 1, 1);
for(int i = 0; i < MAX_CONTRIBUTORS; ++i)
{
if(activeContributors & (1 << i))
{
newColor += contribution(i);
// Stop once fully saturated.
if(newColor >= saturated)
break;
}
}
// Clamp.
newColor = newColor.min(saturated);
}
// Is there a new destination?
if(!activeContributors ||
(!de::fequal(dest.x, newColor.x, COLOR_CHANGE_THRESHOLD) ||
!de::fequal(dest.y, newColor.y, COLOR_CHANGE_THRESHOLD) ||
!de::fequal(dest.z, newColor.z, COLOR_CHANGE_THRESHOLD)))
{
if(interpolating)
{
// Must not lose the half-way interpolation.
Vector3f mid; lerp(mid, biasTime);
// This is current color at this very moment.
color = mid;
}
// This is what we will be interpolating to.
dest = newColor;
interpolating = true;
updateTime = tracker->timeOfLatestContributorUpdate();
}
#undef COLOR_CHANGE_THRESHOLD
}
/**
* Update lighting contribution for the specified contributor @a index.
*
* @param index Unique index of the contributor.
* @param point Point in the map to evaluate.
* @param normalAtPoint Surface normal at @a point.
* @param bspRoot Root BSP element for the map.
*/
void updateContribution(int index, Vector3d const &point,
Vector3f const &normalAtPoint, MapElement &bspRoot)
{
DENG_ASSERT(tracker != 0);
BiasSource const &source = tracker->contributor(index);
Vector3f &casted = contribution(index);
/// @todo LineSightTest should (optionally) perform this test.
Sector *sector = &source.bspLeafAtOrigin().sector();
if((!sector->floor().surface().hasSkyMaskedMaterial() &&
source.origin().z < sector->floor().visHeight()) ||
(!sector->ceiling().surface().hasSkyMaskedMaterial() &&
source.origin().z > sector->ceiling().visHeight()))
{
// This affecting source does not contribute any light.
casted = Vector3f();
return;
}
Vector3d sourceToPoint = source.origin() - point;
if(devUseSightCheck &&
!LineSightTest(source.origin(), point + sourceToPoint / 100)
.trace(bspRoot))
{
// LOS fail.
// This affecting source does not contribute any light.
casted = Vector3f();
return;
}
double distance = sourceToPoint.length();
double dot = sourceToPoint.normalize().dot(normalAtPoint);
// The point faces away from the light?
if(dot < 0)
{
casted = Vector3f();
return;
}
// Apply light casted from this source.
float strength = dot * source.evaluateIntensity() / distance;
casted = source.color() * de::clamp(0.f, strength, 1.f);
}
/**
* Interpolate color from current to destination.
*
* @param result Interpolated color will be written here.
* @param currentTime Time in milliseconds of the last bias frame update.
*/
void lerp(Vector3f &result, uint currentTime)
{
if(!interpolating)
{
// Use the current color.
result = color;
return;
}
float inter = (currentTime - updateTime) / float( lightSpeed );
if(inter > 1)
{
interpolating = false;
color = dest;
result = color;
}
else
{
result = color + (dest - color) * inter;
}
}
};
float const BiasIllum::MIN_INTENSITY = .005f;
BiasIllum::BiasIllum(BiasTracker *tracker) : d(new Instance(tracker))
{}
BiasIllum::BiasIllum(BiasIllum const &other) : d(new Instance(*other.d))
{}
BiasIllum &BiasIllum::operator = (BiasIllum const &other)
{
d.reset(new Instance(*other.d));
return *this;
}
bool BiasIllum::hasTracker() const
{
return d->tracker != 0;
}
BiasTracker &BiasIllum::tracker() const
{
if(d->tracker != 0)
{
return *d->tracker;
}
/// @throw MissingTrackerError Attempted with no tracker assigned.
throw MissingTrackerError("BiasIllum::tracker", "No tracker is assigned");
}
void BiasIllum::setTracker(BiasTracker *newTracker)
{
d->tracker = newTracker;
}
void BiasIllum::evaluate(Vector3f &color, Vector3d const &point,
Vector3f const &normalAtPoint, uint biasTime,
/// @todo Refactor away:
byte activeContributors, byte changedContributions)
{
// Does the tracker have any lighting changes to apply?
if(changedContributions)
{
if(activeContributors & changedContributions)
{
/// @todo Do not assume the current map.
Map &map = App_World().map();
/*
* Recalculate the contribution for each changed light source.
* Continue using the previously calculated value otherwise.
*/
for(int i = 0; i < MAX_CONTRIBUTORS; ++i)
{
if(activeContributors & changedContributions & (1 << i))
{
d->updateContribution(i, point, normalAtPoint, map.bspRoot());
}
}
}
// Accumulate light contributions and initiate interpolation.
d->applyLightingChanges(activeContributors, biasTime);
}
// Factor in the current color (and perform interpolation if needed).
d->lerp(color, biasTime);
}
void BiasIllum::evaluate(ColorRawf &color, Vector3d const &point,
Vector3f const &normalAtPoint, uint biasTime,
/// @todo Refactor away:
byte activeContributors, byte changedContributions)
{
Vector3f tmp; evaluate(tmp, point, normalAtPoint, biasTime,
activeContributors, changedContributions);
for(int c = 0; c < 3; ++c)
color.rgba[c] += tmp[c];
}
void BiasIllum::consoleRegister() // static
{
C_VAR_INT("rend-bias-lightspeed", &lightSpeed, 0, 0, 5000);
// Development variables.
C_VAR_INT("rend-dev-bias-sight", &devUseSightCheck, CVF_NO_ARCHIVE, 0, 1);
}