2016-12-03 13:58:56 +00:00
|
|
|
/*
|
|
|
|
** Polygon Doom software renderer
|
|
|
|
** Copyright (c) 2016 Magnus Norddahl
|
|
|
|
**
|
|
|
|
** This software is provided 'as-is', without any express or implied
|
|
|
|
** warranty. In no event will the authors be held liable for any damages
|
|
|
|
** arising from the use of this software.
|
|
|
|
**
|
|
|
|
** Permission is granted to anyone to use this software for any purpose,
|
|
|
|
** including commercial applications, and to alter it and redistribute it
|
|
|
|
** freely, subject to the following restrictions:
|
|
|
|
**
|
|
|
|
** 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
** claim that you wrote the original software. If you use this software
|
|
|
|
** in a product, an acknowledgment in the product documentation would be
|
|
|
|
** appreciated but is not required.
|
|
|
|
** 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
** misrepresented as being the original software.
|
|
|
|
** 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "templates.h"
|
|
|
|
#include "doomdef.h"
|
|
|
|
#include "p_maputl.h"
|
|
|
|
#include "sbar.h"
|
|
|
|
#include "r_data/r_translate.h"
|
2016-12-27 05:31:55 +00:00
|
|
|
#include "polyrenderer/scene/poly_scene.h"
|
|
|
|
#include "polyrenderer/poly_renderer.h"
|
2017-01-13 14:22:22 +00:00
|
|
|
#include "swrenderer/scene/r_light.h"
|
2016-12-03 13:58:56 +00:00
|
|
|
|
|
|
|
CVAR(Bool, r_debug_cull, 0, 0)
|
|
|
|
EXTERN_CVAR(Int, r_portal_recursions)
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
RenderPolyScene::RenderPolyScene()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderPolyScene::~RenderPolyScene()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPolyScene::SetViewpoint(const TriMatrix &worldToClip, const Vec4f &portalPlane, uint32_t stencilValue)
|
|
|
|
{
|
|
|
|
WorldToClip = worldToClip;
|
|
|
|
StencilValue = stencilValue;
|
|
|
|
PortalPlane = portalPlane;
|
|
|
|
}
|
|
|
|
|
2016-12-09 02:17:35 +00:00
|
|
|
void RenderPolyScene::SetPortalSegments(const std::vector<PolyPortalSegment> &segments)
|
|
|
|
{
|
|
|
|
Cull.ClearSolidSegments();
|
|
|
|
for (const auto &segment : segments)
|
|
|
|
{
|
|
|
|
Cull.MarkSegmentCulled(segment.X1, segment.X2);
|
|
|
|
}
|
|
|
|
Cull.InvertSegments();
|
|
|
|
PortalSegmentsAdded = true;
|
|
|
|
}
|
|
|
|
|
2016-12-03 13:58:56 +00:00
|
|
|
void RenderPolyScene::Render(int portalDepth)
|
|
|
|
{
|
|
|
|
ClearBuffers();
|
2016-12-09 02:17:35 +00:00
|
|
|
if (!PortalSegmentsAdded)
|
|
|
|
Cull.ClearSolidSegments();
|
2016-12-03 13:58:56 +00:00
|
|
|
Cull.CullScene(WorldToClip, PortalPlane);
|
2016-12-09 02:17:35 +00:00
|
|
|
Cull.ClearSolidSegments();
|
2016-12-03 13:58:56 +00:00
|
|
|
RenderSectors();
|
|
|
|
RenderPortals(portalDepth);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPolyScene::ClearBuffers()
|
|
|
|
{
|
|
|
|
SeenSectors.clear();
|
|
|
|
SubsectorDepths.clear();
|
|
|
|
TranslucentObjects.clear();
|
|
|
|
SectorPortals.clear();
|
|
|
|
LinePortals.clear();
|
|
|
|
NextSubsectorDepth = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPolyScene::RenderSectors()
|
|
|
|
{
|
|
|
|
if (r_debug_cull)
|
|
|
|
{
|
|
|
|
for (auto it = Cull.PvsSectors.rbegin(); it != Cull.PvsSectors.rend(); ++it)
|
|
|
|
RenderSubsector(*it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (auto it = Cull.PvsSectors.begin(); it != Cull.PvsSectors.end(); ++it)
|
|
|
|
RenderSubsector(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPolyScene::RenderSubsector(subsector_t *sub)
|
|
|
|
{
|
|
|
|
sector_t *frontsector = sub->sector;
|
|
|
|
frontsector->MoreFlags |= SECF_DRAWN;
|
|
|
|
|
|
|
|
uint32_t subsectorDepth = NextSubsectorDepth++;
|
|
|
|
|
|
|
|
if (sub->sector->CenterFloor() != sub->sector->CenterCeiling())
|
|
|
|
{
|
2016-12-09 02:17:35 +00:00
|
|
|
RenderPolyPlane::RenderPlanes(WorldToClip, PortalPlane, Cull, sub, subsectorDepth, StencilValue, Cull.MaxCeilingHeight, Cull.MinFloorHeight, SectorPortals);
|
2016-12-03 13:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < sub->numlines; i++)
|
|
|
|
{
|
|
|
|
seg_t *line = &sub->firstline[i];
|
|
|
|
if (line->sidedef == nullptr || !(line->sidedef->Flags & WALLF_POLYOBJ))
|
|
|
|
{
|
|
|
|
RenderLine(sub, line, frontsector, subsectorDepth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mainBSP = ((unsigned int)(sub - subsectors) < (unsigned int)numsubsectors);
|
|
|
|
if (mainBSP)
|
|
|
|
{
|
|
|
|
int subsectorIndex = (int)(sub - subsectors);
|
|
|
|
for (int i = ParticlesInSubsec[subsectorIndex]; i != NO_PARTICLE; i = Particles[i].snext)
|
|
|
|
{
|
|
|
|
particle_t *particle = Particles + i;
|
|
|
|
TranslucentObjects.push_back({ particle, sub, subsectorDepth });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SeenSectors.insert(sub->sector);
|
|
|
|
SubsectorDepths[sub] = subsectorDepth;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPolyScene::RenderSprite(AActor *thing, double sortDistance, const DVector2 &left, const DVector2 &right)
|
|
|
|
{
|
|
|
|
if (numnodes == 0)
|
2016-12-07 21:26:18 +00:00
|
|
|
{
|
|
|
|
subsector_t *sub = subsectors;
|
|
|
|
auto it = SubsectorDepths.find(sub);
|
|
|
|
if (it != SubsectorDepths.end())
|
|
|
|
TranslucentObjects.push_back({ thing, sub, it->second, sortDistance, 0.0f, 1.0f });
|
|
|
|
}
|
2016-12-03 13:58:56 +00:00
|
|
|
else
|
2016-12-07 21:26:18 +00:00
|
|
|
{
|
2016-12-03 13:58:56 +00:00
|
|
|
RenderSprite(thing, sortDistance, left, right, 0.0, 1.0, nodes + numnodes - 1); // The head node is the last node output.
|
2016-12-07 21:26:18 +00:00
|
|
|
}
|
2016-12-03 13:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPolyScene::RenderSprite(AActor *thing, double sortDistance, DVector2 left, DVector2 right, double t1, double t2, void *node)
|
|
|
|
{
|
|
|
|
while (!((size_t)node & 1)) // Keep going until found a subsector
|
|
|
|
{
|
|
|
|
node_t *bsp = (node_t *)node;
|
|
|
|
|
|
|
|
DVector2 planePos(FIXED2DBL(bsp->x), FIXED2DBL(bsp->y));
|
|
|
|
DVector2 planeNormal = DVector2(FIXED2DBL(-bsp->dy), FIXED2DBL(bsp->dx));
|
|
|
|
double planeD = planeNormal | planePos;
|
|
|
|
|
|
|
|
int sideLeft = (left | planeNormal) > planeD;
|
|
|
|
int sideRight = (right | planeNormal) > planeD;
|
|
|
|
|
|
|
|
if (sideLeft != sideRight)
|
|
|
|
{
|
|
|
|
double dotLeft = planeNormal | left;
|
|
|
|
double dotRight = planeNormal | right;
|
|
|
|
double t = (planeD - dotLeft) / (dotRight - dotLeft);
|
|
|
|
|
|
|
|
DVector2 mid = left * (1.0 - t) + right * t;
|
|
|
|
double tmid = t1 * (1.0 - t) + t2 * t;
|
|
|
|
|
|
|
|
RenderSprite(thing, sortDistance, mid, right, tmid, t2, bsp->children[sideRight]);
|
|
|
|
right = mid;
|
|
|
|
t2 = tmid;
|
|
|
|
}
|
|
|
|
node = bsp->children[sideLeft];
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:54:41 +00:00
|
|
|
subsector_t *sub = (subsector_t *)((uint8_t *)node - 1);
|
2016-12-03 13:58:56 +00:00
|
|
|
|
|
|
|
auto it = SubsectorDepths.find(sub);
|
|
|
|
if (it != SubsectorDepths.end())
|
|
|
|
TranslucentObjects.push_back({ thing, sub, it->second, sortDistance, (float)t1, (float)t2 });
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPolyScene::RenderLine(subsector_t *sub, seg_t *line, sector_t *frontsector, uint32_t subsectorDepth)
|
|
|
|
{
|
2017-03-12 17:54:39 +00:00
|
|
|
const auto &viewpoint = PolyRenderer::Instance()->Thread.Viewport->viewpoint;
|
|
|
|
|
2016-12-03 13:58:56 +00:00
|
|
|
// Reject lines not facing viewer
|
2017-03-12 17:54:39 +00:00
|
|
|
DVector2 pt1 = line->v1->fPos() - viewpoint.Pos;
|
|
|
|
DVector2 pt2 = line->v2->fPos() - viewpoint.Pos;
|
2016-12-03 13:58:56 +00:00
|
|
|
if (pt1.Y * (pt1.X - pt2.X) + pt1.X * (pt2.Y - pt1.Y) >= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Cull wall if not visible
|
|
|
|
int sx1, sx2;
|
|
|
|
LineSegmentRange segmentRange = Cull.GetSegmentRangeForLine(line->v1->fX(), line->v1->fY(), line->v2->fX(), line->v2->fY(), sx1, sx2);
|
|
|
|
if (segmentRange == LineSegmentRange::NotVisible || (segmentRange == LineSegmentRange::HasSegment && Cull.IsSegmentCulled(sx1, sx2)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Tell automap we saw this
|
2016-12-16 19:52:56 +00:00
|
|
|
if (!PolyRenderer::Instance()->DontMapLines && line->linedef && segmentRange != LineSegmentRange::AlwaysVisible)
|
2016-12-03 13:58:56 +00:00
|
|
|
{
|
|
|
|
line->linedef->flags |= ML_MAPPED;
|
|
|
|
sub->flags |= SSECF_DRAWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render 3D floor sides
|
|
|
|
if (line->backsector && frontsector->e && line->backsector->e->XFloor.ffloors.Size())
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < line->backsector->e->XFloor.ffloors.Size(); i++)
|
|
|
|
{
|
|
|
|
F3DFloor *fakeFloor = line->backsector->e->XFloor.ffloors[i];
|
|
|
|
if (!(fakeFloor->flags & FF_EXISTS)) continue;
|
|
|
|
if (!(fakeFloor->flags & FF_RENDERPLANES)) continue;
|
|
|
|
if (!fakeFloor->model) continue;
|
2016-12-09 02:17:35 +00:00
|
|
|
RenderPolyWall::Render3DFloorLine(WorldToClip, PortalPlane, Cull, line, frontsector, subsectorDepth, StencilValue, fakeFloor, TranslucentObjects);
|
2016-12-03 13:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render wall, and update culling info if its an occlusion blocker
|
2016-12-09 02:17:35 +00:00
|
|
|
if (RenderPolyWall::RenderLine(WorldToClip, PortalPlane, Cull, line, frontsector, subsectorDepth, StencilValue, TranslucentObjects, LinePortals))
|
2016-12-03 13:58:56 +00:00
|
|
|
{
|
|
|
|
if (segmentRange == LineSegmentRange::HasSegment)
|
|
|
|
Cull.MarkSegmentCulled(sx1, sx2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPolyScene::RenderPortals(int portalDepth)
|
|
|
|
{
|
2017-03-06 21:14:54 +00:00
|
|
|
bool foggy = false;
|
2016-12-03 13:58:56 +00:00
|
|
|
if (portalDepth < r_portal_recursions)
|
|
|
|
{
|
|
|
|
for (auto &portal : SectorPortals)
|
|
|
|
portal->Render(portalDepth + 1);
|
|
|
|
|
|
|
|
for (auto &portal : LinePortals)
|
|
|
|
portal->Render(portalDepth + 1);
|
|
|
|
}
|
|
|
|
else // Fill with black
|
|
|
|
{
|
|
|
|
PolyDrawArgs args;
|
|
|
|
args.objectToClip = &WorldToClip;
|
|
|
|
args.mode = TriangleDrawMode::Fan;
|
2017-03-12 19:40:00 +00:00
|
|
|
args.uniforms.globvis = (float)PolyRenderer::Instance()->Thread.Light->WallGlobVis(foggy);
|
2016-12-03 13:58:56 +00:00
|
|
|
args.uniforms.color = 0;
|
|
|
|
args.uniforms.light = 256;
|
|
|
|
args.uniforms.flags = TriUniforms::fixed_light;
|
|
|
|
args.SetClipPlane(PortalPlane.x, PortalPlane.y, PortalPlane.z, PortalPlane.w);
|
|
|
|
|
|
|
|
for (auto &portal : SectorPortals)
|
|
|
|
{
|
|
|
|
args.stenciltestvalue = portal->StencilValue;
|
|
|
|
args.stencilwritevalue = portal->StencilValue + 1;
|
|
|
|
for (const auto &verts : portal->Shape)
|
|
|
|
{
|
|
|
|
args.vinput = verts.Vertices;
|
|
|
|
args.vcount = verts.Count;
|
|
|
|
args.ccw = verts.Ccw;
|
|
|
|
args.uniforms.subsectorDepth = verts.SubsectorDepth;
|
2016-12-15 23:35:45 +00:00
|
|
|
args.blendmode = TriBlendMode::Copy;
|
|
|
|
PolyTriangleDrawer::draw(args);
|
2016-12-03 13:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &portal : LinePortals)
|
|
|
|
{
|
|
|
|
args.stenciltestvalue = portal->StencilValue;
|
|
|
|
args.stencilwritevalue = portal->StencilValue + 1;
|
|
|
|
for (const auto &verts : portal->Shape)
|
|
|
|
{
|
|
|
|
args.vinput = verts.Vertices;
|
|
|
|
args.vcount = verts.Count;
|
|
|
|
args.ccw = verts.Ccw;
|
|
|
|
args.uniforms.subsectorDepth = verts.SubsectorDepth;
|
2016-12-15 23:35:45 +00:00
|
|
|
PolyTriangleDrawer::draw(args);
|
2016-12-03 13:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPolyScene::RenderTranslucent(int portalDepth)
|
|
|
|
{
|
|
|
|
if (portalDepth < r_portal_recursions)
|
|
|
|
{
|
|
|
|
for (auto it = SectorPortals.rbegin(); it != SectorPortals.rend(); ++it)
|
|
|
|
{
|
|
|
|
auto &portal = *it;
|
|
|
|
portal->RenderTranslucent(portalDepth + 1);
|
|
|
|
|
|
|
|
PolyDrawArgs args;
|
|
|
|
args.objectToClip = &WorldToClip;
|
|
|
|
args.mode = TriangleDrawMode::Fan;
|
|
|
|
args.stenciltestvalue = portal->StencilValue + 1;
|
2016-12-08 09:35:51 +00:00
|
|
|
args.stencilwritevalue = StencilValue + 1;
|
2016-12-03 13:58:56 +00:00
|
|
|
args.SetClipPlane(PortalPlane.x, PortalPlane.y, PortalPlane.z, PortalPlane.w);
|
|
|
|
for (const auto &verts : portal->Shape)
|
|
|
|
{
|
|
|
|
args.vinput = verts.Vertices;
|
|
|
|
args.vcount = verts.Count;
|
|
|
|
args.ccw = verts.Ccw;
|
|
|
|
args.uniforms.subsectorDepth = verts.SubsectorDepth;
|
2016-12-15 23:35:45 +00:00
|
|
|
args.writeColor = false;
|
|
|
|
PolyTriangleDrawer::draw(args);
|
2016-12-03 13:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto it = LinePortals.rbegin(); it != LinePortals.rend(); ++it)
|
|
|
|
{
|
|
|
|
auto &portal = *it;
|
|
|
|
portal->RenderTranslucent(portalDepth + 1);
|
|
|
|
|
|
|
|
PolyDrawArgs args;
|
|
|
|
args.objectToClip = &WorldToClip;
|
|
|
|
args.mode = TriangleDrawMode::Fan;
|
|
|
|
args.stenciltestvalue = portal->StencilValue + 1;
|
2016-12-08 09:35:51 +00:00
|
|
|
args.stencilwritevalue = StencilValue + 1;
|
2016-12-03 13:58:56 +00:00
|
|
|
args.SetClipPlane(PortalPlane.x, PortalPlane.y, PortalPlane.z, PortalPlane.w);
|
|
|
|
for (const auto &verts : portal->Shape)
|
|
|
|
{
|
|
|
|
args.vinput = verts.Vertices;
|
|
|
|
args.vcount = verts.Count;
|
|
|
|
args.ccw = verts.Ccw;
|
|
|
|
args.uniforms.subsectorDepth = verts.SubsectorDepth;
|
2016-12-15 23:35:45 +00:00
|
|
|
args.writeColor = false;
|
|
|
|
PolyTriangleDrawer::draw(args);
|
2016-12-03 13:58:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-12 17:54:39 +00:00
|
|
|
const auto &viewpoint = PolyRenderer::Instance()->Thread.Viewport->viewpoint;
|
2016-12-03 13:58:56 +00:00
|
|
|
for (sector_t *sector : SeenSectors)
|
|
|
|
{
|
|
|
|
for (AActor *thing = sector->thinglist; thing != nullptr; thing = thing->snext)
|
|
|
|
{
|
|
|
|
DVector2 left, right;
|
|
|
|
if (!RenderPolySprite::GetLine(thing, left, right))
|
|
|
|
continue;
|
2017-03-12 17:54:39 +00:00
|
|
|
double distanceSquared = (thing->Pos() - viewpoint.Pos).LengthSquared();
|
2016-12-03 13:58:56 +00:00
|
|
|
RenderSprite(thing, distanceSquared, left, right);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::stable_sort(TranslucentObjects.begin(), TranslucentObjects.end());
|
|
|
|
|
|
|
|
for (auto it = TranslucentObjects.rbegin(); it != TranslucentObjects.rend(); ++it)
|
|
|
|
{
|
|
|
|
auto &obj = *it;
|
|
|
|
if (obj.particle)
|
|
|
|
{
|
|
|
|
RenderPolyParticle spr;
|
|
|
|
spr.Render(WorldToClip, PortalPlane, obj.particle, obj.sub, obj.subsectorDepth, StencilValue + 1);
|
|
|
|
}
|
|
|
|
else if (!obj.thing)
|
|
|
|
{
|
2016-12-09 02:17:35 +00:00
|
|
|
obj.wall.Render(WorldToClip, PortalPlane, Cull);
|
2016-12-03 13:58:56 +00:00
|
|
|
}
|
|
|
|
else if ((obj.thing->renderflags & RF_SPRITETYPEMASK) == RF_WALLSPRITE)
|
|
|
|
{
|
|
|
|
RenderPolyWallSprite wallspr;
|
|
|
|
wallspr.Render(WorldToClip, PortalPlane, obj.thing, obj.sub, obj.subsectorDepth, StencilValue + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RenderPolySprite spr;
|
|
|
|
spr.Render(WorldToClip, PortalPlane, obj.thing, obj.sub, obj.subsectorDepth, StencilValue + 1, obj.SpriteLeft, obj.SpriteRight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|