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
|
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// Copyright(C) 1993-1996 Id Software, Inc.
// Copyright(C) 1993-2008 Raven Software
//
// 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.
//
//-----------------------------------------------------------------------------
#include <math.h>
#include "h2def.h"
#include "r_local.h"
int viewangleoffset;
#ifdef __WATCOMC__
int newViewAngleOff;
#endif
int validcount = 1; // increment every time a check is made
lighttable_t *fixedcolormap;
extern lighttable_t **walllights;
int centerx, centery;
fixed_t centerxfrac, centeryfrac;
fixed_t projection;
int framecount; // just for profiling purposes
int sscount, linecount, loopcount;
fixed_t viewx, viewy, viewz;
angle_t viewangle;
fixed_t viewcos, viewsin;
player_t *viewplayer;
int detailshift; // 0 = high, 1 = low
//
// precalculated math tables
//
angle_t clipangle;
// The viewangletox[viewangle + FINEANGLES/4] lookup maps the visible view
// angles to screen X coordinates, flattening the arc to a flat projection
// plane. There will be many angles mapped to the same X.
int viewangletox[FINEANGLES / 2];
// The xtoviewangleangle[] table maps a screen pixel to the lowest viewangle
// that maps back to x ranges from clipangle to -clipangle
angle_t xtoviewangle[SCREENWIDTH + 1];
// the finetangentgent[angle+FINEANGLES/4] table holds the fixed_t tangent
// values for view angles, ranging from MININT to 0 to MAXINT.
// fixed_t finetangent[FINEANGLES/2];
// fixed_t finesine[5*FINEANGLES/4];
fixed_t *finecosine = &finesine[FINEANGLES / 4];
lighttable_t *scalelight[LIGHTLEVELS][MAXLIGHTSCALE];
lighttable_t *scalelightfixed[MAXLIGHTSCALE];
lighttable_t *zlight[LIGHTLEVELS][MAXLIGHTZ];
int extralight; // bumped light from gun blasts
void (*colfunc) (void);
void (*basecolfunc) (void);
void (*fuzzcolfunc) (void);
void (*transcolfunc) (void);
void (*spanfunc) (void);
/*
===================
=
= R_AddPointToBox
=
===================
*/
/*
void R_AddPointToBox (int x, int y, fixed_t *box)
{
if (x< box[BOXLEFT])
box[BOXLEFT] = x;
if (x> box[BOXRIGHT])
box[BOXRIGHT] = x;
if (y< box[BOXBOTTOM])
box[BOXBOTTOM] = y;
if (y> box[BOXTOP])
box[BOXTOP] = y;
}
*/
/*
===============================================================================
=
= R_PointOnSide
=
= Returns side 0 (front) or 1 (back)
===============================================================================
*/
int R_PointOnSide(fixed_t x, fixed_t y, node_t * node)
{
fixed_t dx, dy;
fixed_t left, right;
if (!node->dx)
{
if (x <= node->x)
return node->dy > 0;
return node->dy < 0;
}
if (!node->dy)
{
if (y <= node->y)
return node->dx < 0;
return node->dx > 0;
}
dx = (x - node->x);
dy = (y - node->y);
// try to quickly decide by looking at sign bits
if ((node->dy ^ node->dx ^ dx ^ dy) & 0x80000000)
{
if ((node->dy ^ dx) & 0x80000000)
return 1; // (left is negative)
return 0;
}
left = FixedMul(node->dy >> FRACBITS, dx);
right = FixedMul(dy, node->dx >> FRACBITS);
if (right < left)
return 0; // front side
return 1; // back side
}
int R_PointOnSegSide(fixed_t x, fixed_t y, seg_t * line)
{
fixed_t lx, ly;
fixed_t ldx, ldy;
fixed_t dx, dy;
fixed_t left, right;
lx = line->v1->x;
ly = line->v1->y;
ldx = line->v2->x - lx;
ldy = line->v2->y - ly;
if (!ldx)
{
if (x <= lx)
return ldy > 0;
return ldy < 0;
}
if (!ldy)
{
if (y <= ly)
return ldx < 0;
return ldx > 0;
}
dx = (x - lx);
dy = (y - ly);
// try to quickly decide by looking at sign bits
if ((ldy ^ ldx ^ dx ^ dy) & 0x80000000)
{
if ((ldy ^ dx) & 0x80000000)
return 1; // (left is negative)
return 0;
}
left = FixedMul(ldy >> FRACBITS, dx);
right = FixedMul(dy, ldx >> FRACBITS);
if (right < left)
return 0; // front side
return 1; // back side
}
/*
===============================================================================
=
= R_PointToAngle
=
===============================================================================
*/
// to get a global angle from cartesian coordinates, the coordinates are
// flipped until they are in the first octant of the coordinate system, then
// the y (<=x) is scaled and divided by x to get a tangent (slope) value
// which is looked up in the tantoangle[] table. The +1 size is to handle
// the case when x==y without additional checking.
#define SLOPERANGE 2048
#define SLOPEBITS 11
#define DBITS (FRACBITS-SLOPEBITS)
extern int tantoangle[SLOPERANGE + 1]; // get from tables.c
// int tantoangle[SLOPERANGE+1];
int SlopeDiv(unsigned num, unsigned den)
{
unsigned ans;
if (den < 512)
return SLOPERANGE;
ans = (num << 3) / (den >> 8);
return ans <= SLOPERANGE ? ans : SLOPERANGE;
}
angle_t R_PointToAngle(fixed_t x, fixed_t y)
{
x -= viewx;
y -= viewy;
if ((!x) && (!y))
return 0;
if (x >= 0)
{ // x >=0
if (y >= 0)
{ // y>= 0
if (x > y)
return tantoangle[SlopeDiv(y, x)]; // octant 0
else
return ANG90 - 1 - tantoangle[SlopeDiv(x, y)]; // octant 1
}
else
{ // y<0
y = -y;
if (x > y)
return -tantoangle[SlopeDiv(y, x)]; // octant 8
else
return ANG270 + tantoangle[SlopeDiv(x, y)]; // octant 7
}
}
else
{ // x<0
x = -x;
if (y >= 0)
{ // y>= 0
if (x > y)
return ANG180 - 1 - tantoangle[SlopeDiv(y, x)]; // octant 3
else
return ANG90 + tantoangle[SlopeDiv(x, y)]; // octant 2
}
else
{ // y<0
y = -y;
if (x > y)
return ANG180 + tantoangle[SlopeDiv(y, x)]; // octant 4
else
return ANG270 - 1 - tantoangle[SlopeDiv(x, y)]; // octant 5
}
}
return 0;
}
angle_t R_PointToAngle2(fixed_t x1, fixed_t y1, fixed_t x2, fixed_t y2)
{
viewx = x1;
viewy = y1;
return R_PointToAngle(x2, y2);
}
fixed_t R_PointToDist(fixed_t x, fixed_t y)
{
int angle;
fixed_t dx, dy, temp;
fixed_t dist;
dx = abs(x - viewx);
dy = abs(y - viewy);
if (dy > dx)
{
temp = dx;
dx = dy;
dy = temp;
}
angle =
(tantoangle[FixedDiv(dy, dx) >> DBITS] + ANG90) >> ANGLETOFINESHIFT;
dist = FixedDiv(dx, finesine[angle]); // use as cosine
return dist;
}
/*
=================
=
= R_InitPointToAngle
=
=================
*/
void R_InitPointToAngle(void)
{
// now getting from tables.c
#if 0
int i;
long t;
float f;
//
// slope (tangent) to angle lookup
//
for (i = 0; i <= SLOPERANGE; i++)
{
f = atan((float) i / SLOPERANGE) / (3.141592657 * 2);
t = 0xffffffff * f;
tantoangle[i] = t;
}
#endif
}
//=============================================================================
/*
================
=
= R_ScaleFromGlobalAngle
=
= Returns the texture mapping scale for the current line at the given angle
= rw_distance must be calculated first
================
*/
fixed_t R_ScaleFromGlobalAngle(angle_t visangle)
{
fixed_t scale;
int anglea, angleb;
int sinea, sineb;
fixed_t num, den;
#if 0
{
fixed_t dist, z;
fixed_t sinv, cosv;
sinv = finesine[(visangle - rw_normalangle) >> ANGLETOFINESHIFT];
dist = FixedDiv(rw_distance, sinv);
cosv = finecosine[(viewangle - visangle) >> ANGLETOFINESHIFT];
z = abs(FixedMul(dist, cosv));
scale = FixedDiv(projection, z);
return scale;
}
#endif
anglea = ANG90 + (visangle - viewangle);
angleb = ANG90 + (visangle - rw_normalangle);
// bothe sines are allways positive
sinea = finesine[anglea >> ANGLETOFINESHIFT];
sineb = finesine[angleb >> ANGLETOFINESHIFT];
num = FixedMul(projection, sineb) << detailshift;
den = FixedMul(rw_distance, sinea);
if (den > num >> 16)
{
scale = FixedDiv(num, den);
if (scale > 64 * FRACUNIT)
scale = 64 * FRACUNIT;
else if (scale < 256)
scale = 256;
}
else
scale = 64 * FRACUNIT;
return scale;
}
/*
=================
=
= R_InitTables
=
=================
*/
void R_InitTables(void)
{
// now getting from tables.c
#if 0
int i;
float a, fv;
int t;
//
// viewangle tangent table
//
for (i = 0; i < FINEANGLES / 2; i++)
{
a = (i - FINEANGLES / 4 + 0.5) * PI * 2 / FINEANGLES;
fv = FRACUNIT * tan(a);
t = fv;
finetangent[i] = t;
}
//
// finesine table
//
for (i = 0; i < 5 * FINEANGLES / 4; i++)
{
// OPTIMIZE: mirror...
a = (i + 0.5) * PI * 2 / FINEANGLES;
t = FRACUNIT * sin(a);
finesine[i] = t;
}
#endif
}
/*
=================
=
= R_InitTextureMapping
=
=================
*/
void R_InitTextureMapping(void)
{
int i;
int x;
int t;
fixed_t focallength;
//
// use tangent table to generate viewangletox
// viewangletox will give the next greatest x after the view angle
//
// calc focallength so FIELDOFVIEW angles covers SCREENWIDTH
focallength =
FixedDiv(centerxfrac, finetangent[FINEANGLES / 4 + FIELDOFVIEW / 2]);
for (i = 0; i < FINEANGLES / 2; i++)
{
if (finetangent[i] > FRACUNIT * 2)
t = -1;
else if (finetangent[i] < -FRACUNIT * 2)
t = viewwidth + 1;
else
{
t = FixedMul(finetangent[i], focallength);
t = (centerxfrac - t + FRACUNIT - 1) >> FRACBITS;
if (t < -1)
t = -1;
else if (t > viewwidth + 1)
t = viewwidth + 1;
}
viewangletox[i] = t;
}
//
// scan viewangletox[] to generate xtoviewangleangle[]
//
// xtoviewangle will give the smallest view angle that maps to x
for (x = 0; x <= viewwidth; x++)
{
i = 0;
while (viewangletox[i] > x)
i++;
xtoviewangle[x] = (i << ANGLETOFINESHIFT) - ANG90;
}
//
// take out the fencepost cases from viewangletox
//
for (i = 0; i < FINEANGLES / 2; i++)
{
t = FixedMul(finetangent[i], focallength);
t = centerx - t;
if (viewangletox[i] == -1)
viewangletox[i] = 0;
else if (viewangletox[i] == viewwidth + 1)
viewangletox[i] = viewwidth;
}
clipangle = xtoviewangle[0];
}
//=============================================================================
/*
====================
=
= R_InitLightTables
=
= Only inits the zlight table, because the scalelight table changes
= with view size
=
====================
*/
#define DISTMAP 2
void R_InitLightTables(void)
{
int i, j, level, startmap;
int scale;
//
// Calculate the light levels to use for each level / distance combination
//
for (i = 0; i < LIGHTLEVELS; i++)
{
startmap = ((LIGHTLEVELS - 1 - i) * 2) * NUMCOLORMAPS / LIGHTLEVELS;
for (j = 0; j < MAXLIGHTZ; j++)
{
scale =
FixedDiv((SCREENWIDTH / 2 * FRACUNIT),
(j + 1) << LIGHTZSHIFT);
scale >>= LIGHTSCALESHIFT;
level = startmap - scale / DISTMAP;
if (level < 0)
level = 0;
if (level >= NUMCOLORMAPS)
level = NUMCOLORMAPS - 1;
zlight[i][j] = colormaps + level * 256;
}
}
}
/*
==============
=
= R_SetViewSize
=
= Don't really change anything here, because i might be in the middle of
= a refresh. The change will take effect next refresh.
=
==============
*/
boolean setsizeneeded;
int setblocks, setdetail;
void R_SetViewSize(int blocks, int detail)
{
setsizeneeded = true;
setblocks = blocks;
setdetail = detail;
}
/*
==============
=
= R_ExecuteSetViewSize
=
==============
*/
void R_ExecuteSetViewSize(void)
{
fixed_t cosadj, dy;
int i, j, level, startmap;
setsizeneeded = false;
if (setblocks == 11)
{
scaledviewwidth = SCREENWIDTH;
viewheight = SCREENHEIGHT;
}
else
{
scaledviewwidth = setblocks * 32;
viewheight = (setblocks * 161 / 10);
}
detailshift = setdetail;
viewwidth = scaledviewwidth >> detailshift;
centery = viewheight / 2;
centerx = viewwidth / 2;
centerxfrac = centerx << FRACBITS;
centeryfrac = centery << FRACBITS;
projection = centerxfrac;
if (!detailshift)
{
colfunc = basecolfunc = R_DrawColumn;
fuzzcolfunc = R_DrawFuzzColumn;
transcolfunc = R_DrawTranslatedColumn;
spanfunc = R_DrawSpan;
}
else
{
colfunc = basecolfunc = R_DrawColumnLow;
fuzzcolfunc = R_DrawFuzzColumn;
transcolfunc = R_DrawTranslatedColumn;
spanfunc = R_DrawSpanLow;
}
R_InitBuffer(scaledviewwidth, viewheight);
R_InitTextureMapping();
//
// psprite scales
//
pspritescale = FRACUNIT * viewwidth / SCREENWIDTH;
pspriteiscale = FRACUNIT * SCREENWIDTH / viewwidth;
//
// thing clipping
//
for (i = 0; i < viewwidth; i++)
screenheightarray[i] = viewheight;
//
// planes
//
for (i = 0; i < viewheight; i++)
{
dy = ((i - viewheight / 2) << FRACBITS) + FRACUNIT / 2;
dy = abs(dy);
yslope[i] = FixedDiv((viewwidth << detailshift) / 2 * FRACUNIT, dy);
}
for (i = 0; i < viewwidth; i++)
{
cosadj = abs(finecosine[xtoviewangle[i] >> ANGLETOFINESHIFT]);
distscale[i] = FixedDiv(FRACUNIT, cosadj);
}
//
// Calculate the light levels to use for each level / scale combination
//
for (i = 0; i < LIGHTLEVELS; i++)
{
startmap = ((LIGHTLEVELS - 1 - i) * 2) * NUMCOLORMAPS / LIGHTLEVELS;
for (j = 0; j < MAXLIGHTSCALE; j++)
{
level =
startmap -
j * SCREENWIDTH / (viewwidth << detailshift) / DISTMAP;
if (level < 0)
level = 0;
if (level >= NUMCOLORMAPS)
level = NUMCOLORMAPS - 1;
scalelight[i][j] = colormaps + level * 256;
}
}
//
// draw the border
//
R_DrawViewBorder(); // erase old menu stuff
}
/*
==============
=
= R_Init
=
==============
*/
int detailLevel;
int screenblocks;
void R_Init(void)
{
R_InitData();
R_InitPointToAngle();
R_InitTables();
// viewwidth / viewheight / detailLevel are set by the defaults
R_SetViewSize(screenblocks, detailLevel);
R_InitPlanes();
R_InitLightTables();
R_InitSkyMap();
R_InitTranslationTables();
framecount = 0;
}
/*
==============
=
= R_PointInSubsector
=
==============
*/
subsector_t *R_PointInSubsector(fixed_t x, fixed_t y)
{
node_t *node;
int side, nodenum;
if (!numnodes) // single subsector is a special case
return subsectors;
nodenum = numnodes - 1;
while (!(nodenum & NF_SUBSECTOR))
{
node = &nodes[nodenum];
side = R_PointOnSide(x, y, node);
nodenum = node->children[side];
}
return &subsectors[nodenum & ~NF_SUBSECTOR];
}
//----------------------------------------------------------------------------
//
// PROC R_SetupFrame
//
//----------------------------------------------------------------------------
void R_SetupFrame(player_t * player)
{
int i;
int tableAngle;
int tempCentery;
int intensity;
//drawbsp = 1;
viewplayer = player;
#ifdef __WATCOMC__
if (newViewAngleOff)
{
viewangleoffset = newViewAngleOff << ANGLETOFINESHIFT;
}
#endif
viewangle = player->mo->angle + viewangleoffset;
tableAngle = viewangle >> ANGLETOFINESHIFT;
viewx = player->mo->x;
viewy = player->mo->y;
if (localQuakeHappening[displayplayer] && !paused)
{
intensity = localQuakeHappening[displayplayer];
viewx += ((M_Random() % (intensity << 2))
- (intensity << 1)) << FRACBITS;
viewy += ((M_Random() % (intensity << 2))
- (intensity << 1)) << FRACBITS;
}
extralight = player->extralight;
viewz = player->viewz;
tempCentery = viewheight / 2 + (player->lookdir) * screenblocks / 10;
if (centery != tempCentery)
{
centery = tempCentery;
centeryfrac = centery << FRACBITS;
for (i = 0; i < viewheight; i++)
{
yslope[i] = FixedDiv((viewwidth << detailshift) / 2 * FRACUNIT,
abs(((i - centery) << FRACBITS) +
FRACUNIT / 2));
}
}
viewsin = finesine[tableAngle];
viewcos = finecosine[tableAngle];
sscount = 0;
if (player->fixedcolormap)
{
fixedcolormap = colormaps + player->fixedcolormap
* 256 * sizeof(lighttable_t);
walllights = scalelightfixed;
for (i = 0; i < MAXLIGHTSCALE; i++)
{
scalelightfixed[i] = fixedcolormap;
}
}
else
{
fixedcolormap = 0;
}
framecount++;
validcount++;
if (BorderNeedRefresh)
{
if (setblocks < 10)
{
R_DrawViewBorder();
}
BorderNeedRefresh = false;
BorderTopRefresh = false;
UpdateState |= I_FULLSCRN;
}
if (BorderTopRefresh)
{
if (setblocks < 10)
{
R_DrawTopBorder();
}
BorderTopRefresh = false;
UpdateState |= I_MESSAGES;
}
#ifdef __NeXT__
RD_ClearMapWindow();
#endif
#ifdef __WATCOMC__
destview = destscreen + (viewwindowx >> 2) + viewwindowy * 80;
#endif
#if 0
{
static int frame;
memset(screen, frame, SCREENWIDTH * SCREENHEIGHT);
frame++;
}
#endif
}
/*
==============
=
= R_RenderView
=
==============
*/
void R_RenderPlayerView(player_t * player)
{
R_SetupFrame(player);
R_ClearClipSegs();
R_ClearDrawSegs();
R_ClearPlanes();
R_ClearSprites();
NetUpdate(); // check for new console commands
// Make displayed player invisible locally
if (localQuakeHappening[displayplayer] && gamestate == GS_LEVEL)
{
players[displayplayer].mo->flags2 |= MF2_DONTDRAW;
R_RenderBSPNode(numnodes - 1); // head node is the last node output
players[displayplayer].mo->flags2 &= ~MF2_DONTDRAW;
}
else
{
R_RenderBSPNode(numnodes - 1); // head node is the last node output
}
NetUpdate(); // check for new console commands
R_DrawPlanes();
NetUpdate(); // check for new console commands
R_DrawMasked();
NetUpdate(); // check for new console commands
}
|