-
Notifications
You must be signed in to change notification settings - Fork 5
/
r_bsp.pas
657 lines (569 loc) · 15.5 KB
/
r_bsp.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
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
//------------------------------------------------------------------------------
//
// 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: [email protected]
// Site : https://sourceforge.net/projects/fpcdoom/
//------------------------------------------------------------------------------
{$I FPCDoom.inc}
unit r_bsp;
interface
uses
r_defs;
//==============================================================================
// R_ClearClipSegs
//
// BSP?
//
//==============================================================================
procedure R_ClearClipSegs;
//==============================================================================
//
// R_ClearDrawSegs
//
//==============================================================================
procedure R_ClearDrawSegs;
//==============================================================================
//
// R_RenderBSP
//
//==============================================================================
procedure R_RenderBSP;
type
drawfunc_t = procedure(start: integer; stop: integer);
var
ds_p: integer; // JVAL was: Pdrawseg_t
max_ds_p: integer;
curline: Pseg_t;
frontsector: Psector_t;
backsector: Psector_t;
sidedef: Pside_t;
linedef: Pline_t;
drawsegs: array[0..MAXDRAWSEGS - 1] of Pdrawseg_t;
implementation
uses
d_fpc,
doomdata,
m_fixed,
tables,
m_bbox,
p_setup,
r_segs,
r_main,
r_plane,
r_things,
r_draw,
r_sky;
//==============================================================================
//
// R_ClearDrawSegs
//
//==============================================================================
procedure R_ClearDrawSegs;
begin
ds_p := 0;
end;
//
// ClipWallSegment
// Clips the given range of columns
// and includes it in the new clip list.
//
type
cliprange_t = record
first: integer;
last: integer;
end;
Pcliprange_t = ^cliprange_t;
const
MAXSEGS = 32; // JVAL maybe bigger ???
var
// newend is one past the last valid seg
newend: Pcliprange_t;
solidsegs: array[0..MAXSEGS - 1] of cliprange_t;
// R_ClipSolidWallSegment
// Does handle solid walls,
// e.g. single sided LineDefs (middle texture)
// that entirely block the view.
//
//==============================================================================
procedure R_ClipSolidWallSegment(first, last: integer);
var
next: Pcliprange_t;
start: Pcliprange_t;
tmp: Pcliprange_t;
procedure crunch;
begin
if next = start then
// Post just extended past the bottom of one post.
exit;
while next <> newend do
begin
// Remove a post.
inc(start);
inc(next);
start^ := next^;
end;
newend := start;
inc(newend);
end;
begin
// Find the first range that touches the range
// (adjacent pixels are touching).
start := @solidsegs[0];
while start.last < first - 1 do
inc(start);
if first < start.first then
begin
if last < start.first - 1 then
begin
// Post is entirely visible (above start),
// so insert a new clippost.
R_StoreWallRange(first, last);
next := newend;
inc(newend);
while next <> start do
begin
tmp := next;
dec(tmp);
next^ := tmp^;
dec(next);
end;
next.first := first;
next.last := last;
exit;
end;
// There is a fragment above *start.
R_StoreWallRange(first, start.first - 1);
// Now adjust the clip size.
start.first := first;
end;
// Bottom contained in start?
if last <= start.last then
exit;
next := start;
tmp := next;
inc(tmp);
while last >= tmp.first - 1 do
begin
// There is a fragment between two posts.
R_StoreWallRange(next.last + 1, tmp.first - 1);
inc(next);
inc(tmp);
if last <= next.last then
begin
// Bottom is contained in next.
// Adjust the clip size.
start.last := next.last;
crunch;
exit;
end;
end;
// There is a fragment after *next.
R_StoreWallRange(next.last + 1, last);
// Adjust the clip size.
start.last := last;
// Remove start+1 to next from the clip list,
// because start now covers their area.
crunch;
end;
//==============================================================================
//
// R_ClipPassWallSegment
// Clips the given range of columns,
// but does not includes it in the clip list.
// Does handle windows,
// e.g. LineDefs with upper and lower texture.
//
//==============================================================================
procedure R_ClipPassWallSegment(first, last: integer);
var
start: Pcliprange_t;
tmp: Pcliprange_t;
begin
// Find the first range that touches the range
// (adjacent pixels are touching).
start := @solidsegs[0];
while start.last < first - 1 do
inc(start);
if first < start.first then
begin
if last < start.first - 1 then
begin
// Post is entirely visible (above start).
R_StoreWallRange(first, last);
exit;
end;
// There is a fragment above *start.
R_StoreWallRange(first, start.first - 1);
end;
// Bottom contained in start?
if last <= start.last then
exit;
tmp := start;
inc(tmp);
while last >= tmp.first - 1 do
begin
// There is a fragment between two posts.
R_StoreWallRange(start.last + 1, tmp.first - 1);
inc(start);
inc(tmp);
if last <= start.last then
exit;
end;
// There is a fragment after *next.
R_StoreWallRange(start.last + 1, last);
end;
//==============================================================================
//
// R_ClearClipSegs
//
//==============================================================================
procedure R_ClearClipSegs;
begin
newend := @solidsegs[0];
newend.first := -$7fffffff;
newend.last := -1;
inc(newend);
newend.first := viewwidth;
newend.last := $7fffffff;
inc(newend);
end;
//==============================================================================
//
// R_AddLine
// Clips the given segment
// and adds any visible pieces to the line list.
//
//==============================================================================
procedure R_AddLine(line: Pseg_t);
var
x1: integer;
x2: integer;
angle1: angle_t;
angle2: angle_t;
span: angle_t;
tspan: angle_t;
clipangle2: angle_t;
begin
curline := line;
// OPTIMIZE: quickly reject orthogonal back sides.
angle1 := R_PointToAngle(line.v1.r_x, line.v1.r_y);
angle2 := R_PointToAngle(line.v2.r_x, line.v2.r_y);
// Clip to view edges.
span := angle1 - angle2;
// Back side? I.e. backface culling?
if span >= ANG180 then
exit;
// Global angle needed by segcalc.
rw_angle1 := angle1;
angle1 := angle1 - viewangle;
angle2 := angle2 - viewangle;
tspan := angle1 + clipangle;
clipangle2 := 2 * clipangle;
if tspan > clipangle2 then
begin
tspan := tspan - clipangle2;
// Totally off the left edge?
if tspan >= span then
exit;
angle1 := clipangle;
end;
tspan := clipangle - angle2;
if tspan > clipangle2 then
begin
tspan := tspan - clipangle2;
// Totally off the left edge?
if tspan >= span then
exit;
angle2 := -clipangle;
end;
// The seg is in the view range,
// but not necessarily visible.
angle1 := _SHRW(angle1 + ANG90, ANGLETOFINESHIFT);
angle2 := _SHRW(angle2 + ANG90, ANGLETOFINESHIFT);
x1 := viewangletox[angle1];
x2 := viewangletox[angle2];
// Does not cross a pixel?
if x1 >= x2 then
exit;
backsector := line.backsector;
// Single sided line?
if backsector = nil then
begin
R_ClipSolidWallSegment(x1, x2 - 1);
exit;
end;
// Closed door.
if (backsector.ceilingheight <= frontsector.floorheight) or
(backsector.floorheight >= frontsector.ceilingheight) then
begin
R_ClipSolidWallSegment(x1, x2 - 1);
exit;
end;
// Window.
if (backsector.ceilingheight <> frontsector.ceilingheight) or
(backsector.floorheight <> frontsector.floorheight) then
begin
R_ClipPassWallSegment(x1, x2 - 1);
exit;
end;
// Reject empty lines used for triggers
// and special events.
// Identical floor and ceiling on both sides,
// identical light levels on both sides,
// and no middle texture.
if (backsector.ceilingpic = frontsector.ceilingpic) and
(backsector.floorpic = frontsector.floorpic) and
(backsector.lightlevel = frontsector.lightlevel) and
(curline.sidedef.midtexture = 0) then
exit;
R_ClipPassWallSegment(x1, x2 - 1);
end;
//
// R_CheckBBox
// Checks BSP node/subtree bounding box.
// Returns true
// if some part of the bbox might be visible.
//
const
checkcoord: array[0..11, 0..3] of integer = (
(3, 0, 2, 1),
(3, 0, 2, 0),
(3, 1, 2, 0),
(0, 0, 0, 0),
(2, 0, 2, 1),
(0, 0, 0, 0),
(3, 1, 3, 0),
(0, 0, 0, 0),
(2, 0, 3, 1),
(2, 1, 3, 1),
(2, 1, 3, 0),
(0, 0, 0, 0)
);
//==============================================================================
//
// R_CheckBBox
//
//==============================================================================
function R_CheckBBox(bspcoordA: Pfixed_tArray; const side: integer): boolean;
var
bspcoord: Pfixed_tArray;
boxx: integer;
boxy: integer;
boxpos: integer;
x1: fixed_t;
y1: fixed_t;
x2: fixed_t;
y2: fixed_t;
angle1: angle_t;
angle2: angle_t;
span: angle_t;
tspan: angle_t;
clipangle2: angle_t;
start: Pcliprange_t;
sx1: integer;
sx2: integer;
begin
if side = 0 then
bspcoord := bspcoordA
else
bspcoord := @bspcoordA[4];
// Find the corners of the box
// that define the edges from current viewpoint.
if viewx <= bspcoord[BOXLEFT] then
boxx := 0
else if viewx < bspcoord[BOXRIGHT] then
boxx := 1
else
boxx := 2;
if viewy >= bspcoord[BOXTOP] then
boxy := 0
else if viewy > bspcoord[BOXBOTTOM] then
boxy := 1
else
boxy := 2;
boxpos := boxy * 4 + boxx;
if boxpos = 5 then
begin
result := true;
exit;
end;
x1 := bspcoord[checkcoord[boxpos][0]];
y1 := bspcoord[checkcoord[boxpos][1]];
x2 := bspcoord[checkcoord[boxpos][2]];
y2 := bspcoord[checkcoord[boxpos][3]];
// check clip list for an open space
angle1 := R_PointToAngle(x1, y1) - viewangle;
angle2 := R_PointToAngle(x2, y2) - viewangle;
span := angle1 - angle2;
// Sitting on a line?
if span >= ANG180 then
begin
result := true;
exit;
end;
tspan := angle1 + clipangle;
clipangle2 := 2 * clipangle;
if tspan > clipangle2 then
begin
tspan := tspan - clipangle2;
// Totally off the left edge?
if tspan >= span then
begin
result := false;
exit;
end;
angle1 := clipangle;
end;
tspan := clipangle - angle2;
if tspan > clipangle2 then
begin
tspan := tspan - clipangle2;
// Totally off the left edge?
if tspan >= span then
begin
result := false;
exit;
end;
angle2 := -clipangle;
end;
// Find the first clippost
// that touches the source post
// (adjacent pixels are touching).
angle1 := _SHRW(angle1 + ANG90, ANGLETOFINESHIFT);
angle2 := _SHRW(angle2 + ANG90, ANGLETOFINESHIFT);
sx1 := viewangletox[angle1];
sx2 := viewangletox[angle2];
// Does not cross a pixel.
if sx1 = sx2 then
begin
result := false;
exit;
end;
dec(sx2);
start := @solidsegs[0];
while start.last < sx2 do
inc(start);
if (sx1 >= start.first) and
(sx2 <= start.last) then
// The clippost contains the new span.
result := false
else
result := true;
end;
//==============================================================================
//
// R_Subsector
// Determine floor/ceiling planes.
// Add sprites of things in sector.
// Draw one or more line segments.
//
//==============================================================================
procedure R_Subsector(const num: integer);
var
count: integer;
line: Pseg_t;
i_line: integer;
sub: Psubsector_t;
begin
sub := @subsectors[num];
frontsector := sub.sector;
count := sub.numlines;
i_line := sub.firstline;
line := @segs[i_line];
if frontsector.floorheight < viewz then
begin
floorplane := R_FindPlane(frontsector.floorheight,
frontsector.floorpic,
frontsector.lightlevel);
end
else
floorplane := nil;
if (frontsector.ceilingheight > viewz) or
(frontsector.ceilingpic = skyflatnum) then
begin
ceilingplane := R_FindPlane(frontsector.ceilingheight,
frontsector.ceilingpic,
frontsector.lightlevel);
end
else
ceilingplane := nil;
R_AddSprites(frontsector);
while count <> 0 do
begin
R_AddLine(line);
inc(line);
dec(count);
end;
end;
//==============================================================================
// R_RenderBSPNode
//
// RenderBSPNode
// Renders all subsectors below a given node,
// traversing subtree recursively.
// Just call with BSP root.
//
//==============================================================================
procedure R_RenderBSPNode(const bspnum: integer);
var
bsp: Pnode_t;
begin
// Found a subsector?
if bspnum and NF_SUBSECTOR <> 0 then
begin
if bspnum = -1 then
R_Subsector(0)
else
R_Subsector(bspnum and not NF_SUBSECTOR);
exit;
end;
bsp := @nodes[bspnum];
// Decide which side the view point is on.
if R_PointOnSide(viewx, viewy, bsp) then
begin
// Recursively divide front space.
R_RenderBSPNode(bsp.children[1]);
// Possibly divide back space.
if R_CheckBBox(Pfixed_tArray(@(bsp.bbox)), 0) then
R_RenderBSPNode(bsp.children[0]);
end
else
begin
// Recursively divide front space.
R_RenderBSPNode(bsp.children[0]);
// Possibly divide back space.
if R_CheckBBox(Pfixed_tArray(@(bsp.bbox)), 1) then
R_RenderBSPNode(bsp.children[1]);
end
end;
//==============================================================================
//
// R_RenderBSP
//
//==============================================================================
procedure R_RenderBSP;
begin
// The head node is the last node output.
R_RenderBSPNode(numnodes - 1);
end;
end.