Debian Codegraph @

prboom2/src/r_segs.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-2004 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 * All the clipping: columns, horizontal spans, sky columns.
31 *
32 *-----------------------------------------------------------------------------*/
33 //
34 // 4/25/98, 5/2/98 killough: reformatted, beautified
35
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #include <math.h>
41
42 #include "SDL.h"
43
44 #include "doomstat.h"
45 #include "p_spec.h"
46 #include "r_main.h"
47 #include "r_bsp.h"
48 #include "r_segs.h"
49 #include "r_plane.h"
50 #include "r_things.h"
51 #include "r_draw.h"
52 #include "w_wad.h"
53 #include "v_video.h"
54 #include "lprintf.h"
55
56 #include "dsda/mapinfo.h"
57 #include "dsda/render_stats.h"
58
59 // OPTIMIZE: closed two sided lines as single sided
60
61 // killough 1/6/98: replaced globals with statics where appropriate
62
63 // True if any of the segs textures might be visible.
64 static dboolean segtextured;
65 static dboolean markfloor; // False if the back side is the same plane.
66 static dboolean markceiling;
67 static dboolean maskedtexture;
68 static int toptexture;
69 static int bottomtexture;
70 static int midtexture;
71
72 static fixed_t toptexheight, midtexheight, bottomtexheight; // cph
73
74 angle_t rw_normalangle; // angle to line origin
75 int rw_angle1;
76 fixed_t rw_distance;
77 const lighttable_t **walllights;
78
79 //
80 // regular wall
81 //
82 static int rw_x;
83 static int rw_stopx;
84 static angle_t rw_centerangle;
85 static fixed_t rw_offset;
86 static fixed_t rw_scale;
87 static fixed_t rw_scalestep;
88 static fixed_t rw_midtexturemid;
89 static fixed_t rw_toptexturemid;
90 static fixed_t rw_bottomtexturemid;
91 static int rw_lightlevel;
92 static int worldtop;
93 static int worldbottom;
94 static int worldhigh;
95 static int worldlow;
96 static int64_t pixhigh; // R_WiggleFix
97 static int64_t pixlow; // R_WiggleFix
98 static fixed_t pixhighstep;
99 static fixed_t pixlowstep;
100 static int64_t topfrac; // R_WiggleFix
101 static fixed_t topstep;
102 static int64_t bottomfrac; // R_WiggleFix
103 static fixed_t bottomstep;
104 static int *maskedtexturecol; // dropoff overflow
105
106 static int max_rwscale = 64 * FRACUNIT;
107 static int HEIGHTBITS = 12;
108 static int HEIGHTUNIT = (1 << 12);
109 static int invhgtbits = 4;
110
111 /* cph - allow crappy fake contrast to be disabled */
112 fake_contrast_mode_t fake_contrast_mode;
113
114 //
115 // R_FixWiggle()
116 // Dynamic wall/texture rescaler, AKA "WiggleHack II"
117 // by Kurt "kb1" Baumgardner ("kb")
118 //
119 // [kb] When the rendered view is positioned, such that the viewer is
120 // looking almost parallel down a wall, the result of the scale
121 // calculation in R_ScaleFromGlobalAngle becomes very large. And, the
122 // taller the wall, the larger that value becomes. If these large
123 // values were used as-is, subsequent calculations would overflow
124 // and crash the program.
125 //
126 // Therefore, vanilla Doom clamps this scale calculation, preventing it
127 // from becoming larger than 0x400000 (64*FRACUNIT). This number was
128 // chosen carefully, to allow reasonably-tight angles, with reasonably
129 // tall sectors to be rendered, within the limits of the fixed-point
130 // math system being used. When the scale gets clamped, Doom cannot
131 // properly render the wall, causing an undesirable wall-bending
132 // effect that I call "floor wiggle".
133 //
134 // Modern source ports offer higher video resolutions, which worsens
135 // the issue. And, Doom is simply not adjusted for the taller walls
136 // found in many PWADs.
137 //
138 // WiggleHack II attempts to correct these issues, by dynamically
139 // adjusting the fixed-point math, and the maximum scale clamp,
140 // on a wall-by-wall basis. This has 2 effects:
141 //
142 // 1. Floor wiggle is greatly reduced and/or eliminated.
143 // 2. Overflow is not longer possible, even in levels with maximum
144 // height sectors.
145 //
146 // It is not perfect across all situations. Some floor wiggle can be
147 // seen, and some texture strips may be slight misaligned in extreme
148 // cases. These effects cannot be corrected without increasing the
149 // precision of various renderer variables, and, possibly, suffering
150 // a performance penalty.
151 //
152
153 void R_FixWiggle(sector_t *sec)
154 {
155 static int lastheight = 0;
156
157 static const struct
158 {
159 int clamp;
160 int heightbits;
161 } scale_values[9] = {
162 {2048 * FRACUNIT, 12}, {1024 * FRACUNIT, 12}, {1024 * FRACUNIT, 11},
163 { 512 * FRACUNIT, 11}, { 512 * FRACUNIT, 10}, { 256 * FRACUNIT, 10},
164 { 256 * FRACUNIT, 9}, { 128 * FRACUNIT, 9}, { 64 * FRACUNIT, 9},
165 };
166
167 int height = (sec->ceilingheight - sec->floorheight) >> FRACBITS;
168
169 // disallow negative heights, force cache initialization
170 if (height < 1)
171 height = 1;
172
173 // early out?
174 if (height != lastheight)
175 {
176 lastheight = height;
177
178 // initialize, or handle moving sector
179 if (height != sec->cachedheight)
180 {
181 frontsector->cachedheight = height;
182 frontsector->scaleindex = 0;
183 height >>= 7;
184 // calculate adjustment
185 while ((height >>= 1))
186 frontsector->scaleindex++;
187 }
188
189 // fine-tune renderer for this wall
190 max_rwscale = scale_values[frontsector->scaleindex].clamp;
191 HEIGHTBITS = scale_values[frontsector->scaleindex].heightbits;
192 HEIGHTUNIT = 1 << HEIGHTBITS;
193 invhgtbits = 16 - HEIGHTBITS;
194 }
195 }
196
197 //
198 // R_ScaleFromGlobalAngle
199 // Returns the texture mapping scale
200 // for the current line (horizontal span)
201 // at the given angle.
202 // rw_distance must be calculated first.
203 //
204 // killough 5/2/98: reformatted, cleaned up
205 // CPhipps - moved here from r_main.c
206
207 static fixed_t R_ScaleFromGlobalAngle(angle_t visangle)
208 {
209 angle_t anglea = ANG90 + (visangle - viewangle);
210 angle_t angleb = ANG90 + (visangle - rw_normalangle);
211 int den = FixedMul(rw_distance, finesine[anglea >> ANGLETOFINESHIFT]);
212 // proff 11/06/98: Changed for high-res
213 fixed_t num = FixedMul(projectiony, finesine[angleb >> ANGLETOFINESHIFT]);
214 fixed_t scale;
215
216 if (den > (num >> 16))
217 {
218 scale = FixedDiv(num, den);
219
220 // [kb] use R_WiggleFix clamp
221 if (scale > max_rwscale)
222 scale = max_rwscale;
223 else if (scale < 256)
224 scale = 256;
225 }
226 else
227 scale = max_rwscale;
228
229 return scale;
230 }
231
232 const int fake_contrast_value = 16;
233
234 static dboolean R_FakeContrast(seg_t *seg)
235 {
236 return fake_contrast_mode != FAKE_CONTRAST_MODE_OFF &&
237 !(map_info.flags & MI_EVEN_LIGHTING) &&
238 seg && !(seg->sidedef->flags & SF_NOFAKECONTRAST) && !hexen;
239 }
240
241 static dboolean R_SmoothLighting(seg_t *seg)
242 {
243 return fake_contrast_mode == FAKE_CONTRAST_MODE_SMOOTH ||
244 map_info.flags & MI_SMOOTH_LIGHTING ||
245 seg->sidedef->flags & SF_SMOOTHLIGHTING;
246 }
247
248 void R_AddContrast(seg_t *seg, int *base_lightlevel)
249 {
250 /* cph - ...what is this for? adding contrast to rooms?
251 * It looks crap in outdoor areas */
252 if (R_FakeContrast(seg))
253 {
254 if (seg->linedef->dy == 0)
255 {
256 *base_lightlevel -= fake_contrast_value;
257 }
258 else if (seg->linedef->dx == 0)
259 {
260 *base_lightlevel += fake_contrast_value;
261 }
262 else if (R_SmoothLighting(seg))
263 {
264 double dx, dy;
265
266 dx = (double) seg->linedef->dx / FRACUNIT;
267 dy = (double) seg->linedef->dy / FRACUNIT;
268
269 *base_lightlevel +=
270 lround(fabs(atan(dy / dx) * 2 / M_PI) * (2 * fake_contrast_value) - fake_contrast_value);
271 }
272 };
273 }
274
275 const lighttable_t** GetLightTable(int lightlevel)
276 {
277 int lightnum;
278
279 R_AddContrast(curline, &lightlevel);
280
281 lightnum = (lightlevel >> LIGHTSEGSHIFT) + (extralight * LIGHTBRIGHT);
282
283 return scalelight[BETWEEN(0, LIGHTLEVELS - 1, lightnum)];
284 }
285
286 static void R_UpdateWallLights(int lightlevel)
287 {
288 walllights = GetLightTable(lightlevel);
289 }
290
291 static int R_SideLightLevel(side_t *side, int base_lightlevel)
292 {
293 return side->lightlevel +
294 ((side->flags & SF_LIGHTABSOLUTE) ? 0 : base_lightlevel);
295 }
296
297 int R_TopLightLevel(side_t *side, int base_lightlevel)
298 {
299 return side->lightlevel_top +
300 ((side->flags & SF_LIGHTABSOLUTETOP) ? 0 : R_SideLightLevel(side, base_lightlevel));
301 }
302
303 int R_MidLightLevel(side_t *side, int base_lightlevel)
304 {
305 return side->lightlevel_mid +
306 ((side->flags & SF_LIGHTABSOLUTEMID) ? 0 : R_SideLightLevel(side, base_lightlevel));
307 }
308
309 int R_BottomLightLevel(side_t *side, int base_lightlevel)
310 {
311 return side->lightlevel_bottom +
312 ((side->flags & SF_LIGHTABSOLUTEBOTTOM) ? 0 : R_SideLightLevel(side, base_lightlevel));
313 }
314
315 static void R_ApplyTopLight(side_t *side)
316 {
317 int lightlevel;
318
319 lightlevel = R_TopLightLevel(side, rw_lightlevel);
320
321 R_UpdateWallLights(lightlevel);
322 }
323
324 static void R_ApplyMidLight(side_t *side)
325 {
326 int lightlevel;
327
328 lightlevel = R_MidLightLevel(side, rw_lightlevel);
329
330 R_UpdateWallLights(lightlevel);
331 }
332
333 static void R_ApplyBottomLight(side_t *side)
334 {
335 int lightlevel;
336
337 lightlevel = R_BottomLightLevel(side, rw_lightlevel);
338
339 R_UpdateWallLights(lightlevel);
340 }
341
342 static void R_ApplyLightColormap(draw_column_vars_t *dcvars, fixed_t scale)
343 {
344 if (!fixedcolormap)
345 {
346 int index = (int)(((int64_t) scale * 160 / wide_centerx) >> LIGHTSCALESHIFT);
347 if (index >= MAXLIGHTSCALE)
348 index = MAXLIGHTSCALE - 1;
349
350 dcvars->colormap = walllights[index];
351 }
352 else
353 {
354 dcvars->colormap = fixedcolormap;
355 }
356 }
357
358 //
359 // R_RenderMaskedSegRange
360 //
361
362 void R_RenderMaskedSegRange(drawseg_t *ds, int x1, int x2)
363 {
364 int texnum;
365 sector_t tempsec; // killough 4/13/98
366 const rpatch_t *patch;
367 R_DrawColumn_f colfunc;
368 draw_column_vars_t dcvars;
369
370 R_SetDefaultDrawColumnVars(&dcvars);
371
372 // Calculate light table.
373 // Use different light tables
374 // for horizontal / vertical / diagonal. Diagonal?
375
376 curline = ds->curline; // OPTIMIZE: get rid of LIGHTSEGSHIFT globally
377
378 // killough 4/11/98: draw translucent 2s normal textures
379
380 colfunc = R_GetDrawColumnFunc(RDC_PIPELINE_STANDARD, RDRAW_FILTER_POINT);
381 if (curline->linedef->tranmap)
382 {
383 colfunc = R_GetDrawColumnFunc(RDC_PIPELINE_TRANSLUCENT, RDRAW_FILTER_POINT);
384 tranmap = curline->linedef->tranmap;
385 }
386 // killough 4/11/98: end translucent 2s normal code
387
388 frontsector = curline->frontsector;
389 backsector = curline->backsector;
390
391 // cph 2001/11/25 - middle textures did not animate in v1.2
392 texnum = curline->sidedef->midtexture;
393 if (raven || !comp[comp_maskedanim])
394 texnum = texturetranslation[texnum];
395
396 // killough 4/13/98: get correct lightlevel for 2s normal textures
397 rw_lightlevel = R_FakeFlat(frontsector, &tempsec, NULL, NULL, false) ->lightlevel;
398 R_ApplyMidLight(curline->sidedef);
399
400 maskedtexturecol = ds->maskedtexturecol;
401
402 rw_scalestep = ds->scalestep;
403 spryscale = ds->scale1 + (x1 - ds->x1)*rw_scalestep;
404 mfloorclip = ds->sprbottomclip;
405 mceilingclip = ds->sprtopclip;
406
407 // find positioning
408 if (curline->linedef->flags & ML_DONTPEGBOTTOM)
409 {
410 dcvars.texturemid = frontsector->floorheight > backsector->floorheight
411 ? frontsector->floorheight : backsector->floorheight;
412 dcvars.texturemid = dcvars.texturemid + textureheight[texnum] - viewz;
413 }
414 else
415 {
416 dcvars.texturemid =frontsector->ceilingheight<backsector->ceilingheight
417 ? frontsector->ceilingheight : backsector->ceilingheight;
418 dcvars.texturemid = dcvars.texturemid - viewz;
419 }
420
421 dcvars.texturemid += curline->sidedef->rowoffset + curline->sidedef->rowoffset_mid;
422
423 patch = R_TextureCompositePatchByNum(texnum);
424
425 // draw the columns
426 for (dcvars.x = x1 ; dcvars.x <= x2 ; dcvars.x++, spryscale += rw_scalestep)
427 if (maskedtexturecol[dcvars.x] != INT_MAX) // dropoff overflow
428 {
429 fixed_t texturecolumn;
430
431 R_ApplyLightColormap(&dcvars, spryscale);
432
433 // killough 3/2/98:
434 //
435 // This calculation used to overflow and cause crashes in Doom:
436 //
437 // sprtopscreen = centeryfrac - FixedMul(dcvars.texturemid, spryscale);
438 //
439 // This code fixes it, by using double-precision intermediate
440 // arithmetic and by skipping the drawing of 2s normals whose
441 // mapping to screen coordinates is totally out of range:
442
443 {
444 int64_t t = ((int64_t) centeryfrac << FRACBITS) -
445 (int64_t) dcvars.texturemid * spryscale;
446 if (t + (int64_t) textureheight[texnum] * spryscale < 0 ||
447 t > (int64_t) SCREENHEIGHT << FRACBITS*2)
448 continue; // skip if the texture is out of screen's range
449 sprtopscreen = (int64_t)(t >> FRACBITS); // R_WiggleFix
450 }
451
452 dcvars.iscale = 0xffffffffu / (unsigned) spryscale;
453
454 texturecolumn = maskedtexturecol[dcvars.x] +
455 (curline->sidedef->textureoffset_mid >> FRACBITS);
456
457 // killough 1/25/98: here's where Medusa came in, because
458 // it implicitly assumed that the column was all one patch.
459 // Originally, Doom did not construct complete columns for
460 // multipatched textures, so there were no header or trailer
461 // bytes in the column referred to below, which explains
462 // the Medusa effect. The fix is to construct true columns
463 // when forming multipatched textures (see r_data.c).
464
465 // draw the texture
466 R_DrawMaskedColumn(
467 patch,
468 colfunc,
469 &dcvars,
470 R_GetPatchColumnWrapped(patch, texturecolumn),
471 R_GetPatchColumnWrapped(patch, texturecolumn-1),
472 R_GetPatchColumnWrapped(patch, texturecolumn+1)
473 );
474
475 maskedtexturecol[dcvars.x] = INT_MAX; // dropoff overflow
476 }
477
478 curline = NULL; /* cph 2001/11/18 - must clear curline now we're done with it, so R_ColourMap doesn't try using it for other things */
479 }
480
481 //
482 // R_RenderSegLoop
483 // Draws zero, one, or two textures (and possibly a masked texture) for walls.
484 // Can draw or mark the starting pixel of floor and ceiling textures.
485 // CALLED: CORE LOOPING ROUTINE.
486 //
487
488 static int didsolidcol; /* True if at least one column was marked solid */
489
490 static void R_RenderSegLoop (void)
491 {
492 const rpatch_t *tex_patch;
493 R_DrawColumn_f colfunc = R_GetDrawColumnFunc(RDC_PIPELINE_STANDARD, RDRAW_FILTER_POINT);
494 draw_column_vars_t dcvars;
495 fixed_t texturecolumn = 0;
496 fixed_t specific_texturecolumn = 0;
497
498 R_SetDefaultDrawColumnVars(&dcvars);
499
500 dsda_RecordDrawSeg();
501
502 for ( ; rw_x < rw_stopx ; rw_x++)
503 {
504 // mark floor / ceiling areas
505 int yh = (int)(bottomfrac>>HEIGHTBITS);
506 int yl = (int)((topfrac+HEIGHTUNIT-1)>>HEIGHTBITS);
507
508 // no space above wall?
509 int bottom,top = ceilingclip[rw_x]+1;
510
511 if (yl < top)
512 yl = top;
513
514 if (markceiling)
515 {
516 bottom = yl-1;
517
518 if (bottom >= floorclip[rw_x])
519 bottom = floorclip[rw_x]-1;
520
521 if (top <= bottom)
522 {
523 ceilingplane->top[rw_x] = top;
524 ceilingplane->bottom[rw_x] = bottom;
525 }
526 // SoM: this should be set here
527 ceilingclip[rw_x] = bottom;
528 }
529
530 bottom = floorclip[rw_x]-1;
531 if (yh > bottom)
532 yh = bottom;
533
534 if (markfloor)
535 {
536
537 top = yh < ceilingclip[rw_x] ? ceilingclip[rw_x] : yh;
538
539 if (++top <= bottom)
540 {
541 floorplane->top[rw_x] = top;
542 floorplane->bottom[rw_x] = bottom;
543 }
544 // SoM: This should be set here to prevent overdraw
545 floorclip[rw_x] = top;
546 }
547
548 // texturecolumn and lighting are independent of wall tiers
549 if (segtextured)
550 {
551 // calculate texture offset
552 angle_t angle =(rw_centerangle+xtoviewangle[rw_x])>>ANGLETOFINESHIFT;
553
554 texturecolumn = rw_offset-FixedMul(finetangent[angle],rw_distance);
555 texturecolumn >>= FRACBITS;
556
557 dcvars.x = rw_x;
558 dcvars.iscale = 0xffffffffu / (unsigned)rw_scale;
559 }
560
561 // draw the wall tiers
562 if (midtexture)
563 {
564 specific_texturecolumn = texturecolumn +
565 (curline->sidedef->textureoffset_mid >> FRACBITS);
566
567 dcvars.yl = yl; // single sided line
568 dcvars.yh = yh;
569 dcvars.texturemid = rw_midtexturemid;
570 tex_patch = R_TextureCompositePatchByNum(midtexture);
571 dcvars.source = R_GetTextureColumn(tex_patch, specific_texturecolumn);
572 dcvars.prevsource = R_GetTextureColumn(tex_patch, specific_texturecolumn-1);
573 dcvars.nextsource = R_GetTextureColumn(tex_patch, specific_texturecolumn+1);
574 dcvars.texheight = midtexheight;
575 if (!fixedcolormap)
576 R_ApplyMidLight(curline->sidedef);
577 R_ApplyLightColormap(&dcvars, rw_scale);
578 colfunc(&dcvars);
579 tex_patch = NULL;
580 ceilingclip[rw_x] = viewheight;
581 floorclip[rw_x] = -1;
582 }
583 else
584 {
585 // two sided line
586 if (toptexture)
587 {
588 // top wall
589 int mid = (int)(pixhigh>>HEIGHTBITS);
590 pixhigh += pixhighstep;
591
592 if (mid >= floorclip[rw_x])
593 mid = floorclip[rw_x]-1;
594
595 if (mid >= yl)
596 {
597 specific_texturecolumn = texturecolumn +
598 (curline->sidedef->textureoffset_top >> FRACBITS);
599
600 dcvars.yl = yl;
601 dcvars.yh = mid;
602 dcvars.texturemid = rw_toptexturemid;
603 tex_patch = R_TextureCompositePatchByNum(toptexture);
604 dcvars.source = R_GetTextureColumn(tex_patch,specific_texturecolumn);
605 dcvars.prevsource = R_GetTextureColumn(tex_patch,specific_texturecolumn-1);
606 dcvars.nextsource = R_GetTextureColumn(tex_patch,specific_texturecolumn+1);
607 dcvars.texheight = toptexheight;
608 if (!fixedcolormap)
609 R_ApplyTopLight(curline->sidedef);
610 R_ApplyLightColormap(&dcvars, rw_scale);
611 colfunc(&dcvars);
612 tex_patch = NULL;
613 ceilingclip[rw_x] = mid;
614 }
615 else
616 ceilingclip[rw_x] = yl-1;
617 }
618 else // no top wall
619 {
620 if (markceiling)
621 ceilingclip[rw_x] = yl-1;
622 }
623
624 if (bottomtexture) // bottom wall
625 {
626 int mid = (int)((pixlow+HEIGHTUNIT-1)>>HEIGHTBITS);
627 pixlow += pixlowstep;
628
629 // no space above wall?
630 if (mid <= ceilingclip[rw_x])
631 mid = ceilingclip[rw_x]+1;
632
633 if (mid <= yh)
634 {
635 specific_texturecolumn = texturecolumn +
636 (curline->sidedef->textureoffset_bottom >> FRACBITS);
637
638 dcvars.yl = mid;
639 dcvars.yh = yh;
640 dcvars.texturemid = rw_bottomtexturemid;
641 tex_patch = R_TextureCompositePatchByNum(bottomtexture);
642 dcvars.source = R_GetTextureColumn(tex_patch, specific_texturecolumn);
643 dcvars.prevsource = R_GetTextureColumn(tex_patch, specific_texturecolumn-1);
644 dcvars.nextsource = R_GetTextureColumn(tex_patch, specific_texturecolumn+1);
645 dcvars.texheight = bottomtexheight;
646 if (!fixedcolormap)
647 R_ApplyBottomLight(curline->sidedef);
648 R_ApplyLightColormap(&dcvars, rw_scale);
649 colfunc(&dcvars);
650 tex_patch = NULL;
651 floorclip[rw_x] = mid;
652 }
653 else
654 floorclip[rw_x] = yh+1;
655 }
656 else // no bottom wall
657 {
658 if (markfloor)
659 floorclip[rw_x] = yh+1;
660 }
661
662 // cph - if we completely blocked further sight through this column,
663 // add this info to the solid columns array for r_bsp.c
664 if ((markceiling || markfloor) && (floorclip[rw_x] <= ceilingclip[rw_x] + 1))
665 {
666 solidcol[rw_x] = 1; didsolidcol = 1;
667 }
668
669 // save texturecol for backdrawing of masked mid texture
670 if (maskedtexture)
671 maskedtexturecol[rw_x] = texturecolumn;
672 }
673
674 rw_scale += rw_scalestep;
675 topfrac += topstep;
676 bottomfrac += bottomstep;
677 }
678 }
679
680 //
681 // R_StoreWallRange
682 // A wall segment will be drawn
683 // between start and stop pixels (inclusive).
684 //
685 void R_StoreWallRange(const int start, const int stop)
686 {
687 const int shift_bits = 1;
688 int64_t dx, dy, dx1, dy1, len, dist;
689
690 if (ds_p == drawsegs+maxdrawsegs) // killough 1/98 -- fix 2s line HOM
691 {
692 unsigned pos = ds_p - drawsegs; // jff 8/9/98 fix from ZDOOM1.14a
693 unsigned newmax = maxdrawsegs ? maxdrawsegs*2 : 128; // killough
694 drawsegs = Z_Realloc(drawsegs,newmax*sizeof(*drawsegs));
695 ds_p = drawsegs + pos; // jff 8/9/98 fix from ZDOOM1.14a
696 maxdrawsegs = newmax;
697 }
698
699 if(curline->linedef)
700 curline->linedef->flags |= ML_MAPPED;
701
702 if (V_IsOpenGLMode())
703 {
704 // proff 11/99: the rest of the calculations is not needed for OpenGL
705 ds_p++->curline = curline;
706 gld_AddWall(curline);
707
708 return;
709 }
710
711 #ifdef RANGECHECK
712 if (start >=viewwidth || start > stop)
713 I_Error ("Bad R_RenderWallRange: %i to %i", start , stop);
714 #endif
715
716 sidedef = curline->sidedef;
717 linedef = curline->linedef;
718
719 // mark the segment as visible for auto map
720 linedef->flags |= ML_MAPPED;
721
722 // calculate rw_distance for scale calculation
723 rw_normalangle = curline->pangle + ANG90; // [crispy] use re-calculated angle
724
725 // [Linguica] Fix long wall error
726 // shift right to avoid possibility of int64 overflow in rw_distance calculation
727 dx = ((int64_t)curline->v2->px - curline->v1->px) >> shift_bits;
728 dy = ((int64_t)curline->v2->py - curline->v1->py) >> shift_bits;
729 dx1 = ((int64_t)viewx - curline->v1->px) >> shift_bits;
730 dy1 = ((int64_t)viewy - curline->v1->py) >> shift_bits;
731 len = curline->halflength; // No need to shift
732
733 dist = (((dy * dx1 - dx * dy1) / len) << shift_bits);
734 rw_distance = (fixed_t)BETWEEN(INT_MIN, INT_MAX, dist);
735
736 ds_p->x1 = rw_x = start;
737 ds_p->x2 = stop;
738 ds_p->curline = curline;
739 rw_stopx = stop+1;
740
741 { // killough 1/6/98, 2/1/98: remove limit on openings
742 extern int *openings; // dropoff overflow
743 extern size_t maxopenings;
744 size_t pos = lastopening - openings;
745 size_t need = (rw_stopx - start)*sizeof(*lastopening) + pos;
746 if (need > maxopenings)
747 {
748 drawseg_t *ds; //jff 8/9/98 needed for fix from ZDoom
749 int *oldopenings = openings; // dropoff overflow
750 int *oldlast = lastopening; // dropoff overflow
751
752 do
753 maxopenings = maxopenings ? maxopenings*2 : 16384;
754 while (need > maxopenings);
755 openings = Z_Realloc(openings, maxopenings * sizeof(*openings));
756 lastopening = openings + pos;
757
758 // jff 8/9/98 borrowed fix for openings from ZDOOM1.14
759 // [RH] We also need to adjust the openings pointers that
760 // were already stored in drawsegs.
761 for (ds = drawsegs; ds < ds_p; ds++)
762 {
763 #define ADJUST(p) if (ds->p + ds->x1 >= oldopenings && ds->p + ds->x1 <= oldlast)\
764 ds->p = ds->p - oldopenings + openings;
765 ADJUST (maskedtexturecol);
766 ADJUST (sprtopclip);
767 ADJUST (sprbottomclip);
768 }
769 #undef ADJUST
770 }
771 } // killough: end of code to remove limits on openings
772
773 worldtop = frontsector->ceilingheight - viewz;
774 worldbottom = frontsector->floorheight - viewz;
775
776 R_FixWiggle(frontsector);
777
778 // calculate scale at both ends and step
779
780 ds_p->scale1 = rw_scale =
781 R_ScaleFromGlobalAngle (viewangle + xtoviewangle[start]);
782
783 if (stop > start)
784 {
785 ds_p->scale2 = R_ScaleFromGlobalAngle (viewangle + xtoviewangle[stop]);
786 ds_p->scalestep = rw_scalestep = (ds_p->scale2-rw_scale) / (stop-start);
787 }
788 else
789 ds_p->scale2 = ds_p->scale1;
790
791 // calculate texture boundaries
792 // and decide if floor / ceiling marks are needed
793
794 midtexture = toptexture = bottomtexture = maskedtexture = 0;
795 ds_p->maskedtexturecol = NULL;
796
797 if (!backsector)
798 {
799 // single sided line
800 midtexture = texturetranslation[sidedef->midtexture];
801 midtexheight = (linedef->r_flags & RF_MID_TILE) ? 0 : textureheight[midtexture] >> FRACBITS;
802
803 // a single sided line is terminal, so it must mark ends
804 markfloor = markceiling = true;
805
806 if (linedef->flags & ML_DONTPEGBOTTOM)
807 { // bottom of texture at bottom
808 fixed_t vtop = frontsector->floorheight +
809 textureheight[sidedef->midtexture];
810 rw_midtexturemid = vtop - viewz;
811 }
812 else // top of texture at top
813 rw_midtexturemid = worldtop;
814
815 rw_midtexturemid += FixedMod(sidedef->rowoffset + sidedef->rowoffset_mid,
816 textureheight[midtexture]);
817
818 ds_p->silhouette = SIL_BOTH;
819 ds_p->sprtopclip = screenheightarray;
820 ds_p->sprbottomclip = negonearray;
821 ds_p->bsilheight = INT_MAX;
822 ds_p->tsilheight = INT_MIN;
823 }
824 else // two sided line
825 {
826 ds_p->sprtopclip = ds_p->sprbottomclip = NULL;
827 ds_p->silhouette = 0;
828
829 if (linedef->r_flags & RF_CLOSED)
830 { /* cph - closed 2S line e.g. door */
831 // cph - killough's (outdated) comment follows - this deals with both
832 // "automap fixes", his and mine
833 // killough 1/17/98: this test is required if the fix
834 // for the automap bug (r_bsp.c) is used, or else some
835 // sprites will be displayed behind closed doors. That
836 // fix prevents lines behind closed doors with dropoffs
837 // from being displayed on the automap.
838
839 ds_p->silhouette = SIL_BOTH;
840 ds_p->sprbottomclip = negonearray;
841 ds_p->bsilheight = INT_MAX;
842 ds_p->sprtopclip = screenheightarray;
843 ds_p->tsilheight = INT_MIN;
844 }
845 else
846 { /* not solid - old code */
847 if (frontsector->floorheight > backsector->floorheight)
848 {
849 ds_p->silhouette = SIL_BOTTOM;
850 ds_p->bsilheight = frontsector->floorheight;
851 }
852 else if (backsector->floorheight > viewz)
853 {
854 ds_p->silhouette = SIL_BOTTOM;
855 ds_p->bsilheight = INT_MAX;
856 }
857
858 if (frontsector->ceilingheight < backsector->ceilingheight)
859 {
860 ds_p->silhouette |= SIL_TOP;
861 ds_p->tsilheight = frontsector->ceilingheight;
862 }
863 else if (backsector->ceilingheight < viewz)
864 {
865 ds_p->silhouette |= SIL_TOP;
866 ds_p->tsilheight = INT_MIN;
867 }
868 }
869
870 worldhigh = backsector->ceilingheight - viewz;
871 worldlow = backsector->floorheight - viewz;
872
873 // hack to allow height changes in outdoor areas
874 if (frontsector->ceilingpic == skyflatnum
875 && backsector->ceilingpic == skyflatnum)
876 worldtop = worldhigh;
877
878 markfloor = worldlow != worldbottom
879 || P_FloorPlanesDiffer(frontsector, backsector)
880
881 // killough 4/15/98: prevent 2s normals
882 // from bleeding through deep water
883 || frontsector->heightsec != -1
884
885 || (sidedef->midtexture && (sidedef->flags & SF_CLIPMIDTEX || linedef->flags & ML_CLIPMIDTEX))
886 ;
887
888 markceiling = worldhigh != worldtop
889 || P_CeilingPlanesDiffer(frontsector, backsector)
890
891 // killough 4/15/98: prevent 2s normals
892 // from bleeding through fake ceilings
893 || (frontsector->heightsec != -1 &&
894 frontsector->ceilingpic!=skyflatnum)
895
896 || (sidedef->midtexture && (sidedef->flags & SF_CLIPMIDTEX || linedef->flags & ML_CLIPMIDTEX))
897 ;
898
899 if (backsector->ceilingheight <= frontsector->floorheight
900 || backsector->floorheight >= frontsector->ceilingheight)
901 markceiling = markfloor = true; // closed door
902
903 if (worldhigh < worldtop) // top texture
904 {
905 toptexture = texturetranslation[sidedef->toptexture];
906 toptexheight = (linedef->r_flags & RF_TOP_TILE) ? 0 : textureheight[toptexture] >> FRACBITS;
907 rw_toptexturemid = linedef->flags & ML_DONTPEGTOP ? worldtop :
908 backsector->ceilingheight+textureheight[sidedef->toptexture]-viewz;
909 rw_toptexturemid += FixedMod(sidedef->rowoffset + sidedef->rowoffset_top,
910 textureheight[toptexture]);
911 }
912
913 if (worldlow > worldbottom) // bottom texture
914 {
915 bottomtexture = texturetranslation[sidedef->bottomtexture];
916 bottomtexheight = (linedef->r_flags & RF_BOT_TILE) ? 0 : textureheight[bottomtexture] >> FRACBITS;
917 rw_bottomtexturemid = linedef->flags & ML_DONTPEGBOTTOM ? worldtop :
918 worldlow;
919 rw_bottomtexturemid += FixedMod(sidedef->rowoffset + sidedef->rowoffset_bottom,
920 textureheight[bottomtexture]);
921 }
922
923 // allocate space for masked texture tables
924 if (sidedef->midtexture) // masked midtexture
925 {
926 maskedtexture = true;
927 ds_p->maskedtexturecol = maskedtexturecol = lastopening - rw_x;
928 lastopening += rw_stopx - rw_x;
929 }
930 }
931
932 // calculate rw_offset (only needed for textured lines)
933 segtextured = midtexture | toptexture | bottomtexture | maskedtexture;
934
935 if (segtextured)
936 {
937 rw_offset = (fixed_t)(((dx * dx1 + dy * dy1) / len) << shift_bits);
938
939 rw_offset += sidedef->textureoffset + curline->offset;
940
941 rw_centerangle = ANG90 + viewangle - rw_normalangle;
942
943 rw_lightlevel = frontsector->lightlevel;
944 }
945
946 // if a floor / ceiling plane is on the wrong side of the view
947 // plane, it is definitely invisible and doesn't need to be marked.
948
949 // killough 3/7/98: add deep water check
950 if (frontsector->heightsec == -1)
951 {
952 if (frontsector->floorheight >= viewz) // above view plane
953 markfloor = false;
954 if (frontsector->ceilingheight <= viewz &&
955 frontsector->ceilingpic != skyflatnum) // below view plane
956 markceiling = false;
957 }
958
959 // The original code compared values after shifting them right.
960 // The code above here makes judgements based on the comparison before shifting.
961 // In some cases (see: hexen floor waggles when the texture visibility is ~0),
962 // the discrepancy would cause textures to be drawn with the wrong height.
963 {
964 dboolean low_greater_than_bottom = (worldlow > worldbottom);
965 dboolean high_less_than_top = (worldhigh < worldtop);
966
967 // calculate incremental stepping values for texture edges
968 worldtop >>= invhgtbits;
969 worldbottom >>= invhgtbits;
970
971 topstep = -FixedMul (rw_scalestep, worldtop);
972 topfrac = ((int64_t)centeryfrac>>invhgtbits) - (((int64_t)worldtop*rw_scale)>>FRACBITS); // R_WiggleFix
973
974 bottomstep = -FixedMul (rw_scalestep,worldbottom);
975 bottomfrac = ((int64_t)centeryfrac>>invhgtbits) - (((int64_t)worldbottom*rw_scale)>>FRACBITS); // R_WiggleFix
976
977 if (backsector)
978 {
979 worldhigh >>= invhgtbits;
980 worldlow >>= invhgtbits;
981
982 if (high_less_than_top)
983 {
984 pixhigh = ((int64_t)centeryfrac>>invhgtbits) - (((int64_t)worldhigh*rw_scale)>>FRACBITS); // R_WiggleFix
985 pixhighstep = -FixedMul (rw_scalestep,worldhigh);
986 }
987 if (low_greater_than_bottom)
988 {
989 pixlow = ((int64_t)centeryfrac>>invhgtbits) - (((int64_t)worldlow*rw_scale)>>FRACBITS); // R_WiggleFix
990 pixlowstep = -FixedMul (rw_scalestep,worldlow);
991 }
992 }
993 }
994
995 // render it
996 if (markceiling)
997 {
998 if (ceilingplane) // killough 4/11/98: add NULL ptr checks
999 ceilingplane = R_CheckPlane (ceilingplane, rw_x, rw_stopx-1);
1000 else
1001 markceiling = 0;
1002 }
1003
1004 if (markfloor)
1005 {
1006 if (floorplane) // killough 4/11/98: add NULL ptr checks
1007 {
1008 /* cph 2003/04/18 - ceilingplane and floorplane might be the same
1009 * visplane (e.g. if both skies); R_CheckPlane doesn't know about
1010 * modifications to the plane that might happen in parallel with the check
1011 * being made, so we have to override it and split them anyway if that is
1012 * a possibility, otherwise the floor marking would overwrite the ceiling
1013 * marking, resulting in HOM. */
1014 if (markceiling && ceilingplane == floorplane)
1015 floorplane = R_DupPlane (floorplane, rw_x, rw_stopx-1);
1016 else
1017 floorplane = R_CheckPlane (floorplane, rw_x, rw_stopx-1);
1018 }
1019 else
1020 {
1021 markfloor = 0;
1022 }
1023 }
1024
1025 didsolidcol = 0;
1026 R_RenderSegLoop();
1027
1028 /* cph - if a column was made solid by this wall, we _must_ save full clipping info */
1029 if (backsector && didsolidcol)
1030 {
1031 if (!(ds_p->silhouette & SIL_BOTTOM))
1032 {
1033 ds_p->silhouette |= SIL_BOTTOM;
1034 ds_p->bsilheight = backsector->floorheight;
1035 }
1036 if (!(ds_p->silhouette & SIL_TOP))
1037 {
1038 ds_p->silhouette |= SIL_TOP;
1039 ds_p->tsilheight = backsector->ceilingheight;
1040 }
1041 }
1042
1043 // save sprite clipping info
1044 if ((ds_p->silhouette & SIL_TOP || maskedtexture) && !ds_p->sprtopclip)
1045 {
1046 memcpy (lastopening, ceilingclip+start, sizeof(*lastopening)*(rw_stopx-start)); // dropoff overflow
1047 ds_p->sprtopclip = lastopening - start;
1048 lastopening += rw_stopx - start;
1049 }
1050 if ((ds_p->silhouette & SIL_BOTTOM || maskedtexture) && !ds_p->sprbottomclip)
1051 {
1052 memcpy (lastopening, floorclip+start, sizeof(*lastopening)*(rw_stopx-start)); // dropoff overflow
1053 ds_p->sprbottomclip = lastopening - start;
1054 lastopening += rw_stopx - start;
1055 }
1056 if (maskedtexture && !(ds_p->silhouette & SIL_TOP))
1057 {
1058 ds_p->silhouette |= SIL_TOP;
1059 ds_p->tsilheight = INT_MIN;
1060 }
1061 if (maskedtexture && !(ds_p->silhouette & SIL_BOTTOM))
1062 {
1063 ds_p->silhouette |= SIL_BOTTOM;
1064 ds_p->bsilheight = INT_MAX;
1065 }
1066 ds_p++;
1067 }