-
Notifications
You must be signed in to change notification settings - Fork 5
/
r_draw_light.pas
607 lines (549 loc) · 17.5 KB
/
r_draw_light.pas
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
//------------------------------------------------------------------------------
//
// FPCDoom - Port of Doom to Free Pascal Compiler
// Copyright (C) 1993-1996 by id Software, Inc.
// Copyright (C) 2004-2007 by Jim Valavanis
// Copyright (C) 2017-2022 by Jim Valavanis
//
// 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., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//------------------------------------------------------------------------------
// E-Mail: jimmyvalavanis@yahoo.gr
// Site : https://sourceforge.net/projects/fpcdoom/
//------------------------------------------------------------------------------
{$I FPCDoom.inc}
unit r_draw_light;
interface
uses
d_fpc,
m_fixed;
type
lightparams_t = record
lightsourcex: fixed_t;
lightsourcey: fixed_t;
r, g, b: byte;
dl_iscale: fixed_t;
dl_scale: fixed_t;
dl_texturemid: fixed_t;
dl_x: integer;
dl_yl: integer;
dl_yh: integer;
db_min: LongWord;
db_max: LongWord;
db_dmin: LongWord;
db_dmax: LongWord;
dl_fracstep: fixed_t;
dl_source32: PLongWordArray;
end;
Plightparams_t = ^lightparams_t;
//==============================================================================
//
// R_ValidLightColumn
//
//==============================================================================
function R_ValidLightColumn(const x: integer): boolean;
//==============================================================================
// R_DrawColumnLightmap
//
// Draw column to the lightmap
//
//==============================================================================
procedure R_DrawColumnLightmap(const parms: Plightparams_t);
var
lcolumn: lightparams_t;
//==============================================================================
//
// R_InitLightmap
//
//==============================================================================
procedure R_InitLightmap;
//==============================================================================
//
// R_ShutDownLightmap
//
//==============================================================================
procedure R_ShutDownLightmap;
//==============================================================================
//
// R_StartLightmap
//
//==============================================================================
procedure R_StartLightmap;
//==============================================================================
//
// R_StopLightmap
//
//==============================================================================
procedure R_StopLightmap;
//==============================================================================
//
// R_CalcLightmap
//
//==============================================================================
procedure R_CalcLightmap;
//==============================================================================
// R_FlashSpanLightmap8
//
// Flash one lightmap span (8 bit)
//
//==============================================================================
procedure R_FlashSpanLightmap8(const pls_y: PInteger);
//==============================================================================
// R_FlashSpanLightmap32
//
// Flash one lightmap span (32 bit)
//
//==============================================================================
procedure R_FlashSpanLightmap32(const pls_y: PInteger);
//==============================================================================
// R_FlashLightmap
//
// Flash the entire lightmap
//
//==============================================================================
procedure R_FlashLightmap;
implementation
uses
doomdef,
r_defs,
r_draw,
r_zbuffer,
r_lightmap,
r_main,
r_trans8,
r_hires,
r_render;
type
lightmapitem_t = record
r, b, g: LongWord;
numitems: integer;
end;
Plightmapitem_t = ^lightmapitem_t;
lightmapbuffer_t = array[0..MAXWIDTH * MAXHEIGHT] of lightmapitem_t;
Plightmapbuffer_t = ^lightmapbuffer_t;
const
LM_STORESHIFT = 5;
LM_RESTORESHIFT = FRACBITS - LM_STORESHIFT;
var
LM_YACCURACY: integer = 5;
LM_YMOD: integer = 2;
var
lightmapbuffer: Plightmapbuffer_t;
ylookuplm: array[0..MAXHEIGHT] of Plightmapbuffer_t;
xcolumnsteplm: array[0..MAXWIDTH] of integer;
LMWIDTH, LMHEIGHT: integer;
lightmapactive: boolean = false;
lm_spartspan: integer = MAXWIDTH + 1;
lm_stopspan: integer = -1;
//==============================================================================
//
// LM_Screen2LMx
//
//==============================================================================
function LM_Screen2LMx(const screenx: integer): integer; {$IFDEF FPC}inline;{$ENDIF}
begin
result := screenx;
if result < 0 then
result := 0
else if result >= LMWIDTH then
result := LMWIDTH - 1;
end;
//==============================================================================
//
// LM_Screen2LMy
//
//==============================================================================
function LM_Screen2LMy(const screeny: integer): integer; {$IFDEF FPC}inline;{$ENDIF}
begin
result := (screeny + LM_YMOD) div LM_YACCURACY;
if result < 0 then
result := 0
else if result >= LMHEIGHT then
result := LMHEIGHT - 1;
end;
//==============================================================================
//
// R_LightmapBufferAt
//
//==============================================================================
function R_LightmapBufferAt(const x, y: integer): Plightmapitem_t; {$IFDEF FPC}inline;{$ENDIF}
begin
result := Plightmapitem_t(@((ylookuplm[LM_Screen2LMy(y)]^)[columnofs[LM_Screen2LMx(x)]]));
end;
//==============================================================================
//
// R_ValidLightColumn
//
//==============================================================================
function R_ValidLightColumn(const x: integer): boolean;
begin
result := xcolumnsteplm[x] > 0;
end;
//==============================================================================
//
// R_DrawColumnLightmap
//
//==============================================================================
procedure R_DrawColumnLightmap(const parms: Plightparams_t);
var
count, x, y: integer;
frac: fixed_t;
fracstep: fixed_t;
db: Pzbufferitem_t;
depth: LongWord;
dbmin, dbmax: LongWord;
dbdmin, dbdmax: LongWord;
factor: fixed_t;
dfactor: fixed_t;
scale: fixed_t;
li: Plightmapitem_t;
dls: fixed_t;
seg: Pseg_t;
rendertype: LongWord;
skip: boolean;
sameseg: boolean;
begin
x := parms.dl_x;
if xcolumnsteplm[x] <= 0 then
exit;
count := parms.dl_yh - parms.dl_yl;
if count < 0 then
exit;
frac := parms.dl_texturemid + (parms.dl_yl - centery) * parms.dl_iscale;
fracstep := parms.dl_fracstep;
dbmin := parms.db_min;
dbmax := parms.db_max;
dbdmin := parms.db_dmin;
dbdmax := parms.db_dmax;
scale := parms.dl_scale;
seg := nil;
rendertype := 0;
dfactor := 0;
skip := false;
sameseg := false;
if parms.dl_yl < lm_spartspan then
lm_spartspan := parms.dl_yl;
if parms.dl_yh > lm_stopspan then
lm_stopspan := parms.dl_yh;
for y := parms.dl_yl to parms.dl_yh do
begin
if y mod LM_YACCURACY = LM_YMOD then
begin
dls := parms.dl_source32[(LongWord(frac) shr FRACBITS) and 127];
if dls <> 0 then
begin
db := R_ZBufferAt(x, y);
if ((seg <> db.seg) or (rendertype <> db.rendertype)) and (db.depth >= dbmin) and (db.depth <= dbmax) then
begin
sameseg := (seg = db.seg) and (seg <> nil);
seg := db.seg;
rendertype := db.rendertype;
if rendertype = RIT_SPRITE then
skip := true // we do not cast lights to sprites (why ?)
else if seg <> nil then
begin
if rendertype = RIT_MASKEDWALL then
skip := R_PointOnSegSide(parms.lightsourcex, parms.lightsourcey, seg) <> R_PointOnSegSide(viewx, viewy, seg)
else
skip := R_PointOnSegSide(parms.lightsourcex, parms.lightsourcey, seg);
end
else
skip := false; // we always draw light on spans, wrong! - eg Light source below floor should not cast light
end;
if not skip then
begin
depth := db.depth;
if (depth >= dbmin) and (depth <= dbmax) then
begin
if not sameseg then
begin
dfactor := depth - scale;
if dfactor < 0 then
dfactor := FRACUNIT - FixedDiv(-dfactor, dbdmin)
else
dfactor := FRACUNIT - FixedDiv(dfactor, dbdmax);
end;
if dfactor > 0 then
begin
if dfactor > FRACUNIT then
dfactor := FRACUNIT;
li := R_LightmapBufferAt(parms.dl_x, y);
factor := FixedMul(dls, dfactor);
li.r := li.r + (parms.r * factor) shr LM_STORESHIFT;
li.g := li.g + (parms.g * factor) shr LM_STORESHIFT;
li.b := li.b + (parms.b * factor) shr LM_STORESHIFT;
inc(li.numitems);
end;
end;
end;
end;
end;
inc(frac, fracstep);
end;
end;
//==============================================================================
//
// R_CheckLightmapParams
//
//==============================================================================
procedure R_CheckLightmapParams;
begin
if (lightmapaccuracymode < 0) or (lightmapaccuracymode >= NUMLIGHTMAPACCURACYMODES) then
lightmapaccuracymode := 0;
if (lightmapcolorintensity < MINLMCOLORSENSITIVITY) or (lightmapcolorintensity > MAXLMCOLORSENSITIVITY) then
lightmapcolorintensity := DEFLMCOLORSENSITIVITY;
if (lightwidthfactor < MINLIGHTWIDTHFACTOR) or (lightwidthfactor > MAXLIGHTWIDTHFACTOR) then
lightwidthfactor := DEFLIGHTWIDTHFACTOR;
end;
//==============================================================================
//
// R_InitLightmap
//
//==============================================================================
procedure R_InitLightmap;
begin
R_CheckLightmapParams;
LMWIDTH := SCREENWIDTH;
LMHEIGHT := SCREENHEIGHT div LM_YACCURACY;
lightmapbuffer := mallocz((LMWIDTH + 1) * (LMHEIGHT + 1) * SizeOf(lightmapitem_t));
lightmapactive := false;
end;
//==============================================================================
//
// R_ShutDownLightmap
//
//==============================================================================
procedure R_ShutDownLightmap;
begin
memfree(lightmapbuffer, (LMWIDTH + 1) * (LMHEIGHT + 1) * SizeOf(lightmapitem_t));
end;
var
llastviewwidth: integer = -1;
llastviewheight: integer = -1;
llastaccuracymode: integer = -1;
// Called in each render tic before we start lightmap
//==============================================================================
//
// R_StartLightMap
//
//==============================================================================
procedure R_StartLightMap;
var
i: integer;
begin
if lightmapactive then
exit;
R_CheckLightmapParams;
lm_spartspan := MAXWIDTH + 1;
lm_stopspan := -1;
if (llastviewwidth <> viewwidth) or (llastviewheight <> viewheight) or (llastaccuracymode <> lightmapaccuracymode) then
begin
if llastaccuracymode <> lightmapaccuracymode then
begin
lightmapaccuracymode := lightmapaccuracymode mod NUMLIGHTMAPACCURACYMODES;
llastaccuracymode := lightmapaccuracymode;
LM_YACCURACY := R_CalcLigmapYAccuracy;
LM_YMOD := LM_YACCURACY div 2;
end;
memfree(lightmapbuffer, (LMWIDTH + 1) * (LMHEIGHT + 1) * SizeOf(lightmapitem_t));
LMWIDTH := viewwidth;
LMHEIGHT := viewheight div LM_YACCURACY;
lightmapbuffer := mallocz((LMWIDTH + 1) * (LMHEIGHT + 1) * SizeOf(lightmapitem_t));
llastviewwidth := viewwidth;
llastviewheight := viewheight;
for i := 0 to LMHEIGHT do
ylookuplm[i] := Plightmapbuffer_t(@lightmapbuffer[i * LMWIDTH]);
end;
lightmapactive := true;
end;
//==============================================================================
//
// R_StopLightmap
//
//==============================================================================
procedure R_StopLightmap;
begin
lightmapactive := false;
ZeroMemory(lightmapbuffer, (LMWIDTH + 1) * (LMHEIGHT + 1) * SizeOf(lightmapitem_t));
end;
//==============================================================================
//
// R_CalcLightmap
//
// JVAL:
// Calculate the ranges in x/width space of lightmap accuracy
// depending on zbuffer content (R_ZGetCriticalX) and quality step
//
//==============================================================================
procedure R_CalcLightmap;
var
x: integer;
x1: integer;
xstep: integer;
begin
for x := 0 to LMWIDTH - LM_YACCURACY - 1 do
begin
if R_ZGetCriticalX(x) then
xcolumnsteplm[x] := 1
else
xcolumnsteplm[x] := 0;
end;
for x := LMWIDTH - LM_YACCURACY to LMWIDTH do
xcolumnsteplm[x] := 1;
for x := 1 to LMWIDTH - LM_YACCURACY - 1 do
begin
if xcolumnsteplm[x] = 0 then
begin
xstep := 1;
for x1 := x + 1 to x + LM_YACCURACY - 1 do
begin
if xcolumnsteplm[x1] = 0 then
begin
xcolumnsteplm[x1] := -1;
inc(xstep);
end
else
break;
end;
if xcolumnsteplm[x - 1] = 1 then
begin
xcolumnsteplm[x - 1] := xcolumnsteplm[x - 1] + xstep;
xcolumnsteplm[x] := -1;
end
else
xcolumnsteplm[x] := xstep;
end;
end;
end;
//==============================================================================
// R_FlashSpanLightmap8
//
// Flash lightmap span to screen - 8 bit
//
//==============================================================================
procedure R_FlashSpanLightmap8(const pls_y: PInteger);
var
x, i: integer;
li: Plightmapitem_t;
r, g, b: LongWord;
dest: PByte;
color8: word;
nsteps: integer;
begin
dest := @((ylookup8[pls_y^]^)[columnofs[0]]);
li := R_LightmapBufferAt(0, pls_y^);
x := 0;
while x < LMWIDTH do
begin
nsteps := xcolumnsteplm[x];
if (nsteps > 0) and (li.numitems > 0) then
begin
r := ((li.r shr LM_RESTORESHIFT) * lightmapcolorintensity) div 256;
if r > 255 then r := 255;
g := ((li.g shr LM_RESTORESHIFT) * lightmapcolorintensity) div 256;
if g > 255 then g := 255;
b := ((li.b shr LM_RESTORESHIFT) * lightmapcolorintensity) div 256;
if b > 255 then b := 255;
color8 := R_FastApproxColorIndex(r, g, b);
for i := 0 to nsteps - 1 do
begin
dest^ := coloraddtrans8table[dest^ * 256 + color8];
inc(dest);
end;
end
else
inc(dest, nsteps);
if nsteps > 0 then
begin
inc(x, nsteps);
inc(li, nsteps);
end
else
begin
inc(x);
inc(li);
end;
end;
end;
//==============================================================================
// R_FlashSpanLightmap32
//
// Flash lightmap span to screen - 32 bit
//
//==============================================================================
procedure R_FlashSpanLightmap32(const pls_y: PInteger);
var
x, i: integer;
li: Plightmapitem_t;
r, g, b: LongWord;
destl: PLongWord;
nsteps: integer;
begin
destl := @((ylookup32[pls_y^]^)[columnofs[0]]);
li := R_LightmapBufferAt(0, pls_y^);
x := 0;
while x < LMWIDTH do
begin
nsteps := xcolumnsteplm[x];
if (nsteps > 0) and (li.numitems > 0) then
begin
r := ((li.r shr LM_RESTORESHIFT) * lightmapcolorintensity) div 256;
if r > 255 then r := 255;
g := ((li.g shr LM_RESTORESHIFT) * lightmapcolorintensity) div 256;
if g > 255 then g := 255;
b := ((li.b shr LM_RESTORESHIFT) * lightmapcolorintensity) div 256;
if b > 255 then b := 255;
for i := 0 to nsteps - 1 do
begin
destl^ := R_ColorLightAdd(destl^, r, g, b);
inc(destl);
end;
end
else
inc(destl, nsteps);
if nsteps > 0 then
begin
inc(x, nsteps);
inc(li, nsteps);
end
else
begin
inc(x);
inc(li);
end;
end;
end;
//==============================================================================
//
// R_FlashLightmap
//
//==============================================================================
procedure R_FlashLightmap;
var
h: integer;
begin
if lm_spartspan < 0 then
lm_spartspan := 0
else if lm_spartspan >= viewheight then
lm_spartspan := viewheight - 1;
if lm_stopspan < 0 then
lm_stopspan := 0
else if lm_stopspan >= viewheight then
lm_stopspan := viewheight - 1;
for h := lm_spartspan to lm_stopspan do
R_AddRenderTask(lightmapflashfunc, RF_LIGHTMAP, @h);
end;
end.