2017-01-03 06:17:54 +00:00
|
|
|
//
|
|
|
|
// 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-31 10:42:49 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <float.h>
|
|
|
|
#include "templates.h"
|
|
|
|
#include "i_system.h"
|
|
|
|
#include "w_wad.h"
|
|
|
|
#include "doomdef.h"
|
|
|
|
#include "doomstat.h"
|
|
|
|
#include "r_sky.h"
|
|
|
|
#include "stats.h"
|
|
|
|
#include "v_video.h"
|
|
|
|
#include "a_sharedglobal.h"
|
|
|
|
#include "c_console.h"
|
|
|
|
#include "cmdlib.h"
|
|
|
|
#include "d_net.h"
|
|
|
|
#include "g_level.h"
|
2017-01-11 19:42:39 +00:00
|
|
|
#include "swrenderer/scene/r_opaque_pass.h"
|
2016-12-31 10:42:49 +00:00
|
|
|
#include "r_flatplane.h"
|
2016-12-31 11:45:07 +00:00
|
|
|
#include "swrenderer/scene/r_3dfloors.h"
|
2016-12-31 10:42:49 +00:00
|
|
|
#include "v_palette.h"
|
|
|
|
#include "r_data/colormaps.h"
|
|
|
|
#include "swrenderer/drawers/r_draw_rgba.h"
|
|
|
|
#include "gl/dynlights/gl_dynlight.h"
|
2016-12-31 11:45:07 +00:00
|
|
|
#include "swrenderer/segments/r_clipsegment.h"
|
|
|
|
#include "swrenderer/segments/r_drawsegment.h"
|
|
|
|
#include "swrenderer/scene/r_portal.h"
|
2017-01-12 15:21:46 +00:00
|
|
|
#include "swrenderer/scene/r_scene.h"
|
|
|
|
#include "swrenderer/scene/r_light.h"
|
2017-01-11 22:27:35 +00:00
|
|
|
#include "swrenderer/plane/r_visibleplane.h"
|
2017-02-02 14:10:06 +00:00
|
|
|
#include "swrenderer/viewport/r_viewport.h"
|
2016-12-31 10:42:49 +00:00
|
|
|
#include "swrenderer/r_memory.h"
|
|
|
|
|
|
|
|
namespace swrenderer
|
|
|
|
{
|
2017-01-28 15:36:39 +00:00
|
|
|
void RenderFlatPlane::Render(VisiblePlane *pl, double _xscale, double _yscale, fixed_t alpha, bool additive, bool masked, FDynamicColormap *colormap, FTexture *texture)
|
2016-12-31 10:42:49 +00:00
|
|
|
{
|
|
|
|
if (alpha <= 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-30 11:46:17 +00:00
|
|
|
drawerargs.SetSolidColor(3);
|
|
|
|
drawerargs.SetTexture(texture);
|
2017-01-28 15:36:39 +00:00
|
|
|
|
2016-12-31 10:42:49 +00:00
|
|
|
double planeang = (pl->xform.Angle + pl->xform.baseAngle).Radians();
|
|
|
|
double xstep, ystep, leftxfrac, leftyfrac, rightxfrac, rightyfrac;
|
|
|
|
double x;
|
|
|
|
|
2017-01-30 11:46:17 +00:00
|
|
|
xscale = xs_ToFixed(32 - drawerargs.TextureWidthBits(), _xscale);
|
|
|
|
yscale = xs_ToFixed(32 - drawerargs.TextureHeightBits(), _yscale);
|
2016-12-31 10:42:49 +00:00
|
|
|
if (planeang != 0)
|
|
|
|
{
|
|
|
|
double cosine = cos(planeang), sine = sin(planeang);
|
|
|
|
pviewx = FLOAT2FIXED(pl->xform.xOffs + ViewPos.X * cosine - ViewPos.Y * sine);
|
|
|
|
pviewy = FLOAT2FIXED(pl->xform.yOffs - ViewPos.X * sine - ViewPos.Y * cosine);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pviewx = FLOAT2FIXED(pl->xform.xOffs + ViewPos.X);
|
|
|
|
pviewy = FLOAT2FIXED(pl->xform.yOffs - ViewPos.Y);
|
|
|
|
}
|
|
|
|
|
|
|
|
pviewx = FixedMul(xscale, pviewx);
|
|
|
|
pviewy = FixedMul(yscale, pviewy);
|
|
|
|
|
|
|
|
// left to right mapping
|
|
|
|
planeang += (ViewAngle - 90).Radians();
|
|
|
|
|
2017-02-01 15:02:21 +00:00
|
|
|
auto viewport = RenderViewport::Instance();
|
|
|
|
|
2016-12-31 10:42:49 +00:00
|
|
|
// Scale will be unit scale at FocalLengthX (normally SCREENWIDTH/2) distance
|
2017-02-01 15:02:21 +00:00
|
|
|
xstep = cos(planeang) / viewport->FocalLengthX;
|
|
|
|
ystep = -sin(planeang) / viewport->FocalLengthX;
|
2016-12-31 10:42:49 +00:00
|
|
|
|
|
|
|
// [RH] flip for mirrors
|
2017-01-05 03:55:26 +00:00
|
|
|
RenderPortal *renderportal = RenderPortal::Instance();
|
|
|
|
if (renderportal->MirrorFlags & RF_XFLIP)
|
2016-12-31 10:42:49 +00:00
|
|
|
{
|
|
|
|
xstep = -xstep;
|
|
|
|
ystep = -ystep;
|
|
|
|
}
|
|
|
|
|
|
|
|
planeang += M_PI / 2;
|
|
|
|
double cosine = cos(planeang), sine = -sin(planeang);
|
|
|
|
x = pl->right - centerx - 0.5;
|
|
|
|
rightxfrac = _xscale * (cosine + x * xstep);
|
|
|
|
rightyfrac = _yscale * (sine + x * ystep);
|
|
|
|
x = pl->left - centerx - 0.5;
|
|
|
|
leftxfrac = _xscale * (cosine + x * xstep);
|
|
|
|
leftyfrac = _yscale * (sine + x * ystep);
|
|
|
|
|
|
|
|
basexfrac = rightxfrac;
|
|
|
|
baseyfrac = rightyfrac;
|
|
|
|
xstepscale = (rightxfrac - leftxfrac) / (pl->right - pl->left);
|
|
|
|
ystepscale = (rightyfrac - leftyfrac) / (pl->right - pl->left);
|
|
|
|
|
|
|
|
planeheight = fabs(pl->height.Zat0() - ViewPos.Z);
|
|
|
|
|
2017-01-12 20:29:19 +00:00
|
|
|
basecolormap = colormap;
|
2017-01-26 08:49:07 +00:00
|
|
|
GlobVis = LightVisibility::Instance()->FlatPlaneGlobVis() / planeheight;
|
2017-01-30 04:07:07 +00:00
|
|
|
|
2017-01-26 09:22:54 +00:00
|
|
|
CameraLight *cameraLight = CameraLight::Instance();
|
|
|
|
if (cameraLight->fixedlightlev >= 0)
|
2016-12-31 10:42:49 +00:00
|
|
|
{
|
2017-02-02 10:16:18 +00:00
|
|
|
drawerargs.SetLight(basecolormap, 0, FIXEDLIGHT2SHADE(cameraLight->fixedlightlev));
|
2016-12-31 10:42:49 +00:00
|
|
|
plane_shade = false;
|
|
|
|
}
|
2017-01-26 09:22:54 +00:00
|
|
|
else if (cameraLight->fixedcolormap)
|
2016-12-31 10:42:49 +00:00
|
|
|
{
|
2017-02-02 10:16:18 +00:00
|
|
|
drawerargs.SetLight(cameraLight->fixedcolormap, 0, 0);
|
2016-12-31 10:42:49 +00:00
|
|
|
plane_shade = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
plane_shade = true;
|
|
|
|
planeshade = LIGHT2SHADE(pl->lightlevel);
|
|
|
|
}
|
|
|
|
|
2017-01-30 11:46:17 +00:00
|
|
|
drawerargs.SetStyle(masked, additive, alpha);
|
2016-12-31 13:45:41 +00:00
|
|
|
|
2017-01-11 20:59:26 +00:00
|
|
|
light_list = pl->lights;
|
2016-12-31 13:45:41 +00:00
|
|
|
|
2017-01-11 20:59:26 +00:00
|
|
|
RenderLines(pl);
|
2016-12-31 10:42:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:59:26 +00:00
|
|
|
void RenderFlatPlane::RenderLine(int y, int x1, int x2)
|
2016-12-31 10:42:49 +00:00
|
|
|
{
|
|
|
|
double distance;
|
|
|
|
|
|
|
|
#ifdef RANGECHECK
|
|
|
|
if (x2 < x1 || x1<0 || x2 >= viewwidth || (unsigned)y >= (unsigned)viewheight)
|
|
|
|
{
|
|
|
|
I_FatalError("R_MapPlane: %i, %i at %i", x1, x2, y);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// [RH] Notice that I dumped the caching scheme used by Doom.
|
|
|
|
// It did not offer any appreciable speedup.
|
|
|
|
|
|
|
|
distance = planeheight * yslope[y];
|
|
|
|
|
2017-01-30 11:46:17 +00:00
|
|
|
if (drawerargs.TextureWidthBits() != 0)
|
2016-12-31 10:42:49 +00:00
|
|
|
{
|
2017-01-30 11:46:17 +00:00
|
|
|
drawerargs.SetTextureUStep(xs_ToFixed(32 - drawerargs.TextureWidthBits(), distance * xstepscale));
|
|
|
|
drawerargs.SetTextureUPos(xs_ToFixed(32 - drawerargs.TextureWidthBits(), distance * basexfrac) + pviewx);
|
2016-12-31 10:42:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-30 11:46:17 +00:00
|
|
|
drawerargs.SetTextureUStep(0);
|
|
|
|
drawerargs.SetTextureUPos(0);
|
2016-12-31 10:42:49 +00:00
|
|
|
}
|
2017-01-30 11:46:17 +00:00
|
|
|
|
|
|
|
if (drawerargs.TextureHeightBits() != 0)
|
2016-12-31 10:42:49 +00:00
|
|
|
{
|
2017-01-30 11:46:17 +00:00
|
|
|
drawerargs.SetTextureVStep(xs_ToFixed(32 - drawerargs.TextureHeightBits(), distance * ystepscale));
|
|
|
|
drawerargs.SetTextureVPos(xs_ToFixed(32 - drawerargs.TextureHeightBits(), distance * baseyfrac) + pviewy);
|
2016-12-31 10:42:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-30 11:46:17 +00:00
|
|
|
drawerargs.SetTextureVStep(0);
|
|
|
|
drawerargs.SetTextureVPos(0);
|
2016-12-31 10:42:49 +00:00
|
|
|
}
|
2017-02-01 15:02:21 +00:00
|
|
|
|
|
|
|
auto viewport = RenderViewport::Instance();
|
2016-12-31 10:42:49 +00:00
|
|
|
|
2017-02-01 20:42:08 +00:00
|
|
|
if (viewport->RenderTarget->IsBgra())
|
2016-12-31 10:42:49 +00:00
|
|
|
{
|
|
|
|
double distance2 = planeheight * yslope[(y + 1 < viewheight) ? y + 1 : y - 1];
|
2017-02-01 15:02:21 +00:00
|
|
|
double xmagnitude = fabs(ystepscale * (distance2 - distance) * viewport->FocalLengthX);
|
|
|
|
double ymagnitude = fabs(xstepscale * (distance2 - distance) * viewport->FocalLengthX);
|
2016-12-31 10:42:49 +00:00
|
|
|
double magnitude = MAX(ymagnitude, xmagnitude);
|
|
|
|
double min_lod = -1000.0;
|
2017-01-30 11:46:17 +00:00
|
|
|
drawerargs.SetTextureLOD(MAX(log2(magnitude) + r_lod_bias, min_lod));
|
2016-12-31 10:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (plane_shade)
|
|
|
|
{
|
|
|
|
// Determine lighting based on the span's distance from the viewer.
|
2017-02-02 10:16:18 +00:00
|
|
|
drawerargs.SetLight(basecolormap, (float)(GlobVis * fabs(viewport->CenterY - y)), planeshade);
|
2016-12-31 10:42:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r_dynlights)
|
|
|
|
{
|
|
|
|
// Find row position in view space
|
2017-02-01 15:02:21 +00:00
|
|
|
float zspan = (float)(planeheight / (fabs(y + 0.5 - viewport->CenterY) / viewport->InvZtoScale));
|
|
|
|
drawerargs.dc_viewpos.X = (float)((x1 + 0.5 - viewport->CenterX) / viewport->CenterX * zspan);
|
2017-01-29 06:49:04 +00:00
|
|
|
drawerargs.dc_viewpos.Y = zspan;
|
2017-02-01 15:02:21 +00:00
|
|
|
drawerargs.dc_viewpos.Z = (float)((viewport->CenterY - y - 0.5) / viewport->InvZtoScale * zspan);
|
|
|
|
drawerargs.dc_viewpos_step.X = (float)(zspan / viewport->CenterX);
|
2016-12-31 10:42:49 +00:00
|
|
|
|
|
|
|
static TriLight lightbuffer[64 * 1024];
|
|
|
|
static int nextlightindex = 0;
|
2017-01-02 05:52:50 +00:00
|
|
|
|
|
|
|
// Plane normal
|
2017-01-29 06:49:04 +00:00
|
|
|
drawerargs.dc_normal.X = 0.0f;
|
|
|
|
drawerargs.dc_normal.Y = 0.0f;
|
2017-02-01 15:02:21 +00:00
|
|
|
drawerargs.dc_normal.Z = (y >= viewport->CenterY) ? 1.0f : -1.0f;
|
2016-12-31 10:42:49 +00:00
|
|
|
|
2017-01-02 05:52:50 +00:00
|
|
|
// Setup lights for row
|
2017-01-29 06:49:04 +00:00
|
|
|
drawerargs.dc_num_lights = 0;
|
|
|
|
drawerargs.dc_lights = lightbuffer + nextlightindex;
|
2017-01-19 02:11:49 +00:00
|
|
|
VisiblePlaneLight *cur_node = light_list;
|
2016-12-31 10:42:49 +00:00
|
|
|
while (cur_node && nextlightindex < 64 * 1024)
|
|
|
|
{
|
|
|
|
double lightX = cur_node->lightsource->X() - ViewPos.X;
|
|
|
|
double lightY = cur_node->lightsource->Y() - ViewPos.Y;
|
|
|
|
double lightZ = cur_node->lightsource->Z() - ViewPos.Z;
|
|
|
|
|
|
|
|
float lx = (float)(lightX * ViewSin - lightY * ViewCos);
|
2017-01-29 06:49:04 +00:00
|
|
|
float ly = (float)(lightX * ViewTanCos + lightY * ViewTanSin) - drawerargs.dc_viewpos.Y;
|
|
|
|
float lz = (float)lightZ - drawerargs.dc_viewpos.Z;
|
2016-12-31 10:42:49 +00:00
|
|
|
|
|
|
|
// Precalculate the constant part of the dot here so the drawer doesn't have to.
|
2017-01-02 05:52:50 +00:00
|
|
|
bool is_point_light = (cur_node->lightsource->flags4 & MF4_ATTENUATE) != 0;
|
2016-12-31 10:42:49 +00:00
|
|
|
float lconstant = ly * ly + lz * lz;
|
2017-01-29 06:49:04 +00:00
|
|
|
float nlconstant = is_point_light ? lz * drawerargs.dc_normal.Z : 0.0f;
|
2016-12-31 10:42:49 +00:00
|
|
|
|
|
|
|
// Include light only if it touches this row
|
|
|
|
float radius = cur_node->lightsource->GetRadius();
|
2017-01-02 05:52:50 +00:00
|
|
|
if (radius * radius >= lconstant && nlconstant >= 0.0f)
|
2016-12-31 10:42:49 +00:00
|
|
|
{
|
|
|
|
uint32_t red = cur_node->lightsource->GetRed();
|
|
|
|
uint32_t green = cur_node->lightsource->GetGreen();
|
|
|
|
uint32_t blue = cur_node->lightsource->GetBlue();
|
|
|
|
|
|
|
|
nextlightindex++;
|
2017-01-29 06:49:04 +00:00
|
|
|
auto &light = drawerargs.dc_lights[drawerargs.dc_num_lights++];
|
2016-12-31 10:42:49 +00:00
|
|
|
light.x = lx;
|
|
|
|
light.y = lconstant;
|
2017-01-02 05:52:50 +00:00
|
|
|
light.z = nlconstant;
|
2016-12-31 10:42:49 +00:00
|
|
|
light.radius = 256.0f / radius;
|
|
|
|
light.color = (red << 16) | (green << 8) | blue;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_node = cur_node->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nextlightindex == 64 * 1024)
|
|
|
|
nextlightindex = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-29 06:49:04 +00:00
|
|
|
drawerargs.dc_num_lights = 0;
|
2016-12-31 10:42:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 11:46:17 +00:00
|
|
|
drawerargs.SetDestY(y);
|
|
|
|
drawerargs.SetDestX1(x1);
|
|
|
|
drawerargs.SetDestX2(x2);
|
2016-12-31 10:42:49 +00:00
|
|
|
|
2017-01-30 04:26:57 +00:00
|
|
|
drawerargs.DrawSpan();
|
2016-12-31 10:42:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:59:26 +00:00
|
|
|
void RenderFlatPlane::StepColumn()
|
2016-12-31 10:42:49 +00:00
|
|
|
{
|
2017-01-11 20:59:26 +00:00
|
|
|
basexfrac -= xstepscale;
|
|
|
|
baseyfrac -= ystepscale;
|
2016-12-31 10:42:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 20:59:26 +00:00
|
|
|
void RenderFlatPlane::SetupSlope()
|
2016-12-31 10:42:49 +00:00
|
|
|
{
|
2017-02-01 15:02:21 +00:00
|
|
|
auto viewport = RenderViewport::Instance();
|
2016-12-31 10:42:49 +00:00
|
|
|
|
2017-02-01 15:02:21 +00:00
|
|
|
int i = 0;
|
|
|
|
int e = viewheight;
|
|
|
|
float focus = float(viewport->FocalLengthY);
|
2016-12-31 10:42:49 +00:00
|
|
|
float den;
|
2017-02-01 15:02:21 +00:00
|
|
|
float cy = float(viewport->CenterY);
|
2016-12-31 10:42:49 +00:00
|
|
|
if (i < centery)
|
|
|
|
{
|
|
|
|
den = cy - i - 0.5f;
|
|
|
|
if (e <= centery)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
yslope[i] = focus / den;
|
|
|
|
den -= 1;
|
|
|
|
} while (++i < e);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
yslope[i] = focus / den;
|
|
|
|
den -= 1;
|
|
|
|
} while (++i < centery);
|
|
|
|
den = i - cy + 0.5f;
|
|
|
|
do {
|
|
|
|
yslope[i] = focus / den;
|
|
|
|
den += 1;
|
|
|
|
} while (++i < e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
den = i - cy + 0.5f;
|
|
|
|
do {
|
|
|
|
yslope[i] = focus / den;
|
|
|
|
den += 1;
|
|
|
|
} while (++i < e);
|
|
|
|
}
|
|
|
|
}
|
2017-01-11 20:59:26 +00:00
|
|
|
|
|
|
|
float RenderFlatPlane::yslope[MAXHEIGHT];
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2017-01-19 02:11:49 +00:00
|
|
|
void RenderColoredPlane::Render(VisiblePlane *pl)
|
2017-01-11 20:59:26 +00:00
|
|
|
{
|
|
|
|
RenderLines(pl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderColoredPlane::RenderLine(int y, int x1, int x2)
|
|
|
|
{
|
2017-01-29 06:49:04 +00:00
|
|
|
drawerargs.DrawColoredSpan(y, x1, x2);
|
2017-01-11 20:59:26 +00:00
|
|
|
}
|
2016-12-31 10:42:49 +00:00
|
|
|
}
|