]> icculus.org git repositories - theoddone33/hhexen.git/blob - base/r_plane.c
Round 1: $HOME/.hhexen/ for configs and savegames.
[theoddone33/hhexen.git] / base / r_plane.c
1
2 //**************************************************************************
3 //**
4 //** r_plane.c : Heretic 2 : Raven Software, Corp.
5 //**
6 //** $RCSfile$
7 //** $Revision$
8 //** $Date$
9 //** $Author$
10 //**
11 //**************************************************************************
12
13 // HEADER FILES ------------------------------------------------------------
14
15 #include "h2def.h"
16 #include "r_local.h"
17
18 // MACROS ------------------------------------------------------------------
19
20 // TYPES -------------------------------------------------------------------
21
22 // EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
23
24 // PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
25
26 // PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
27
28 // EXTERNAL DATA DECLARATIONS ----------------------------------------------
29
30 extern fixed_t Sky1ScrollDelta;
31 extern fixed_t Sky2ScrollDelta;
32
33 // PUBLIC DATA DEFINITIONS -------------------------------------------------
34
35 int Sky1Texture;
36 int Sky2Texture;
37 fixed_t Sky1ColumnOffset;
38 fixed_t Sky2ColumnOffset;
39 int skyflatnum;
40 int skytexturemid;
41 fixed_t skyiscale;
42 boolean DoubleSky;
43 planefunction_t floorfunc, ceilingfunc;
44
45 // Opening
46 visplane_t visplanes[MAXVISPLANES], *lastvisplane;
47 visplane_t *floorplane, *ceilingplane;
48 short openings[MAXOPENINGS], *lastopening;
49
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];
55
56 // spanstart holds the start of a plane span, initialized to 0
57 int spanstart[SCREENHEIGHT];
58 int spanstop[SCREENHEIGHT];
59
60 // Texture mapping
61 lighttable_t **planezlight;
62 fixed_t planeheight;
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];
70
71 // PRIVATE DATA DEFINITIONS ------------------------------------------------
72
73 // CODE --------------------------------------------------------------------
74
75 //==========================================================================
76 //
77 // R_InitSky
78 //
79 // Called at level load.
80 //
81 //==========================================================================
82
83 void R_InitSky(int map)
84 {
85         Sky1Texture = P_GetMapSky1Texture(map);
86         Sky2Texture = P_GetMapSky2Texture(map);
87         Sky1ScrollDelta = P_GetMapSky1ScrollDelta(map);
88         Sky2ScrollDelta = P_GetMapSky2ScrollDelta(map);
89         Sky1ColumnOffset = 0;
90         Sky2ColumnOffset = 0;
91         DoubleSky = P_GetMapDoubleSky(map);
92 }
93
94 //==========================================================================
95 //
96 // R_InitSkyMap
97 //
98 // Called whenever the view size changes.
99 //
100 //==========================================================================
101
102 void R_InitSkyMap(void)
103 {
104         skyflatnum = R_FlatNumForName("F_SKY");
105         skytexturemid = 200*FRACUNIT;
106         skyiscale = FRACUNIT;
107 }
108
109 //==========================================================================
110 //
111 // R_InitPlanes
112 //
113 // Called at game startup.
114 //
115 //==========================================================================
116
117 void R_InitPlanes(void)
118 {
119 }
120
121 //==========================================================================
122 //
123 // R_MapPlane
124 //
125 // Globals used: planeheight, ds_source, basexscale, baseyscale,
126 // viewx, viewy.
127 //
128 //==========================================================================
129
130 void R_MapPlane(int y, int x1, int x2)
131 {
132 #ifndef RENDER3D
133         angle_t angle;
134         fixed_t distance, length;
135         unsigned index;
136
137 #ifdef RANGECHECK
138         if(x2 < x1 || x1 < 0 || x2 >= viewwidth || (unsigned)y > viewheight)
139         {
140                 I_Error("R_MapPlane: %i, %i at %i", x1, x2, y);
141         }
142 #endif
143
144         if(planeheight != cachedheight[y])
145         {
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);
150         }
151         else
152         {
153                 distance = cacheddistance[y];
154                 ds_xstep = cachedxstep[y];
155                 ds_ystep = cachedystep[y];
156         }
157         
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);
162
163         if(fixedcolormap)
164         {
165                 ds_colormap = fixedcolormap;
166         }
167         else
168         {
169                 index = distance >> LIGHTZSHIFT;
170                 if(index >= MAXLIGHTZ )
171                 {
172                         index = MAXLIGHTZ-1;
173                 }
174                 ds_colormap = planezlight[index];
175         }
176
177         ds_y = y;
178         ds_x1 = x1;
179         ds_x2 = x2;
180
181         spanfunc(); // High or low detail
182 #endif  // !RENDER3D
183 }
184
185 //==========================================================================
186 //
187 // R_ClearPlanes
188 //
189 // Called at the beginning of each frame.
190 //
191 //==========================================================================
192
193 void R_ClearPlanes(void)
194 {
195         int i;
196         angle_t angle;
197
198         // Opening / clipping determination
199         for(i = 0; i < viewwidth; i++)
200         {
201                 floorclip[i] = viewheight;
202                 ceilingclip[i] = -1;
203         }
204
205         lastvisplane = visplanes;
206         lastopening = openings;
207
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);
214 }
215
216 //==========================================================================
217 //
218 // R_FindPlane
219 //
220 //==========================================================================
221
222 visplane_t *R_FindPlane(fixed_t height, int picnum,
223         int lightlevel, int special)
224 {
225         visplane_t *check;
226
227         if(special < 150)
228         { // Don't let low specials affect search
229                 special = 0;
230         }
231
232         if(picnum == skyflatnum)
233         { // All skies map together
234                 height = 0;
235                 lightlevel = 0;
236         }
237
238         for(check = visplanes; check < lastvisplane; check++)
239         {
240                 if(height == check->height
241                 && picnum == check->picnum
242                 && lightlevel == check->lightlevel
243                 && special == check->special)
244                         break;
245         }
246
247         if(check < lastvisplane)
248         {
249                 return(check);
250         }
251
252         if(lastvisplane-visplanes == MAXVISPLANES)
253         {
254                 I_Error("R_FindPlane: no more visplanes");
255         }
256
257         lastvisplane++;
258         check->height = height;
259         check->picnum = picnum;
260         check->lightlevel = lightlevel;
261         check->special = special;
262         check->minx = SCREENWIDTH;
263         check->maxx = -1;
264         memset(check->top, 0xff, sizeof(check->top));
265         return(check);
266 }
267
268 //==========================================================================
269 //
270 // R_CheckPlane
271 //
272 //==========================================================================
273
274 visplane_t *R_CheckPlane(visplane_t *pl, int start, int stop)
275 {
276         int intrl, intrh;
277         int unionl, unionh;
278         int x;
279
280         if(start < pl->minx)
281         {
282                 intrl = pl->minx;
283                 unionl = start;
284         }
285         else
286         {
287                 unionl = pl->minx;
288                 intrl = start;
289         }
290         if(stop > pl->maxx)
291         {
292                 intrh = pl->maxx;
293                 unionh = stop;
294         }
295         else
296         {
297                 unionh = pl->maxx;
298                 intrh = stop;
299         }
300
301         for(x = intrl; x <= intrh; x++)
302         {
303                 if(pl->top[x] != 0xff)
304                 {
305                         break;
306                 }
307         }
308
309         if(x > intrh)
310         {
311                 pl->minx = unionl;
312                 pl->maxx = unionh;
313                 return pl; // use the same visplane
314         }
315
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;
321         pl = lastvisplane++;
322         pl->minx = start;
323         pl->maxx = stop;
324         memset(pl->top, 0xff, sizeof(pl->top));
325
326         return pl;
327 }
328
329 //==========================================================================
330 //
331 // R_MakeSpans
332 //
333 //==========================================================================
334
335 void R_MakeSpans(int x, int t1, int b1, int t2, int b2)
336 {
337         while(t1 < t2 && t1 <= b1)
338         {
339                 R_MapPlane(t1, spanstart[t1], x-1);
340                 t1++;
341         }
342         while(b1 > b2 && b1 >= t1)
343         {
344                 R_MapPlane(b1, spanstart[b1], x-1);
345                 b1--;
346         }
347         while(t2 < t1 && t2 <= b2)
348         {
349                 spanstart[t2] = x;
350                 t2++;
351         }
352         while(b2 > b1 && b2 >= t2)
353         {
354                 spanstart[b2] = x;
355                 b2--;
356         }
357 }
358
359 //==========================================================================
360 //
361 // R_DrawPlanes
362 //
363 //==========================================================================
364
365 #define SKYTEXTUREMIDSHIFTED 200
366
367 void R_DrawPlanes(void)
368 {
369 #ifndef RENDER3D
370         visplane_t *pl;
371         int light;
372         int x, stop;
373         int angle;
374         byte *tempSource;
375         byte *source;
376         byte *source2;
377         byte *dest;
378         int count;
379         int offset;
380         int skyTexture;
381         int offset2;
382         int skyTexture2;
383         int scrollOffset;
384
385         extern byte *ylookup[MAXHEIGHT];
386         extern int columnofs[MAXWIDTH];
387
388 #ifdef RANGECHECK
389         if(ds_p-drawsegs > MAXDRAWSEGS)
390         {
391                 I_Error("R_DrawPlanes: drawsegs overflow (%i)", ds_p-drawsegs);
392         }
393         if(lastvisplane-visplanes > MAXVISPLANES)
394         {
395                 I_Error("R_DrawPlanes: visplane overflow (%i)",
396                         lastvisplane-visplanes);
397         }
398         if(lastopening-openings > MAXOPENINGS)
399         {
400                 I_Error("R_DrawPlanes: opening overflow (%i)",
401                         lastopening-openings);
402         }
403 #endif
404
405         for(pl = visplanes; pl < lastvisplane; pl++)
406         {
407                 if(pl->minx > pl->maxx)
408                 {
409                         continue;
410                 }
411                 if(pl->picnum == skyflatnum)
412                 { // Sky flat
413                         if(DoubleSky)
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++)
420                                 {
421                                         dc_yl = pl->top[x];
422                                         dc_yh = pl->bottom[x];
423                                         if(dc_yl <= dc_yh)
424                                         {
425                                                 count = dc_yh-dc_yl;
426                                                 if(count < 0)
427                                                 {
428                                                         return;
429                                                 }
430                                                 angle = (viewangle+xtoviewangle[x])
431                                                         >>ANGLETOSKYSHIFT;
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];
437                                                 do
438                                                 {
439                                                         if(*source)
440                                                         {
441                                                                 *dest = *source++;
442                                                                 source2++;
443                                                         }
444                                                         else
445                                                         {
446                                                                 *dest = *source2++;
447                                                                 source++;
448                                                         }
449                                                         dest += SCREENWIDTH;
450                                                 } while(count--);
451                                         }
452                                 }
453                                 continue; // Next visplane
454                         }
455                         else
456                         { // Render single layer
457                                 if(pl->special == 200)
458                                 { // Use sky 2
459                                         offset = Sky2ColumnOffset>>16;
460                                         skyTexture = texturetranslation[Sky2Texture];
461                                 }
462                                 else
463                                 { // Use sky 1
464                                         offset = Sky1ColumnOffset>>16;
465                                         skyTexture = texturetranslation[Sky1Texture];
466                                 }
467                                 for(x = pl->minx; x <= pl->maxx; x++)
468                                 {
469                                         dc_yl = pl->top[x];
470                                         dc_yh = pl->bottom[x];
471                                         if(dc_yl <= dc_yh)
472                                         {
473                                                 count = dc_yh-dc_yl;
474                                                 if(count < 0)
475                                                 {
476                                                         return;
477                                                 }
478                                                 angle = (viewangle+xtoviewangle[x])
479                                                         >>ANGLETOSKYSHIFT;
480                                                 source = R_GetColumn(skyTexture, angle+offset)
481                                                         +SKYTEXTUREMIDSHIFTED+(dc_yl-centery);
482                                                 dest = ylookup[dc_yl]+columnofs[x];
483                                                 do
484                                                 {
485                                                         *dest = *source++;
486                                                         dest += SCREENWIDTH;
487                                                 } while(count--);
488                                         }
489                                 }
490                                 continue; // Next visplane
491                         }
492                 }
493                 // Regular flat
494                 tempSource = W_CacheLumpNum(firstflat+
495                         flattranslation[pl->picnum], PU_STATIC);
496                 scrollOffset = leveltime>>1&63;
497                 switch(pl->special)
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);
502                                 break;
503                         case 204: case 205: case 206: // Scroll_East_xxx
504                                 ds_source = tempSource+((63-scrollOffset)
505                                         <<(pl->special-204)&63);
506                                 break;
507                         case 207: case 208: case 209: // Scroll_South_xxx
508                                 ds_source = tempSource+(((63-scrollOffset)
509                                         <<(pl->special-207)&63)<<6);
510                                 break;
511                         case 210: case 211: case 212: // Scroll_West_xxx
512                                 ds_source = tempSource+(scrollOffset
513                                         <<(pl->special-210)&63);
514                                 break;
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);
519                                 break;
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);
524                                 break;
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);
529                                 break;
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);
534                                 break;
535                         default:
536                                 ds_source = tempSource;
537                                 break;
538                 }
539                 planeheight = abs(pl->height-viewz);
540                 light = (pl->lightlevel >> LIGHTSEGSHIFT)+extralight;
541                 if(light >= LIGHTLEVELS)
542                 {
543                         light = LIGHTLEVELS-1;
544                 }
545                 if(light < 0)
546                 {
547                         light = 0;
548                 }
549                 planezlight = zlight[light];
550
551                 pl->top[pl->maxx+1] = 0xff;
552                 pl->top[pl->minx-1] = 0xff;
553
554                 stop = pl->maxx+1;
555                 for(x = pl->minx; x <= stop; x++)
556                 {
557                         R_MakeSpans(x, pl->top[x-1], pl->bottom[x-1],
558                                 pl->top[x], pl->bottom[x]);
559                 }
560                 Z_ChangeTag(tempSource, PU_CACHE);
561         }
562 #endif !RENDER3D
563 }
564