/
r_lgrid.c
994 lines (837 loc) · 27.9 KB
/
r_lgrid.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
/**\file
*\section License
* License: GPL
* Online License Link: http://www.gnu.org/licenses/gpl.html
*
*\author Copyright © 2006-2007 Jaakko Keränen <jaakko.keranen@iki.fi>
*\author Copyright © 2006-2007 Daniel Swanson <danij@dengine.net>
*\author Copyright © 2006 Jamie Jones <yagisan@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
*/
/**
* r_lgrid.c: Light Grid (Large-Scale FakeRadio)
*
* Very simple global illumination method utilizing a 2D grid of light
* levels.
*/
// HEADER FILES ------------------------------------------------------------
#include "de_base.h"
#include "de_refresh.h"
#include "de_render.h"
#include "de_graphics.h"
#include "de_misc.h"
#include <math.h>
#include <assert.h>
// MACROS ------------------------------------------------------------------
#define GRID_BLOCK(x, y) (&grid[(y)*lgBlockWidth + (x)])
#define GBF_CHANGED 0x1 // Grid block sector light has changed.
#define GBF_CONTRIBUTOR 0x2 // Contributes light to a changed block.
// TYPES -------------------------------------------------------------------
typedef struct gridblock_s {
struct sector_s *sector;
byte flags;
// Positive bias means that the light is shining in the floor of
// the sector.
char bias;
// Color of the light:
float rgb[3];
float oldrgb[3]; // Used instead of rgb if the lighting in this
// block has changed and we haven't yet done a
// a full grid update.
} gridblock_t;
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
// PUBLIC DATA DEFINITIONS -------------------------------------------------
// PRIVATE DATA DEFINITIONS ------------------------------------------------
int lgEnabled = false;
static boolean lgInited;
static boolean needsUpdate = true;
static int lgShowDebug = false;
static float lgDebugSize = 1.5f;
static int lgBlockSize = 31;
static float lgOrigin[3];
static int lgBlockWidth;
static int lgBlockHeight;
static gridblock_t *grid;
static int lgMXSample = 1; // Default is mode 1 (5 samples per block)
// CODE --------------------------------------------------------------------
/**
* Registers console variables.
*/
void LG_Register(void)
{
C_VAR_INT("rend-bias-grid", &lgEnabled, 0, 0, 1);
C_VAR_INT("rend-bias-grid-debug", &lgShowDebug, 0, 0, 1);
C_VAR_FLOAT("rend-bias-grid-debug-size", &lgDebugSize, 0, .1f, 100);
C_VAR_INT("rend-bias-grid-blocksize", &lgBlockSize, 0, 8, 1024);
C_VAR_INT("rend-bias-grid-multisample", &lgMXSample, 0, 0, 7);
}
/**
* Determines if the index (x, y) is in the bitfield.
*/
static boolean HasIndexBit(int x, int y, uint *bitfield)
{
uint index = x + y * lgBlockWidth;
// Assume 32-bit uint.
return (bitfield[index >> 5] & (1 << (index & 0x1f))) != 0;
}
/**
* Sets the index (x, y) in the bitfield.
* Count is incremented when a zero bit is changed to one.
*/
static void AddIndexBit(int x, int y, uint *bitfield, int *count)
{
uint index = x + y * lgBlockWidth;
// Assume 32-bit uint.
if(!HasIndexBit(index, 0, bitfield))
{
(*count)++;
}
bitfield[index >> 5] |= (1 << (index & 0x1f));
}
/**
* Initialize the light grid for the current level.
*/
void LG_Init(void)
{
uint startTime = Sys_GetRealTime();
#define MSFACTORS 7
typedef struct lgsamplepoint_s {
float pos[3];
} lgsamplepoint_t;
// Diagonal in maze arrangement of natural numbers.
// Up to 65 samples per-block(!)
static int multisample[] = {1, 5, 9, 17, 25, 37, 49, 65};
float max[3];
float width, height;
int i = 0;
int a, b, x, y;
int count;
int changedCount;
size_t bitfieldSize;
uint *indexBitfield = 0;
uint *contributorBitfield = 0;
gridblock_t *block;
int *sampleResults = 0;
int n, size, numSamples, center, best;
uint s;
float off[2];
lgsamplepoint_t *samplePoints = 0, sample;
sector_t **ssamples;
sector_t **blkSampleSectors;
gamemap_t *map = P_GetCurrentMap();
if(!lgEnabled)
{
lgInited = false;
return;
}
lgInited = true;
// Allocate the grid.
P_GetMapBounds(map, &lgOrigin[0], &max[0]);
width = max[VX] - lgOrigin[VX];
height = max[VY] - lgOrigin[VY];
lgBlockWidth = ROUND(width / lgBlockSize) + 1;
lgBlockHeight = ROUND(height / lgBlockSize) + 1;
// Clamp the multisample factor.
if(lgMXSample > MSFACTORS)
lgMXSample = MSFACTORS;
else if(lgMXSample < 0)
lgMXSample = 0;
numSamples = multisample[lgMXSample];
// Allocate memory for sample points array.
samplePoints = M_Malloc(sizeof(lgsamplepoint_t) * numSamples);
/**
* It would be possible to only allocate memory for the unique
* sample results. And then select the appropriate sample in the loop
* for initializing the grid instead of copying the previous results in
* the loop for acquiring the sample points.
*
* Calculate with the equation (number of unique sample points):
*
* ((1 + lgBlockHeight * lgMXSample) * (1 + lgBlockWidth * lgMXSample)) +
* (size % 2 == 0? numBlocks : 0)
* OR
*
* We don't actually need to store the ENTIRE sample points array. It
* would be sufficent to only store the results from the start of the
* previous row to current col index. This would save a bit of memory.
*
* However until lightgrid init is finalized it would be rather silly
* to optimize this much further.
*/
// Allocate memory for all the sample results.
ssamples = M_Malloc(sizeof(sector_t*) *
((lgBlockWidth * lgBlockHeight) * numSamples));
// Determine the size^2 of the samplePoint array plus its center.
size = center = 0;
if(numSamples > 1)
{
float f = sqrt(numSamples);
if(ceil(f) != floor(f))
{
size = sqrt(numSamples -1);
center = 0;
}
else
{
size = (int) f;
center = size+1;
}
}
// Construct the sample point offset array.
// This way we can use addition only during calculation of:
// (lgBlockHeight*lgBlockWidth)*numSamples
if(center == 0)
{ // Zero is the center so do that first.
samplePoints[0].pos[VX] = lgBlockSize / 2;
samplePoints[0].pos[VY] = lgBlockSize / 2;
}
if(numSamples > 1)
{
float bSize = (float) lgBlockSize / (size-1);
// Is there an offset?
if(center == 0)
n = 1;
else
n = 0;
for(y = 0; y < size; ++y)
for(x = 0; x < size; ++x, ++n)
{
samplePoints[n].pos[VX] = ROUND(x * bSize);
samplePoints[n].pos[VY] = ROUND(y * bSize);
}
}
/*
#if _DEBUG
for(n = 0; n < numSamples; ++n)
Con_Message(" %i of %i %i(%f %f)\n",
n, numSamples, (n == center)? 1 : 0,
samplePoints[n].pos[VX], samplePoints[n].pos[VY]);
#endif
*/
// Acquire the sectors at ALL the sample points.
for(y = 0; y < lgBlockHeight; ++y)
{
off[VY] = y * lgBlockSize;
for(x = 0; x < lgBlockWidth; ++x)
{
int blk = (x + y * lgBlockWidth);
int idx;
off[VX] = x * lgBlockSize;
n = 0;
if(center == 0)
{ // Center point is not considered with the term 'size'.
// Sample this point and place at index 0 (at the start
// of the samples for this block).
idx = blk * (numSamples);
sample.pos[VX] = lgOrigin[VX] + off[VX] + samplePoints[0].pos[VX];
sample.pos[VY] = lgOrigin[VY] + off[VY] + samplePoints[0].pos[VY];
ssamples[idx] =
R_PointInSubsector(sample.pos[VX], sample.pos[VY])->sector;
if(!R_IsPointInSector2(sample.pos[VX], sample.pos[VY], ssamples[idx]))
ssamples[idx] = NULL;
n++; // Offset the index in the samplePoints array bellow.
}
count = blk * size;
for(b = 0; b < size; ++b)
{
i = (b + count) * size;
for(a = 0; a < size; ++a, ++n)
{
idx = a + i;
if(center == 0)
idx += blk + 1;
if(numSamples > 1 && ((x > 0 && a == 0) || (y > 0 && b == 0)))
{ // We have already sampled this point.
// Get the previous result.
int prevX, prevY, prevA, prevB;
int previdx;
prevX = x; prevY = y; prevA = a; prevB = b;
if(x > 0 && a == 0)
{
prevA = size -1;
prevX--;
}
if(y > 0 && b == 0)
{
prevB = size -1;
prevY--;
}
previdx = prevA + (prevB + (prevX + prevY * lgBlockWidth) * size) * size;
if(center == 0)
previdx += (prevX + prevY * lgBlockWidth) + 1;
ssamples[idx] = ssamples[previdx];
}
else
{ // We haven't sampled this point yet.
sample.pos[VX] = lgOrigin[VX] + off[VX] + samplePoints[n].pos[VX];
sample.pos[VY] = lgOrigin[VY] + off[VY] + samplePoints[n].pos[VY];
ssamples[idx] =
R_PointInSubsector(sample.pos[VX], sample.pos[VY])->sector;
if(!R_IsPointInSector2(sample.pos[VX], sample.pos[VY], ssamples[idx]))
ssamples[idx] = NULL;
}
}
}
}
}
// We're done with the samplePoints block.
M_Free(samplePoints);
// Bitfields for marking affected blocks. Make sure each bit is in a word.
bitfieldSize = 4 * (31 + lgBlockWidth * lgBlockHeight) / 32;
indexBitfield = M_Calloc(bitfieldSize);
contributorBitfield = M_Calloc(bitfieldSize);
// \todo It would be possible to only allocate memory for the grid
// blocks that are going to be in use.
// Allocate memory for the entire grid.
grid = Z_Calloc(sizeof(gridblock_t) * lgBlockWidth * lgBlockHeight,
PU_LEVEL, NULL);
Con_Message("LG_Init: %i x %i grid (%lu bytes).\n",
lgBlockWidth, lgBlockHeight,
(unsigned long) (sizeof(gridblock_t) * lgBlockWidth * lgBlockHeight));
// Allocate memory used for the collection of the sample results.
blkSampleSectors = M_Malloc(sizeof(sector_t*) * numSamples);
if(numSamples > 1)
sampleResults = M_Calloc(sizeof(int) * numSamples);
// Initialize the grid.
for(block = grid, y = 0; y < lgBlockHeight; ++y)
{
off[VY] = y * lgBlockSize;
for(x = 0; x < lgBlockWidth; ++x, ++block)
{
off[VX] = x * lgBlockSize;
/**
* Pick the sector at each of the sample points.
* \todo We don't actually need the blkSampleSectors array
* anymore. Now that ssamples stores the results consecutively
* a simple index into ssamples would suffice.
* However if the optimization to save memory is implemented as
* described in the comments above we WOULD still require it.
* Therefore, for now I'm making use of it to clarify the code.
*/
n = (x + y * lgBlockWidth) * numSamples;
for(i = 0; i < numSamples; ++i)
blkSampleSectors[i] = ssamples[i + n];
block->sector = NULL;
if(numSamples == 1)
{
block->sector = blkSampleSectors[center];
}
else
{ // Pick the sector which had the most hits.
best = -1;
memset(sampleResults, 0, sizeof(int) * numSamples);
for(i = 0; i < numSamples; ++i)
if(blkSampleSectors[i])
for(a = 0; a < numSamples; ++a)
if(blkSampleSectors[a] == blkSampleSectors[i] &&
blkSampleSectors[a])
{
sampleResults[a]++;
if(sampleResults[a] > best)
best = i;
}
if(best != -1)
{ // Favour the center sample if its a draw.
if(sampleResults[best] == sampleResults[center] &&
blkSampleSectors[center])
block->sector = blkSampleSectors[center];
else
block->sector = blkSampleSectors[best];
}
}
}
}
// We're done with sector samples completely.
M_Free(ssamples);
// We're done with the sample results arrays.
M_Free(blkSampleSectors);
if(numSamples > 1)
M_Free(sampleResults);
// Find the blocks of all sectors.
for(s = 0; s < numsectors; ++s)
{
sector_t *sector = SECTOR_PTR(s);
// Clear the bitfields.
memset(indexBitfield, 0, bitfieldSize);
memset(contributorBitfield, 0, bitfieldSize);
count = changedCount = 0;
for(block = grid, y = 0; y < lgBlockHeight; ++y)
{
for(x = 0; x < lgBlockWidth; ++x, ++block)
{
if(block->sector == sector)
{
// \todo Determine min/max a/b before going into the loop.
for(b = -2; b <= 2; ++b)
{
if(y + b < 0 || y + b >= lgBlockHeight)
continue;
for(a = -2; a <= 2; ++a)
{
if(x + a < 0 || x + a >= lgBlockWidth)
continue;
AddIndexBit(x + a, y + b, indexBitfield,
&changedCount);
}
}
}
}
}
// Determine contributor blocks. Contributors are the blocks that are
// close enough to contribute light to affected blocks.
for(y = 0; y < lgBlockHeight; ++y)
{
for(x = 0; x < lgBlockWidth; ++x)
{
if(!HasIndexBit(x, y, indexBitfield))
continue;
// Add the contributor blocks.
for(b = -2; b <= 2; ++b)
{
if(y + b < 0 || y + b >= lgBlockHeight)
continue;
for(a = -2; a <= 2; ++a)
{
if(x + a < 0 || x + a >= lgBlockWidth)
continue;
if(!HasIndexBit(x + a, y + b, indexBitfield))
{
AddIndexBit(x + a, y + b, contributorBitfield,
&count);
}
}
}
}
}
VERBOSE2(Con_Message(" Sector %i: %i / %i\n", s, changedCount, count));
sector->changedblockcount = changedCount;
sector->blockcount = changedCount + count;
if(sector->blockcount > 0)
{
sector->blocks = Z_Malloc(sizeof(unsigned short) * sector->blockcount,
PU_LEVELSTATIC, 0);
for(x = 0, a = 0, b = changedCount; x < lgBlockWidth * lgBlockHeight;
++x)
{
if(HasIndexBit(x, 0, indexBitfield))
sector->blocks[a++] = x;
else if(HasIndexBit(x, 0, contributorBitfield))
sector->blocks[b++] = x;
}
assert(a == changedCount);
//assert(b == info->blockcount);
}
else
{
sector->blocks = NULL;
}
}
M_Free(indexBitfield);
M_Free(contributorBitfield);
// How much time did we spend?
VERBOSE(Con_Message
("LG_Init: Done in %.2f seconds.\n",
(Sys_GetRealTime() - startTime) / 1000.0f));
}
/**
* Apply the sector's lighting to the block.
*/
static void LG_ApplySector(gridblock_t *block, const float *color, float level,
float factor, int bias)
{
int i;
// Apply a bias to the light level.
level -= (0.95f - level);
if(level < 0)
level = 0;
level *= factor;
if(level <= 0)
return;
for(i = 0; i < 3; ++i)
{
float c = color[i] * level;
c = MINMAX_OF(0, c, 1);
if(block->rgb[i] + c > 1)
{
block->rgb[i] = 1;
}
else
{
block->rgb[i] += c;
}
}
// Influenced by the source bias.
i = block->bias * (1 - factor) + bias * factor;
i = MINMAX_OF(-0x80, i, 0x7f);
block->bias = i;
}
/**
* Called when a sector has changed its light level.
*/
void LG_SectorChanged(sector_t *sector)
{
uint i, j;
unsigned short n;
if(!lgInited)
return;
// Mark changed blocks and contributors.
for(i = 0; i < sector->changedblockcount; ++i)
{
n = sector->blocks[i];
// The color will be recalculated.
if(!(grid[n].flags & GBF_CHANGED))
memcpy(grid[n].oldrgb, grid[n].rgb, sizeof(float) * 3);
for(j = 0; j < 3; ++j)
grid[n].rgb[j] = 0;
grid[n].flags |= GBF_CHANGED | GBF_CONTRIBUTOR;
}
for(; i < sector->blockcount; ++i)
{
grid[sector->blocks[i]].flags |= GBF_CONTRIBUTOR;
}
needsUpdate = true;
}
/**
* Called when a setting is changed which affects the lightgrid.
*/
void LG_MarkAllForUpdate(cvar_t *unused)
{
uint i;
if(!lgInited)
return;
// Mark all blocks and contributors.
for(i = 0; i < numsectors; ++i)
{
LG_SectorChanged(§ors[i]);
}
}
#if 0
/*
* Determines whether it is necessary to recalculate the lighting of a
* grid block. Updates are needed when there has been a light level
* or color change in a sector that affects the block.
*/
static boolean LG_BlockNeedsUpdate(int x, int y)
{
// First check the block itself.
gridblock_t *block = GRID_BLOCK(x, y);
sector_t *blockSector;
int a, b;
int limitA[2];
int limitB;
blockSector = block->sector;
if(!blockSector)
{
// The sector needs to be determined.
return true;
}
if(SECT_INFO(blockSector)->flags & SIF_LIGHT_CHANGED)
{
return true;
}
// Check neighbor blocks as well.
// Determine neighbor boundaries. Y coordinate.
if(y >= 2)
{
b = y - 2;
}
else
{
b = 0;
}
if(y <= lgBlockHeight - 3)
{
limitB = y + 2;
}
else
{
limitB = lgBlockHeight - 1;
}
// X coordinate.
if(x >= 2)
{
limitA[0] = x - 2;
}
else
{
limitA[0] = 0;
}
if(x <= lgBlockWidth - 3)
{
limitA[1] = x + 2;
}
else
{
limitA[1] = lgBlockWidth - 1;
}
// Iterate through neighbors.
for(; b <= limitB; ++b)
{
a = limitA[0];
block = GRID_BLOCK(a, b);
for(; a <= limitA[1]; ++a, ++block)
{
if(!a && !b) continue;
if(block->sector == blockSector)
continue;
if(SECT_INFO(block->sector)->flags & SIF_LIGHT_CHANGED)
{
return true;
}
}
}
return false;
}
#endif
/**
* Update the grid by finding the strongest light source in each grid
* block.
*/
void LG_Update(void)
{
gridblock_t *block, *lastBlock, *other;
int x, y, a, b;
sector_t *sector;
const float *color;
int bias;
int height;
static float factors[5 * 5] =
{
.1f, .2f, .25f, .2f, .1f,
.2f, .4f, .5f, .4f, .2f,
.25f, .5f, 1.f, .5f, .25f,
.2f, .4f, .5f, .4f, .2f,
.1f, .2f, .25f, .2f, .1f
};
if(!lgInited || !needsUpdate)
return;
#if 0
for(block = grid, y = 0; y < lgBlockHeight; ++y)
{
for(x = 0; x < lgBlockWidth; ++x, block++)
{
if(LG_BlockNeedsUpdate(x, y))
{
block->flags |= GBF_CHANGED;
// Clear to zero (will be recalculated).
memset(block->rgb, 0, sizeof(float) * 3);
// Mark contributors.
for(b = -2; b <= 2; ++b)
{
if(y + b < 0 || y + b >= lgBlockHeight)
continue;
for(a = -2; a <= 2; ++a)
{
if(x + a < 0 || x + a >= lgBlockWidth)
continue;
GRID_BLOCK(x + a, y + b)->flags |= GBF_CONTRIBUTOR;
}
}
}
else
{
block->flags &= ~GBF_CHANGED;
}
}
}
#endif
for(block = grid, y = 0; y < lgBlockHeight; ++y)
{
for(x = 0; x < lgBlockWidth; ++x, ++block)
{
boolean isSkyFloor, isSkyCeil;
// Unused blocks can't contribute.
if(!(block->flags & GBF_CONTRIBUTOR) || !block->sector)
continue;
// Determine the color of the ambient light in this sector.
sector = block->sector;
color = R_GetSectorLightColor(sector);
height = (int) (sector->SP_ceilheight - sector->SP_floorheight);
isSkyFloor = R_IsSkySurface(§or->SP_ceilsurface);
isSkyCeil = R_IsSkySurface(§or->SP_floorsurface);
if(isSkyFloor && !isSkyCeil)
{
bias = -height / 6;
}
else if(!isSkyFloor && isSkyCeil)
{
bias = height / 6;
}
else if(height > 100)
{
bias = (height - 100) / 2;
}
else
{
bias = 0;
}
// \todo Calculate min/max for a and b.
for(a = -2; a <= 2; ++a)
{
for(b = -2; b <= 2; ++b)
{
if(x + a < 0 || y + b < 0 || x + a > lgBlockWidth - 1 ||
y + b > lgBlockHeight - 1) continue;
other = GRID_BLOCK(x + a, y + b);
if(other->flags & GBF_CHANGED)
{
LG_ApplySector(other, color, sector->lightlevel,
factors[(b + 2)*5 + a + 2]/8, bias);
}
}
}
}
}
// Clear all changed and contribution flags.
lastBlock = &grid[lgBlockWidth * lgBlockHeight];
for(block = grid; block != lastBlock; ++block)
{
block->flags = 0;
}
needsUpdate = false;
}
/**
* Calculate the light level for a 3D point in the world.
*
* @param point 3D point.
* @param color Evaluated color of the point (return value).
*/
void LG_Evaluate(const float *point, float *color)
{
int x, y, i;
float dz = 0, dimming;
gridblock_t *block;
if(!lgInited)
{
memset(color, 0, sizeof(float) * 3);
return;
}
x = ROUND((point[VX] - lgOrigin[VX]) / lgBlockSize);
y = ROUND((point[VY] - lgOrigin[VY]) / lgBlockSize);
x = MINMAX_OF(1, x, lgBlockWidth - 2);
y = MINMAX_OF(1, y, lgBlockHeight - 2);
block = &grid[y * lgBlockWidth + x];
if(block->sector)
{
if(block->bias < 0)
{
// Calculate Z difference to the ceiling.
dz = block->sector->SP_ceilheight - point[VZ];
}
else if(block->bias > 0)
{
// Calculate Z difference to the floor.
dz = point[VZ] - block->sector->SP_floorheight;
}
dz -= 50;
if(dz < 0)
dz = 0;
if(block->flags & GBF_CHANGED)
{ // We are waiting for an updated value, for now use the old.
color[0] = block->oldrgb[0];
color[1] = block->oldrgb[1];
color[2] = block->oldrgb[2];
}
else
{
color[0] = block->rgb[0];
color[1] = block->rgb[1];
color[2] = block->rgb[2];
}
}
#if 0
else
{
// DEBUG:
// Bright purple if the block doesn't have a sector.
color[0] = 1;
color[1] = 0;
color[2] = 1;
}
#endif
// Biased ambient light causes a dimming in the Z direction.
if(dz && block->bias)
{
if(block->bias < 0)
dimming = 1 - (dz * (float) -block->bias) / 35000.0f;
else
dimming = 1 - (dz * (float) block->bias) / 35000.0f;
if(dimming < .5f)
dimming = .5f;
for(i = 0; i < 3; ++i)
{
// Add the light range compression factor
color[i] += Rend_GetLightAdaptVal(color[i]);
// Apply the dimming
color[i] *= dimming;
}
}
else
{
// Just add the light range compression factor
for(i = 0; i < 3; ++i)
color[i] += Rend_GetLightAdaptVal(color[i]);
}
}
/**
* Draw the grid in 2D HUD mode.
*/
void LG_Debug(void)
{
gridblock_t *block;
int x, y;
int vx, vy;
static int blink = 0;
if(!lgInited || !lgShowDebug)
return;
blink++;
vx = ROUND((viewPlayer->mo->pos[VX] - lgOrigin[VX]) / lgBlockSize);
vy = ROUND((viewPlayer->mo->pos[VY] - lgOrigin[VY]) / lgBlockSize);
vx = MINMAX_OF(1, vx, lgBlockWidth - 2);
vy = MINMAX_OF(1, vy, lgBlockHeight - 2);
// Go into screen projection mode.
gl.MatrixMode(DGL_PROJECTION);
gl.PushMatrix();
gl.LoadIdentity();
gl.Ortho(0, 0, theWindow->width, theWindow->height, -1, 1);
gl.Disable(DGL_TEXTURING);
for(block = grid, y = 0; y < lgBlockHeight; ++y)
{
gl.Begin(DGL_QUADS);
for(x = 0; x < lgBlockWidth; ++x, ++block)
{
if(!block->sector)
continue;
if(x == vx && y == vy && (blink & 16))
gl.Color3f(1, 0, 0);
else
gl.Color3fv(block->rgb);
gl.Vertex2f(x * lgDebugSize, y * lgDebugSize);
gl.Vertex2f(x * lgDebugSize + lgDebugSize, y * lgDebugSize);
gl.Vertex2f(x * lgDebugSize + lgDebugSize,
y * lgDebugSize + lgDebugSize);
gl.Vertex2f(x * lgDebugSize, y * lgDebugSize + lgDebugSize);
}
gl.End();
}
gl.Enable(DGL_TEXTURING);
gl.MatrixMode(DGL_PROJECTION);
gl.PopMatrix();
}