2 //**************************************************************************
4 //** r_plane.c : Heretic 2 : Raven Software, Corp.
11 //**************************************************************************
13 // HEADER FILES ------------------------------------------------------------
18 // MACROS ------------------------------------------------------------------
20 // TYPES -------------------------------------------------------------------
22 // EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
24 // PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
26 // PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
28 // EXTERNAL DATA DECLARATIONS ----------------------------------------------
30 extern fixed_t Sky1ScrollDelta;
31 extern fixed_t Sky2ScrollDelta;
33 // PUBLIC DATA DEFINITIONS -------------------------------------------------
37 fixed_t Sky1ColumnOffset;
38 fixed_t Sky2ColumnOffset;
43 planefunction_t floorfunc, ceilingfunc;
46 visplane_t visplanes[MAXVISPLANES], *lastvisplane;
47 visplane_t *floorplane, *ceilingplane;
48 short openings[MAXOPENINGS], *lastopening;
50 // Clip values are the solid pixel bounding the range.
51 // floorclip start out SCREENHEIGHT
52 // ceilingclip starts out -1
53 short floorclip[SCREENWIDTH];
54 short ceilingclip[SCREENWIDTH];
56 // spanstart holds the start of a plane span, initialized to 0
57 int spanstart[SCREENHEIGHT];
58 int spanstop[SCREENHEIGHT];
61 lighttable_t **planezlight;
63 fixed_t yslope[SCREENHEIGHT];
64 fixed_t distscale[SCREENWIDTH];
65 fixed_t basexscale, baseyscale;
66 fixed_t cachedheight[SCREENHEIGHT];
67 fixed_t cacheddistance[SCREENHEIGHT];
68 fixed_t cachedxstep[SCREENHEIGHT];
69 fixed_t cachedystep[SCREENHEIGHT];
71 // PRIVATE DATA DEFINITIONS ------------------------------------------------
73 // CODE --------------------------------------------------------------------
75 //==========================================================================
79 // Called at level load.
81 //==========================================================================
83 void R_InitSky(int map)
85 Sky1Texture = P_GetMapSky1Texture(map);
86 Sky2Texture = P_GetMapSky2Texture(map);
87 Sky1ScrollDelta = P_GetMapSky1ScrollDelta(map);
88 Sky2ScrollDelta = P_GetMapSky2ScrollDelta(map);
91 DoubleSky = P_GetMapDoubleSky(map);
94 //==========================================================================
98 // Called whenever the view size changes.
100 //==========================================================================
102 void R_InitSkyMap(void)
104 skyflatnum = R_FlatNumForName("F_SKY");
105 skytexturemid = 200*FRACUNIT;
106 skyiscale = FRACUNIT;
109 //==========================================================================
113 // Called at game startup.
115 //==========================================================================
117 void R_InitPlanes(void)
121 //==========================================================================
125 // Globals used: planeheight, ds_source, basexscale, baseyscale,
128 //==========================================================================
130 void R_MapPlane(int y, int x1, int x2)
134 fixed_t distance, length;
138 if(x2 < x1 || x1 < 0 || x2 >= viewwidth || (unsigned)y > viewheight)
140 I_Error("R_MapPlane: %i, %i at %i", x1, x2, y);
144 if(planeheight != cachedheight[y])
146 cachedheight[y] = planeheight;
147 distance = cacheddistance[y] = FixedMul(planeheight, yslope[y]);
148 ds_xstep = cachedxstep[y] = FixedMul(distance, basexscale);
149 ds_ystep = cachedystep[y] = FixedMul(distance, baseyscale);
153 distance = cacheddistance[y];
154 ds_xstep = cachedxstep[y];
155 ds_ystep = cachedystep[y];
158 length = FixedMul(distance, distscale[x1]);
159 angle = (viewangle+xtoviewangle[x1])>>ANGLETOFINESHIFT;
160 ds_xfrac = viewx+FixedMul(finecosine[angle], length);
161 ds_yfrac = -viewy-FixedMul(finesine[angle], length);
165 ds_colormap = fixedcolormap;
169 index = distance >> LIGHTZSHIFT;
170 if(index >= MAXLIGHTZ )
174 ds_colormap = planezlight[index];
181 spanfunc(); // High or low detail
185 //==========================================================================
189 // Called at the beginning of each frame.
191 //==========================================================================
193 void R_ClearPlanes(void)
198 // Opening / clipping determination
199 for(i = 0; i < viewwidth; i++)
201 floorclip[i] = viewheight;
205 lastvisplane = visplanes;
206 lastopening = openings;
208 // Texture calculation
209 memset(cachedheight, 0, sizeof(cachedheight));
210 angle = (viewangle-ANG90)>>ANGLETOFINESHIFT; // left to right mapping
211 // Scale will be unit scale at SCREENWIDTH/2 distance
212 basexscale = FixedDiv(finecosine[angle], centerxfrac);
213 baseyscale = -FixedDiv(finesine[angle], centerxfrac);
216 //==========================================================================
220 //==========================================================================
222 visplane_t *R_FindPlane(fixed_t height, int picnum,
223 int lightlevel, int special)
228 { // Don't let low specials affect search
232 if(picnum == skyflatnum)
233 { // All skies map together
238 for(check = visplanes; check < lastvisplane; check++)
240 if(height == check->height
241 && picnum == check->picnum
242 && lightlevel == check->lightlevel
243 && special == check->special)
247 if(check < lastvisplane)
252 if(lastvisplane-visplanes == MAXVISPLANES)
254 I_Error("R_FindPlane: no more visplanes");
258 check->height = height;
259 check->picnum = picnum;
260 check->lightlevel = lightlevel;
261 check->special = special;
262 check->minx = SCREENWIDTH;
264 memset(check->top, 0xff, sizeof(check->top));
268 //==========================================================================
272 //==========================================================================
274 visplane_t *R_CheckPlane(visplane_t *pl, int start, int stop)
301 for(x = intrl; x <= intrh; x++)
303 if(pl->top[x] != 0xff)
313 return pl; // use the same visplane
316 // Make a new visplane
317 lastvisplane->height = pl->height;
318 lastvisplane->picnum = pl->picnum;
319 lastvisplane->lightlevel = pl->lightlevel;
320 lastvisplane->special = pl->special;
324 memset(pl->top, 0xff, sizeof(pl->top));
329 //==========================================================================
333 //==========================================================================
335 void R_MakeSpans(int x, int t1, int b1, int t2, int b2)
337 while(t1 < t2 && t1 <= b1)
339 R_MapPlane(t1, spanstart[t1], x-1);
342 while(b1 > b2 && b1 >= t1)
344 R_MapPlane(b1, spanstart[b1], x-1);
347 while(t2 < t1 && t2 <= b2)
352 while(b2 > b1 && b2 >= t2)
359 //==========================================================================
363 //==========================================================================
365 #define SKYTEXTUREMIDSHIFTED 200
367 void R_DrawPlanes(void)
385 extern byte *ylookup[MAXHEIGHT];
386 extern int columnofs[MAXWIDTH];
389 if(ds_p-drawsegs > MAXDRAWSEGS)
391 I_Error("R_DrawPlanes: drawsegs overflow (%i)", ds_p-drawsegs);
393 if(lastvisplane-visplanes > MAXVISPLANES)
395 I_Error("R_DrawPlanes: visplane overflow (%i)",
396 lastvisplane-visplanes);
398 if(lastopening-openings > MAXOPENINGS)
400 I_Error("R_DrawPlanes: opening overflow (%i)",
401 lastopening-openings);
405 for(pl = visplanes; pl < lastvisplane; pl++)
407 if(pl->minx > pl->maxx)
411 if(pl->picnum == skyflatnum)
414 { // Render 2 layers, sky 1 in front
415 offset = Sky1ColumnOffset>>16;
416 skyTexture = texturetranslation[Sky1Texture];
417 offset2 = Sky2ColumnOffset>>16;
418 skyTexture2 = texturetranslation[Sky2Texture];
419 for(x = pl->minx; x <= pl->maxx; x++)
422 dc_yh = pl->bottom[x];
430 angle = (viewangle+xtoviewangle[x])
432 source = R_GetColumn(skyTexture, angle+offset)
433 +SKYTEXTUREMIDSHIFTED+(dc_yl-centery);
434 source2 = R_GetColumn(skyTexture2, angle+offset2)
435 +SKYTEXTUREMIDSHIFTED+(dc_yl-centery);
436 dest = ylookup[dc_yl]+columnofs[x];
453 continue; // Next visplane
456 { // Render single layer
457 if(pl->special == 200)
459 offset = Sky2ColumnOffset>>16;
460 skyTexture = texturetranslation[Sky2Texture];
464 offset = Sky1ColumnOffset>>16;
465 skyTexture = texturetranslation[Sky1Texture];
467 for(x = pl->minx; x <= pl->maxx; x++)
470 dc_yh = pl->bottom[x];
478 angle = (viewangle+xtoviewangle[x])
480 source = R_GetColumn(skyTexture, angle+offset)
481 +SKYTEXTUREMIDSHIFTED+(dc_yl-centery);
482 dest = ylookup[dc_yl]+columnofs[x];
490 continue; // Next visplane
494 tempSource = W_CacheLumpNum(firstflat+
495 flattranslation[pl->picnum], PU_STATIC);
496 scrollOffset = leveltime>>1&63;
498 { // Handle scrolling flats
499 case 201: case 202: case 203: // Scroll_North_xxx
500 ds_source = tempSource+((scrollOffset
501 <<(pl->special-201)&63)<<6);
503 case 204: case 205: case 206: // Scroll_East_xxx
504 ds_source = tempSource+((63-scrollOffset)
505 <<(pl->special-204)&63);
507 case 207: case 208: case 209: // Scroll_South_xxx
508 ds_source = tempSource+(((63-scrollOffset)
509 <<(pl->special-207)&63)<<6);
511 case 210: case 211: case 212: // Scroll_West_xxx
512 ds_source = tempSource+(scrollOffset
513 <<(pl->special-210)&63);
515 case 213: case 214: case 215: // Scroll_NorthWest_xxx
516 ds_source = tempSource+(scrollOffset
517 <<(pl->special-213)&63)+((scrollOffset
518 <<(pl->special-213)&63)<<6);
520 case 216: case 217: case 218: // Scroll_NorthEast_xxx
521 ds_source = tempSource+((63-scrollOffset)
522 <<(pl->special-216)&63)+((scrollOffset
523 <<(pl->special-216)&63)<<6);
525 case 219: case 220: case 221: // Scroll_SouthEast_xxx
526 ds_source = tempSource+((63-scrollOffset)
527 <<(pl->special-219)&63)+(((63-scrollOffset)
528 <<(pl->special-219)&63)<<6);
530 case 222: case 223: case 224: // Scroll_SouthWest_xxx
531 ds_source = tempSource+(scrollOffset
532 <<(pl->special-222)&63)+(((63-scrollOffset)
533 <<(pl->special-222)&63)<<6);
536 ds_source = tempSource;
539 planeheight = abs(pl->height-viewz);
540 light = (pl->lightlevel >> LIGHTSEGSHIFT)+extralight;
541 if(light >= LIGHTLEVELS)
543 light = LIGHTLEVELS-1;
549 planezlight = zlight[light];
551 pl->top[pl->maxx+1] = 0xff;
552 pl->top[pl->minx-1] = 0xff;
555 for(x = pl->minx; x <= stop; x++)
557 R_MakeSpans(x, pl->top[x-1], pl->bottom[x-1],
558 pl->top[x], pl->bottom[x]);
560 Z_ChangeTag(tempSource, PU_CACHE);