/
rend_decor.cpp
517 lines (415 loc) · 15.2 KB
/
rend_decor.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/** @file rend_decor.cpp Surface Decorations.
*
* @authors Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2006-2007 Jamie Jones <jamie_jones_au@yahoo.com.au>
*
* @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_console.h"
#include "de_play.h"
#include "de_render.h"
#include "def_main.h"
#include "m_profiler.h"
#include "MaterialVariantSpec"
#include "map/gamemap.h"
#include <de/math.h>
#include <de/vector1.h>
#include "render/rend_decor.h"
using namespace de;
/// Quite a bit of decorations, there!
#define MAX_DECOR_LIGHTS (16384)
/// No decorations are visible beyond this.
#define MAX_DECOR_DISTANCE (2048)
BEGIN_PROF_TIMERS()
PROF_DECOR_UPDATE,
PROF_DECOR_BEGIN_FRAME,
PROF_DECOR_ADD_LUMINOUS
END_PROF_TIMERS()
/// @todo This abstraction is now unnecessary (merge with Surface::DecorSource) -ds
typedef struct decorsource_s {
coord_t origin[3];
coord_t maxDistance;
Surface const *surface;
BspLeaf *bspLeaf;
uint lumIdx; // Index+1 of linked lumobj, or 0.
float fadeMul;
MaterialSnapshot::Decoration const *decor;
struct decorsource_s *next;
} decorsource_t;
byte useLightDecorations = true;
float decorLightBrightFactor = 1;
float decorLightFadeAngle = .1f;
static uint sourceCount = 0;
static decorsource_t *sourceFirst = 0;
static decorsource_t *sourceCursor = 0;
static void plotSourcesForLineSide(Line::Side &side, int section);
static void plotSourcesForPlane(Plane &pln);
void Rend_DecorRegister()
{
C_VAR_BYTE("rend-light-decor", &useLightDecorations, 0, 0, 1);
C_VAR_FLOAT("rend-light-decor-angle", &decorLightFadeAngle, 0, 0, 1);
C_VAR_FLOAT("rend-light-decor-bright", &decorLightBrightFactor, 0, 0, 10);
}
static void recycleSources()
{
sourceCount = 0;
sourceCursor = sourceFirst;
}
static void projectSource(decorsource_t const &src)
{
MaterialSnapshot::Decoration const *decor = src.decor;
// Don't project decorations which emit no color.
if(decor->color.x == 0 && decor->color.y == 0 && decor->color.z == 0) return;
// Does it pass the sector light limitation?
float min = decor->lightLevels[0];
float max = decor->lightLevels[1];
float lightLevel = src.bspLeaf->sector().lightLevel();
Rend_ApplyLightAdaptation(&lightLevel);
float brightness = R_CheckSectorLight(lightLevel, min, max);
if(!(brightness > 0)) return;
if(src.fadeMul <= 0) return;
// Is the point in range?
coord_t distance = Rend_PointDist3D(src.origin);
if(distance > src.maxDistance) return;
/// @todo dj: Why is LO_GetLuminous returning NULL given a supposedly valid index?
if(!LO_GetLuminous(src.lumIdx)) return;
/*
* Light decorations become flare-type vissprites.
*/
vissprite_t *vis = R_NewVisSprite();
vis->type = VSPR_FLARE;
V3d_Copy(vis->origin, src.origin);
vis->distance = distance;
lumobj_t const *lum = LO_GetLuminous(src.lumIdx);
vis->data.flare.isDecoration = true;
vis->data.flare.lumIdx = src.lumIdx;
// Color is taken from the associated lumobj.
V3f_Copy(vis->data.flare.color, LUM_OMNI(lum)->color);
if(decor->haloRadius > 0)
vis->data.flare.size = MAX_OF(1, decor->haloRadius * 60 * (50 + haloSize) / 100.0f);
else
vis->data.flare.size = 0;
if(decor->flareTex != 0)
{
vis->data.flare.tex = decor->flareTex;
}
else
{ // Primary halo disabled.
vis->data.flare.flags |= RFF_NO_PRIMARY;
vis->data.flare.tex = 0;
}
// Halo brightness drops as the angle gets too big.
vis->data.flare.mul = 1;
if(decor->elevation < 2 && decorLightFadeAngle > 0) // Close the surface?
{
float vector[3];
V3f_Set(vector, src.origin[VX] - vOrigin[VX],
src.origin[VY] - vOrigin[VZ],
src.origin[VZ] - vOrigin[VY]);
V3f_Normalize(vector);
float dot = -(src.surface->normal()[VX] * vector[VX] +
src.surface->normal()[VY] * vector[VY] +
src.surface->normal()[VZ] * vector[VZ]);
if(dot < decorLightFadeAngle / 2)
vis->data.flare.mul = 0;
else if(dot < 3 * decorLightFadeAngle)
vis->data.flare.mul = (dot - decorLightFadeAngle / 2) / (2.5f * decorLightFadeAngle);
}
}
void Rend_DecorInitForMap()
{
recycleSources();
}
void Rend_DecorProject()
{
if(!useLightDecorations) return;
for(decorsource_t *src = sourceFirst; src != sourceCursor; src = src->next)
{
projectSource(*src);
}
}
static void addLuminousDecoration(decorsource_t &src)
{
MaterialSnapshot::Decoration const *decor = src.decor;
// Don't add decorations which emit no color.
if(decor->color.x == 0 && decor->color.y == 0 && decor->color.z == 0) return;
// Does it pass the sector light limitation?
float min = decor->lightLevels[0];
float max = decor->lightLevels[1];
float lightLevel = src.bspLeaf->sector().lightLevel();
Rend_ApplyLightAdaptation(&lightLevel);
float brightness = R_CheckSectorLight(lightLevel, min, max);
if(!(brightness > 0)) return;
// Apply the brightness factor (was calculated using sector lightlevel).
src.fadeMul = brightness * decorLightBrightFactor;
src.lumIdx = 0;
if(src.fadeMul <= 0) return;
/**
* @todo From here on is pretty much the same as LO_AddLuminous,
* reconcile the two.
*/
uint lumIdx = LO_NewLuminous(LT_OMNI, src.bspLeaf);
lumobj_t *l = LO_GetLuminous(lumIdx);
V3d_Copy(l->origin, src.origin);
l->maxDistance = src.maxDistance;
l->decorSource = (void*)&src;
LUM_OMNI(l)->zOff = 0;
LUM_OMNI(l)->tex = decor->tex;
LUM_OMNI(l)->ceilTex = decor->ceilTex;
LUM_OMNI(l)->floorTex = decor->floorTex;
// These are the same rules as in DL_MobjRadius().
LUM_OMNI(l)->radius = decor->radius * 40 * loRadiusFactor;
// Don't make a too small or too large light.
if(LUM_OMNI(l)->radius > loMaxRadius)
LUM_OMNI(l)->radius = loMaxRadius;
for(uint i = 0; i < 3; ++i)
LUM_OMNI(l)->color[i] = decor->color[i] * src.fadeMul;
src.lumIdx = lumIdx;
}
void Rend_DecorAddLuminous()
{
BEGIN_PROF( PROF_DECOR_ADD_LUMINOUS );
if(useLightDecorations && sourceFirst != sourceCursor)
{
decorsource_t *src = sourceFirst;
do
{
addLuminousDecoration(*src);
} while((src = src->next) != sourceCursor);
}
END_PROF( PROF_DECOR_ADD_LUMINOUS );
}
/**
* Create a new decoration source.
*/
static decorsource_t *allocDecorSource()
{
decorsource_t *src;
// If the cursor is NULL, new sources must be allocated.
if(!sourceCursor)
{
// Allocate a new entry.
src = (decorsource_t *) Z_Calloc(sizeof(decorsource_t), PU_APPSTATIC, NULL);
if(!sourceFirst)
{
sourceFirst = src;
}
else
{
src->next = sourceFirst;
sourceFirst = src;
}
}
else
{
// There are old sources to use.
src = sourceCursor;
src->fadeMul = 0;
src->lumIdx = 0;
src->maxDistance = 0;
V3d_Set(src->origin, 0, 0, 0);
src->bspLeaf = 0;
src->surface = 0;
src->decor = 0;
// Advance the cursor.
sourceCursor = sourceCursor->next;
}
return src;
}
/**
* A source is created from the specified surface decoration.
*/
static void newSource(Surface const &suf, Surface::DecorSource const &dec)
{
// Out of sources?
if(sourceCount >= MAX_DECOR_LIGHTS) return;
++sourceCount;
decorsource_t *src = allocDecorSource();
// Fill in the data for a new surface decoration.
V3d_Set(src->origin, dec.origin.x, dec.origin.y, dec.origin.z);
src->maxDistance = MAX_DECOR_DISTANCE;
src->bspLeaf = dec.bspLeaf;
src->surface = &suf;
src->fadeMul = 1;
src->decor = dec.decor;
}
static void plotSourcesForSurface(Surface &suf)
{
if(suf._decorationData.needsUpdate)
{
suf.clearDecorations();
switch(suf.owner().type())
{
case DMU_SIDE: {
Line::Side *side = suf.owner().castTo<Line::Side>();
plotSourcesForLineSide(*side, &side->middle() == &suf? Line::Side::Middle :
&side->bottom() == &suf? Line::Side::Bottom : Line::Side::Top);
break; }
case DMU_PLANE: {
Plane *plane = suf.owner().castTo<Plane>();
plotSourcesForPlane(*plane);
break; }
default:
DENG2_ASSERT(0); // Invalid type.
}
suf._decorationData.needsUpdate = false;
}
if(useLightDecorations)
{
Surface::DecorSource const *sources = (Surface::DecorSource const *)suf._decorationData.sources;
for(uint i = 0; i < suf.decorationCount(); ++i)
{
newSource(suf, sources[i]);
}
}
}
static inline void getDecorationSkipPattern(Vector2i const &patternSkip, Vector2i &skip)
{
// Skip values must be at least one.
skip.x = de::max(patternSkip.x + 1, 1);
skip.y = de::max(patternSkip.y + 1, 1);
}
static uint generateDecorLights(MaterialSnapshot::Decoration const &decor,
Vector2i const &patternOffset, Vector2i const &patternSkip, Surface &suf,
Material &material, Vector3d const &v1, Vector3d const &/*v2*/,
Vector2d sufDimensions, Vector3d const &delta, int axis,
Vector2f const &offset, Sector *containingSector)
{
uint decorCount = 0;
// Skip must be at least one.
Vector2i skip;
getDecorationSkipPattern(patternSkip, skip);
float patternW = material.width() * skip.x;
float patternH = material.height() * skip.y;
if(de::fequal(patternW, 0) && de::fequal(patternH, 0)) return 0;
Vector3d topLeft = v1 + Vector3d(decor.elevation * suf.normal()[VX],
decor.elevation * suf.normal()[VY],
decor.elevation * suf.normal()[VZ]);
// Determine the leftmost point.
float s = de::wrap(decor.pos[0] - material.width() * patternOffset.x + offset.x,
0.f, patternW);
// Plot decorations.
for(; s < sufDimensions.x; s += patternW)
{
// Determine the topmost point for this row.
float t = de::wrap(decor.pos[1] - material.height() * patternOffset.y + offset.y,
0.f, patternH);
for(; t < sufDimensions.y; t += patternH)
{
float const offS = s / sufDimensions.x;
float const offT = t / sufDimensions.y;
Vector3d origin = topLeft + Vector3d(delta.x * offS,
delta.y * (axis == VZ? offT : offS),
delta.z * (axis == VZ? offS : offT));
if(containingSector)
{
// The point must be inside the correct sector.
if(!P_IsPointInSector(origin, *containingSector))
continue;
}
if(Surface::DecorSource *source = suf.newDecoration())
{
source->origin = origin;
source->bspLeaf = theMap->bspLeafAtPoint(origin);
source->decor = &decor;
decorCount++;
}
}
}
return decorCount;
}
/**
* Generate decorations for the specified surface.
*/
static void updateSurfaceDecorations(Surface &suf, Vector2f const &offset,
Vector3d const &v1, Vector3d const &v2, Sector *sec = 0)
{
Vector3d delta = v2 - v1;
if(de::fequal(delta.length(), 0)) return;
int const axis = suf.normal().maxAxis();
Vector2d sufDimensions;
if(axis == 0 || axis == 1)
{
sufDimensions.x = std::sqrt(delta.x * delta.x + delta.y * delta.y);
sufDimensions.y = delta.z;
}
else
{
sufDimensions.x = std::sqrt(delta.x * delta.x);
sufDimensions.y = delta.y;
}
if(sufDimensions.x < 0) sufDimensions.x = -sufDimensions.x;
if(sufDimensions.y < 0) sufDimensions.y = -sufDimensions.y;
// Generate a number of lights.
MaterialSnapshot const &ms = suf.material().prepare(Rend_MapSurfaceMaterialSpec());
Material::Decorations const &decorations = suf.material().decorations();
for(int i = 0; i < decorations.count(); ++i)
{
MaterialSnapshot::Decoration const &decor = ms.decoration(i);
MaterialDecoration const *def = decorations[i];
generateDecorLights(decor, def->patternOffset(), def->patternSkip(),
suf, suf.material(), v1, v2, sufDimensions,
delta, axis, offset, sec);
}
}
static void plotSourcesForPlane(Plane &pln)
{
Surface &surface = pln.surface();
if(!surface.hasMaterial()) return;
Sector §or = pln.sector();
AABoxd const §orAABox = sector.aaBox();
Vector3d v1(sectorAABox.minX, pln.type() == Plane::Floor? sectorAABox.maxY : sectorAABox.minY, pln.visHeight());
Vector3d v2(sectorAABox.maxX, pln.type() == Plane::Floor? sectorAABox.minY : sectorAABox.maxY, pln.visHeight());
Vector2f offset(-fmod(sectorAABox.minX, 64) - surface.visMaterialOrigin()[0],
-fmod(sectorAABox.minY, 64) - surface.visMaterialOrigin()[1]);
updateSurfaceDecorations(surface, offset, v1, v2, §or);
}
static void plotSourcesForLineSide(Line::Side &side, int section)
{
if(!side.hasSections()) return;
if(!side.surface(section).hasMaterial()) return;
// Is the line section potentially visible?
coord_t bottom, top;
Vector2f materialOrigin;
R_SideSectionCoords(side, section, &bottom, &top, &materialOrigin);
if(!(top > bottom)) return;
Vector3d v1(side.from().origin().x, side.from().origin().y, top);
Vector3d v2( side.to().origin().x, side.to().origin().y, bottom);
updateSurfaceDecorations(side.surface(section), -materialOrigin, v1, v2);
}
void Rend_DecorBeginFrame()
{
#ifdef DD_PROFILE
static int i;
if(++i > 40)
{
i = 0;
PRINT_PROF( PROF_DECOR_UPDATE );
PRINT_PROF( PROF_DECOR_BEGIN_FRAME );
PRINT_PROF( PROF_DECOR_ADD_LUMINOUS );
}
#endif
// This only needs to be done if decorations have been enabled.
if(!useLightDecorations) return;
BEGIN_PROF( PROF_DECOR_BEGIN_FRAME );
recycleSources();
foreach(Surface *surface, theMap->decoratedSurfaces())
{
plotSourcesForSurface(*surface);
}
END_PROF( PROF_DECOR_BEGIN_FRAME );
}