/
rend_dyn.c
680 lines (565 loc) · 19.4 KB
/
rend_dyn.c
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2008 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2005-2008 Daniel Swanson <danij@dengine.net>
*
* 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
*/
/**
* rend_dyn.c: Projected lumobjs (dynlight) lists.
*/
// HEADER FILES ------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include "de_base.h"
#include "de_console.h"
#include "de_refresh.h"
#include "de_graphics.h"
#include "de_render.h"
#include "de_play.h"
#include "de_misc.h"
// MACROS ------------------------------------------------------------------
#define LUM_FACTOR(dist, radius) (1.5f - 1.5f*(dist)/(radius))
// TYPES -------------------------------------------------------------------
typedef struct dynnode_s {
struct dynnode_s* next, *nextUsed;
dynlight_t dyn;
} dynnode_t;
typedef struct dynlist_s {
boolean sortBrightestFirst;
dynnode_t* head;
} dynlist_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
int useDynLights = true, dlBlend = 0;
float dlFactor = .7f;
float dlFogBright = .15f;
int useWallGlow = true;
float glowHeightFactor = 3; // Glow height as a multiplier.
int glowHeightMax = 100; // 100 is the default (0-1024).
// PRIVATE DATA DEFINITIONS ------------------------------------------------
// Dynlight nodes.
static dynnode_t* dynFirst, *dynCursor;
// Surface light link lists.
static uint numDynlightLinkLists = 0, dynlightLinkListCursor = 0;
static dynlist_t* dynlightLinkLists;
// CODE --------------------------------------------------------------------
void DL_Register(void)
{
// Cvars
C_VAR_INT("rend-glow", &glowingTextures, 0, 0, 1);
C_VAR_INT("rend-glow-wall", &useWallGlow, 0, 0, 1);
C_VAR_INT("rend-glow-height", &glowHeightMax, 0, 0, 1024);
C_VAR_FLOAT("rend-glow-scale", &glowHeightFactor, 0, 0.1f, 10);
C_VAR_INT2("rend-light", &useDynLights, 0, 0, 1, LO_UnlinkMobjLumobjs);
C_VAR_INT("rend-light-blend", &dlBlend, 0, 0, 2);
C_VAR_FLOAT("rend-light-bright", &dlFactor, 0, 0, 1);
C_VAR_FLOAT("rend-light-fog-bright", &dlFogBright, 0, 0, 1);
C_VAR_INT("rend-light-multitex", &useMultiTexLights, 0, 0, 1);
C_VAR_INT("rend-mobj-light-auto", &useMobjAutoLights, 0, 0, 1);
LO_Register();
Rend_DecorRegister();
}
/**
* Initialize the dynlight system in preparation for rendering view(s) of the
* game world. Called by R_InitLevel().
*/
void DL_InitForMap(void)
{
dynlightLinkLists = NULL;
numDynlightLinkLists = 0, dynlightLinkListCursor = 0;
}
/**
* Moves all used dynlight nodes to the list of unused nodes, so they
* can be reused.
*/
void DL_InitForNewFrame(void)
{
// Start reusing nodes from the first one in the list.
dynCursor = dynFirst;
// Clear the surface light link lists.
dynlightLinkListCursor = 0;
if(numDynlightLinkLists)
memset(dynlightLinkLists, 0, numDynlightLinkLists * sizeof(dynlist_t));
}
/**
* Create a new dynlight list.
*
* @param sortBrightestFirst Nodes in the list will be auto-sorted when
* added by the brightness of the associated dynlight
* in descending order.
* @return Identifier for the new list.
*/
static uint newDynlightList(boolean sortBrightestFirst)
{
dynlist_t* list;
// Ran out of light link lists?
if(++dynlightLinkListCursor >= numDynlightLinkLists)
{
uint newNum = numDynlightLinkLists * 2;
if(!newNum)
newNum = 2;
dynlightLinkLists =
Z_Realloc(dynlightLinkLists, newNum * sizeof(dynlist_t), PU_MAP);
numDynlightLinkLists = newNum;
}
list = &dynlightLinkLists[dynlightLinkListCursor-1];
list->head = NULL;
list->sortBrightestFirst = sortBrightestFirst;
return dynlightLinkListCursor - 1;
}
static dynnode_t* newDynNode(void)
{
dynnode_t* node;
// Have we run out of nodes?
if(dynCursor == NULL)
{
node = Z_Malloc(sizeof(dynnode_t), PU_STATIC, NULL);
// Link the new node to the list.
node->nextUsed = dynFirst;
dynFirst = node;
}
else
{
node = dynCursor;
dynCursor = dynCursor->nextUsed;
}
node->next = NULL;
return node;
}
/**
* Returns a new dynlight node. If the list of unused nodes is empty,
* a new node is created.
*/
static dynnode_t* newDynLight(float* s, float* t)
{
dynnode_t* node = newDynNode();
dynlight_t* dyn = &node->dyn;
if(s)
{
dyn->s[0] = s[0];
dyn->s[1] = s[1];
}
if(t)
{
dyn->t[0] = t[0];
dyn->t[1] = t[1];
}
return node;
}
static void linkDynNodeToList(dynnode_t* node, uint listIndex)
{
dynlist_t* list = &dynlightLinkLists[listIndex];
if(list->sortBrightestFirst && list->head)
{
float light = (node->dyn.color[0] +
node->dyn.color[1] + node->dyn.color[2]) / 3;
dynnode_t* last, *iter;
last = iter = list->head;
do
{
// Is this brighter than the one being added?
if((iter->dyn.color[0] + iter->dyn.color[1] +
iter->dyn.color[2]) / 3 > light)
{
last = iter;
iter = iter->next;
}
else
{ // Need to insert it here.
node->next = last->next;
last->next = node;
return;
}
} while(iter);
}
node->next = list->head;
list->head = node;
}
/**
* Blend the given light value with the lumobj's color, apply any global
* modifiers and output the result.
*
* @param outRGB The location the calculated result will be written to.
* @param lum Ptr to the lumobj from which the color will be used.
* @param light The light value to be used in the calculation.
*/
static void calcDynLightColor(float* outRGB, const float* inRGB, float light)
{
uint i;
if(light < 0)
light = 0;
else if(light > 1)
light = 1;
light *= dlFactor;
// In fog, additive blending is used. The normal fog color
// is way too bright.
if(usingFog)
light *= dlFogBright; // Would be too much.
// Multiply with the light color.
for(i = 0; i < 3; ++i)
{
outRGB[i] = light * inRGB[i];
}
}
/**
* Project the given planelight onto the specified seg. If it would be lit,
* a new dynlight node will be created and returned.
*
* @param lum Ptr to the lumobj lighting the seg.
* @param bottom Z height (bottom) of the section being lit.
* @param top Z height (top) of the section being lit.
*
* @return Ptr to the projected light, ELSE @c NULL.
*/
static dynnode_t* projectPlaneGlowOnSegSection(const lumobj_t* lum,
float bottom, float top)
{
float glowHeight;
float s[2], t[2];
if(bottom >= top)
return NULL; // No height.
glowHeight =
(MAX_GLOWHEIGHT * LUM_PLANE(lum)->intensity) * glowHeightFactor;
// Don't make too small or too large glows.
if(glowHeight <= 2)
return NULL;
if(glowHeight > glowHeightMax)
glowHeight = glowHeightMax;
// Calculate texture coords for the light.
if(LUM_PLANE(lum)->normal[VZ] < 0)
{ // Light is cast downwards.
t[1] = t[0] = (lum->pos[VZ] - top) / glowHeight;
t[1]+= (top - bottom) / glowHeight;
}
else
{ // Light is cast upwards.
t[0] = t[1] = (bottom - lum->pos[VZ]) / glowHeight;
t[0]+= (top - bottom) / glowHeight;
}
if(!(t[0] <= 1 || t[1] >= 0))
return NULL; // Is above/below on the Y axis.
// The horizontal direction is easy.
s[0] = 0;
s[1] = 1;
return newDynLight(s, t);
}
/**
* Given a normalized normal, construct up and right vectors, oriented to
* the original normal. Note all vectors and normals are in world-space.
*
* @param up The up vector will be written back here.
* @param right The right vector will be written back here.
* @param normal Normal to construct vectors for.
*/
static void buildUpRight(pvec3_t up, pvec3_t right, const pvec3_t normal)
{
const vec3_t rotm[3] = {
{0.f, 0.f, 1.f},
{0.f, 0.f, 1.f},
{0.f, 0.f, 1.f}
};
int axis = VX;
vec3_t fn;
V3_Set(fn, fabsf(normal[VX]), fabsf(normal[VY]), fabsf(normal[VZ]));
if(fn[VY] > fn[axis])
axis = VY;
if(fn[VZ] > fn[axis])
axis = VZ;
if(fabsf(fn[VX] - 1.0f) < FLT_EPSILON ||
fabsf(fn[VY] - 1.0f) < FLT_EPSILON ||
fabsf(fn[VZ] - 1.0f) < FLT_EPSILON)
{ // We must build the right vector manually.
if(axis == VX && normal[VX] > 0.f)
{
V3_Set(right, 0.f, 1.f, 0.f);
}
else if(axis == VX)
{
V3_Set(right, 0.f, -1.f, 0.f);
}
if(axis == VY && normal[VY] > 0.f)
{
V3_Set(right, -1.f, 0.f, 0.f);
}
else if(axis == VY)
{
V3_Set(right, 1.f, 0.f, 0.f);
}
if(axis == VZ)
{
V3_Set(right, 1.f, 0.f, 0.f);
}
}
else
{ // Can use a cross product of the surface normal.
V3_CrossProduct(right, (const pvec3_t) rotm[axis], normal);
V3_Normalize(right);
}
V3_CrossProduct(up, right, normal);
V3_Normalize(up);
}
/**
* Generate texcoords on surface centered on point.
*
* @param point Point on surface around which texture is centered.
* @param scale Scale multiplier for texture.
* @param s Texture s coords written back here.
* @param t Texture t coords written back here.
* @param v1 Top left vertex of the surface being projected on.
* @param v2 Bottom right vertex of the surface being projected on.
* @param normal Normal of the surface being projected on.
*
* @return @c true, if the generated coords are within bounds.
*/
static boolean genTexCoords(const pvec3_t point, float scale,
pvec2_t s, pvec2_t t, const pvec3_t v1,
const pvec3_t v2, const pvec3_t normal)
{
vec3_t vToPoint, right, up;
buildUpRight(up, right, normal);
V3_Subtract(vToPoint, v1, point);
s[0] = V3_DotProduct(vToPoint, right) * scale + .5f;
t[0] = V3_DotProduct(vToPoint, up) * scale + .5f;
V3_Subtract(vToPoint, v2, point);
s[1] = V3_DotProduct(vToPoint, right) * scale + .5f;
t[1] = V3_DotProduct(vToPoint, up) * scale + .5f;
// Would the light be visible?
if(!(s[0] <= 1 || s[1] >= 0))
return false; // Is right/left on the X axis.
if(!(t[0] <= 1 || t[1] >= 0))
return false; // Is above/below on the Y axis.
return true;
}
typedef struct surfacelumobjiterparams_s {
vec3_t v1, v2;
vec3_t normal;
boolean sortBrightestFirst;
boolean haveList;
uint listIdx;
boolean isPlane;
byte useTex; /* For omni lights:
@c 0 = side tex, @c 1 = floortex, @c 2 = ceiltex. */
} surfacelumobjiterparams_t;
boolean DLIT_SurfaceLumobjContacts(void* ptr, void* data)
{
lumobj_t* lum = (lumobj_t*) ptr;
dynnode_t* node = NULL;
surfacelumobjiterparams_t* params = data;
DGLuint tex = 0;
float lightBrightness = 1;
float* lightRGB;
uint lumIdx;
if(!(lum->type == LT_OMNI || lum->type == LT_PLANE))
return true; // Continue iteration.
lumIdx = LO_ToIndex(lum);
switch(lum->type)
{
case LT_OMNI:
if(LO_IsHidden(lumIdx, viewPlayer - ddPlayers))
return true;
switch(params->useTex)
{
case 0:
default: tex = LUM_OMNI(lum)->tex; break;
case 1: tex = LUM_OMNI(lum)->floorTex; break;
case 2: tex = LUM_OMNI(lum)->ceilTex; break;
}
lightRGB = LUM_OMNI(lum)->color;
if(tex)
{
vec3_t lumCenter, vToLum;
V3_Set(lumCenter, lum->pos[VX], lum->pos[VY],
lum->pos[VZ] + LUM_OMNI(lum)->zOff);
// On the right side?
V3_Subtract(vToLum, params->v1, lumCenter);
if(V3_DotProduct(vToLum, params->normal) < 0.f)
{
float dist;
vec3_t point;
// Calculate 3D distance between surface and lumobj.
V3_ClosestPointOnPlane(point, params->normal, params->v1, lumCenter);
dist = V3_Distance(point, lumCenter);
if(dist > 0 && dist <= LUM_OMNI(lum)->radius)
{
lightBrightness = LUM_FACTOR(dist, LUM_OMNI(lum)->radius);
// If a max distance limit is set, lumobjs fade out.
if(lum->maxDistance > 0)
{
float distFromViewer =
LO_DistanceToViewer(lumIdx, viewPlayer - ddPlayers);
if(distFromViewer > lum->maxDistance)
lightBrightness = 0;
if(distFromViewer > .67f * lum->maxDistance)
{
lightBrightness *= (lum->maxDistance - distFromViewer) /
(.33f * lum->maxDistance);
}
}
if(lightBrightness >= .05f)
{
vec2_t s, t;
float scale =
1.0f / ((2.f * LUM_OMNI(lum)->radius) - dist);
if(genTexCoords(point, scale, s, t, params->v1,
params->v2, params->normal))
{
node = newDynLight(s, t);
}
}
}
}
}
break;
case LT_PLANE:
if(!params->isPlane) // Plannar glows don't currently affect planes.
tex = LUM_PLANE(lum)->tex;
if(tex)
{
lightRGB = LUM_PLANE(lum)->color;
node = projectPlaneGlowOnSegSection(lum, params->v2[VZ], params->v1[VZ]);
}
break;
default:
Con_Error("DLIT_SegLumobjContacts: Invalid value, lum->type = %i.",
(int) lum->type);
break;
}
if(node)
{
dynlight_t* dyn = &node->dyn;
dyn->texture = tex;
calcDynLightColor(dyn->color, lightRGB, lightBrightness);
// Got a list for this surface yet?
if(!params->haveList)
{
params->listIdx =
newDynlightList(params->sortBrightestFirst);
params->haveList = true;
}
linkDynNodeToList(node, params->listIdx);
}
return true; // Continue iteration.
}
static uint processSubSector(subsector_t* ssec, surfacelumobjiterparams_t* params)
{
// Process each lumobj contacting the subsector.
R_IterateSubsectorContacts(ssec, OT_LUMOBJ, DLIT_SurfaceLumobjContacts,
params);
// Did we generate a light list?
if(params->haveList)
return params->listIdx + 1;
return 0; // Nope.
}
/**
* Process dynamic lights for the specified seg.
*/
uint DL_ProcessSegSection(seg_t* seg, subsector_t* ssec, float bottom,
float top, boolean sortBrightestFirst)
{
sidedef_t* side;
surfacelumobjiterparams_t params;
if(!useDynLights && !useWallGlow)
return 0; // Disabled.
if(!seg || !ssec)
return 0;
side = SEG_SIDEDEF(seg);
V3_Set(params.v1, seg->SG_v1pos[VX], seg->SG_v1pos[VY], top);
V3_Set(params.v2, seg->SG_v2pos[VX], seg->SG_v2pos[VY], bottom);
params.normal[VX] = side->SW_middlenormal[VX];
params.normal[VY] = side->SW_middlenormal[VY];
params.normal[VZ] = side->SW_middlenormal[VZ];
params.sortBrightestFirst = sortBrightestFirst;
params.haveList = false;
params.listIdx = 0;
params.useTex = 0;
params.isPlane = false;
return processSubSector(ssec, ¶ms);
}
uint DL_ProcessSubSectorPlane(subsector_t* ssec, uint plane)
{
plane_t* pln;
surfacelumobjiterparams_t params;
if(!useDynLights && !useWallGlow)
return 0; // Disabled.
if(!ssec)
return 0;
assert(plane < ssec->sector->planeCount); // Sanity check.
pln = ssec->sector->SP_plane(plane);
if(pln->type == PLN_FLOOR)
{
V3_Set(params.v1, ssec->bBox[0].pos[VX], ssec->bBox[1].pos[VY],
pln->visHeight);
V3_Set(params.v2, ssec->bBox[1].pos[VX], ssec->bBox[0].pos[VY],
pln->visHeight);
}
else
{
V3_Set(params.v1, ssec->bBox[0].pos[VX], ssec->bBox[0].pos[VY],
pln->visHeight);
V3_Set(params.v2, ssec->bBox[1].pos[VX], ssec->bBox[1].pos[VY],
pln->visHeight);
}
params.normal[VX] = pln->PS_normal[VX];
params.normal[VY] = pln->PS_normal[VY];
params.normal[VZ] = pln->PS_normal[VZ];
params.sortBrightestFirst = false;
params.haveList = false;
params.listIdx = 0;
params.useTex = (pln->type == PLN_CEILING? 2 : 1);
params.isPlane = true;
return processSubSector(ssec, ¶ms);
}
/**
* Calls func for all projected dynlights in the given list.
*
* @param listIdx Identifier of the list to process.
* @param data Ptr to pass to the callback.
* @param func Callback to make for each object.
*
* @return @c true, iff every callback returns @c true.
*/
boolean DL_ListIterator(uint listIdx, void* data,
boolean (*func) (const dynlight_t*, void*))
{
dynnode_t* node;
boolean retVal, isDone;
if(listIdx == 0 || listIdx > numDynlightLinkLists)
return true;
listIdx--;
node = dynlightLinkLists[listIdx].head;
retVal = true;
isDone = false;
while(node && !isDone)
{
if(!func(&node->dyn, data))
{
retVal = false;
isDone = true;
}
else
node = node->next;
}
return retVal;
}