aboutsummaryrefslogtreecommitdiff
path: root/src/r_plane.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/r_plane.c')
-rw-r--r--src/r_plane.c468
1 files changed, 468 insertions, 0 deletions
diff --git a/src/r_plane.c b/src/r_plane.c
new file mode 100644
index 0000000..8eaa1e1
--- /dev/null
+++ b/src/r_plane.c
@@ -0,0 +1,468 @@
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 * Here is a core component: drawing the floors and ceilings,
31 * while maintaining a per column clipping list only.
32 * Moreover, the sky areas have to be determined.
33 *
34 * MAXVISPLANES is no longer a limit on the number of visplanes,
35 * but a limit on the number of hash slots; larger numbers mean
36 * better performance usually but after a point they are wasted,
37 * and memory and time overheads creep in.
38 *
39 * For more information on visplanes, see:
40 *
41 * http://classicgaming.com/doom/editing/
42 *
43 * Lee Killough
44 *
45 *-----------------------------------------------------------------------------*/
46
47#ifdef HAVE_CONFIG_H
48#include "config.h"
49#endif
50
51#include "z_zone.h" /* memory allocation wrappers -- killough */
52
53#include "doomstat.h"
54#include "w_wad.h"
55#include "r_main.h"
56#include "r_draw.h"
57#include "r_things.h"
58#include "r_sky.h"
59#include "r_plane.h"
60#include "v_video.h"
61#include "lprintf.h"
62
63#define MAXVISPLANES 128 /* must be a power of 2 */
64
65static visplane_t *visplanes[MAXVISPLANES]; // killough
66static visplane_t *freetail; // killough
67static visplane_t **freehead = &freetail; // killough
68visplane_t *floorplane, *ceilingplane;
69
70// killough -- hash function for visplanes
71// Empirically verified to be fairly uniform:
72
73#define visplane_hash(picnum,lightlevel,height) \
74 ((unsigned)((picnum)*3+(lightlevel)+(height)*7) & (MAXVISPLANES-1))
75
76size_t maxopenings;
77int *openings,*lastopening; // dropoff overflow
78
79// Clip values are the solid pixel bounding the range.
80// floorclip starts out SCREENHEIGHT
81// ceilingclip starts out -1
82
83int floorclip[MAX_SCREENWIDTH], ceilingclip[MAX_SCREENWIDTH]; // dropoff overflow
84
85// spanstart holds the start of a plane span; initialized to 0 at start
86
87static int spanstart[MAX_SCREENHEIGHT]; // killough 2/8/98
88
89//
90// texture mapping
91//
92
93static const lighttable_t **planezlight;
94static fixed_t planeheight;
95
96// killough 2/8/98: make variables static
97
98static fixed_t basexscale, baseyscale;
99static fixed_t cachedheight[MAX_SCREENHEIGHT];
100static fixed_t cacheddistance[MAX_SCREENHEIGHT];
101static fixed_t cachedxstep[MAX_SCREENHEIGHT];
102static fixed_t cachedystep[MAX_SCREENHEIGHT];
103static fixed_t xoffs,yoffs; // killough 2/28/98: flat offsets
104
105fixed_t yslope[MAX_SCREENHEIGHT], distscale[MAX_SCREENWIDTH];
106
107//
108// R_InitPlanes
109// Only at game startup.
110//
111void R_InitPlanes (void)
112{
113}
114
115//
116// R_MapPlane
117//
118// Uses global vars:
119// planeheight
120// dsvars.source
121// basexscale
122// baseyscale
123// viewx
124// viewy
125// xoffs
126// yoffs
127//
128// BASIC PRIMITIVE
129//
130
131static void R_MapPlane(int y, int x1, int x2, draw_span_vars_t *dsvars)
132{
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>(unsigned)viewheight)
139 I_Error ("R_MapPlane: %i, %i at %i",x1,x2,y);
140#endif
141
142 if (planeheight != cachedheight[y])
143 {
144 cachedheight[y] = planeheight;
145 distance = cacheddistance[y] = FixedMul (planeheight, yslope[y]);
146 dsvars->xstep = cachedxstep[y] = FixedMul (distance,basexscale);
147 dsvars->ystep = cachedystep[y] = FixedMul (distance,baseyscale);
148 }
149 else
150 {
151 distance = cacheddistance[y];
152 dsvars->xstep = cachedxstep[y];
153 dsvars->ystep = cachedystep[y];
154 }
155
156 length = FixedMul (distance,distscale[x1]);
157 angle = (viewangle + xtoviewangle[x1])>>ANGLETOFINESHIFT;
158
159 // killough 2/28/98: Add offsets
160 dsvars->xfrac = viewx + FixedMul(finecosine[angle], length) + xoffs;
161 dsvars->yfrac = -viewy - FixedMul(finesine[angle], length) + yoffs;
162
163 if (drawvars.filterfloor == RDRAW_FILTER_LINEAR) {
164 dsvars->xfrac -= (FRACUNIT>>1);
165 dsvars->yfrac -= (FRACUNIT>>1);
166 }
167
168 if (!(dsvars->colormap = fixedcolormap))
169 {
170 dsvars->z = distance;
171 index = distance >> LIGHTZSHIFT;
172 if (index >= MAXLIGHTZ )
173 index = MAXLIGHTZ-1;
174 dsvars->colormap = planezlight[index];
175 dsvars->nextcolormap = planezlight[index+1 >= MAXLIGHTZ ? MAXLIGHTZ-1 : index+1];
176 }
177 else
178 {
179 dsvars->z = 0;
180 }
181
182 dsvars->y = y;
183 dsvars->x1 = x1;
184 dsvars->x2 = x2;
185
186 if (V_GetMode() != VID_MODEGL)
187 R_DrawSpan(dsvars);
188}
189
190//
191// R_ClearPlanes
192// At begining of frame.
193//
194
195void R_ClearPlanes(void)
196{
197 int i;
198
199 // opening / clipping determination
200 for (i=0 ; i<viewwidth ; i++)
201 floorclip[i] = viewheight, ceilingclip[i] = -1;
202
203 for (i=0;i<MAXVISPLANES;i++) // new code -- killough
204 for (*freehead = visplanes[i], visplanes[i] = NULL; *freehead; )
205 freehead = &(*freehead)->next;
206
207 lastopening = openings;
208
209 // texture calculation
210 memset (cachedheight, 0, sizeof(cachedheight));
211
212 // scale will be unit scale at SCREENWIDTH/2 distance
213 basexscale = FixedDiv (viewsin,projection);
214 baseyscale = FixedDiv (viewcos,projection);
215}
216
217// New function, by Lee Killough
218
219static visplane_t *new_visplane(unsigned hash)
220{
221 visplane_t *check = freetail;
222 if (!check)
223 check = calloc(1, sizeof *check);
224 else
225 if (!(freetail = freetail->next))
226 freehead = &freetail;
227 check->next = visplanes[hash];
228 visplanes[hash] = check;
229 return check;
230}
231
232/*
233 * R_DupPlane
234 *
235 * cph 2003/04/18 - create duplicate of existing visplane and set initial range
236 */
237visplane_t *R_DupPlane(const visplane_t *pl, int start, int stop)
238{
239 unsigned hash = visplane_hash(pl->picnum, pl->lightlevel, pl->height);
240 visplane_t *new_pl = new_visplane(hash);
241
242 new_pl->height = pl->height;
243 new_pl->picnum = pl->picnum;
244 new_pl->lightlevel = pl->lightlevel;
245 new_pl->xoffs = pl->xoffs; // killough 2/28/98
246 new_pl->yoffs = pl->yoffs;
247 new_pl->minx = start;
248 new_pl->maxx = stop;
249 memset(new_pl->top, 0xff, sizeof new_pl->top);
250 return new_pl;
251}
252//
253// R_FindPlane
254//
255// killough 2/28/98: Add offsets
256
257visplane_t *R_FindPlane(fixed_t height, int picnum, int lightlevel,
258 fixed_t xoffs, fixed_t yoffs)
259{
260 visplane_t *check;
261 unsigned hash; // killough
262
263 if (picnum == skyflatnum || picnum & PL_SKYFLAT)
264 height = lightlevel = 0; // killough 7/19/98: most skies map together
265
266 // New visplane algorithm uses hash table -- killough
267 hash = visplane_hash(picnum,lightlevel,height);
268
269 for (check=visplanes[hash]; check; check=check->next) // killough
270 if (height == check->height &&
271 picnum == check->picnum &&
272 lightlevel == check->lightlevel &&
273 xoffs == check->xoffs && // killough 2/28/98: Add offset checks
274 yoffs == check->yoffs)
275 return check;
276
277 check = new_visplane(hash); // killough
278
279 check->height = height;
280 check->picnum = picnum;
281 check->lightlevel = lightlevel;
282 check->minx = viewwidth; // Was SCREENWIDTH -- killough 11/98
283 check->maxx = -1;
284 check->xoffs = xoffs; // killough 2/28/98: Save offsets
285 check->yoffs = yoffs;
286
287 memset (check->top, 0xff, sizeof check->top);
288
289 return check;
290}
291
292//
293// R_CheckPlane
294//
295visplane_t *R_CheckPlane(visplane_t *pl, int start, int stop)
296{
297 int intrl, intrh, unionl, unionh, x;
298
299 if (start < pl->minx)
300 intrl = pl->minx, unionl = start;
301 else
302 unionl = pl->minx, intrl = start;
303
304 if (stop > pl->maxx)
305 intrh = pl->maxx, unionh = stop;
306 else
307 unionh = pl->maxx, intrh = stop;
308
309 for (x=intrl ; x <= intrh && pl->top[x] == 0xffffffffu; x++) // dropoff overflow
310 ;
311
312 if (x > intrh) { /* Can use existing plane; extend range */
313 pl->minx = unionl; pl->maxx = unionh;
314 return pl;
315 } else /* Cannot use existing plane; create a new one */
316 return R_DupPlane(pl,start,stop);
317}
318
319//
320// R_MakeSpans
321//
322
323static void R_MakeSpans(int x, unsigned int t1, unsigned int b1,
324 unsigned int t2, unsigned int b2,
325 draw_span_vars_t *dsvars)
326{
327 for (; t1 < t2 && t1 <= b1; t1++)
328 R_MapPlane(t1, spanstart[t1], x-1, dsvars);
329 for (; b1 > b2 && b1 >= t1; b1--)
330 R_MapPlane(b1, spanstart[b1] ,x-1, dsvars);
331 while (t2 < t1 && t2 <= b2)
332 spanstart[t2++] = x;
333 while (b2 > b1 && b2 >= t2)
334 spanstart[b2--] = x;
335}
336
337// New function, by Lee Killough
338
339static void R_DoDrawPlane(visplane_t *pl)
340{
341 register int x;
342 draw_column_vars_t dcvars;
343 R_DrawColumn_f colfunc = R_GetDrawColumnFunc(RDC_PIPELINE_STANDARD, drawvars.filterwall, drawvars.filterz);
344
345 R_SetDefaultDrawColumnVars(&dcvars);
346
347 if (pl->minx <= pl->maxx) {
348 if (pl->picnum == skyflatnum || pl->picnum & PL_SKYFLAT) { // sky flat
349 int texture;
350 const rpatch_t *tex_patch;
351 angle_t an, flip;
352
353 // killough 10/98: allow skies to come from sidedefs.
354 // Allows scrolling and/or animated skies, as well as
355 // arbitrary multiple skies per level without having
356 // to use info lumps.
357
358 an = viewangle;
359
360 if (pl->picnum & PL_SKYFLAT)
361 {
362 // Sky Linedef
363 const line_t *l = &lines[pl->picnum & ~PL_SKYFLAT];
364
365 // Sky transferred from first sidedef
366 const side_t *s = *l->sidenum + sides;
367
368 // Texture comes from upper texture of reference sidedef
369 texture = texturetranslation[s->toptexture];
370
371 // Horizontal offset is turned into an angle offset,
372 // to allow sky rotation as well as careful positioning.
373 // However, the offset is scaled very small, so that it
374 // allows a long-period of sky rotation.
375
376 an += s->textureoffset;
377
378 // Vertical offset allows careful sky positioning.
379
380 dcvars.texturemid = s->rowoffset - 28*FRACUNIT;
381
382 // We sometimes flip the picture horizontally.
383 //
384 // Doom always flipped the picture, so we make it optional,
385 // to make it easier to use the new feature, while to still
386 // allow old sky textures to be used.
387
388 flip = l->special==272 ? 0u : ~0u;
389 }
390 else
391 { // Normal Doom sky, only one allowed per level
392 dcvars.texturemid = skytexturemid; // Default y-offset
393 texture = skytexture; // Default texture
394 flip = 0; // Doom flips it
395 }
396
397 /* Sky is always drawn full bright, i.e. colormaps[0] is used.
398 * Because of this hack, sky is not affected by INVUL inverse mapping.
399 * Until Boom fixed this. Compat option added in MBF. */
400
401 if (comp[comp_skymap] || !(dcvars.colormap = fixedcolormap))
402 dcvars.colormap = fullcolormap; // killough 3/20/98
403
404 dcvars.nextcolormap = dcvars.colormap; // for filtering -- POPE
405
406 //dcvars.texturemid = skytexturemid;
407 dcvars.texheight = textureheight[skytexture]>>FRACBITS; // killough
408 // proff 09/21/98: Changed for high-res
409 dcvars.iscale = FRACUNIT*200/viewheight;
410
411 tex_patch = R_CacheTextureCompositePatchNum(texture);
412
413 // killough 10/98: Use sky scrolling offset, and possibly flip picture
414 for (x = pl->minx; (dcvars.x = x) <= pl->maxx; x++)
415 if ((dcvars.yl = pl->top[x]) != -1 && dcvars.yl <= (dcvars.yh = pl->bottom[x])) // dropoff overflow
416 {
417 dcvars.source = R_GetTextureColumn(tex_patch, ((an + xtoviewangle[x])^flip) >> ANGLETOSKYSHIFT);
418 dcvars.prevsource = R_GetTextureColumn(tex_patch, ((an + xtoviewangle[x-1])^flip) >> ANGLETOSKYSHIFT);
419 dcvars.nextsource = R_GetTextureColumn(tex_patch, ((an + xtoviewangle[x+1])^flip) >> ANGLETOSKYSHIFT);
420 colfunc(&dcvars);
421 }
422
423 R_UnlockTextureCompositePatchNum(texture);
424
425 } else { // regular flat
426
427 int stop, light;
428 draw_span_vars_t dsvars;
429
430 dsvars.source = W_CacheLumpNum(firstflat + flattranslation[pl->picnum]);
431
432 xoffs = pl->xoffs; // killough 2/28/98: Add offsets
433 yoffs = pl->yoffs;
434 planeheight = D_abs(pl->height-viewz);
435 light = (pl->lightlevel >> LIGHTSEGSHIFT) + extralight;
436
437 if (light >= LIGHTLEVELS)
438 light = LIGHTLEVELS-1;
439
440 if (light < 0)
441 light = 0;
442
443 stop = pl->maxx + 1;
444 planezlight = zlight[light];
445 pl->top[pl->minx-1] = pl->top[stop] = 0xffffffffu; // dropoff overflow
446
447 for (x = pl->minx ; x <= stop ; x++)
448 R_MakeSpans(x,pl->top[x-1],pl->bottom[x-1],
449 pl->top[x],pl->bottom[x], &dsvars);
450
451 W_UnlockLumpNum(firstflat + flattranslation[pl->picnum]);
452 }
453 }
454}
455
456//
457// RDrawPlanes
458// At the end of each frame.
459//
460
461void R_DrawPlanes (void)
462{
463 visplane_t *pl;
464 int i;
465 for (i=0;i<MAXVISPLANES;i++)
466 for (pl=visplanes[i]; pl; pl=pl->next, rendered_visplanes++)
467 R_DoDrawPlane(pl);
468}