Debian Codegraph @

prboom2/src/r_main.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom: a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright (C) 1999 by
8 * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
9 * Copyright (C) 1999-2000 by
10 * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
11 * Copyright 2005, 2006 by
12 * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 * 02111-1307, USA.
28 *
29 * DESCRIPTION:
30 * Rendering main loop and setup functions,
31 * utility functions (BSP, geometry, trigonometry).
32 * See tables.c, too.
33 *
34 *-----------------------------------------------------------------------------*/
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 #include "SDL.h"
40
41 #include "doomstat.h"
42 #include "d_net.h"
43 #include "w_wad.h"
44 #include "r_main.h"
45 #include "r_things.h"
46 #include "r_plane.h"
47 #include "r_bsp.h"
48 #include "r_draw.h"
49 #include "m_bbox.h"
50 #include "r_sky.h"
51 #include "v_video.h"
52 #include "lprintf.h"
53 #include "st_stuff.h"
54 #include "i_main.h"
55 #include "i_system.h"
56 #include "i_video.h"
57 #include "g_game.h"
58 #include "smooth.h"
59 #include "r_fps.h"
60 #include <math.h>
61 #include "e6y.h"//e6y
62 #include "xs_Float.h"
63
64 #include "dsda/configuration.h"
65 #include "dsda/exhud.h"
66 #include "dsda/features.h"
67 #include "dsda/map_format.h"
68 #include "dsda/mapinfo.h"
69 #include "dsda/render_stats.h"
70 #include "dsda/settings.h"
71 #include "dsda/signal_context.h"
72 #include "dsda/stretch.h"
73 #include "dsda/gl/render_scale.h"
74
75 #include "hexen/a_action.h"
76
77 // e6y
78 // Now they are variables. Depends from render_doom_lightmaps variable.
79 // Unify colour maping logic by cph is removed, because of bugs.
80 int LIGHTLEVELS = 32;
81 int LIGHTSEGSHIFT = 3;
82 int LIGHTBRIGHT = 2;
83
84 int r_frame_count;
85
86 // Fineangles in the SCREENWIDTH wide window.
87 #define FIELDOFVIEW 2048
88
89 #define HEXEN_PI 3.141592657
90
91 #define FIXED2DOUBLE(x) ((x)/(double)FRACUNIT)
92
93 int validcount = 1; // increment every time a check is made
94 int validcount2 = 1;
95 const lighttable_t *fixedcolormap;
96 int centerx, centery;
97 // e6y: wide-res
98 int wide_centerx;
99
100 fixed_t focallength;
101 fixed_t focallengthy;
102 fixed_t globaluclip, globaldclip;
103 fixed_t centerxfrac, centeryfrac;
104 fixed_t yaspectmul;
105 fixed_t viewheightfrac; //e6y: for correct cliping of things
106 fixed_t projection;
107 // proff 11/06/98: Added for high-res
108 fixed_t projectiony;
109 fixed_t skyiscale;
110 fixed_t viewx, viewy, viewz;
111 angle_t viewangle;
112 fixed_t viewcos, viewsin;
113 fixed_t viewtancos, viewtansin;
114 player_t *viewplayer;
115 // e6y: Added for more precise flats drawing
116 fixed_t viewfocratio;
117
118 int r_nearclip = 5;
119
120 int FieldOfView;
121 int viewport[4];
122 float modelMatrix[16];
123 float projMatrix[16];
124
125 extern const lighttable_t **walllights;
126
127 //
128 // precalculated math tables
129 //
130
131 angle_t clipangle;
132
133 // The viewangletox[viewangle + FINEANGLES/4] lookup
134 // maps the visible view angles to screen X coordinates,
135 // flattening the arc to a flat projection plane.
136 // There will be many angles mapped to the same X.
137
138 int viewangletox[FINEANGLES/2];
139
140 // The xtoviewangleangle[] table maps a screen pixel
141 // to the lowest viewangle that maps back to x ranges
142 // from clipangle to -clipangle.
143
144 // e6y: resolution limitation is removed
145 angle_t *xtoviewangle; // killough 2/8/98
146
147 // [FG] linear horizontal sky scrolling
148 angle_t *linearskyangle;
149
150 // killough 3/20/98: Support dynamic colormaps, e.g. deep water
151 // killough 4/4/98: support dynamic number of them as well
152
153 int numcolormaps;
154 const lighttable_t *(*c_scalelight)[LIGHTLEVELS_MAX][MAXLIGHTSCALE];
155 const lighttable_t *(*c_zlight)[LIGHTLEVELS_MAX][MAXLIGHTZ];
156 const lighttable_t *(*scalelight)[MAXLIGHTSCALE];
157 const lighttable_t *(*zlight)[MAXLIGHTZ];
158 const lighttable_t *fullcolormap;
159 const lighttable_t **colormaps;
160
161 const byte* colormap_lump;
162
163 // killough 3/20/98, 4/4/98: end dynamic colormaps
164
165 //e6y: for Boom colormaps in OpenGL mode
166 dboolean use_boom_cm;
167 int boom_cm; // current colormap
168 int frame_fixedcolormap = 0;
169
170 int extralight; // bumped light from gun blasts
171
172 //
173 // R_PointOnSide
174 // Traverse BSP (sub) tree,
175 // check point against partition plane.
176 // Returns side 0 (front) or 1 (back).
177 //
178 // killough 5/2/98: reformatted
179 //
180
181 // Workaround for optimization bug in clang
182 // fixes desync in competn/doom/fp2-3655.lmp and in dmnsns.wad dmn01m909.lmp
183 #if defined(__clang__)
184 PUREFUNC int R_CompatiblePointOnSide(volatile fixed_t x, volatile fixed_t y, const node_t *node)
185 #else
186 PUREFUNC int R_CompatiblePointOnSide(fixed_t x, fixed_t y, const node_t *node)
187 #endif
188 {
189 if (!node->dx)
190 return x <= node->x ? node->dy > 0 : node->dy < 0;
191
192 if (!node->dy)
193 return y <= node->y ? node->dx < 0 : node->dx > 0;
194
195 x -= node->x;
196 y -= node->y;
197
198 // Try to quickly decide by looking at sign bits.
199 if ((node->dy ^ node->dx ^ x ^ y) < 0)
200 return (node->dy ^ x) < 0; // (left is negative)
201 return FixedMul(y, node->dx>>FRACBITS) >= FixedMul(node->dy>>FRACBITS, x);
202 }
203
204 #if defined(__clang__)
205 PUREFUNC int R_ZDoomPointOnSide(volatile fixed_t x, volatile fixed_t y, const node_t *node)
206 #else
207 PUREFUNC int R_ZDoomPointOnSide(fixed_t x, fixed_t y, const node_t *node)
208 #endif
209 {
210 if (!node->dx)
211 return x <= node->x ? node->dy > 0 : node->dy < 0;
212
213 if (!node->dy)
214 return y <= node->y ? node->dx < 0 : node->dx > 0;
215
216 x -= node->x;
217 y -= node->y;
218
219 // Try to quickly decide by looking at sign bits.
220 if ((node->dy ^ node->dx ^ x ^ y) < 0)
221 return (node->dy ^ x) < 0; // (left is negative)
222 return (long long) y * node->dx >= (long long) x * node->dy;
223 }
224
225 int (*R_PointOnSide)(fixed_t x, fixed_t y, const node_t *node);
226
227 // killough 5/2/98: reformatted
228
229 PUREFUNC int R_CompatiblePointOnSegSide(fixed_t x, fixed_t y, const seg_t *line)
230 {
231 fixed_t lx = line->v1->x;
232 fixed_t ly = line->v1->y;
233 fixed_t ldx = line->v2->x - lx;
234 fixed_t ldy = line->v2->y - ly;
235
236 if (!ldx)
237 return x <= lx ? ldy > 0 : ldy < 0;
238
239 if (!ldy)
240 return y <= ly ? ldx < 0 : ldx > 0;
241
242 x -= lx;
243 y -= ly;
244
245 // Try to quickly decide by looking at sign bits.
246 if ((ldy ^ ldx ^ x ^ y) < 0)
247 return (ldy ^ x) < 0; // (left is negative)
248 return FixedMul(y, ldx>>FRACBITS) >= FixedMul(ldy>>FRACBITS, x);
249 }
250
251 PUREFUNC int R_ZDoomPointOnSegSide(fixed_t x, fixed_t y, const seg_t *line)
252 {
253 fixed_t lx = line->v1->x;
254 fixed_t ly = line->v1->y;
255 fixed_t ldx = line->v2->x - lx;
256 fixed_t ldy = line->v2->y - ly;
257
258 if (!ldx)
259 return x <= lx ? ldy > 0 : ldy < 0;
260
261 if (!ldy)
262 return y <= ly ? ldx < 0 : ldx > 0;
263
264 x -= lx;
265 y -= ly;
266
267 // Try to quickly decide by looking at sign bits.
268 if ((ldy ^ ldx ^ x ^ y) < 0)
269 return (ldy ^ x) < 0; // (left is negative)
270 return (long long) y * ldx >= (long long) x * ldy;
271 }
272
273 int (*R_PointOnSegSide)(fixed_t x, fixed_t y, const seg_t *line);
274
275 //
276 // R_PointToAngle
277 // To get a global angle from cartesian coordinates,
278 // the coordinates are flipped until they are in
279 // the first octant of the coordinate system, then
280 // the y (<=x) is scaled and divided by x to get a
281 // tangent (slope) value which is looked up in the
282 // tantoangle[] table. The +1 size of tantoangle[]
283 // is to handle the case when x==y without additional
284 // checking.
285 //
286 // killough 5/2/98: reformatted, cleaned up
287
288 angle_t R_PointToAngleSlope(fixed_t x1, fixed_t y1, fixed_t x, fixed_t y, slope_div_fn slope_div)
289 {
290 return (y -= y1, (x -= x1) || y) ?
291 x >= 0 ?
292 y >= 0 ?
293 (x > y) ? tantoangle[slope_div(y,x)] : // octant 0
294 ANG90-1-tantoangle[slope_div(x,y)] : // octant 1
295 x > (y = -y) ? 0-tantoangle[slope_div(y,x)] : // octant 8
296 ANG270+tantoangle[slope_div(x,y)] : // octant 7
297 y >= 0 ? (x = -x) > y ? ANG180-1-tantoangle[slope_div(y,x)] : // octant 3
298 ANG90 + tantoangle[slope_div(x,y)] : // octant 2
299 (x = -x) > (y = -y) ? ANG180+tantoangle[slope_div(y,x)] : // octant 4
300 ANG270-1-tantoangle[slope_div(x,y)] : // octant 5
301 0;
302 }
303
304 angle_t R_PointToAngle2(fixed_t x1, fixed_t y1, fixed_t x, fixed_t y)
305 {
306 return R_PointToAngleSlope(x1, y1, x, y, SlopeDiv);
307 }
308
309 angle_t R_PointToAngleEx(fixed_t x, fixed_t y)
310 {
311 return R_PointToAngleEx2(viewx, viewy, x, y);
312 }
313
314 angle_t R_PointToAngleEx2(fixed_t x1, fixed_t y1, fixed_t x, fixed_t y)
315 {
316 // [crispy] fix overflows for very long distances
317 int64_t y_viewy = (int64_t)y - y1;
318 int64_t x_viewx = (int64_t)x - x1;
319
320 // [crispy] the worst that could happen is e.g. INT_MIN-INT_MAX = 2*INT_MIN
321 if (x_viewx < INT_MIN || x_viewx > INT_MAX ||y_viewy < INT_MIN || y_viewy > INT_MAX)
322 {
323 // [crispy] preserving the angle by halfing the distance in both directions
324 x = (int)(x_viewx / 2 + x1);
325 y = (int)(y_viewy / 2 + y1);
326 }
327
328 return R_PointToAngleSlope(x1, y1, x, y, SlopeDivEx);
329 }
330
331
332 //-----------------------------------------------------------------------------
333 //
334 // ! Returns the pseudoangle between the line p1 to (infinity, p1.y) and the
335 // line from p1 to p2. The pseudoangle has the property that the ordering of
336 // points by true angle anround p1 and ordering of points by pseudoangle are the
337 // same.
338 //
339 // For clipping exact angles are not needed. Only the ordering matters.
340 // This is about as fast as the fixed point R_PointToAngle2 but without
341 // the precision issues associated with that function.
342 //
343 //-----------------------------------------------------------------------------
344
345 angle_t R_PointToPseudoAngle (fixed_t x, fixed_t y)
346 {
347 // Note: float won't work here as it's less precise than the BAM values being passed as parameters
348 double vecx = (double)x - viewx;
349 double vecy = (double)y - viewy;
350
351 if (vecx == 0 && vecy == 0)
352 {
353 return 0;
354 }
355 else
356 {
357 double result = vecy / (fabs(vecx) + fabs(vecy));
358 if (vecx < 0)
359 {
360 result = 2.0 - result;
361 }
362 return (angle_t)xs_CRoundToInt(result * (1 << 30));
363 }
364 }
365
366 //
367 // R_InitTextureMapping
368 //
369 // killough 5/2/98: reformatted
370
371 static void R_InitTextureMapping (void)
372 {
373 int i,x,angle;
374 double linearskyfactor;
375 FieldOfView = FIELDOFVIEW;
376
377 // For widescreen displays, increase the FOV so that the middle part of the
378 // screen that would be visible on a 4:3 display has the requested FOV.
379 if (wide_centerx != centerx)
380 { // wide_centerx is what centerx would be if the display was not widescreen
381 FieldOfView = (int)(atan((double)centerx * tan((double)FieldOfView * M_PI / FINEANGLES) / (double)wide_centerx) * FINEANGLES / M_PI);
382 if (FieldOfView > 160 * FINEANGLES / 360)
383 FieldOfView = 160 * FINEANGLES / 360;
384 }
385
386 // Use tangent table to generate viewangletox:
387 // viewangletox will give the next greatest x
388 // after the view angle.
389 //
390 // Calc focallength
391 // so FIELDOFVIEW angles covers SCREENWIDTH.
392
393 focallength = FixedDiv(centerxfrac, finetangent[FINEANGLES/4 + FieldOfView/2]);
394 focallengthy = Scale(centerxfrac, yaspectmul, finetangent[FINEANGLES/4 + FieldOfView/2]);
395
396 for (i=0 ; i<FINEANGLES/2 ; i++)
397 {
398 int t;
399 int limit = finetangent[FINEANGLES/4 + FieldOfView/2];
400 if (finetangent[i] > limit)
401 t = -1;
402 else
403 if (finetangent[i] < -limit)
404 t = viewwidth+1;
405 else
406 {
407 t = FixedMul(finetangent[i], focallength);
408 t = (centerxfrac - t + FRACUNIT-1) >> FRACBITS;
409 if (t < -1)
410 t = -1;
411 else
412 if (t > viewwidth+1)
413 t = viewwidth+1;
414 }
415 viewangletox[i] = t;
416 }
417
418 // Scan viewangletox[] to generate xtoviewangle[]:
419 // xtoviewangle will give the smallest view angle
420 // that maps to x.
421
422 linearskyfactor = FIXED2DOUBLE(finetangent[FINEANGLES/4 + FieldOfView/2]) * ANG90;
423
424 for (x=0; x<=viewwidth; x++)
425 {
426 for (i=0; viewangletox[i] > x; i++)
427 ;
428 xtoviewangle[x] = (i<<ANGLETOFINESHIFT)-ANG90;
429
430 // [FG] linear horizontal sky scrolling
431 angle = (0.5 - x / (double)viewwidth) * linearskyfactor;
432 linearskyangle[x] = (angle >= 0) ? angle : ANGLE_MAX + angle;
433 }
434
435 // Take out the fencepost cases from viewangletox.
436 for (i=0; i<FINEANGLES/2; i++)
437 if (viewangletox[i] == -1)
438 viewangletox[i] = 0;
439 else
440 if (viewangletox[i] == viewwidth+1)
441 viewangletox[i] = viewwidth;
442
443 clipangle = xtoviewangle[0];
444 }
445
446 //
447 // R_InitLightTables
448 // Only inits the zlight table,
449 // because the scalelight table changes with view size.
450 //
451
452 #define DISTMAP 2
453
454 static void R_InitLightTables (void)
455 {
456 int i;
457 int render_doom_lightmaps;
458
459 // killough 4/4/98: dynamic colormaps
460 c_zlight = Z_Malloc(sizeof(*c_zlight) * numcolormaps);
461 c_scalelight = Z_Malloc(sizeof(*c_scalelight) * numcolormaps);
462
463 // hexen_note: does hexen require render_doom_lightmaps?
464 render_doom_lightmaps = dsda_IntConfig(dsda_config_render_doom_lightmaps);
465
466 LIGHTLEVELS = (render_doom_lightmaps ? 16 : 32);
467 LIGHTSEGSHIFT = (render_doom_lightmaps ? 4 : 3);
468 LIGHTBRIGHT = (render_doom_lightmaps ? 1 : 2);
469
470 // Calculate the light levels to use
471 // for each level / distance combination.
472 for (i=0; i< LIGHTLEVELS; i++)
473 {
474 // SoM: the LIGHTBRIGHT constant must be used to scale the start offset of
475 // the colormaps, otherwise the levels are staggered and become slightly
476 // darker.
477 int j, startmap = ((LIGHTLEVELS-LIGHTBRIGHT-i)*2)*NUMCOLORMAPS/LIGHTLEVELS;
478 for (j=0; j<MAXLIGHTZ; j++)
479 {
480 // CPhipps - use 320 here instead of SCREENWIDTH, otherwise hires is
481 // brighter than normal res
482 int scale = FixedDiv ((320/2*FRACUNIT), (j+1)<<LIGHTZSHIFT);
483 int t, level = startmap - (scale >>= LIGHTSCALESHIFT)/DISTMAP;
484
485 if (level < 0)
486 level = 0;
487 else
488 if (level >= NUMCOLORMAPS)
489 level = NUMCOLORMAPS-1;
490
491 // killough 3/20/98: Initialize multiple colormaps
492 level *= 256;
493 for (t=0; t<numcolormaps; t++) // killough 4/4/98
494 c_zlight[t][i][j] = colormaps[t] + level;
495 }
496 }
497 }
498
499 //
500 // R_SetViewSize
501 // Do not really change anything here,
502 // because it might be in the middle of a refresh.
503 // The change will take effect next refresh.
504 //
505
506 extern dboolean BorderNeedRefresh;
507 dboolean setsizeneeded;
508 static int setblocks;
509
510 void R_SetViewSize(void)
511 {
512 BorderNeedRefresh = true;
513 setsizeneeded = true;
514 setblocks = dsda_IntConfig(dsda_config_screenblocks);
515 }
516
517 void R_MultMatrixVecd(const float matrix[16], const float in[4], float out[4])
518 {
519 int i;
520
521 for (i = 0; i < 4; i++)
522 {
523 out[i] =
524 in[0] * matrix[0*4+i] +
525 in[1] * matrix[1*4+i] +
526 in[2] * matrix[2*4+i] +
527 in[3] * matrix[3*4+i];
528 }
529 }
530
531 int R_Project(float objx, float objy, float objz, float *winx, float *winy, float *winz)
532 {
533 float in[4];
534 float out[4];
535
536 in[0] = objx;
537 in[1] = objy;
538 in[2] = objz;
539 in[3] = 1.0f;
540
541 R_MultMatrixVecd(modelMatrix, in, out);
542 R_MultMatrixVecd(projMatrix, out, in);
543
544 if (in[3] == 0.0f)
545 return false;
546
547 in[0] /= in[3];
548 in[1] /= in[3];
549 in[2] /= in[3];
550
551 /* Map x, y and z to range 0-1 */
552 in[0] = in[0] * 0.5f + 0.5f;
553 in[1] = in[1] * 0.5f + 0.5f;
554 in[2] = in[2] * 0.5f + 0.5f;
555
556 /* Map x,y to viewport */
557 in[0] = in[0] * viewport[2] + viewport[0];
558 in[1] = in[1] * viewport[3] + viewport[1];
559
560 *winx = in[0];
561 *winy = in[1];
562 *winz = in[2];
563
564 return true;
565 }
566
567 void R_SetupViewport(void)
568 {
569 viewport[0] = 0;
570 viewport[1] = (SCREENHEIGHT - viewheight) / 2;
571 viewport[2] = viewwidth;
572 viewport[3] = SCREENHEIGHT;
573 }
574
575 void R_SetupPerspective(float fovy, float aspect, float znear)
576 {
577 int i;
578 float focallength = 1.0f / (float)tan(fovy * (float)M_PI / 360.0f);
579 float *m = projMatrix;
580
581 for (i = 0; i < 16; i++)
582 m[i] = 0.0f;
583
584 m[0] = focallength / aspect;
585 m[5] = focallength;
586 m[10] = -1;
587 m[11] = -1;
588 m[14] = -2 * znear;
589 }
590
591 void R_BuildModelViewMatrix(void)
592 {
593 float x, y, z;
594 float yaw, pitch;
595 float A, B, C, D;
596 float *m = modelMatrix;
597
598 yaw = 270.0f - (float)(viewangle>>ANGLETOFINESHIFT) * 360.0f / FINEANGLES;
599 yaw *= (float)M_PI / 180.0f;
600 pitch = 0;
601 if (V_IsOpenGLMode())
602 {
603 pitch = (float)(viewpitch>>ANGLETOFINESHIFT) * 360.0f / FINEANGLES;
604 pitch *= (float)M_PI / 180.0f;
605 }
606
607 x = (float)viewx / MAP_SCALE;
608 z = -(float)viewy / MAP_SCALE;
609 y = -(float)viewz / MAP_SCALE;
610
611 /*
612 R_LoadIdentity(modelMatrix);
613 R_Rotate(modelMatrix, pitch, 0);
614 R_Rotate(modelMatrix, yaw, 1);
615 R_Translate(modelMatrix, x, y, z);
616 */
617
618 A = (float)cos(pitch);
619 B = (float)sin(pitch);
620 C = (float)cos(yaw);
621 D = (float)sin(yaw);
622
623 m[0] = C;
624 m[1] = D*B;
625 m[2] = -D*A;
626 m[3] = 0;
627
628 m[4] = 0;
629 m[5] = A;
630 m[6] = B;
631 m[7] = 0;
632
633 m[8] = D;
634 m[9] = -C*B;
635 m[10] = A*C;
636 m[11] = 0;
637
638 m[12] = 0;
639 m[13] = 0;
640 m[14] = 0;
641 m[15] = 1;
642
643 m[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
644 m[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
645 m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
646 m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
647 }
648
649 //
650 // R_ExecuteSetViewSize
651 //
652
653 void R_ExecuteSetViewSize (void)
654 {
655 int i;
656 int cheight;
657
658 setsizeneeded = false;
659
660 SetRatio(SCREENWIDTH, SCREENHEIGHT);
661
662 if (setblocks == 11)
663 {
664 viewheight = SCREENHEIGHT;
665 freelookviewheight = viewheight;
666 }
667 // proff 09/24/98: Added for high-res
668 else
669 {
670 viewheight = SCREENHEIGHT - ST_SCALED_HEIGHT;
671 freelookviewheight = SCREENHEIGHT;
672 }
673
674 viewwidth = SCREENWIDTH;
675
676 viewheightfrac = viewheight<<FRACBITS;//e6y
677
678 centery = viewheight/2;
679 centerx = viewwidth/2;
680 centerxfrac = centerx<<FRACBITS;
681 centeryfrac = centery<<FRACBITS;
682
683 if (tallscreen)
684 {
685 wide_centerx = centerx;
686 cheight = SCREENHEIGHT * ratio_multiplier / ratio_scale;
687 }
688 else
689 {
690 wide_centerx = centerx * ratio_multiplier / ratio_scale;
691 cheight = SCREENHEIGHT;
692 }
693
694 // e6y: wide-res
695 projection = wide_centerx<<FRACBITS;
696
697 // proff 11/06/98: Added for high-res
698 // calculate projectiony using int64_t math to avoid overflow when SCREENWIDTH>4228
699 projectiony = (fixed_t)((((int64_t)cheight * centerx * 320) / 200) / SCREENWIDTH * FRACUNIT);
700 // e6y: this is a precalculated value for more precise flats drawing (see R_MapPlane)
701 viewfocratio = projectiony / wide_centerx;
702
703 dsda_SetupStretchParams();
704
705 R_InitBuffer (SCREENWIDTH, viewheight);
706
707 R_InitTextureMapping();
708
709 // psprite scales
710 // proff 08/17/98: Changed for high-res
711 // proff 11/06/98: Added for high-res
712 // e6y: wide-res
713 pspritexscale = (wide_centerx << FRACBITS) / 160;
714 pspriteyscale = (cheight << FRACBITS) / 200;
715 pspriteiscale = FixedDiv (FRACUNIT, pspritexscale);
716 // [FG] make sure that the product of the weapon sprite scale factor
717 // and its reciprocal is always at least FRACUNIT to
718 // fix garbage lines at the top of weapon sprites
719 pspriteiyscale = FixedDiv (FRACUNIT, pspriteyscale);
720 while (FixedMul(pspriteiyscale, pspriteyscale) < FRACUNIT)
721 pspriteiyscale++;
722
723 //e6y: added for GL
724 pspritexscale_f = (float)wide_centerx/160.0f;
725 pspriteyscale_f = (float)cheight / 200.0f;
726
727 skyiscale = (200 << FRACBITS) / SCREENHEIGHT;
728
729 // [RH] Sky height fix for screens not 200 (or 240) pixels tall
730 R_InitSkyMap();
731
732 // thing clipping
733 for (i=0 ; i<viewwidth ; i++)
734 screenheightarray[i] = viewheight;
735
736 for (i=0 ; i<viewwidth ; i++)
737 {
738 fixed_t cosadj = D_abs(finecosine[xtoviewangle[i]>>ANGLETOFINESHIFT]);
739 distscale[i] = FixedDiv(FRACUNIT,cosadj);
740 }
741
742 // e6y
743 // Calculate the light levels to use
744 // for each level / scale combination.
745 // Calculate the light levels to use
746 // for each level / scale combination.
747 for (i=0; i<LIGHTLEVELS; i++)
748 {
749 int j, startmap = ((LIGHTLEVELS-LIGHTBRIGHT-i)*2)*NUMCOLORMAPS/LIGHTLEVELS;
750 for (j=0 ; j<MAXLIGHTSCALE ; j++)
751 {
752 int t, level = startmap - j/DISTMAP;
753
754 if (level < 0)
755 level = 0;
756
757 if (level >= NUMCOLORMAPS)
758 level = NUMCOLORMAPS-1;
759
760 // killough 3/20/98: initialize multiple colormaps
761 level *= 256;
762
763 for (t=0; t<numcolormaps; t++) // killough 4/4/98
764 c_scalelight[t][i][j] = colormaps[t] + level;
765 }
766 }
767
768 I_SetWindowRect();
769 I_SetViewportRect();
770
771 if (V_IsOpenGLMode())
772 dsda_GLSetRenderViewportParams();
773
774 dsda_InitExHud();
775 dsda_BeginRenderStats();
776 }
777
778 //
779 // R_Init
780 //
781
782 void R_Init (void)
783 {
784 // CPhipps - R_DrawColumn isn't constant anymore, so must
785 // initialise in code
786 // current column draw function
787 lprintf(LO_DEBUG, "\nR_LoadTrigTables: ");
788 R_LoadTrigTables();
789 lprintf(LO_DEBUG, "\nR_InitData: ");
790 R_InitData();
791 R_SetViewSize();
792 lprintf(LO_DEBUG, "\nR_Init: R_InitPlanes ");
793 R_InitPlanes();
794 lprintf(LO_DEBUG, "R_InitLightTables ");
795 R_InitLightTables();
796 lprintf(LO_DEBUG, "R_InitSkyMap ");
797 R_InitSkyMap();
798 lprintf(LO_DEBUG, "R_InitTranslationsTables ");
799 R_InitTranslationTables();
800 lprintf(LO_DEBUG, "R_InitPatches ");
801 R_InitPatches();
802 }
803
804 //
805 // R_PointInSubsector
806 //
807 // killough 5/2/98: reformatted, cleaned up
808
809 subsector_t *R_PointInSubsector(fixed_t x, fixed_t y)
810 {
811 int nodenum = numnodes-1;
812
813 // special case for trivial maps (single subsector, no nodes)
814 if (numnodes == 0)
815 return subsectors;
816
817 while (!(nodenum & NF_SUBSECTOR))
818 nodenum = nodes[nodenum].children[R_PointOnSide(x, y, nodes+nodenum)];
819 return &subsectors[nodenum & ~NF_SUBSECTOR];
820 }
821
822 sector_t *R_PointInSector(fixed_t x, fixed_t y)
823 {
824 return R_PointInSubsector(x, y)->sector;
825 }
826
827 void R_SectorCenter(fixed_t *x, fixed_t *y, sector_t *sec)
828 {
829 int i;
830 *x = 0;
831 *y = 0;
832
833 if (!sec->linecount)
834 return;
835
836 for (i = 0; i < sec->linecount; ++i)
837 {
838 *x += sec->lines[i]->v1->x;
839 *y += sec->lines[i]->v1->y;
840 }
841
842 *x /= sec->linecount;
843 *y /= sec->linecount;
844
845 if (R_PointInSector(*x, *y) != sec)
846 {
847 *x = sec->lines[0]->v1->x;
848 *y = sec->lines[0]->v1->y;
849 }
850 }
851
852 void R_LineCenter(fixed_t *x, fixed_t *y, line_t *line)
853 {
854 if (!line->v1 || !line->v2)
855 {
856 *x = 0;
857 *y = 0;
858
859 return;
860 }
861
862 *x = (line->v1->x + line->v2->x) / 2;
863 *y = (line->v1->y + line->v2->y) / 2;
864 }
865
866 //
867 // R_SetupFreelook
868 //
869
870 void R_SetupFreelook(void)
871 {
872 if (V_IsSoftwareMode())
873 {
874 fixed_t InvZtoScale;
875 fixed_t dy;
876 int i;
877
878 centery = viewheight / 2;
879 if (raven || dsda_MouseLook())
880 {
881 dy = FixedMul(focallengthy, finetangent[(ANG90-viewpitch)>>ANGLETOFINESHIFT]);
882 centery += dy >> FRACBITS;
883 }
884 centeryfrac = centery<<FRACBITS;
885
886 InvZtoScale = yaspectmul * centerx;
887 globaluclip = FixedDiv (-centeryfrac, InvZtoScale);
888 globaldclip = FixedDiv ((viewheight<<FRACBITS)-centeryfrac, InvZtoScale);
889
890 for (i=0; i<viewheight; i++)
891 {
892 dy = D_abs(((i-centery)<<FRACBITS)+FRACUNIT/2);
893 yslope[i] = FixedDiv(projectiony, dy);
894 }
895 }
896 }
897
898 //
899 // R_SetupMatrix
900 //
901
902 void R_SetupMatrix(void)
903 {
904 float fovy, aspect, znear;
905 int r_nearclip = 5;
906
907 R_SetupViewport();
908
909 fovy = gl_render_fovy;
910 aspect = gl_render_ratio;
911 znear = (float)r_nearclip / 100.0f;
912
913 R_SetupPerspective(fovy, aspect, znear);
914 R_BuildModelViewMatrix();
915 }
916
917 void R_ResetColorMap(void)
918 {
919 fullcolormap = colormaps[0];
920 fixedcolormap = 0;
921 }
922
923 //
924 // R_SetupFrame
925 //
926
927 static void R_SetupFrame (player_t *player)
928 {
929 dboolean HU_CrosshairEnabled(void);
930
931 int i, cm;
932
933 int FocalTangent = finetangent[FINEANGLES/4 + FieldOfView/2];
934 int extra_brightness = dsda_IntConfig(dsda_config_extra_level_brightness);
935
936 viewplayer = player;
937
938 extralight = player->extralight;
939
940 if (extra_brightness < 0 || extra_brightness > 4) {
941 extra_brightness = 0;
942 }
943 if (extra_brightness != 0) {
944 dsda_TrackFeature(uf_levelbrightness);
945 }
946 extralight += extra_brightness;
947
948 viewsin = finesine[viewangle>>ANGLETOFINESHIFT];
949 viewcos = finecosine[viewangle>>ANGLETOFINESHIFT];
950
951 viewtansin = FixedMul(FocalTangent, viewsin);
952 viewtancos = FixedMul(FocalTangent, viewcos);
953
954 R_SetupFreelook();
955
956 // killough 3/20/98, 4/4/98: select colormap based on player status
957
958 if (player->mo->subsector->sector->colormap)
959 cm = player->mo->subsector->sector->colormap;
960 else if (player->mo->subsector->sector->heightsec != -1)
961 {
962 const sector_t *s = player->mo->subsector->sector->heightsec + sectors;
963 cm = viewz < s->floorheight ? s->bottommap :
964 viewz > s->ceilingheight ? s->topmap :
965 s->midmap;
966 if (cm < 0 || cm > numcolormaps)
967 cm = 0;
968 }
969 else
970 cm = map_info.default_colormap;
971
972 //e6y: save previous and current colormap
973 boom_cm = cm;
974
975 fullcolormap = colormaps[cm];
976 zlight = c_zlight[cm];
977 scalelight = c_scalelight[cm];
978
979 //e6y
980 frame_fixedcolormap = player->fixedcolormap;
981 if (frame_fixedcolormap < 0 || frame_fixedcolormap > NUMCOLORMAPS)
982 {
983 I_Error("<fixedcolormap> value out of range: %d\n", player->fixedcolormap);
984 }
985
986 if (player->fixedcolormap)
987 {
988 // killough 3/20/98: localize scalelightfixed (readability/optimization)
989 static const lighttable_t *scalelightfixed[MAXLIGHTSCALE];
990
991 fixedcolormap = fullcolormap // killough 3/20/98: use fullcolormap
992 + player->fixedcolormap*256*sizeof(lighttable_t);
993
994 walllights = scalelightfixed;
995
996 for (i=0 ; i<MAXLIGHTSCALE ; i++)
997 scalelightfixed[i] = fixedcolormap;
998 }
999 else
1000 fixedcolormap = 0;
1001
1002 R_SetClipPlanes();
1003
1004 if (V_IsOpenGLMode() || HU_CrosshairEnabled())
1005 R_SetupMatrix();
1006
1007 validcount++;
1008 }
1009
1010 static void R_InitDrawScene(void)
1011 {
1012 // Framerate-independent fuzz progression
1013 static int fuzzgametic = 0;
1014 static int savedfuzzpos = 0;
1015
1016 if (V_IsOpenGLMode())
1017 {
1018 // proff 11/99: clear buffers
1019 gld_InitDrawScene();
1020
1021 if (!automap_on)
1022 {
1023 // proff 11/99: switch to perspective mode
1024 gld_StartDrawScene();
1025 }
1026 } else {
1027 if (dsda_IntConfig(dsda_config_flashing_hom))
1028 { // killough 2/10/98: add flashing red HOM indicators
1029 unsigned char color=(gametic % 20) < 9 ? 0xb0 : 0;
1030 V_FillRect(0, 0, 0, viewwidth, viewheight, color);
1031 R_DrawViewBorder();
1032 }
1033
1034 // Only progress software fuzz offset if the gametic has progressed
1035 if (fuzzgametic != gametic)
1036 {
1037 fuzzgametic = gametic;
1038 savedfuzzpos = R_GetFuzzPos();
1039 }
1040 else
1041 {
1042 R_SetFuzzPos(savedfuzzpos);
1043 }
1044 }
1045 }
1046
1047 static void R_RenderBSPNodes(void)
1048 {
1049 // Make displayed player invisible locally
1050 if (localQuakeHappening[displayplayer] && gamestate == GS_LEVEL)
1051 {
1052 players[displayplayer].mo->flags2 |= MF2_DONTDRAW;
1053 R_RenderBSPNode(numnodes - 1); // head node is the last node output
1054 players[displayplayer].mo->flags2 &= ~MF2_DONTDRAW;
1055 }
1056 else
1057 {
1058 // The head node is the last node output.
1059 R_RenderBSPNode(numnodes - 1);
1060 }
1061
1062 if (map_format.zdoom && V_IsOpenGLMode())
1063 {
1064 R_ForceRenderPolyObjs();
1065 }
1066 }
1067
1068 //
1069 // R_RenderView
1070 //
1071
1072 void R_RenderPlayerView (player_t* player)
1073 {
1074 r_frame_count++;
1075
1076 DSDA_ADD_CONTEXT(sf_setup_frame);
1077 R_SetupFrame (player);
1078 DSDA_REMOVE_CONTEXT(sf_setup_frame);
1079
1080 DSDA_ADD_CONTEXT(sf_clear);
1081 R_ClearClipSegs ();
1082 R_ClearDrawSegs ();
1083 R_ClearPlanes ();
1084 R_ClearSprites ();
1085 DSDA_REMOVE_CONTEXT(sf_clear);
1086
1087 DSDA_ADD_CONTEXT(sf_init_scene);
1088 R_InitDrawScene();
1089 DSDA_REMOVE_CONTEXT(sf_init_scene);
1090
1091 FakeNetUpdate();
1092
1093 if (V_IsOpenGLMode()) {
1094 DSDA_ADD_CONTEXT(sf_gl_frustum);
1095 gld_FrustumSetup();
1096 DSDA_REMOVE_CONTEXT(sf_gl_frustum);
1097 }
1098
1099 DSDA_ADD_CONTEXT(sf_bsp_nodes);
1100 R_RenderBSPNodes();
1101 DSDA_REMOVE_CONTEXT(sf_bsp_nodes);
1102
1103 FakeNetUpdate();
1104
1105 if (V_IsSoftwareMode())
1106 {
1107 DSDA_ADD_CONTEXT(sf_draw_planes);
1108 R_DrawPlanes();
1109 DSDA_REMOVE_CONTEXT(sf_draw_planes);
1110 }
1111
1112 DSDA_ADD_CONTEXT(sf_reset_column_buffer);
1113 R_ResetColumnBuffer();
1114 DSDA_REMOVE_CONTEXT(sf_reset_column_buffer);
1115
1116 FakeNetUpdate();
1117
1118 if (V_IsSoftwareMode()) {
1119 DSDA_ADD_CONTEXT(sf_draw_masked);
1120 R_DrawMasked ();
1121 R_ResetColumnBuffer();
1122 DSDA_REMOVE_CONTEXT(sf_draw_masked);
1123 }
1124
1125 FakeNetUpdate();
1126
1127 if (V_IsOpenGLMode() && !automap_on) {
1128 DSDA_ADD_CONTEXT(sf_draw_scene);
1129 gld_DrawScene(player);
1130 gld_EndDrawScene();
1131 DSDA_REMOVE_CONTEXT(sf_draw_scene);
1132 }
1133 }