/
rend_dyn.c
1727 lines (1453 loc) · 48.2 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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**\file
*\section Copyright and License Summary
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2003-2006 Jaakko Keränen <skyjake@dengine.net>
*\author Copyright © 2005-2006 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: Dynamic Lights
*/
// HEADER FILES ------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "de_base.h"
#include "de_console.h"
#include "de_render.h"
#include "de_refresh.h"
#include "de_play.h"
#include "de_graphics.h"
#include "de_misc.h"
// MACROS ------------------------------------------------------------------
#define X_TO_DLBX(cx) ( ((cx) - dlBlockOrig.pos[VX]) >> (FRACBITS+7) )
#define Y_TO_DLBY(cy) ( ((cy) - dlBlockOrig.pos[VY]) >> (FRACBITS+7) )
#define DLB_ROOT_DLBXY(bx, by) (dlBlockLinks + bx + by*dlBlockWidth)
#define LUM_FACTOR(dist) (1.5f - 1.5f*(dist)/lum->radius)
// TYPES -------------------------------------------------------------------
typedef struct {
boolean isLit;
float height;
DGLuint decorMap;
} planeitervars_t;
typedef struct {
float color[3];
float size;
float flareSize;
float xOffset, yOffset;
} lightconfig_t;
typedef struct seglight_s {
dynlight_t *wallSection[3];
} seglight_t;
typedef struct subseclight_s {
dynlight_t **planes;
} subseclight_t;
typedef struct lumcontact_s {
struct lumcontact_s *next; // Next in the subsector.
struct lumcontact_s *nextUsed; // Next used contact.
lumobj_t *lum;
} lumcontact_t;
typedef struct contactfinder_data_s {
fixed_t box[4];
boolean didSpread;
lumobj_t *lum;
int firstValid;
} contactfinder_data_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
extern int useDynLights;
extern int useBias;
// PUBLIC DATA DEFINITIONS -------------------------------------------------
boolean dlInited = false;
int useDynLights = true, dlBlend = 0;
float dlFactor = 0.7f; // was 0.6f
int useWallGlow = true;
float glowHeightFactor = 3; // glow height as a multiplier
int glowHeightMax = 100; // 100 is the default (0-1024)
float glowFogBright = .15f;
lumobj_t *luminousList = 0;
int numLuminous = 0, maxLuminous = 0;
int dlMaxRad = 256; // Dynamic lights maximum radius.
float dlRadFactor = 3;
int maxDynLights = 0;
int useMobjAutoLights = true; // Enable automaticaly calculated lights
// attached to mobjs.
//int clipLights = 1;
byte rendInfoLums = false;
int dlMinRadForBias = 136; // Lights smaller than this will NEVER
// be converted to BIAS sources.
// PRIVATE DATA DEFINITIONS ------------------------------------------------
// Dynlight nodes.
static dynlight_t *dynFirst, *dynCursor;
static lumobj_t **dlBlockLinks = 0;
static vertex_t dlBlockOrig;
static int dlBlockWidth, dlBlockHeight; // In 128 blocks.
static lumobj_t **dlSubLinks = 0;
// A list of dynlight nodes for each surface (seg, subsector-planes[]).
// The segs are indexed by seg index, subSecs are indexed by
// subsector index.
static seglight_t *segLightLinks;
static subseclight_t *subSecLightLinks;
// List of unused and used lumobj-subsector contacts.
static lumcontact_t *contFirst, *contCursor;
// List of lumobj contacts for each subsector.
static lumcontact_t **subContacts;
// A framecount for each block. Used to prevent multiple processing of
// a block during one frame.
static int *spreadBlocks;
// Used when iterating planes.
static int maxPlanes = 0;
static planeitervars_t *planeVars;
// 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_FLOAT("rend-glow-fog-bright", &glowFogBright, 0, 0, 1);
C_VAR_BYTE("rend-info-lums", &rendInfoLums, 0, 0, 1);
C_VAR_INT("rend-light", &useDynLights, 0, 0, 1);
C_VAR_INT("rend-light-blend", &dlBlend, 0, 0, 2);
C_VAR_FLOAT("rend-light-bright", &dlFactor, 0, 0, 1);
C_VAR_INT("rend-light-num", &maxDynLights, 0, 0, 8000);
C_VAR_FLOAT("rend-light-radius-scale", &dlRadFactor, 0, 0.1f, 10);
C_VAR_INT("rend-light-radius-max", &dlMaxRad, 0, 64, 512);
C_VAR_INT("rend-light-radius-min-bias", &dlMinRadForBias, 0, 128, 1024);
C_VAR_INT("rend-light-multitex", &useMultiTexLights, 0, 0, 1);
C_VAR_INT("rend-mobj-light-auto", &useMobjAutoLights, 0, 0, 1);
Rend_DecorRegister();
}
/**
* Moves all used dynlight nodes to the list of unused nodes, so they
* can be reused.
*/
static void DL_DeleteUsed(void)
{
int i;
// Start reusing nodes from the first one in the list.
dynCursor = dynFirst;
contCursor = contFirst;
// Clear the surface light links.
memset(segLightLinks, 0, numsegs * sizeof(seglight_t));
for(i=0; i < numsubsectors; ++i)
{
memset(subSecLightLinks[i].planes, 0,
SUBSECTOR_PTR(i)->sector->planecount * sizeof(dynlight_t*));
}
// Clear lumobj contacts.
memset(subContacts, 0, numsubsectors * sizeof(lumcontact_t *));
}
/**
* Returns a new dynlight node. If the list of unused nodes is empty,
* a new node is created.
*/
dynlight_t *DL_New(float *s, float *t)
{
dynlight_t *dyn;
// Have we run out of nodes?
if(dynCursor == NULL)
{
dyn = Z_Malloc(sizeof(dynlight_t), PU_STATIC, NULL);
// Link the new node to the list.
dyn->nextUsed = dynFirst;
dynFirst = dyn;
}
else
{
dyn = dynCursor;
dynCursor = dynCursor->nextUsed;
}
dyn->next = NULL;
dyn->flags = 0;
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 dyn;
}
/**
* Links the dynlight node to the list.
*/
static void DL_Link(dynlight_t *dyn, dynlight_t **list, int index)
{
dyn->next = list[index];
list[index] = dyn;
}
static void DL_SubSecLink(dynlight_t *dyn, int index, int plane)
{
DL_Link(dyn, &subSecLightLinks[index].planes[plane], 0);
}
/**
* Returns a pointer to the list of dynlights for the subsector plane.
*/
dynlight_t *DL_GetSubSecLightLinks(int ssec, int plane)
{
return subSecLightLinks[ssec].planes[plane];
}
static void DL_SegLink(dynlight_t *dyn, int index, int segPart)
{
DL_Link(dyn, &segLightLinks[index].wallSection[segPart], 0);
}
/**
* Returns a pointer to the list of dynlights for the segment part.
*/
dynlight_t *DL_GetSegLightLinks(int seg, int whichpart)
{
return segLightLinks[seg].wallSection[whichpart];
}
/**
* Returns a new lumobj contact. If there are nodes in the list of unused
* nodes, the new contact is taken from there.
*/
static lumcontact_t *DL_NewContact(lumobj_t * lum)
{
lumcontact_t *con;
if(contCursor == NULL)
{
con = Z_Malloc(sizeof(lumcontact_t), PU_STATIC, NULL);
// Link to the list of lumcontact nodes.
con->nextUsed = contFirst;
contFirst = con;
}
else
{
con = contCursor;
contCursor = contCursor->nextUsed;
}
con->lum = lum;
return con;
}
/**
* Link the contact to the subsector's list of contacts.
* The lumobj is contacting the subsector.
* This called if a light passes the sector spread test.
* Returns true because this function is also used as an iterator.
*/
static boolean DL_AddContact(subsector_t *subsector, void *lum)
{
lumcontact_t *con = DL_NewContact(lum);
lumcontact_t **list = subContacts + GET_SUBSECTOR_IDX(subsector);
con->next = *list;
*list = con;
return true;
}
void DL_ThingRadius(lumobj_t * lum, lightconfig_t * cf)
{
lum->radius = cf->size * 40 * dlRadFactor;
// Don't make a too small or too large light.
if(lum->radius < 32)
lum->radius = 32;
if(lum->radius > dlMaxRad)
lum->radius = dlMaxRad;
lum->flareSize = cf->flareSize * 60 * (50 + haloSize) / 100.0f;
if(lum->flareSize < 8)
lum->flareSize = 8;
}
static void DL_ThingColor(lumobj_t * lum, DGLubyte * outRGB, float light)
{
int i;
if(light < 0)
light = 0;
if(light > 1)
light = 1;
light *= dlFactor;
// If fog is enabled, make the light dimmer.
// FIXME: This should be a cvar.
if(usingFog)
light *= .5f; // Would be too much.
// Multiply with the light color.
if(lum->decorMap)
{ // Decoration maps are pre-colored.
light *= 255;
memset(outRGB, (DGLubyte) (light), sizeof(DGLubyte) * 3);
}
else
{
for(i = 0; i < 3; ++i)
{
outRGB[i] = (DGLubyte) (light * lum->rgb[i]);
}
}
}
void DL_InitLinks(void)
{
int i;
vertex_t min, max;
// First initialize the subsector links (root pointers).
dlSubLinks = Z_Calloc(sizeof(lumobj_t *) * numsubsectors, PU_LEVEL, 0);
// Then the blocklinks.
R_GetMapSize(&min, &max);
// Origin has fixed-point coordinates.
memcpy(&dlBlockOrig, &min, sizeof(min));
max.pos[VX] -= min.pos[VX];
max.pos[VY] -= min.pos[VY];
dlBlockWidth = (max.pos[VX] >> (FRACBITS + 7)) + 1;
dlBlockHeight = (max.pos[VY] >> (FRACBITS + 7)) + 1;
// Blocklinks is a table of lumobj_t pointers.
dlBlockLinks =
M_Realloc(dlBlockLinks,
sizeof(lumobj_t *) * dlBlockWidth * dlBlockHeight);
// Initialize the dynlight -> surface links.
segLightLinks =
Z_Calloc(numsegs * sizeof(seglight_t), PU_LEVELSTATIC, 0);
subSecLightLinks =
Z_Calloc(numsubsectors * sizeof(subseclight_t), PU_LEVELSTATIC, 0);
for(i = 0; i < numsubsectors; ++i)
{
int planecount = SUBSECTOR_PTR(i)->sector->planecount;
subSecLightLinks[i].planes =
Z_Calloc(planecount * sizeof(dynlight_t*), PU_LEVELSTATIC, 0);
}
// Initialize lumobj -> subsector contacts.
subContacts =
Z_Calloc(numsubsectors * sizeof(lumcontact_t *), PU_LEVELSTATIC, 0);
// A framecount was each block.
spreadBlocks =
Z_Malloc(sizeof(int) * dlBlockWidth * dlBlockHeight, PU_LEVEL, 0);
}
/**
* Returns true if the coords are in range.
*/
static boolean DL_SegTexCoords(float *t, float top, float bottom,
lumobj_t * lum)
{
float lightZ = FIX2FLT(lum->thing->pos[VZ]) + lum->center;
float radius = lum->radius / DYN_ASPECT;
t[0] = (lightZ + radius - top) / (2 * radius);
t[1] = t[0] + (top - bottom) / (2 * radius);
return t[0] < 1 && t[1] > 0;
}
/**
* The front sector must be given because of polyobjs.
*/
static void DL_ProcessWallSeg(lumobj_t * lum, seg_t *seg, sector_t *frontsec)
{
#define SMIDDLE 0x1
#define STOP 0x2
#define SBOTTOM 0x4
int present = 0;
sector_t *backsec = seg->backsector;
side_t *sdef = seg->sidedef;
float pos[2][2], s[2], t[2];
float dist, pntLight[2];
float fceil, ffloor, bceil, bfloor;
float top[2], bottom[2];
dynlight_t *dyn;
int segindex = GET_SEG_IDX(seg);
boolean backSide = false;
DGLubyte lumRGB[3];
fceil = SECT_CEIL(frontsec);
ffloor = SECT_FLOOR(frontsec);
// A zero-volume sector?
if(fceil <= ffloor)
return;
// Which side?
if(seg->linedef->sides[0] != seg->sidedef)
backSide = true;
if(backsec)
{
bceil = SECT_CEIL(backsec);
bfloor = SECT_FLOOR(backsec);
}
else
{
bceil = bfloor = 0;
}
// Let's begin with an analysis of the visible surfaces.
// Is there a mid wall segment?
if(Rend_IsWallSectionPVisible(seg->linedef, SEG_MIDDLE, backSide))
{
present |= SMIDDLE;
if(backsec)
{
// Check the middle texture's mask status.
if(sdef->middle.texture > 0)
{
if(sdef->middle.isflat)
GL_PrepareFlat2(sdef->middle.texture, true);
else
GL_GetTextureInfo(sdef->middle.texture);
}
/*if(texmask)
{
// We can't light masked textures.
// FIXME: Use vertex lighting.
present &= ~SMIDDLE;
} */
}
}
// Is there a top wall segment?
if(Rend_IsWallSectionPVisible(seg->linedef, SEG_TOP, backSide))
present |= STOP;
// Is there a lower wall segment?
if(Rend_IsWallSectionPVisible(seg->linedef, SEG_BOTTOM, backSide))
present |= SBOTTOM;
// There are no surfaces to light!
if(!present)
return;
// We will only calculate light placement for segs that are facing
// the viewpoint.
if(!(seg->info->flags & SEGINF_FACINGFRONT))
return;
pos[0][VX] = seg->fv1.pos[VX];
pos[0][VY] = seg->fv1.pos[VY];
pos[1][VX] = seg->fv2.pos[VX];
pos[1][VY] = seg->fv2.pos[VY];
pntLight[VX] = FIX2FLT(lum->thing->pos[VX]);
pntLight[VY] = FIX2FLT(lum->thing->pos[VY]);
// Calculate distance between seg and light source.
dist = ((pos[0][VY] - pntLight[VY]) * (pos[1][VX] - pos[0][VX]) -
(pos[0][VX] - pntLight[VX]) * (pos[1][VY] - pos[0][VY])) / seg->length;
// Is it close enough and on the right side?
if(dist < 0 || dist > lum->radius)
return; // Nope.
// Do a scalar projection for the offset.
s[0] = (-((pos[0][VY] - pntLight[VY]) * (pos[0][VY] - pos[1][VY]) -
(pos[0][VX] - pntLight[VX]) * (pos[1][VX] - pos[0][VX])) / seg->length +
lum->radius) / (2 * lum->radius);
s[1] = s[0] + seg->length / (2 * lum->radius);
// Would the light be visible?
if(s[0] >= 1 || s[1] <= 0)
return; // Outside the seg.
DL_ThingColor(lum, lumRGB, LUM_FACTOR(dist));
// Process the visible parts of the segment.
if(present & SMIDDLE)
{
if(backsec)
{
top[0] = top[1] = (fceil < bceil ? fceil : bceil);
bottom[0] = bottom[1] = (ffloor > bfloor ? ffloor : bfloor);
Rend_MidTexturePos(&bottom[0], &bottom[1], &top[0], &top[1],
NULL, sdef->middle.offy,
seg->linedef ? (seg->linedef->
flags & ML_DONTPEGBOTTOM) !=
0 : false);
}
else
{
top[0] = top[1] = fceil;
bottom[0] = bottom[1] = ffloor;
}
if(DL_SegTexCoords(t, top[0], bottom[0], lum) &&
DL_SegTexCoords(t, top[1], bottom[1], lum))
{
dyn = DL_New(s, t);
memcpy(dyn->color, lumRGB, sizeof(DGLubyte) * 3);
dyn->texture = lum->tex;
DL_SegLink(dyn, segindex, SEG_MIDDLE);
}
}
if(present & STOP)
{
if(DL_SegTexCoords(t, fceil, MAX_OF(ffloor, bceil), lum))
{
dyn = DL_New(s, t);
memcpy(dyn->color, lumRGB, sizeof(DGLubyte) * 3);
dyn->texture = lum->tex;
DL_SegLink(dyn, segindex, SEG_TOP);
}
}
if(present & SBOTTOM)
{
if(DL_SegTexCoords(t, MIN_OF(bfloor, fceil), ffloor, lum))
{
dyn = DL_New(s, t);
memcpy(dyn->color, lumRGB, sizeof(DGLubyte) * 3);
dyn->texture = lum->tex;
DL_SegLink(dyn, segindex, SEG_BOTTOM);
}
}
#undef SMIDDLE
#undef STOP
#undef SBOTTOM
}
/**
* Generates one dynlight node per plane glow. The light is attached to
* the appropriate seg part.
*/
static void DL_CreateGlowLights(seg_t *seg, int part, float segtop,
float segbottom, boolean glow_floor,
boolean glow_ceil)
{
int i, g, segindex = GET_SEG_IDX(seg);
float ceil, floor, top, bottom, s[2], t[2];
float glowHeight;
dynlight_t *dyn;
sector_t *sect = seg->sidedef->sector;
// Check the heights.
if(segtop <= segbottom)
return; // No height.
ceil = SECT_CEIL(sect);
floor = SECT_FLOOR(sect);
if(segtop > ceil)
segtop = ceil;
if(segbottom < floor)
segbottom = floor;
for(g = 0; g < 2; ++g)
{
// Only do what's told.
if((g == PLN_CEILING && !glow_ceil) || (g == PLN_FLOOR && !glow_floor))
continue;
// Calculate texture coords for the light.
// The horizontal direction is easy.
s[0] = 0;
s[1] = 1;
glowHeight =
(MAX_GLOWHEIGHT * sect->planes[g]->glow) * glowHeightFactor;
// Don't make too small or too large glows.
if(glowHeight <= 2)
continue;
if(glowHeight > glowHeightMax)
glowHeight = glowHeightMax;
if(g == PLN_CEILING)
{ // Ceiling glow.
top = ceil;
bottom = ceil - glowHeight;
t[1] = t[0] = (top - segtop) / glowHeight;
t[1]+= (segtop - segbottom) / glowHeight;
if(t[0] > 1 || t[1] < 0)
continue;
}
else
{ // Floor glow.
bottom = floor;
top = floor + glowHeight;
t[0] = t[1] = (segbottom - bottom) / glowHeight;
t[0]+= (segtop - segbottom) / glowHeight;
if(t[1] > 1 || t[0] < 0)
continue;
}
dyn = DL_New(s, t);
memcpy(dyn->color, sect->planes[g]->glowrgb, 3);
dyn->texture = GL_PrepareLSTexture(LST_GRADIENT);
for(i = 0; i < 3; ++i)
{
dyn->color[i] *= dlFactor;
// In fog, additive blending is used. The normal fog color
// is way too bright.
if(usingFog)
dyn->color[i] *= glowFogBright;
}
DL_SegLink(dyn, segindex, part);
}
}
/**
* If necessary, generate dynamic lights for plane glow.
*/
static void DL_ProcessWallGlow(seg_t *seg, sector_t *sect)
{
boolean do_floor = (sect->SP_floorglow > 0)? true : false;
boolean do_ceil = (sect->SP_ceilglow > 0)? true : false;
sector_t *back = seg->backsector;
side_t *sdef = seg->sidedef;
float fceil, ffloor, bceil, bfloor;
float opentop, openbottom; //, top, bottom;
boolean backSide = false;
// Check if this segment is actually facing our way.
if(!(seg->info->flags & SEGINF_FACINGFRONT))
return; // Nope...
// Which side?
if(seg->linedef->sides[0] != seg->sidedef)
backSide = true;
// Visible plane heights.
fceil = SECT_CEIL(sect);
ffloor = SECT_FLOOR(sect);
if(back)
{
bceil = SECT_CEIL(back);
bfloor = SECT_FLOOR(back);
}
// Determine which portions of the segment get lit.
if(!back)
{
// One sided.
DL_CreateGlowLights(seg, SEG_MIDDLE, fceil, ffloor, do_floor, do_ceil);
}
else
{
// Two-sided.
opentop = MIN_OF(fceil, bceil);
openbottom = MAX_OF(ffloor, bfloor);
// The glow can only be visible in the front sector's height range.
// Is there a middle?
if(Rend_IsWallSectionPVisible(seg->linedef, SEG_MIDDLE, backSide))
{
if(sdef->middle.texture > 0)
{
if(sdef->middle.isflat)
GL_PrepareFlat2(sdef->middle.texture, true);
else
GL_GetTextureInfo(sdef->middle.texture);
}
if(!texmask)
{
DL_CreateGlowLights(seg, SEG_MIDDLE, opentop, openbottom,
do_floor, do_ceil);
}
}
// Top?
if(Rend_IsWallSectionPVisible(seg->linedef, SEG_TOP, backSide))
{
DL_CreateGlowLights(seg, SEG_TOP, fceil, bceil, do_floor, do_ceil);
}
// Bottom?
if(Rend_IsWallSectionPVisible(seg->linedef, SEG_BOTTOM, backSide))
{
DL_CreateGlowLights(seg, SEG_BOTTOM, bfloor, ffloor, do_floor,
do_ceil);
}
}
}
void DL_Clear(void)
{
if(luminousList)
Z_Free(luminousList);
luminousList = 0;
maxLuminous = numLuminous = 0;
M_Free(dlBlockLinks);
dlBlockLinks = 0;
dlBlockOrig.pos[VX] = dlBlockOrig.pos[VY] = 0;
dlBlockWidth = dlBlockHeight = 0;
if(planeVars)
M_Free(planeVars);
maxPlanes = 0;
}
void DL_ClearForFrame(void)
{
#ifdef DD_PROFILE
static int i;
if(++i > 40)
{
i = 0;
PRINT_PROF(PROF_DYN_INIT_DEL);
PRINT_PROF(PROF_DYN_INIT_ADD);
PRINT_PROF(PROF_DYN_INIT_LINK);
}
#endif
// Clear all the roots.
memset(dlSubLinks, 0, sizeof(lumobj_t *) * numsubsectors);
memset(dlBlockLinks, 0, sizeof(lumobj_t *) * dlBlockWidth * dlBlockHeight);
numLuminous = 0;
}
/**
* Allocates a new lumobj and returns a pointer to it.
*/
int DL_NewLuminous(void)
{
lumobj_t *newList;
numLuminous++;
// Only allocate memory when it's needed.
// FIXME: No upper limit?
if(numLuminous > maxLuminous)
{
maxLuminous *= 2;
// The first time, allocate thirty two lumobjs.
if(!maxLuminous)
maxLuminous = 32;
newList = Z_Malloc(sizeof(lumobj_t) * maxLuminous, PU_STATIC, 0);
// Copy the old data over to the new list.
if(luminousList)
{
memcpy(newList, luminousList,
sizeof(lumobj_t) * (numLuminous - 1));
Z_Free(luminousList);
}
luminousList = newList;
}
// Clear the new lumobj.
memset(luminousList + numLuminous - 1, 0, sizeof(lumobj_t));
return numLuminous; // == index + 1
}
/**
* Returns a pointer to the lumobj with the given 1-based index.
*/
lumobj_t *DL_GetLuminous(int index)
{
if(index <= 0 || index > numLuminous)
return NULL;
return luminousList + index - 1;
}
/**
* Returns true if we HAVE to use a dynamic light for this light defintion.
*/
static boolean DL_MustUseDynamic(ded_light_t *def)
{
// Are any of the light directions disabled or use a custom lightmap?
if(def && (def->sides.tex != 0 || def->up.tex != 0 || def->down.tex != 0))
return true;
else
return false;
}
/**
* Registers the given thing as a luminous, light-emitting object.
* Note that this is called each frame for each luminous object!
*/
void DL_AddLuminous(mobj_t *thing)
{
int i, lump;
float mul;
float xOff;
float center;
int flags = 0;
int radius, flareSize;
float rgb[3];
lumobj_t *lum;
lightconfig_t cf;
ded_light_t *def = 0;
modeldef_t *mf, *nextmf;
spritedef_t *sprdef;
spriteframe_t *sprframe;
// Has BIAS lighting been disabled?
// If this thing has aquired a BIAS source we need to delete it.
if(thing->usingBias)
{
if(!useBias)
{
SB_Delete(thing->light - 1);
thing->light = 0;
thing->usingBias = false;
}
}
else
thing->light = 0;
if(((thing->state && (thing->state->flags & STF_FULLBRIGHT)) &&
!(thing->ddflags & DDMF_DONTDRAW)) ||
(thing->ddflags & DDMF_ALWAYSLIT))
{
// Are the automatically calculated light values for fullbright
// sprite frames in use?
if(thing->state &&
(!useMobjAutoLights || (thing->state->flags & STF_NOAUTOLIGHT)) &&
!thing->state->light)
return;
// Determine the sprite frame lump of the source.
sprdef = &sprites[thing->sprite];
sprframe = &sprdef->spriteframes[thing->frame];
if(sprframe->rotate)
{
lump =
sprframe->
lump[(R_PointToAngle(thing->pos[VX], thing->pos[VY]) - thing->angle +
(unsigned) (ANG45 / 2) * 9) >> 29];
}
else
{
lump = sprframe->lump[0];
}
// This'll ensure we have up-to-date information about the texture.
GL_PrepareSprite(lump, 0);
// Let's see what our light should look like.
cf.size = cf.flareSize = spritelumps[lump]->lumsize;
cf.xOffset = spritelumps[lump]->flarex;
cf.yOffset = spritelumps[lump]->flarey;
// X offset to the flare position.
xOff = cf.xOffset - spritelumps[lump]->width / 2.0f;
// Does the thing have an active light definition?
if(thing->state && thing->state->light)
{
def = (ded_light_t *) thing->state->light;
if(def->size)
cf.size = def->size;
if(def->offset[VX])
{
// Set the x offset here.
xOff = cf.xOffset = def->offset[VX];
}
if(def->offset[VY])
cf.yOffset = def->offset[VY];
if(def->halo_radius)
cf.flareSize = def->halo_radius;
flags |= def->flags;
}
center =
spritelumps[lump]->topoffset -
FIX2FLT(thing->floorclip + R_GetBobOffset(thing)) -
cf.yOffset;
// Will the sprite be allowed to go inside the floor?
mul =
FIX2FLT(thing->pos[VZ]) + spritelumps[lump]->topoffset -
spritelumps[lump]->height -
FIX2FLT(thing->subsector->sector->planes[PLN_FLOOR]->height);
if(!(thing->ddflags & DDMF_NOFITBOTTOM) && mul < 0)
{
// Must adjust.
center -= mul;
}
// Sets the dynlight and flare radii.
//DL_ThingRadius(lum, &cf);
radius = cf.size * 40 * dlRadFactor;
// Don't make a too small light.
if(radius < 32)
radius = 32;
flareSize = cf.flareSize * 60 * (50 + haloSize) / 100.0f;
if(flareSize < 8)
flareSize = 8;
// Does the mobj use a light scale?
if(thing->ddflags & DDMF_LIGHTSCALE)
{
// Also reduce the size of the light according to
// the scale flags. *Won't affect the flare.*
mul =
1.0f -
((thing->ddflags & DDMF_LIGHTSCALE) >> DDMF_LIGHTSCALESHIFT) /
4.0f;
radius *= mul;
}
if(def && (def->color[0] || def->color[1] || def->color[2]))
{
// If any of the color components are != 0, use the
// definition's color.
for(i = 0; i < 3; ++i)
rgb[i] = def->color[i];
}
else
{
// Use the sprite's (amplified) color.
GL_GetSpriteColorf(lump, rgb);
}
if(useBias && thing->usingBias)
{ // We have previously acquired a BIAS source for this thing.