/
rend_decor.cpp
588 lines (489 loc) · 16.4 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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/** @file rend_decor.cpp Surface Decorations.
*
* @author Copyright © 2003-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
* @author Copyright © 2006-2013 Daniel Swanson <danij@dengine.net>
* @author 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 <cmath>
#include "de_base.h"
#include "de_console.h"
#include "de_play.h"
#include "de_graphics.h"
#include "de_render.h"
#include "de_misc.h"
#include <de/vector1.h>
#include "def_main.h"
#include "resource/materialvariant.h"
using namespace de;
// Quite a bit of decorations, there!
#define MAX_DECOR_LIGHTS (16384)
BEGIN_PROF_TIMERS()
PROF_DECOR_UPDATE,
PROF_DECOR_PROJECT,
PROF_DECOR_ADD_LUMINOUS
END_PROF_TIMERS()
typedef struct decorsource_s {
coord_t origin[3];
coord_t maxDistance;
Surface const *surface;
BspLeaf *bspLeaf;
unsigned int lumIdx; // Index+1 of linked lumobj, or 0.
float fadeMul;
ded_decorlight_t const *def;
DGLuint flareTex;
struct decorsource_s *next;
} decorsource_t;
static void updateSideSectionDecorations(LineDef *lineDef, byte side, SideDefSection section);
static void updatePlaneDecorations(Plane *pln);
byte useLightDecorations = true;
float decorMaxDist = 2048; // No decorations are visible beyond this.
float decorLightBrightFactor = 1;
float decorLightFadeAngle = .1f;
static uint numDecorations = 0;
static decorsource_t *sourceFirst = 0;
static decorsource_t *sourceCursor = 0;
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);
}
/**
* Clears the list of decoration dummies.
*/
static void clearDecorations()
{
numDecorations = 0;
sourceCursor = sourceFirst;
}
extern void getLightingParams(coord_t x, coord_t y, coord_t z, BspLeaf* bspLeaf,
coord_t distance, boolean fullBright,
float ambientColor[3], uint* lightListIdx);
static void projectDecoration(decorsource_t *src)
{
float brightness, min, max;
lumobj_t const *lum;
vissprite_t *vis;
coord_t distance;
// Does it pass the sector light limitation?
min = src->def->lightLevels[0];
max = src->def->lightLevels[1];
if(!((brightness = R_CheckSectorLight(src->bspLeaf->sector->lightLevel, min, max)) > 0))
return;
if(src->fadeMul <= 0)
return;
// Is the point in range?
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;
/**
* Model decorations become model-type vissprites.
* Light decorations become flare-type vissprites.
*/
vis = R_NewVisSprite();
vis->type = VSPR_FLARE;
V3d_Copy(vis->origin, src->origin);
vis->distance = distance;
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(src->def->haloRadius > 0)
vis->data.flare.size = MAX_OF(1, src->def->haloRadius * 60 * (50 + haloSize) / 100.0f);
else
vis->data.flare.size = 0;
if(src->flareTex != 0)
vis->data.flare.tex = src->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(src->def->elevation < 2 && decorLightFadeAngle > 0) // Close the surface?
{
float vector[3], dot;
V3f_Set(vector, src->origin[VX] - vOrigin[VX],
src->origin[VY] - vOrigin[VZ],
src->origin[VZ] - vOrigin[VY]);
V3f_Normalize(vector);
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);
}
}
/**
* Re-initialize the decoration source tracking (might be called during a map
* load or othersuch situation).
*/
void Rend_DecorInit()
{
clearDecorations();
}
/**
* Project all the non-clipped decorations. They become regular vissprites.
*/
void Rend_ProjectDecorations()
{
if(!useLightDecorations) return;
if(sourceFirst == sourceCursor) return;
decorsource_t *src = sourceFirst;
do
{
projectDecoration(src);
} while((src = src->next) != sourceCursor);
}
static void addLuminousDecoration(decorsource_t *src)
{
ded_decorlight_t const *def = src->def;
float brightness;
uint i, lumIdx;
float min, max;
lumobj_t* l;
// Does it pass the sector light limitation?
min = def->lightLevels[0];
max = def->lightLevels[1];
if(!((brightness = R_CheckSectorLight(src->bspLeaf->sector->lightLevel, min, max)) > 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.
*/
lumIdx = LO_NewLuminous(LT_OMNI, src->bspLeaf);
l = LO_GetLuminous(lumIdx);
V3d_Copy(l->origin, src->origin);
l->maxDistance = src->maxDistance;
l->decorSource = src;
LUM_OMNI(l)->zOff = 0;
LUM_OMNI(l)->tex = GL_PrepareLightmap(def->sides);
LUM_OMNI(l)->ceilTex = GL_PrepareLightmap(def->up);
LUM_OMNI(l)->floorTex = GL_PrepareLightmap(def->down);
// These are the same rules as in DL_MobjRadius().
LUM_OMNI(l)->radius = def->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(i = 0; i < 3; ++i)
LUM_OMNI(l)->color[i] = def->color[i] * src->fadeMul;
src->lumIdx = lumIdx;
}
/**
* Create lumobjs for all decorations who want them.
*/
void Rend_AddLuminousDecorations()
{
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 *addDecoration()
{
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->def = NULL;
src->flareTex = 0;
// Advance the cursor.
sourceCursor = sourceCursor->next;
}
return src;
}
/**
* A decorsource is created from the specified surface decoration.
*/
static void createDecorSource(Surface const *suf, surfacedecor_t const *dec, float maxDistance)
{
// Out of sources?
if(numDecorations >= MAX_DECOR_LIGHTS) return;
++numDecorations;
// Fill in the data for a new surface decoration.
decorsource_t *src = addDecoration();
V3d_Copy(src->origin, dec->origin);
src->maxDistance = maxDistance;
src->bspLeaf = dec->bspLeaf;
src->surface = suf;
src->fadeMul = 1;
src->def = dec->def;
if(src->def)
{
ded_decorlight_t const *def = src->def;
if(!def->flare || Str_CompareIgnoreCase(Uri_Path(def->flare), "-"))
{
src->flareTex = GL_PrepareFlareTexture(def->flare, def->flareTexture);
}
}
}
/**
* @return As this can also be used with iterators, will always return @c true.
*/
boolean R_ProjectSurfaceDecorations(Surface *suf, void *context)
{
float maxDist = *((float *) context);
uint i;
if(suf->inFlags & SUIF_UPDATE_DECORATIONS)
{
R_ClearSurfaceDecorations(suf);
switch(DMU_GetType(suf->owner))
{
case DMU_SIDEDEF: {
SideDef *sideDef = (SideDef *)suf->owner;
LineDef *line = sideDef->line;
updateSideSectionDecorations(line, sideDef == line->L_frontsidedef? FRONT : BACK,
&sideDef->SW_middlesurface == suf? SS_MIDDLE :
&sideDef->SW_bottomsurface == suf? SS_BOTTOM : SS_TOP);
break; }
case DMU_PLANE:
updatePlaneDecorations((Plane *)suf->owner);
break;
default:
Con_Error("R_ProjectSurfaceDecorations: Internal Error, unknown type %s.", DMU_Str(DMU_GetType(suf->owner)));
break;
}
suf->inFlags &= ~SUIF_UPDATE_DECORATIONS;
}
if(useLightDecorations)
for(i = 0; i < suf->numDecorations; ++i)
{
surfacedecor_t const *d = &suf->decorations[i];
createDecorSource(suf, d, maxDist);
}
return true;
}
/**
* Determine proper skip values.
*/
static void getDecorationSkipPattern(int const patternSkip[2], int *skip)
{
for(uint i = 0; i < 2; ++i)
{
// Skip must be at least one.
skip[i] = patternSkip[i] + 1;
if(skip[i] < 1) skip[i] = 1;
}
}
static uint generateDecorLights(ded_decorlight_t const *def, Surface *suf,
material_t *mat, pvec3d_t const v1, pvec3d_t const /*v2*/, coord_t width, coord_t height,
pvec3d_t const delta, int axis, float offsetS, float offsetT, Sector* sec)
{
vec3d_t originBase, origin;
coord_t patternW, patternH;
float s, t; // Horizontal and vertical offset.
int skip[2];
uint num;
if(!mat || !Def_IsValidLightDecoration(def)) return 0;
// Skip must be at least one.
getDecorationSkipPattern(def->patternSkip, skip);
patternW = Material_Width(mat) * skip[0];
patternH = Material_Height(mat) * skip[1];
if(0 == patternW && 0 == patternH) return 0;
V3d_Set(originBase, def->elevation * suf->normal[VX],
def->elevation * suf->normal[VY],
def->elevation * suf->normal[VZ]);
V3d_Sum(originBase, originBase, v1);
// Let's see where the top left light is.
s = M_CycleIntoRange(def->pos[0] -
Material_Width(mat) * def->patternOffset[0] +
offsetS, patternW);
num = 0;
for(; s < width; s += patternW)
{
t = M_CycleIntoRange(def->pos[1] -
Material_Height(mat) * def->patternOffset[1] +
offsetT, patternH);
for(; t < height; t += patternH)
{
surfacedecor_t *d;
float offS = s / width, offT = t / height;
V3d_Set(origin, delta[VX] * offS,
delta[VY] * (axis == VZ? offT : offS),
delta[VZ] * (axis == VZ? offS : offT));
V3d_Sum(origin, originBase, origin);
if(sec)
{
// The point must be inside the correct sector.
if(!P_IsPointXYInSector(origin[VX], origin[VY], sec))
continue;
}
d = R_CreateSurfaceDecoration(suf);
if(d)
{
V3d_Copy(d->origin, origin);
d->bspLeaf = P_BspLeafAtPoint(d->origin);
d->def = def;
num++;
}
}
}
return num;
}
/**
* Generate decorations for the specified surface.
*/
static void updateSurfaceDecorations2(Surface *suf, float offsetS, float offsetT,
vec3d_t v1, vec3d_t v2, Sector *sec, boolean visible)
{
if(!visible) return;
ded_decor_t const *def = Material_Manifest(suf->material).decorationDef();
if(!def) return;
vec3d_t delta;
V3d_Subtract(delta, v2, v1);
if(delta[VX] * delta[VY] != 0 || delta[VX] * delta[VZ] != 0 || delta[VY] * delta[VZ] != 0)
{
int const axis = V3f_MajorAxis(suf->normal);
coord_t width, height;
if(axis == VX || axis == VY)
{
width = sqrt(delta[VX] * delta[VX] + delta[VY] * delta[VY]);
height = delta[VZ];
}
else
{
width = sqrt(delta[VX] * delta[VX]);
height = delta[VY];
}
if(width < 0) width = -width;
if(height < 0) height = -height;
// Generate a number of lights.
for(uint i = 0; i < DED_DECOR_NUM_LIGHTS; ++i)
{
generateDecorLights(&def->lights[i], suf, suf->material, v1, v2, width, height,
delta, axis, offsetS, offsetT, sec);
}
}
}
/**
* Generate decorations for a plane.
*/
static void updatePlaneDecorations(Plane *pln)
{
Sector *sec = pln->sector;
Surface *suf = &pln->surface;
vec3d_t v1, v2;
float offsetS, offsetT;
if(pln->type == PLN_FLOOR)
{
V3d_Set(v1, sec->aaBox.minX, sec->aaBox.maxY, pln->visHeight);
V3d_Set(v2, sec->aaBox.maxX, sec->aaBox.minY, pln->visHeight);
}
else
{
V3d_Set(v1, sec->aaBox.minX, sec->aaBox.minY, pln->visHeight);
V3d_Set(v2, sec->aaBox.maxX, sec->aaBox.maxY, pln->visHeight);
}
offsetS = -fmod(sec->aaBox.minX, 64) - suf->visOffset[0];
offsetT = -fmod(sec->aaBox.minY, 64) - suf->visOffset[1];
updateSurfaceDecorations2(suf, offsetS, offsetT, v1, v2, sec, suf->material? true : false);
}
static void updateSideSectionDecorations(LineDef *line, byte side, SideDefSection section)
{
float matOffset[2];
Surface *surface;
vec3d_t v1, v2;
boolean visible = false;
if(!line || !line->L_sidedef(side)) return;
surface = &line->L_sidedef(side)->SW_surface(section);
if(surface->material)
{
Sector *frontSec = line->L_sector(side);
Sector *backSec = line->L_sector(side^1);
SideDef *frontDef = line->L_sidedef(side);
SideDef *backDef = line->L_sidedef(side^1);
coord_t low, hi;
visible = R_FindBottomTop2(section, line->flags, frontSec, backSec, frontDef, backDef,
&low, &hi, matOffset);
if(visible)
{
V3d_Set(v1, line->L_vorigin(side )[VX], line->L_vorigin(side )[VY], hi);
V3d_Set(v2, line->L_vorigin(side^1)[VX], line->L_vorigin(side^1)[VY], low);
}
}
updateSurfaceDecorations2(surface, -matOffset[0], -matOffset[1], v1, v2, NULL, visible);
}
/**
* Decorations are generated for each frame.
*/
void Rend_InitDecorationsForFrame()
{
surfacelist_t *slist;
#ifdef DD_PROFILE
static int i;
if(++i > 40)
{
i = 0;
PRINT_PROF( PROF_DECOR_UPDATE );
PRINT_PROF( PROF_DECOR_PROJECT );
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_PROJECT );
clearDecorations();
slist = GameMap_DecoratedSurfaces(theMap);
if(slist)
{
R_SurfaceListIterate(slist, R_ProjectSurfaceDecorations, &decorMaxDist);
}
END_PROF( PROF_DECOR_PROJECT );
}