gzdoom-gles/src/swrenderer/things/r_decal.cpp

330 lines
8.6 KiB
C++
Raw Normal View History

//
// Copyright (C) 1993-1996 by id Software, Inc.
//
// This source is available for distribution and/or modification
// only under the terms of the DOOM Source Code License as
// published by id Software. All rights reserved.
//
// The source is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
// for more details.
//
2016-12-30 07:31:02 +00:00
#include <stdlib.h>
#include <stddef.h>
#include "templates.h"
#include "i_system.h"
#include "doomdef.h"
#include "doomstat.h"
#include "doomdata.h"
#include "p_lnspec.h"
#include "r_sky.h"
#include "v_video.h"
#include "m_swap.h"
#include "w_wad.h"
#include "stats.h"
#include "a_sharedglobal.h"
#include "d_net.h"
#include "g_level.h"
#include "swrenderer/scene/r_opaque_pass.h"
2016-12-30 07:31:02 +00:00
#include "r_decal.h"
2016-12-31 11:45:07 +00:00
#include "swrenderer/scene/r_3dfloors.h"
2016-12-30 07:31:02 +00:00
#include "swrenderer/drawers/r_draw.h"
#include "v_palette.h"
#include "r_data/colormaps.h"
2017-01-01 09:28:35 +00:00
#include "swrenderer/line/r_wallsetup.h"
#include "swrenderer/line/r_walldraw.h"
2016-12-31 11:45:07 +00:00
#include "swrenderer/segments/r_drawsegment.h"
#include "swrenderer/scene/r_portal.h"
#include "swrenderer/scene/r_scene.h"
#include "swrenderer/scene/r_viewport.h"
#include "swrenderer/scene/r_light.h"
#include "swrenderer/things/r_wallsprite.h"
2016-12-30 07:31:02 +00:00
#include "swrenderer/r_memory.h"
EXTERN_CVAR(Bool, r_fullbrightignoresectorcolor);
2016-12-30 07:31:02 +00:00
namespace swrenderer
{
2017-01-12 20:29:19 +00:00
void RenderDecal::RenderDecals(side_t *sidedef, drawseg_t *draw_segment, int wallshade, float lightleft, float lightstep, seg_t *curline, const FWallCoords &wallC, bool foggy, FDynamicColormap *basecolormap)
2016-12-30 07:31:02 +00:00
{
for (DBaseDecal *decal = sidedef->AttachedDecals; decal != NULL; decal = decal->WallNext)
{
2017-01-12 20:29:19 +00:00
Render(sidedef, decal, draw_segment, wallshade, lightleft, lightstep, curline, wallC, foggy, basecolormap, 0);
2016-12-30 07:31:02 +00:00
}
}
// pass = 0: when seg is first drawn
// = 1: drawing masked textures (including sprites)
// Currently, only pass = 0 is done or used
2017-01-12 20:29:19 +00:00
void RenderDecal::Render(side_t *wall, DBaseDecal *decal, drawseg_t *clipper, int wallshade, float lightleft, float lightstep, seg_t *curline, FWallCoords WallC, bool foggy, FDynamicColormap *basecolormap, int pass)
2016-12-30 07:31:02 +00:00
{
DVector2 decal_left, decal_right, decal_pos;
int x1, x2;
double yscale;
BYTE flipx;
double zpos;
int needrepeat = 0;
sector_t *front, *back;
bool calclighting;
bool rereadcolormap;
FDynamicColormap *usecolormap;
float light = 0;
short *mfloorclip;
short *mceilingclip;
2016-12-30 07:31:02 +00:00
if (decal->RenderFlags & RF_INVISIBLE || !viewactive || !decal->PicNum.isValid())
return;
// Determine actor z
zpos = decal->Z;
front = curline->frontsector;
back = (curline->backsector != NULL) ? curline->backsector : curline->frontsector;
switch (decal->RenderFlags & RF_RELMASK)
{
default:
zpos = decal->Z;
break;
case RF_RELUPPER:
if (curline->linedef->flags & ML_DONTPEGTOP)
{
zpos = decal->Z + front->GetPlaneTexZ(sector_t::ceiling);
}
else
{
zpos = decal->Z + back->GetPlaneTexZ(sector_t::ceiling);
}
break;
case RF_RELLOWER:
if (curline->linedef->flags & ML_DONTPEGBOTTOM)
{
zpos = decal->Z + front->GetPlaneTexZ(sector_t::ceiling);
}
else
{
zpos = decal->Z + back->GetPlaneTexZ(sector_t::floor);
}
break;
case RF_RELMID:
if (curline->linedef->flags & ML_DONTPEGBOTTOM)
{
zpos = decal->Z + front->GetPlaneTexZ(sector_t::floor);
}
else
{
zpos = decal->Z + front->GetPlaneTexZ(sector_t::ceiling);
}
}
2017-01-09 13:51:34 +00:00
FTexture *WallSpriteTile = TexMan(decal->PicNum, true);
2016-12-30 07:31:02 +00:00
flipx = (BYTE)(decal->RenderFlags & RF_XFLIP);
if (WallSpriteTile == NULL || WallSpriteTile->UseType == FTexture::TEX_Null)
{
return;
}
// Determine left and right edges of sprite. Since this sprite is bound
// to a wall, we use the wall's angle instead of the decal's. This is
// pretty much the same as what R_AddLine() does.
FWallCoords savecoord = WallC;
double edge_right = WallSpriteTile->GetWidth();
double edge_left = WallSpriteTile->LeftOffset;
edge_right = (edge_right - edge_left) * decal->ScaleX;
edge_left *= decal->ScaleX;
double dcx, dcy;
decal->GetXY(wall, dcx, dcy);
decal_pos = { dcx, dcy };
DVector2 angvec = (curline->v2->fPos() - curline->v1->fPos()).Unit();
float maskedScaleY;
2016-12-30 07:31:02 +00:00
decal_left = decal_pos - edge_left * angvec - ViewPos;
decal_right = decal_pos + edge_right * angvec - ViewPos;
double texturemid;
2016-12-30 07:31:02 +00:00
if (WallC.Init(decal_left, decal_right, TOO_CLOSE_Z))
goto done;
x1 = WallC.sx1;
x2 = WallC.sx2;
if (x1 >= clipper->x2 || x2 <= clipper->x1)
goto done;
FWallTmapVals WallT;
2016-12-30 07:31:02 +00:00
WallT.InitFromWallCoords(&WallC);
// Get the top and bottom clipping arrays
switch (decal->RenderFlags & RF_CLIPMASK)
{
case RF_CLIPFULL:
if (curline->backsector == NULL)
{
if (pass != 0)
{
goto done;
}
mceilingclip = walltop;
mfloorclip = wallbottom;
}
else if (pass == 0)
{
mceilingclip = walltop;
mfloorclip = RenderOpaquePass::Instance()->ceilingclip;
2016-12-30 07:31:02 +00:00
needrepeat = 1;
}
else
{
2017-01-15 21:21:21 +00:00
mceilingclip = clipper->sprtopclip - clipper->x1;
mfloorclip = clipper->sprbottomclip - clipper->x1;
2016-12-30 07:31:02 +00:00
}
break;
case RF_CLIPUPPER:
if (pass != 0)
{
goto done;
}
mceilingclip = walltop;
mfloorclip = RenderOpaquePass::Instance()->ceilingclip;
2016-12-30 07:31:02 +00:00
break;
case RF_CLIPMID:
if (curline->backsector != NULL && pass != 2)
{
goto done;
}
2017-01-15 21:21:21 +00:00
mceilingclip = clipper->sprtopclip - clipper->x1;
mfloorclip = clipper->sprbottomclip - clipper->x1;
2016-12-30 07:31:02 +00:00
break;
case RF_CLIPLOWER:
if (pass != 0)
{
goto done;
}
mceilingclip = RenderOpaquePass::Instance()->floorclip;
2016-12-30 07:31:02 +00:00
mfloorclip = wallbottom;
break;
}
yscale = decal->ScaleY;
texturemid = WallSpriteTile->TopOffset + (zpos - ViewPos.Z) / yscale;
2016-12-30 07:31:02 +00:00
// Clip sprite to drawseg
x1 = MAX<int>(clipper->x1, x1);
x2 = MIN<int>(clipper->x2, x2);
if (x1 >= x2)
{
goto done;
}
PrepWall(swall, lwall, WallSpriteTile->GetWidth(), x1, x2, WallT);
2016-12-30 07:31:02 +00:00
if (flipx)
{
int i;
int right = (WallSpriteTile->GetWidth() << FRACBITS) - 1;
for (i = x1; i < x2; i++)
{
lwall[i] = right - lwall[i];
}
}
// Prepare lighting
calclighting = false;
usecolormap = basecolormap;
rereadcolormap = true;
// Decals that are added to the scene must fade to black.
if (decal->RenderStyle == LegacyRenderStyles[STYLE_Add] && usecolormap->Fade != 0)
{
usecolormap = GetSpecialLights(usecolormap->Color, 0, usecolormap->Desaturate);
rereadcolormap = false;
}
light = lightleft + (x1 - savecoord.sx1) * lightstep;
2016-12-30 07:31:02 +00:00
if (fixedlightlev >= 0)
R_SetColorMapLight((r_fullbrightignoresectorcolor) ? &FullNormalLight : usecolormap, 0, FIXEDLIGHT2SHADE(fixedlightlev));
else if (fixedcolormap != NULL)
R_SetColorMapLight(fixedcolormap, 0, 0);
else if (!foggy && (decal->RenderFlags & RF_FULLBRIGHT))
R_SetColorMapLight((r_fullbrightignoresectorcolor) ? &FullNormalLight : usecolormap, 0, 0);
else
calclighting = true;
// Draw it
bool sprflipvert;
2016-12-30 07:31:02 +00:00
if (decal->RenderFlags & RF_YFLIP)
{
sprflipvert = true;
yscale = -yscale;
2017-01-12 21:52:17 +00:00
texturemid -= WallSpriteTile->GetHeight();
2016-12-30 07:31:02 +00:00
}
else
{
sprflipvert = false;
}
maskedScaleY = float(1 / yscale);
2016-12-30 07:31:02 +00:00
do
{
int x = x1;
2017-01-12 20:29:19 +00:00
bool visible = R_SetPatchStyle(decal->RenderStyle, (float)decal->Alpha, decal->Translation, decal->AlphaColor, basecolormap);
2016-12-30 07:31:02 +00:00
// R_SetPatchStyle can modify basecolormap.
if (rereadcolormap)
{
usecolormap = basecolormap;
}
if (visible)
{
while (x < x2)
{
if (calclighting)
{ // calculate lighting
R_SetColorMapLight(usecolormap, light, wallshade);
2016-12-30 07:31:02 +00:00
}
2017-01-12 21:52:17 +00:00
DrawColumn(x, WallSpriteTile, texturemid, maskedScaleY, sprflipvert, mfloorclip, mceilingclip);
light += lightstep;
2016-12-30 07:31:02 +00:00
x++;
}
}
// If this sprite is RF_CLIPFULL on a two-sided line, needrepeat will
// be set 1 if we need to draw on the lower wall. In all other cases,
// needrepeat will be 0, and the while will fail.
mceilingclip = RenderOpaquePass::Instance()->floorclip;
2016-12-30 07:31:02 +00:00
mfloorclip = wallbottom;
} while (needrepeat--);
colfunc = basecolfunc;
done:
WallC = savecoord;
}
2017-01-12 21:52:17 +00:00
void RenderDecal::DrawColumn(int x, FTexture *WallSpriteTile, double texturemid, float maskedScaleY, bool sprflipvert, const short *mfloorclip, const short *mceilingclip)
{
float iscale = swall[x] * maskedScaleY;
double spryscale = 1 / iscale;
double sprtopscreen;
if (sprflipvert)
2017-01-12 21:52:17 +00:00
sprtopscreen = CenterY + texturemid * spryscale;
else
2017-01-12 21:52:17 +00:00
sprtopscreen = CenterY - texturemid * spryscale;
R_DrawMaskedColumn(x, FLOAT2FIXED(iscale), WallSpriteTile, lwall[x], spryscale, sprtopscreen, sprflipvert, mfloorclip, mceilingclip);
}
2016-12-30 07:31:02 +00:00
}