SRB2/src/r_plane.c

1304 lines
32 KiB
C
Raw Normal View History

2014-03-15 16:59:03 +00:00
// SONIC ROBO BLAST 2
//-----------------------------------------------------------------------------
// Copyright (C) 1993-1996 by id Software, Inc.
// Copyright (C) 1998-2000 by DooM Legacy Team.
2018-11-25 12:35:38 +00:00
// Copyright (C) 1999-2018 by Sonic Team Junior.
2014-03-15 16:59:03 +00:00
//
// This program is free software distributed under the
// terms of the GNU General Public License, version 2.
// See the 'LICENSE' file for more details.
//-----------------------------------------------------------------------------
/// \file r_plane.c
/// \brief Here is a core component: drawing the floors and ceilings,
/// while maintaining a per column clipping list only.
/// Moreover, the sky areas have to be determined.
#include "doomdef.h"
#include "console.h"
#include "g_game.h"
2018-12-14 17:08:25 +00:00
#include "p_setup.h" // levelflats
#include "p_slopes.h"
2014-03-15 16:59:03 +00:00
#include "r_data.h"
#include "r_local.h"
#include "r_state.h"
#include "r_splats.h" // faB(21jan):testing
#include "r_sky.h"
#include "r_portal.h"
2014-03-15 16:59:03 +00:00
#include "v_video.h"
#include "w_wad.h"
#include "z_zone.h"
#include "p_tick.h"
2018-12-14 17:08:25 +00:00
#ifdef TIMING
#include "p5prof.h"
INT64 mycount;
INT64 mytotal = 0;
UINT32 nombre = 100000;
#endif
2014-03-15 16:59:03 +00:00
//
// opening
//
// Quincunx antialiasing of flats!
//#define QUINCUNX
//SoM: 3/23/2000: Use Boom visplane hashing.
visplane_t *visplanes[MAXVISPLANES];
2014-03-15 16:59:03 +00:00
static visplane_t *freetail;
static visplane_t **freehead = &freetail;
visplane_t *floorplane;
visplane_t *ceilingplane;
static visplane_t *currentplane;
2018-12-14 17:08:25 +00:00
visffloor_t ffloor[MAXFFLOORS];
2014-03-15 16:59:03 +00:00
INT32 numffloors;
//SoM: 3/23/2000: Boom visplane hashing routine.
#define visplane_hash(picnum,lightlevel,height) \
((unsigned)((picnum)*3+(lightlevel)+(height)*7) & (MAXVISPLANES-1))
//SoM: 3/23/2000: Use boom opening limit removal
size_t maxopenings;
INT16 *openings, *lastopening; /// \todo free leak
//
// Clip values are the solid pixel bounding the range.
// floorclip starts out SCREENHEIGHT
// ceilingclip starts out -1
//
INT16 floorclip[MAXVIDWIDTH], ceilingclip[MAXVIDWIDTH];
fixed_t frontscale[MAXVIDWIDTH];
//
// spanstart holds the start of a plane span
// initialized to 0 at start
//
static INT32 spanstart[MAXVIDHEIGHT];
//
// texture mapping
//
lighttable_t **planezlight;
2014-03-15 16:59:03 +00:00
static fixed_t planeheight;
//added : 10-02-98: yslopetab is what yslope used to be,
// yslope points somewhere into yslopetab,
// now (viewheight/2) slopes are calculated above and
// below the original viewheight for mouselook
// (this is to calculate yslopes only when really needed)
// (when mouselookin', yslope is moving into yslopetab)
// Check R_SetupFrame, R_SetViewSize for more...
2018-12-14 17:08:25 +00:00
fixed_t yslopetab[MAXVIDHEIGHT*16];
2014-03-15 16:59:03 +00:00
fixed_t *yslope;
fixed_t basexscale, baseyscale;
fixed_t cachedheight[MAXVIDHEIGHT];
fixed_t cacheddistance[MAXVIDHEIGHT];
fixed_t cachedxstep[MAXVIDHEIGHT];
fixed_t cachedystep[MAXVIDHEIGHT];
static fixed_t xoffs, yoffs;
//
// R_InitPlanes
// Only at game startup.
//
void R_InitPlanes(void)
{
// FIXME: unused
}
//
// R_MapPlane
//
// Uses global vars:
// basexscale
// baseyscale
2018-12-14 17:08:25 +00:00
// centerx
2014-03-15 16:59:03 +00:00
// viewx
// viewy
2018-12-14 17:08:25 +00:00
// viewsin
// viewcos
// viewheight
2014-03-15 16:59:03 +00:00
#ifndef NOWATER
INT32 ds_bgofs;
INT32 ds_waterofs;
2014-03-15 16:59:03 +00:00
static INT32 wtofs=0;
static boolean itswater;
#endif
void R_MapPlane(INT32 y, INT32 x1, INT32 x2)
{
2018-12-14 17:08:25 +00:00
angle_t angle, planecos, planesin;
fixed_t distance, span;
2014-03-15 16:59:03 +00:00
size_t pindex;
#ifdef RANGECHECK
if (x2 < x1 || x1 < 0 || x2 >= viewwidth || y > viewheight)
I_Error("R_MapPlane: %d, %d at %d", x1, x2, y);
#endif
// from r_splats's R_RenderFloorSplat
if (x1 >= vid.width) x1 = vid.width - 1;
2018-12-14 17:08:25 +00:00
angle = (currentplane->viewangle + currentplane->plangle)>>ANGLETOFINESHIFT;
planecos = FINECOSINE(angle);
planesin = FINESINE(angle);
2014-03-15 16:59:03 +00:00
if (planeheight != cachedheight[y])
{
cachedheight[y] = planeheight;
distance = cacheddistance[y] = FixedMul(planeheight, yslope[y]);
ds_xstep = cachedxstep[y] = FixedMul(distance, basexscale);
ds_ystep = cachedystep[y] = FixedMul(distance, baseyscale);
2018-12-14 17:08:25 +00:00
if ((span = abs(centery-y)))
{
ds_xstep = cachedxstep[y] = FixedMul(planesin, planeheight) / span;
ds_ystep = cachedystep[y] = FixedMul(planecos, planeheight) / span;
}
2014-03-15 16:59:03 +00:00
}
else
{
distance = cacheddistance[y];
ds_xstep = cachedxstep[y];
ds_ystep = cachedystep[y];
}
2018-12-14 17:08:25 +00:00
ds_xfrac = xoffs + FixedMul(planecos, distance) + (x1 - centerx) * ds_xstep;
ds_yfrac = yoffs - FixedMul(planesin, distance) + (x1 - centerx) * ds_ystep;
2014-03-15 16:59:03 +00:00
#ifndef NOWATER
if (itswater)
{
const INT32 yay = (wtofs + (distance>>9) ) & 8191;
// ripples da water texture
ds_bgofs = FixedDiv(FINESINE(yay), (1<<12) + (distance>>11))>>FRACBITS;
2018-12-14 17:08:25 +00:00
angle = (currentplane->viewangle + currentplane->plangle + xtoviewangle[x1])>>ANGLETOFINESHIFT;
2014-03-15 16:59:03 +00:00
2018-12-14 17:08:25 +00:00
angle = (angle + 2048) & 8191; // 90 degrees
ds_xfrac += FixedMul(FINECOSINE(angle), (ds_bgofs<<FRACBITS));
ds_yfrac += FixedMul(FINESINE(angle), (ds_bgofs<<FRACBITS));
2014-03-15 16:59:03 +00:00
if (y+ds_bgofs>=viewheight)
ds_bgofs = viewheight-y-1;
if (y+ds_bgofs<0)
ds_bgofs = -y;
2014-03-15 16:59:03 +00:00
}
#endif
pindex = distance >> LIGHTZSHIFT;
if (pindex >= MAXLIGHTZ)
pindex = MAXLIGHTZ - 1;
#ifdef ESLOPE
if (currentplane->slope)
ds_colormap = colormaps;
else
#endif
2014-03-15 16:59:03 +00:00
ds_colormap = planezlight[pindex];
if (currentplane->extra_colormap)
ds_colormap = currentplane->extra_colormap->colormap + (ds_colormap - colormaps);
ds_y = y;
ds_x1 = x1;
ds_x2 = x2;
// profile drawer
#ifdef TIMING
ProfZeroTimer();
#endif
spanfunc();
#ifdef TIMING
RDMSR(0x10, &mycount);
mytotal += mycount; // 64bit add
if (!(nombre--))
I_Error("spanfunc() CPU Spy reports: 0x%d %d\n", *((INT32 *)&mytotal+1), (INT32)mytotal);
#endif
}
void R_ClearFFloorClips (void)
{
INT32 i, p;
// opening / clipping determination
for (i = 0; i < viewwidth; i++)
{
for (p = 0; p < MAXFFLOORS; p++)
{
ffloor[p].f_clip[i] = (INT16)viewheight;
ffloor[p].c_clip[i] = -1;
}
}
numffloors = 0;
}
2014-03-15 16:59:03 +00:00
//
// R_ClearPlanes
// At begining of frame.
//
void R_ClearPlanes(void)
{
INT32 i, p;
angle_t angle;
// opening / clipping determination
for (i = 0; i < viewwidth; i++)
{
floorclip[i] = (INT16)viewheight;
2018-12-14 17:08:25 +00:00
ceilingclip[i] = -1;
2014-03-15 16:59:03 +00:00
frontscale[i] = INT32_MAX;
for (p = 0; p < MAXFFLOORS; p++)
{
ffloor[p].f_clip[i] = (INT16)viewheight;
2018-12-14 17:08:25 +00:00
ffloor[p].c_clip[i] = -1;
2014-03-15 16:59:03 +00:00
}
}
for (i = 0; i < MAXVISPLANES; i++)
for (*freehead = visplanes[i], visplanes[i] = NULL;
freehead && *freehead ;)
{
freehead = &(*freehead)->next;
}
lastopening = openings;
// texture calculation
memset(cachedheight, 0, sizeof (cachedheight));
// left to right mapping
angle = (viewangle-ANGLE_90)>>ANGLETOFINESHIFT;
// scale will be unit scale at SCREENWIDTH/2 distance
basexscale = FixedDiv (FINECOSINE(angle),centerxfrac);
baseyscale = -FixedDiv (FINESINE(angle),centerxfrac);
}
static visplane_t *new_visplane(unsigned hash)
{
visplane_t *check = freetail;
if (!check)
{
check = calloc(2, sizeof (*check));
if (check == NULL) I_Error("%s: Out of memory", "new_visplane"); // FIXME: ugly
}
else
{
freetail = freetail->next;
if (!freetail)
freehead = &freetail;
}
check->next = visplanes[hash];
visplanes[hash] = check;
return check;
}
//
// R_FindPlane: Seek a visplane having the identical values:
// Same height, same flattexture, same lightlevel.
// If not, allocates another of them.
//
visplane_t *R_FindPlane(fixed_t height, INT32 picnum, INT32 lightlevel,
fixed_t xoff, fixed_t yoff, angle_t plangle, extracolormap_t *planecolormap,
2015-04-20 07:10:14 +00:00
ffloor_t *pfloor
#ifdef POLYOBJECTS_PLANES
, polyobj_t *polyobj
#endif
2015-04-20 07:10:14 +00:00
#ifdef ESLOPE
, pslope_t *slope
#endif
)
2014-03-15 16:59:03 +00:00
{
visplane_t *check;
unsigned hash;
2015-05-23 07:18:32 +00:00
#ifdef ESLOPE
if (slope); else // Don't mess with this right now if a slope is involved
#endif
2014-03-15 16:59:03 +00:00
{
xoff += viewx;
yoff -= viewy;
2017-03-30 20:57:36 +00:00
if (plangle != 0)
{
// Add the view offset, rotated by the plane angle.
fixed_t cosinecomponent = FINECOSINE(plangle>>ANGLETOFINESHIFT);
fixed_t sinecomponent = FINESINE(plangle>>ANGLETOFINESHIFT);
fixed_t oldxoff = xoff;
xoff = FixedMul(xoff,cosinecomponent)+FixedMul(yoff,sinecomponent);
yoff = -FixedMul(oldxoff,sinecomponent)+FixedMul(yoff,cosinecomponent);
}
2014-03-15 16:59:03 +00:00
}
#ifdef POLYOBJECTS_PLANES
if (polyobj)
{
if (polyobj->angle != 0)
{
angle_t fineshift = polyobj->angle >> ANGLETOFINESHIFT;
xoff -= FixedMul(FINECOSINE(fineshift), polyobj->centerPt.x)+FixedMul(FINESINE(fineshift), polyobj->centerPt.y);
yoff -= FixedMul(FINESINE(fineshift), polyobj->centerPt.x)-FixedMul(FINECOSINE(fineshift), polyobj->centerPt.y);
}
else
{
xoff -= polyobj->centerPt.x;
yoff += polyobj->centerPt.y;
}
}
#endif
2014-03-15 16:59:03 +00:00
// This appears to fix the Nimbus Ruins sky bug.
if (picnum == skyflatnum && pfloor)
{
height = 0; // all skies map together
lightlevel = 0;
}
// New visplane algorithm uses hash table
hash = visplane_hash(picnum, lightlevel, height);
for (check = visplanes[hash]; check; check = check->next)
{
2014-11-12 00:55:07 +00:00
#ifdef POLYOBJECTS_PLANES
if (check->polyobj && pfloor)
continue;
if (polyobj != check->polyobj)
continue;
2014-11-12 00:55:07 +00:00
#endif
2014-03-15 16:59:03 +00:00
if (height == check->height && picnum == check->picnum
&& lightlevel == check->lightlevel
&& xoff == check->xoffs && yoff == check->yoffs
&& planecolormap == check->extra_colormap
&& !pfloor && !check->ffloor
&& check->viewx == viewx && check->viewy == viewy && check->viewz == viewz
2015-04-20 07:10:14 +00:00
&& check->viewangle == viewangle
&& check->plangle == plangle
2015-04-20 07:10:14 +00:00
#ifdef ESLOPE
&& check->slope == slope
#endif
)
2014-03-15 16:59:03 +00:00
{
return check;
}
}
check = new_visplane(hash);
check->height = height;
check->picnum = picnum;
check->lightlevel = lightlevel;
check->minx = vid.width;
check->maxx = -1;
check->xoffs = xoff;
check->yoffs = yoff;
check->extra_colormap = planecolormap;
check->ffloor = pfloor;
check->viewx = viewx;
check->viewy = viewy;
2014-03-15 16:59:03 +00:00
check->viewz = viewz;
check->viewangle = viewangle;
2014-03-15 16:59:03 +00:00
check->plangle = plangle;
2014-11-12 00:55:07 +00:00
#ifdef POLYOBJECTS_PLANES
check->polyobj = polyobj;
2014-11-12 00:55:07 +00:00
#endif
2015-04-20 07:10:14 +00:00
#ifdef ESLOPE
check->slope = slope;
#endif
2014-03-15 16:59:03 +00:00
memset(check->top, 0xff, sizeof (check->top));
memset(check->bottom, 0x00, sizeof (check->bottom));
return check;
}
//
// R_CheckPlane: return same visplane or alloc a new one if needed
//
visplane_t *R_CheckPlane(visplane_t *pl, INT32 start, INT32 stop)
{
INT32 intrl, intrh;
INT32 unionl, unionh;
INT32 x;
if (start < pl->minx)
{
intrl = pl->minx;
unionl = start;
}
else
{
unionl = pl->minx;
intrl = start;
}
if (stop > pl->maxx)
{
intrh = pl->maxx;
unionh = stop;
}
else
{
unionh = pl->maxx;
intrh = stop;
}
// 0xff is not equal to -1 with shorts...
for (x = intrl; x <= intrh; x++)
if (pl->top[x] != 0xffff || pl->bottom[x] != 0x0000)
break;
if (x > intrh) /* Can use existing plane; extend range */
{
pl->minx = unionl;
pl->maxx = unionh;
}
else /* Cannot use existing plane; create a new one */
{
unsigned hash =
visplane_hash(pl->picnum, pl->lightlevel, pl->height);
visplane_t *new_pl = new_visplane(hash);
new_pl->height = pl->height;
new_pl->picnum = pl->picnum;
new_pl->lightlevel = pl->lightlevel;
new_pl->xoffs = pl->xoffs;
new_pl->yoffs = pl->yoffs;
new_pl->extra_colormap = pl->extra_colormap;
new_pl->ffloor = pl->ffloor;
new_pl->viewx = pl->viewx;
new_pl->viewy = pl->viewy;
2014-03-15 16:59:03 +00:00
new_pl->viewz = pl->viewz;
new_pl->viewangle = pl->viewangle;
new_pl->plangle = pl->plangle;
#ifdef POLYOBJECTS_PLANES
new_pl->polyobj = pl->polyobj;
2015-04-20 07:10:14 +00:00
#endif
#ifdef ESLOPE
new_pl->slope = pl->slope;
2014-03-15 16:59:03 +00:00
#endif
pl = new_pl;
pl->minx = start;
pl->maxx = stop;
memset(pl->top, 0xff, sizeof pl->top);
memset(pl->bottom, 0x00, sizeof pl->bottom);
}
return pl;
}
//
// R_ExpandPlane
//
// This function basically expands the visplane or I_Errors.
// The reason for this is that when creating 3D floor planes, there is no
// need to create new ones with R_CheckPlane, because 3D floor planes
// are created by subsector and there is no way a subsector can graphically
// overlap.
void R_ExpandPlane(visplane_t *pl, INT32 start, INT32 stop)
{
2018-02-07 18:11:32 +00:00
// INT32 unionl, unionh;
2014-03-15 16:59:03 +00:00
// INT32 x;
#ifdef POLYOBJECTS_PLANES
// Don't expand polyobject planes here - we do that on our own.
if (pl->polyobj)
return;
#endif
2018-02-07 18:11:32 +00:00
if (pl->minx > start) pl->minx = start;
if (pl->maxx < stop) pl->maxx = stop;
/*
2014-03-15 16:59:03 +00:00
if (start < pl->minx)
{
unionl = start;
}
else
{
unionl = pl->minx;
}
if (stop > pl->maxx)
{
unionh = stop;
}
else
{
unionh = pl->maxx;
}
for (x = start; x <= stop; x++)
if (pl->top[x] != 0xffff || pl->bottom[x] != 0x0000)
break;
if (x <= stop)
I_Error("R_ExpandPlane: planes in same subsector overlap?!\nminx: %d, maxx: %d, start: %d, stop: %d\n", pl->minx, pl->maxx, start, stop);
2018-02-07 18:11:32 +00:00
2014-03-15 16:59:03 +00:00
pl->minx = unionl, pl->maxx = unionh;
2018-02-07 18:11:32 +00:00
*/
2014-03-15 16:59:03 +00:00
}
//
// R_MakeSpans
//
void R_MakeSpans(INT32 x, INT32 t1, INT32 b1, INT32 t2, INT32 b2)
{
// Alam: from r_splats's R_RenderFloorSplat
if (t1 >= vid.height) t1 = vid.height-1;
if (b1 >= vid.height) b1 = vid.height-1;
if (t2 >= vid.height) t2 = vid.height-1;
if (b2 >= vid.height) b2 = vid.height-1;
if (x-1 >= vid.width) x = vid.width;
while (t1 < t2 && t1 <= b1)
{
R_MapPlane(t1, spanstart[t1], x - 1);
t1++;
}
while (b1 > b2 && b1 >= t1)
{
R_MapPlane(b1, spanstart[b1], x - 1);
b1--;
}
while (t2 < t1 && t2 <= b2)
spanstart[t2++] = x;
while (b2 > b1 && b2 >= t2)
spanstart[b2--] = x;
}
void R_DrawPlanes(void)
{
visplane_t *pl;
INT32 i;
// Note: are these two lines really needed?
// R_DrawSinglePlane and R_DrawSkyPlane do span/column drawer resets themselves anyway
2014-03-15 16:59:03 +00:00
spanfunc = basespanfunc;
wallcolfunc = walldrawerfunc;
for (i = 0; i < MAXVISPLANES; i++, pl++)
{
for (pl = visplanes[i]; pl; pl = pl->next)
{
if (pl->ffloor != NULL
#ifdef POLYOBJECTS_PLANES
|| pl->polyobj != NULL
#endif
)
2014-03-15 16:59:03 +00:00
continue;
R_DrawSinglePlane(pl);
}
}
#ifndef NOWATER
ds_waterofs = (leveltime & 1)*16384;
2014-03-15 16:59:03 +00:00
wtofs = leveltime * 140;
#endif
}
// R_DrawSkyPlane
//
// Draws the sky within the plane's top/bottom bounds
// Note: this uses column drawers instead of span drawers, since the sky is always a texture
//
static void R_DrawSkyPlane(visplane_t *pl)
{
INT32 x;
INT32 angle;
// Reset column drawer function (note: couldn't we just call walldrawerfunc directly?)
// (that is, unless we'll need to switch drawers in future for some reason)
wallcolfunc = walldrawerfunc;
// use correct aspect ratio scale
dc_iscale = skyscale;
// Sky is always drawn full bright,
// i.e. colormaps[0] is used.
// Because of this hack, sky is not affected
// by sector colormaps (INVUL inverse mapping is not implemented in SRB2 so is irrelevant).
dc_colormap = colormaps;
dc_texturemid = skytexturemid;
dc_texheight = textureheight[skytexture]
>>FRACBITS;
for (x = pl->minx; x <= pl->maxx; x++)
{
dc_yl = pl->top[x];
dc_yh = pl->bottom[x];
if (dc_yl <= dc_yh)
{
angle = (pl->viewangle + xtoviewangle[x])>>ANGLETOSKYSHIFT;
dc_iscale = FixedMul(skyscale, FINECOSINE(xtoviewangle[x]>>ANGLETOFINESHIFT));
dc_x = x;
dc_source =
R_GetColumn(texturetranslation[skytexture],
-angle); // get negative of angle for each column to display sky correct way round! --Monster Iestyn 27/01/18
wallcolfunc();
}
}
}
2019-10-13 21:37:41 +00:00
//
// R_CheckPowersOfTwo
//
// Self-explanatory?
//
2019-05-21 18:24:26 +00:00
boolean R_CheckPowersOfTwo(void)
{
2019-09-12 17:32:31 +00:00
boolean wpow2 = (!(ds_flatwidth & (ds_flatwidth - 1)));
boolean hpow2 = (!(ds_flatheight & (ds_flatheight - 1)));
// Initially, the flat isn't powers-of-two-sized.
ds_powersoftwo = false;
// But if the width and height are powers of two,
// and are EQUAL, then it's okay :]
if ((ds_flatwidth == ds_flatheight) && (wpow2 && hpow2))
ds_powersoftwo = true;
2019-09-12 17:32:31 +00:00
// Just return ds_powersoftwo.
return ds_powersoftwo;
2019-05-21 18:24:26 +00:00
}
2019-10-13 21:37:41 +00:00
//
// R_CheckFlatLength
//
// Determine the flat's dimensions from the lump length.
//
2019-05-21 18:24:26 +00:00
void R_CheckFlatLength(size_t size)
{
switch (size)
{
case 4194304: // 2048x2048 lump
nflatmask = 0x3FF800;
nflatxshift = 21;
nflatyshift = 10;
nflatshiftup = 5;
ds_flatwidth = ds_flatheight = 2048;
break;
case 1048576: // 1024x1024 lump
nflatmask = 0xFFC00;
nflatxshift = 22;
nflatyshift = 12;
nflatshiftup = 6;
ds_flatwidth = ds_flatheight = 1024;
break;
case 262144:// 512x512 lump
nflatmask = 0x3FE00;
nflatxshift = 23;
nflatyshift = 14;
nflatshiftup = 7;
ds_flatwidth = ds_flatheight = 512;
break;
case 65536: // 256x256 lump
nflatmask = 0xFF00;
nflatxshift = 24;
nflatyshift = 16;
nflatshiftup = 8;
ds_flatwidth = ds_flatheight = 256;
break;
case 16384: // 128x128 lump
nflatmask = 0x3F80;
nflatxshift = 25;
nflatyshift = 18;
nflatshiftup = 9;
ds_flatwidth = ds_flatheight = 128;
break;
case 1024: // 32x32 lump
nflatmask = 0x3E0;
nflatxshift = 27;
nflatyshift = 22;
nflatshiftup = 11;
ds_flatwidth = ds_flatheight = 32;
break;
default: // 64x64 lump
nflatmask = 0xFC0;
nflatxshift = 26;
nflatyshift = 20;
nflatshiftup = 10;
ds_flatwidth = ds_flatheight = 64;
break;
}
}
2019-10-13 21:37:41 +00:00
//
// R_GenerateFlat
//
// Generate a flat from specified width and height.
//
static UINT8 *R_GenerateFlat(UINT16 width, UINT16 height)
{
UINT8 *flat = Z_Malloc(width * height, PU_LEVEL, NULL);
memset(flat, TRANSPARENTPIXEL, width * height);
return flat;
}
//
// R_GetTextureFlat
//
// Convert a texture or patch to a flat.
//
static UINT8 *R_GetTextureFlat(levelflat_t *levelflat, boolean leveltexture, boolean ispng)
{
2019-06-28 22:43:37 +00:00
UINT8 *flat;
2019-05-21 18:24:26 +00:00
textureflat_t *texflat = &texflats[levelflat->texturenum];
patch_t *patch = NULL;
2019-05-21 18:24:26 +00:00
boolean texturechanged = (leveltexture ? (levelflat->texturenum != levelflat->lasttexturenum) : false);
2019-05-21 18:24:26 +00:00
// Check if the texture changed.
if (leveltexture && (!texturechanged))
{
if (texflat != NULL && texflat->flat)
{
2019-06-28 22:43:37 +00:00
flat = texflat->flat;
2019-05-21 18:24:26 +00:00
ds_flatwidth = texflat->width;
ds_flatheight = texflat->height;
texturechanged = false;
}
else
texturechanged = true;
}
// If the texture changed, or the patch doesn't exist, convert either of them to a flat.
if (levelflat->flatpatch == NULL || texturechanged)
{
2019-10-13 21:37:41 +00:00
// Level texture
2019-05-21 18:24:26 +00:00
if (leveltexture)
{
texture_t *texture = textures[levelflat->texturenum];
texflat->width = ds_flatwidth = texture->width;
texflat->height = ds_flatheight = texture->height;
2019-10-13 21:37:41 +00:00
texflat->flat = R_GenerateFlat(ds_flatwidth, ds_flatheight);
2019-05-27 02:37:23 +00:00
R_TextureToFlat(levelflat->texturenum, texflat->flat);
2019-06-28 22:43:37 +00:00
flat = texflat->flat;
2019-05-21 18:24:26 +00:00
2019-06-28 22:43:37 +00:00
levelflat->flatpatch = flat;
levelflat->width = ds_flatwidth;
levelflat->height = ds_flatheight;
2019-05-21 18:24:26 +00:00
}
2019-10-13 21:37:41 +00:00
// Patch (never happens yet)
2019-05-21 18:24:26 +00:00
else
{
patch = (patch_t *)ds_source;
2019-05-27 02:37:23 +00:00
#ifndef NO_PNG_LUMPS
if (ispng)
{
2019-10-13 21:37:41 +00:00
levelflat->flatpatch = R_PNGToFlat(&levelflat->width, &levelflat->height, ds_source, W_LumpLength(levelflat->lumpnum));
2019-05-27 02:37:23 +00:00
levelflat->topoffset = levelflat->leftoffset = 0;
ds_flatwidth = levelflat->width;
ds_flatheight = levelflat->height;
2019-05-27 02:37:23 +00:00
}
else
#endif
{
levelflat->width = ds_flatwidth = SHORT(patch->width);
levelflat->height = ds_flatheight = SHORT(patch->height);
2019-05-27 02:37:23 +00:00
levelflat->topoffset = patch->topoffset * FRACUNIT;
levelflat->leftoffset = patch->leftoffset * FRACUNIT;
2019-10-13 21:37:41 +00:00
levelflat->flatpatch = R_GenerateFlat(ds_flatwidth, ds_flatheight);
2019-05-27 02:37:23 +00:00
R_PatchToFlat(patch, levelflat->flatpatch);
}
2019-06-28 22:43:37 +00:00
flat = levelflat->flatpatch;
}
}
else
{
2019-06-28 22:43:37 +00:00
flat = levelflat->flatpatch;
ds_flatwidth = levelflat->width;
ds_flatheight = levelflat->height;
}
2019-10-13 21:37:41 +00:00
xoffs += levelflat->leftoffset;
yoffs += levelflat->topoffset;
2019-05-21 18:24:26 +00:00
levelflat->lasttexturenum = levelflat->texturenum;
2019-06-28 22:43:37 +00:00
return flat;
}
2014-03-15 16:59:03 +00:00
void R_DrawSinglePlane(visplane_t *pl)
{
2019-06-28 22:43:37 +00:00
UINT8 *flat;
2014-03-15 16:59:03 +00:00
INT32 light = 0;
INT32 x;
INT32 stop, angle;
size_t size;
ffloor_t *rover;
levelflat_t *levelflat;
2019-09-12 17:32:31 +00:00
boolean rawflat = false;
2014-03-15 16:59:03 +00:00
if (!(pl->minx <= pl->maxx))
return;
// sky flat
if (pl->picnum == skyflatnum)
{
R_DrawSkyPlane(pl);
return;
}
2014-03-15 16:59:03 +00:00
#ifndef NOWATER
itswater = false;
#endif
spanfunc = basespanfunc;
2014-11-12 00:55:07 +00:00
#ifdef POLYOBJECTS_PLANES
if (pl->polyobj && pl->polyobj->translucency != 0) {
spanfunc = R_DrawTranslucentSpan_8;
// Hacked up support for alpha value in software mode Tails 09-24-2002 (sidenote: ported to polys 10-15-2014, there was no time travel involved -Red)
if (pl->polyobj->translucency >= 10)
return; // Don't even draw it
else if (pl->polyobj->translucency > 0)
ds_transmap = transtables + ((pl->polyobj->translucency-1)<<FF_TRANSSHIFT);
2014-11-12 00:55:07 +00:00
else // Opaque, but allow transparent flat pixels
spanfunc = splatfunc;
if (!pl->extra_colormap || !(pl->extra_colormap->fog & 2))
2014-11-12 00:55:07 +00:00
light = (pl->lightlevel >> LIGHTSEGSHIFT);
else
light = LIGHTLEVELS-1;
2014-11-12 00:55:07 +00:00
} else
#endif
2014-03-15 16:59:03 +00:00
if (pl->ffloor)
{
// Don't draw planes that shouldn't be drawn.
for (rover = pl->ffloor->target->ffloors; rover; rover = rover->next)
{
if ((pl->ffloor->flags & FF_CUTEXTRA) && (rover->flags & FF_EXTRA))
{
if (pl->ffloor->flags & FF_EXTRA)
{
// The plane is from an extra 3D floor... Check the flags so
// there are no undesired cuts.
if (((pl->ffloor->flags & (FF_FOG|FF_SWIMMABLE)) == (rover->flags & (FF_FOG|FF_SWIMMABLE)))
&& pl->height < *rover->topheight
&& pl->height > *rover->bottomheight)
return;
}
}
}
if (pl->ffloor->flags & FF_TRANSLUCENT)
{
spanfunc = R_DrawTranslucentSpan_8;
// Hacked up support for alpha value in software mode Tails 09-24-2002
if (pl->ffloor->alpha < 12)
return; // Don't even draw it
else if (pl->ffloor->alpha < 38)
ds_transmap = transtables + ((tr_trans90-1)<<FF_TRANSSHIFT);
2014-03-15 16:59:03 +00:00
else if (pl->ffloor->alpha < 64)
ds_transmap = transtables + ((tr_trans80-1)<<FF_TRANSSHIFT);
2014-03-15 16:59:03 +00:00
else if (pl->ffloor->alpha < 89)
ds_transmap = transtables + ((tr_trans70-1)<<FF_TRANSSHIFT);
2014-03-15 16:59:03 +00:00
else if (pl->ffloor->alpha < 115)
ds_transmap = transtables + ((tr_trans60-1)<<FF_TRANSSHIFT);
2014-03-15 16:59:03 +00:00
else if (pl->ffloor->alpha < 140)
ds_transmap = transtables + ((tr_trans50-1)<<FF_TRANSSHIFT);
2014-03-15 16:59:03 +00:00
else if (pl->ffloor->alpha < 166)
ds_transmap = transtables + ((tr_trans40-1)<<FF_TRANSSHIFT);
2014-03-15 16:59:03 +00:00
else if (pl->ffloor->alpha < 192)
ds_transmap = transtables + ((tr_trans30-1)<<FF_TRANSSHIFT);
2014-03-15 16:59:03 +00:00
else if (pl->ffloor->alpha < 217)
ds_transmap = transtables + ((tr_trans20-1)<<FF_TRANSSHIFT);
2014-03-15 16:59:03 +00:00
else if (pl->ffloor->alpha < 243)
ds_transmap = transtables + ((tr_trans10-1)<<FF_TRANSSHIFT);
2014-03-15 16:59:03 +00:00
else // Opaque, but allow transparent flat pixels
spanfunc = splatfunc;
if (!pl->extra_colormap || !(pl->extra_colormap->fog & 2))
2014-03-15 16:59:03 +00:00
light = (pl->lightlevel >> LIGHTSEGSHIFT);
else
light = LIGHTLEVELS-1;
}
else if (pl->ffloor->flags & FF_FOG)
{
spanfunc = R_DrawFogSpan_8;
light = (pl->lightlevel >> LIGHTSEGSHIFT);
}
else light = (pl->lightlevel >> LIGHTSEGSHIFT);
#ifndef NOWATER
if (pl->ffloor->flags & FF_RIPPLE
#ifdef ESLOPE
&& !pl->slope
#endif
)
2014-03-15 16:59:03 +00:00
{
INT32 top, bottom;
itswater = true;
if (spanfunc == R_DrawTranslucentSpan_8)
{
spanfunc = R_DrawTranslucentWaterSpan_8;
// Copy the current scene, ugh
top = pl->high-8;
bottom = pl->low+8;
if (top < 0)
top = 0;
if (bottom > vid.height)
bottom = vid.height;
// Only copy the part of the screen we need
VID_BlitLinearScreen((splitscreen && viewplayer == &players[secondarydisplayplayer]) ? screens[0] + (top+(vid.height>>1))*vid.width : screens[0]+((top)*vid.width), screens[1]+((top)*vid.width),
vid.width, bottom-top,
vid.width, vid.width);
}
}
#endif
}
else light = (pl->lightlevel >> LIGHTSEGSHIFT);
2015-05-14 15:55:43 +00:00
#ifdef ESLOPE
if (!pl->slope) // Don't mess with angle on slopes! We'll handle this ourselves later
#endif
if (viewangle != pl->viewangle+pl->plangle)
2014-03-15 16:59:03 +00:00
{
memset(cachedheight, 0, sizeof (cachedheight));
angle = (pl->viewangle+pl->plangle-ANGLE_90)>>ANGLETOFINESHIFT;
2014-03-15 16:59:03 +00:00
basexscale = FixedDiv(FINECOSINE(angle),centerxfrac);
baseyscale = -FixedDiv(FINESINE(angle),centerxfrac);
viewangle = pl->viewangle+pl->plangle;
2014-03-15 16:59:03 +00:00
}
xoffs = pl->xoffs;
yoffs = pl->yoffs;
planeheight = abs(pl->height - pl->viewz);
2014-03-15 16:59:03 +00:00
currentplane = pl;
levelflat = &levelflats[pl->picnum];
2019-05-21 18:24:26 +00:00
size = W_LumpLength(levelflat->lumpnum);
2019-05-27 02:37:23 +00:00
ds_source = (UINT8 *)W_CacheLumpNum(levelflat->lumpnum, PU_STATIC); // Stay here until Z_ChangeTag
2014-03-15 16:59:03 +00:00
2019-05-26 19:22:33 +00:00
// Check if the flat is actually a wall texture.
if (levelflat->texturenum != 0 && levelflat->texturenum != -1)
2019-10-13 21:37:41 +00:00
flat = R_GetTextureFlat(levelflat, true, false);
2019-09-06 22:41:29 +00:00
#ifndef NO_PNG_LUMPS
2019-05-27 02:37:23 +00:00
// Maybe it's a PNG?!
else if (R_IsLumpPNG(ds_source, size))
2019-10-13 21:37:41 +00:00
flat = R_GetTextureFlat(levelflat, false, true);
2019-09-06 22:41:29 +00:00
#endif
// Maybe it's just a patch, then?
else if (R_CheckIfPatch(levelflat->lumpnum))
2019-10-13 21:37:41 +00:00
flat = R_GetTextureFlat(levelflat, false, false);
2019-05-26 19:22:33 +00:00
// It's a raw flat.
else
2014-03-15 16:59:03 +00:00
{
2019-09-12 17:32:31 +00:00
rawflat = true;
2019-05-21 18:24:26 +00:00
R_CheckFlatLength(size);
2019-06-28 22:43:37 +00:00
flat = ds_source;
2014-03-15 16:59:03 +00:00
}
2019-06-28 22:43:37 +00:00
Z_ChangeTag(ds_source, PU_CACHE);
ds_source = flat;
if (ds_source == NULL)
return;
2014-03-15 16:59:03 +00:00
2019-09-12 17:32:31 +00:00
// Raw flats always have dimensions that are powers-of-two numbers.
if (rawflat)
ds_powersoftwo = true;
// Otherwise, check if this texture or patch has such dimensions.
else if (R_CheckPowersOfTwo())
{
2019-05-21 18:24:26 +00:00
R_CheckFlatLength(ds_flatwidth * ds_flatheight);
if (spanfunc == basespanfunc)
spanfunc = mmxspanfunc;
}
if (light >= LIGHTLEVELS)
light = LIGHTLEVELS-1;
if (light < 0)
light = 0;
2015-04-20 07:10:14 +00:00
#ifdef ESLOPE
if (pl->slope) {
// Potentially override other stuff for now cus we're mean. :< But draw a slope plane!
// I copied ZDoom's code and adapted it to SRB2... -Red
floatv3_t p, m, n;
float ang;
float vx, vy, vz;
2019-06-25 17:40:00 +00:00
float fudge = 0;
// compiler complains when P_GetZAt is used in FLOAT_TO_FIXED directly
// use this as a temp var to store P_GetZAt's return value each time
fixed_t temp;
2015-04-20 07:10:14 +00:00
angle_t hack = (pl->plangle & (ANGLE_90-1));
yoffs *= 1;
if (ds_powersoftwo)
{
2019-09-07 21:20:49 +00:00
// Okay, look, don't ask me why this works, but without this setup there's a disgusting-looking misalignment with the textures. -Red
fudge = ((1<<nflatshiftup)+1.0f)/(1<<nflatshiftup);
if (hack)
{
/*
Essentially: We can't & the components along the regular axes when the plane is rotated.
This is because the distance on each regular axis in order to loop is different.
We rotate them, & the components, add them together, & them again, and then rotate them back.
These three seperate & operations are done per axis in order to prevent overflows.
toast 10/04/17
*/
const fixed_t cosinecomponent = FINECOSINE(hack>>ANGLETOFINESHIFT);
const fixed_t sinecomponent = FINESINE(hack>>ANGLETOFINESHIFT);
const fixed_t modmask = ((1 << (32-nflatshiftup)) - 1);
fixed_t ox = (FixedMul(pl->slope->o.x,cosinecomponent) & modmask) - (FixedMul(pl->slope->o.y,sinecomponent) & modmask);
fixed_t oy = (-FixedMul(pl->slope->o.x,sinecomponent) & modmask) - (FixedMul(pl->slope->o.y,cosinecomponent) & modmask);
temp = ox & modmask;
oy &= modmask;
ox = FixedMul(temp,cosinecomponent)+FixedMul(oy,-sinecomponent); // negative sine for opposite direction
oy = -FixedMul(temp,-sinecomponent)+FixedMul(oy,cosinecomponent);
temp = xoffs;
xoffs = (FixedMul(temp,cosinecomponent) & modmask) + (FixedMul(yoffs,sinecomponent) & modmask);
yoffs = (-FixedMul(temp,sinecomponent) & modmask) + (FixedMul(yoffs,cosinecomponent) & modmask);
temp = xoffs & modmask;
yoffs &= modmask;
xoffs = FixedMul(temp,cosinecomponent)+FixedMul(yoffs,-sinecomponent); // ditto
yoffs = -FixedMul(temp,-sinecomponent)+FixedMul(yoffs,cosinecomponent);
xoffs -= (pl->slope->o.x - ox);
yoffs += (pl->slope->o.y + oy);
}
else
{
xoffs &= ((1 << (32-nflatshiftup))-1);
yoffs &= ((1 << (32-nflatshiftup))-1);
xoffs -= (pl->slope->o.x + (1 << (31-nflatshiftup))) & ~((1 << (32-nflatshiftup))-1);
yoffs += (pl->slope->o.y + (1 << (31-nflatshiftup))) & ~((1 << (32-nflatshiftup))-1);
}
2019-06-25 17:40:00 +00:00
xoffs = (fixed_t)(xoffs*fudge);
yoffs = (fixed_t)(yoffs/fudge);
}
vx = FIXED_TO_FLOAT(pl->viewx+xoffs);
vy = FIXED_TO_FLOAT(pl->viewy-yoffs);
vz = FIXED_TO_FLOAT(pl->viewz);
2015-04-20 07:10:14 +00:00
temp = P_GetZAt(pl->slope, pl->viewx, pl->viewy);
zeroheight = FIXED_TO_FLOAT(temp);
2015-04-20 07:10:14 +00:00
#define ANG2RAD(angle) ((float)((angle)*M_PI)/ANGLE_180)
2015-04-20 07:10:14 +00:00
// p is the texture origin in view space
// Don't add in the offsets at this stage, because doing so can result in
// errors if the flat is rotated.
ang = ANG2RAD(ANGLE_270 - pl->viewangle);
p.x = vx * cos(ang) - vy * sin(ang);
p.z = vx * sin(ang) + vy * cos(ang);
temp = P_GetZAt(pl->slope, -xoffs, yoffs);
p.y = FIXED_TO_FLOAT(temp) - vz;
2015-04-20 07:10:14 +00:00
// m is the v direction vector in view space
ang = ANG2RAD(ANGLE_180 - (pl->viewangle + pl->plangle));
m.x = cos(ang);
m.z = sin(ang);
2015-04-20 07:10:14 +00:00
// n is the u direction vector in view space
n.x = sin(ang);
n.z = -cos(ang);
ang = ANG2RAD(pl->plangle);
temp = P_GetZAt(pl->slope, pl->viewx + FLOAT_TO_FIXED(sin(ang)), pl->viewy + FLOAT_TO_FIXED(cos(ang)));
m.y = FIXED_TO_FLOAT(temp) - zeroheight;
temp = P_GetZAt(pl->slope, pl->viewx + FLOAT_TO_FIXED(cos(ang)), pl->viewy - FLOAT_TO_FIXED(sin(ang)));
n.y = FIXED_TO_FLOAT(temp) - zeroheight;
2019-06-25 17:40:00 +00:00
if (ds_powersoftwo)
{
m.x /= fudge;
m.y /= fudge;
m.z /= fudge;
2019-06-25 17:40:00 +00:00
n.x *= fudge;
n.y *= fudge;
n.z *= fudge;
}
// Eh. I tried making this stuff fixed-point and it exploded on me. Here's a macro for the only floating-point vector function I recall using.
#define CROSS(d, v1, v2) \
d.x = (v1.y * v2.z) - (v1.z * v2.y);\
d.y = (v1.z * v2.x) - (v1.x * v2.z);\
d.z = (v1.x * v2.y) - (v1.y * v2.x)
CROSS(ds_su, p, m);
CROSS(ds_sv, p, n);
CROSS(ds_sz, m, n);
#undef CROSS
2015-04-20 07:10:14 +00:00
ds_su.z *= focallengthf;
ds_sv.z *= focallengthf;
ds_sz.z *= focallengthf;
// Premultiply the texture vectors with the scale factors
2019-09-12 17:32:31 +00:00
#define SFMULT 65536.f
2019-06-25 17:40:00 +00:00
if (ds_powersoftwo)
{
2019-09-12 17:32:31 +00:00
ds_su.x *= (SFMULT * (1<<nflatshiftup));
ds_su.y *= (SFMULT * (1<<nflatshiftup));
ds_su.z *= (SFMULT * (1<<nflatshiftup));
ds_sv.x *= (SFMULT * (1<<nflatshiftup));
ds_sv.y *= (SFMULT * (1<<nflatshiftup));
ds_sv.z *= (SFMULT * (1<<nflatshiftup));
}
else
{
// I'm essentially multiplying the vectors by FRACUNIT...
2019-06-25 17:40:00 +00:00
ds_su.x *= SFMULT;
ds_su.y *= SFMULT;
ds_su.z *= SFMULT;
ds_sv.x *= SFMULT;
ds_sv.y *= SFMULT;
ds_sv.z *= SFMULT;
}
2019-09-12 17:32:31 +00:00
#undef SFMULT
2015-04-20 07:10:14 +00:00
2015-05-17 17:24:20 +00:00
if (spanfunc == R_DrawTranslucentSpan_8)
spanfunc = R_DrawTiltedTranslucentSpan_8;
else if (spanfunc == splatfunc)
spanfunc = R_DrawTiltedSplat_8;
2015-05-17 17:24:20 +00:00
else
spanfunc = R_DrawTiltedSpan_8;
2015-04-20 07:10:14 +00:00
planezlight = scalelight[light];
} else
#endif // ESLOPE
2014-03-15 16:59:03 +00:00
planezlight = zlight[light];
// set the maximum value for unsigned
pl->top[pl->maxx+1] = 0xffff;
pl->top[pl->minx-1] = 0xffff;
pl->bottom[pl->maxx+1] = 0x0000;
pl->bottom[pl->minx-1] = 0x0000;
stop = pl->maxx + 1;
if (viewx != pl->viewx || viewy != pl->viewy)
{
viewx = pl->viewx;
viewy = pl->viewy;
}
if (viewz != pl->viewz)
viewz = pl->viewz;
2014-03-15 16:59:03 +00:00
for (x = pl->minx; x <= stop; x++)
{
R_MakeSpans(x, pl->top[x-1], pl->bottom[x-1],
pl->top[x], pl->bottom[x]);
}
/*
QUINCUNX anti-aliasing technique (sort of)
Normally, Quincunx antialiasing staggers pixels
in a 5-die pattern like so:
o o
o
o o
To simulate this, we offset the plane by
FRACUNIT/4 in each direction, and draw
at 50% translucency. The result is
a 'smoothing' of the texture while
using the palette colors.
*/
#ifdef QUINCUNX
if (spanfunc == R_DrawSpan_8)
{
INT32 i;
ds_transmap = transtables + ((tr_trans50-1)<<FF_TRANSSHIFT);
2014-03-15 16:59:03 +00:00
spanfunc = R_DrawTranslucentSpan_8;
for (i=0; i<4; i++)
{
xoffs = pl->xoffs;
yoffs = pl->yoffs;
switch(i)
{
case 0:
xoffs -= FRACUNIT/4;
yoffs -= FRACUNIT/4;
break;
case 1:
xoffs -= FRACUNIT/4;
yoffs += FRACUNIT/4;
break;
case 2:
xoffs += FRACUNIT/4;
yoffs -= FRACUNIT/4;
break;
case 3:
xoffs += FRACUNIT/4;
yoffs += FRACUNIT/4;
break;
}
planeheight = abs(pl->height - pl->viewz);
if (light >= LIGHTLEVELS)
light = LIGHTLEVELS-1;
if (light < 0)
light = 0;
planezlight = zlight[light];
// set the maximum value for unsigned
pl->top[pl->maxx+1] = 0xffff;
pl->top[pl->minx-1] = 0xffff;
pl->bottom[pl->maxx+1] = 0x0000;
pl->bottom[pl->minx-1] = 0x0000;
stop = pl->maxx + 1;
for (x = pl->minx; x <= stop; x++)
R_MakeSpans(x, pl->top[x-1], pl->bottom[x-1],
pl->top[x], pl->bottom[x]);
}
}
#endif
}
void R_PlaneBounds(visplane_t *plane)
{
INT32 i;
INT32 hi, low;
hi = plane->top[plane->minx];
low = plane->bottom[plane->minx];
for (i = plane->minx + 1; i <= plane->maxx; i++)
{
if (plane->top[i] < hi)
hi = plane->top[i];
if (plane->bottom[i] > low)
low = plane->bottom[i];
}
plane->high = hi;
plane->low = low;
}