From e09c7d239f57b8bebdc4a5f9046bfcb26ffac7cc Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Sat, 3 Dec 2016 14:58:56 +0100 Subject: [PATCH] Adjust class naming to closer match the GZDoom renderer and split portal rendering part to its own file --- src/CMakeLists.txt | 1 + src/r_poly.cpp | 16 +- src/r_poly.h | 7 +- src/r_poly_plane.cpp | 2 +- src/r_poly_portal.cpp | 320 +-------------------------------------- src/r_poly_portal.h | 84 +---------- src/r_poly_scene.cpp | 341 ++++++++++++++++++++++++++++++++++++++++++ src/r_poly_scene.h | 103 +++++++++++++ src/r_poly_sky.cpp | 2 +- src/r_swrenderer.cpp | 8 +- 10 files changed, 470 insertions(+), 414 deletions(-) create mode 100644 src/r_poly_scene.cpp create mode 100644 src/r_poly_scene.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 73a3074bb..ea07dec7b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1030,6 +1030,7 @@ set( NOT_COMPILED_SOURCE_FILES set( FASTMATH_PCH_SOURCES r_swrenderer.cpp r_poly.cpp + r_poly_scene.cpp r_poly_portal.cpp r_poly_cull.cpp r_poly_decal.cpp diff --git a/src/r_poly.cpp b/src/r_poly.cpp index cadf92adf..4ea047925 100644 --- a/src/r_poly.cpp +++ b/src/r_poly.cpp @@ -37,13 +37,13 @@ extern bool r_showviewer; ///////////////////////////////////////////////////////////////////////////// -RenderPolyScene *RenderPolyScene::Instance() +PolyRenderer *PolyRenderer::Instance() { - static RenderPolyScene scene; + static PolyRenderer scene; return &scene; } -void RenderPolyScene::RenderViewToCanvas(AActor *actor, DCanvas *canvas, int x, int y, int width, int height, bool dontmaplines) +void PolyRenderer::RenderViewToCanvas(AActor *actor, DCanvas *canvas, int x, int y, int width, int height, bool dontmaplines) { const bool savedviewactive = viewactive; const bool savedoutputformat = swrenderer::r_swtruecolor; @@ -70,7 +70,7 @@ void RenderPolyScene::RenderViewToCanvas(AActor *actor, DCanvas *canvas, int x, swrenderer::r_swtruecolor = savedoutputformat; } -void RenderPolyScene::RenderActorView(AActor *actor, bool dontmaplines) +void PolyRenderer::RenderActorView(AActor *actor, bool dontmaplines) { NetUpdate(); @@ -106,12 +106,12 @@ void RenderPolyScene::RenderActorView(AActor *actor, bool dontmaplines) NetUpdate(); } -void RenderPolyScene::RenderRemainingPlayerSprites() +void PolyRenderer::RenderRemainingPlayerSprites() { PlayerSprites.RenderRemainingSprites(); } -void RenderPolyScene::ClearBuffers() +void PolyRenderer::ClearBuffers() { PolyVertexBuffer::Clear(); PolyStencilBuffer::Instance()->Clear(RenderTarget->GetWidth(), RenderTarget->GetHeight(), 0); @@ -119,7 +119,7 @@ void RenderPolyScene::ClearBuffers() NextStencilValue = 0; } -void RenderPolyScene::SetSceneViewport() +void PolyRenderer::SetSceneViewport() { if (RenderTarget == screen) // Rendering to screen { @@ -138,7 +138,7 @@ void RenderPolyScene::SetSceneViewport() } } -void RenderPolyScene::SetupPerspectiveMatrix() +void PolyRenderer::SetupPerspectiveMatrix() { static bool bDidSetup = false; diff --git a/src/r_poly.h b/src/r_poly.h index d67e39570..bc108a8a1 100644 --- a/src/r_poly.h +++ b/src/r_poly.h @@ -36,15 +36,14 @@ class AActor; class DCanvas; -// Renders a scene -class RenderPolyScene +class PolyRenderer { public: void RenderViewToCanvas(AActor *actor, DCanvas *canvas, int x, int y, int width, int height, bool dontmaplines); void RenderActorView(AActor *actor, bool dontmaplines); void RenderRemainingPlayerSprites(); - static RenderPolyScene *Instance(); + static PolyRenderer *Instance(); uint32_t GetNextStencilValue() { uint32_t value = NextStencilValue; NextStencilValue += 2; return value; } @@ -54,7 +53,7 @@ private: void SetupPerspectiveMatrix(); TriMatrix WorldToClip; - RenderPolyPortal MainPortal; + RenderPolyScene MainPortal; PolySkyDome Skydome; RenderPolyPlayerSprites PlayerSprites; uint32_t NextStencilValue = 0; diff --git a/src/r_poly_plane.cpp b/src/r_poly_plane.cpp index 41898fa8b..24b00af00 100644 --- a/src/r_poly_plane.cpp +++ b/src/r_poly_plane.cpp @@ -254,7 +254,7 @@ void RenderPolyPlane::Render(const TriMatrix &worldToClip, const Vec4f &clipPlan if (swrenderer::fixedlightlev >= 0 || swrenderer::fixedcolormap) args.uniforms.light = 256; args.uniforms.flags = 0; - args.uniforms.subsectorDepth = isSky ? RenderPolyPortal::SkySubsectorDepth : subsectorDepth; + args.uniforms.subsectorDepth = isSky ? RenderPolyScene::SkySubsectorDepth : subsectorDepth; TriVertex *vertices = PolyVertexBuffer::GetVertices(sub->numlines); if (!vertices) diff --git a/src/r_poly_portal.cpp b/src/r_poly_portal.cpp index b5553665c..1bb600779 100644 --- a/src/r_poly_portal.cpp +++ b/src/r_poly_portal.cpp @@ -30,323 +30,13 @@ #include "r_poly.h" #include "gl/data/gl_data.h" -CVAR(Bool, r_debug_cull, 0, 0) -EXTERN_CVAR(Int, r_portal_recursions) - extern bool r_showviewer; ///////////////////////////////////////////////////////////////////////////// -RenderPolyPortal::RenderPolyPortal() -{ -} - -RenderPolyPortal::~RenderPolyPortal() -{ -} - -void RenderPolyPortal::SetViewpoint(const TriMatrix &worldToClip, const Vec4f &portalPlane, uint32_t stencilValue) -{ - WorldToClip = worldToClip; - StencilValue = stencilValue; - PortalPlane = portalPlane; -} - -void RenderPolyPortal::Render(int portalDepth) -{ - ClearBuffers(); - Cull.CullScene(WorldToClip, PortalPlane); - RenderSectors(); - RenderPortals(portalDepth); -} - -void RenderPolyPortal::ClearBuffers() -{ - SeenSectors.clear(); - SubsectorDepths.clear(); - TranslucentObjects.clear(); - SectorPortals.clear(); - LinePortals.clear(); - NextSubsectorDepth = 0; -} - -void RenderPolyPortal::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 RenderPolyPortal::RenderSubsector(subsector_t *sub) -{ - sector_t *frontsector = sub->sector; - frontsector->MoreFlags |= SECF_DRAWN; - - uint32_t subsectorDepth = NextSubsectorDepth++; - - if (sub->sector->CenterFloor() != sub->sector->CenterCeiling()) - { - RenderPolyPlane::RenderPlanes(WorldToClip, PortalPlane, sub, subsectorDepth, StencilValue, Cull.MaxCeilingHeight, Cull.MinFloorHeight, SectorPortals); - } - - 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 RenderPolyPortal::RenderSprite(AActor *thing, double sortDistance, const DVector2 &left, const DVector2 &right) -{ - if (numnodes == 0) - RenderSprite(thing, sortDistance, left, right, 0.0, 1.0, subsectors); - else - RenderSprite(thing, sortDistance, left, right, 0.0, 1.0, nodes + numnodes - 1); // The head node is the last node output. -} - -void RenderPolyPortal::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]; - } - - subsector_t *sub = (subsector_t *)((BYTE *)node - 1); - - auto it = SubsectorDepths.find(sub); - if (it != SubsectorDepths.end()) - TranslucentObjects.push_back({ thing, sub, it->second, sortDistance, (float)t1, (float)t2 }); -} - -void RenderPolyPortal::RenderLine(subsector_t *sub, seg_t *line, sector_t *frontsector, uint32_t subsectorDepth) -{ - // Reject lines not facing viewer - DVector2 pt1 = line->v1->fPos() - ViewPos; - DVector2 pt2 = line->v2->fPos() - ViewPos; - 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 - if (!swrenderer::r_dontmaplines && line->linedef && segmentRange != LineSegmentRange::AlwaysVisible) - { - 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; - RenderPolyWall::Render3DFloorLine(WorldToClip, PortalPlane, line, frontsector, subsectorDepth, StencilValue, fakeFloor, TranslucentObjects); - } - } - - // Render wall, and update culling info if its an occlusion blocker - if (RenderPolyWall::RenderLine(WorldToClip, PortalPlane, line, frontsector, subsectorDepth, StencilValue, TranslucentObjects, LinePortals)) - { - if (segmentRange == LineSegmentRange::HasSegment) - Cull.MarkSegmentCulled(sx1, sx2); - } -} - -void RenderPolyPortal::RenderPortals(int portalDepth) -{ - 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; - 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; - PolyTriangleDrawer::draw(args, TriDrawVariant::FillNormal, TriBlendMode::Copy); - } - } - - 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; - PolyTriangleDrawer::draw(args, TriDrawVariant::FillNormal, TriBlendMode::Copy); - } - } - } -} - -void RenderPolyPortal::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; - args.stencilwritevalue = StencilValue; - 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; - PolyTriangleDrawer::draw(args, TriDrawVariant::StencilClose, TriBlendMode::Copy); - } - } - - 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; - args.stencilwritevalue = StencilValue; - 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; - PolyTriangleDrawer::draw(args, TriDrawVariant::StencilClose, TriBlendMode::Copy); - } - } - } - - 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; - double distanceSquared = (thing->Pos() - ViewPos).LengthSquared(); - 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) - { - obj.wall.Render(WorldToClip, PortalPlane); - } - 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); - } - } -} - -///////////////////////////////////////////////////////////////////////////// - PolyDrawSectorPortal::PolyDrawSectorPortal(FSectorPortal *portal, bool ceiling) : Portal(portal), Ceiling(ceiling) { - StencilValue = RenderPolyScene::Instance()->GetNextStencilValue(); + StencilValue = PolyRenderer::Instance()->GetNextStencilValue(); } void PolyDrawSectorPortal::Render(int portalDepth) @@ -356,7 +46,7 @@ void PolyDrawSectorPortal::Render(int portalDepth) SaveGlobals(); - // To do: get this information from RenderPolyScene instead of duplicating the code.. + // To do: get this information from PolyRenderer instead of duplicating the code.. double radPitch = ViewPitch.Normalized180().Radians(); double angx = cos(radPitch); double angy = sin(radPitch) * glset.pixelstretch; @@ -444,19 +134,19 @@ void PolyDrawSectorPortal::RestoreGlobals() PolyDrawLinePortal::PolyDrawLinePortal(FLinePortal *portal) : Portal(portal) { - StencilValue = RenderPolyScene::Instance()->GetNextStencilValue(); + StencilValue = PolyRenderer::Instance()->GetNextStencilValue(); } PolyDrawLinePortal::PolyDrawLinePortal(line_t *mirror) : Mirror(mirror) { - StencilValue = RenderPolyScene::Instance()->GetNextStencilValue(); + StencilValue = PolyRenderer::Instance()->GetNextStencilValue(); } void PolyDrawLinePortal::Render(int portalDepth) { SaveGlobals(); - // To do: get this information from RenderPolyScene instead of duplicating the code.. + // To do: get this information from PolyRenderer instead of duplicating the code.. double radPitch = ViewPitch.Normalized180().Radians(); double angx = cos(radPitch); double angy = sin(radPitch) * glset.pixelstretch; diff --git a/src/r_poly_portal.h b/src/r_poly_portal.h index e983ebd46..7835bb042 100644 --- a/src/r_poly_portal.h +++ b/src/r_poly_portal.h @@ -22,85 +22,7 @@ #pragma once -#include -#include -#include -#include -#include "doomdata.h" -#include "r_utility.h" -#include "r_main.h" -#include "r_poly_triangle.h" -#include "r_poly_intersection.h" -#include "r_poly_wall.h" -#include "r_poly_sprite.h" -#include "r_poly_wallsprite.h" -#include "r_poly_playersprite.h" -#include "r_poly_particle.h" -#include "r_poly_plane.h" -#include "r_poly_cull.h" -#include -#include - -class PolyTranslucentObject -{ -public: - PolyTranslucentObject(particle_t *particle, subsector_t *sub, uint32_t subsectorDepth) : particle(particle), sub(sub), subsectorDepth(subsectorDepth) { } - PolyTranslucentObject(AActor *thing, subsector_t *sub, uint32_t subsectorDepth, double dist, float t1, float t2) : thing(thing), sub(sub), subsectorDepth(subsectorDepth), DistanceSquared(dist), SpriteLeft(t1), SpriteRight(t2) { } - PolyTranslucentObject(RenderPolyWall wall) : wall(wall), subsectorDepth(wall.SubsectorDepth), DistanceSquared(1.e6) { } - - bool operator<(const PolyTranslucentObject &other) const - { - return subsectorDepth != other.subsectorDepth ? subsectorDepth < other.subsectorDepth : DistanceSquared < other.DistanceSquared; - } - - particle_t *particle = nullptr; - AActor *thing = nullptr; - subsector_t *sub = nullptr; - - RenderPolyWall wall; - - uint32_t subsectorDepth = 0; - double DistanceSquared = 0.0; - - float SpriteLeft = 0.0f, SpriteRight = 1.0f; -}; - -class PolyDrawSectorPortal; -class PolyDrawLinePortal; - -// Renders everything from a specific viewpoint -class RenderPolyPortal -{ -public: - RenderPolyPortal(); - ~RenderPolyPortal(); - void SetViewpoint(const TriMatrix &worldToClip, const Vec4f &portalPlane, uint32_t stencilValue); - void Render(int portalDepth); - void RenderTranslucent(int portalDepth); - - static const uint32_t SkySubsectorDepth = 0x7fffffff; - -private: - void ClearBuffers(); - void RenderPortals(int portalDepth); - void RenderSectors(); - void RenderSubsector(subsector_t *sub); - void RenderLine(subsector_t *sub, seg_t *line, sector_t *frontsector, uint32_t subsectorDepth); - void RenderSprite(AActor *thing, double sortDistance, const DVector2 &left, const DVector2 &right); - void RenderSprite(AActor *thing, double sortDistance, DVector2 left, DVector2 right, double t1, double t2, void *node); - - TriMatrix WorldToClip; - Vec4f PortalPlane; - uint32_t StencilValue = 0; - PolyCull Cull; - uint32_t NextSubsectorDepth = 0; - std::set SeenSectors; - std::unordered_map SubsectorDepths; - std::vector TranslucentObjects; - - std::vector> SectorPortals; - std::vector> LinePortals; -}; +#include "r_poly_scene.h" struct PolyPortalVertexRange { @@ -128,7 +50,7 @@ private: void RestoreGlobals(); bool Ceiling; - RenderPolyPortal RenderPortal; + RenderPolyScene RenderPortal; int savedextralight; DVector3 savedpos; @@ -156,7 +78,7 @@ private: void SaveGlobals(); void RestoreGlobals(); - RenderPolyPortal RenderPortal; + RenderPolyScene RenderPortal; int savedextralight; DVector3 savedpos; diff --git a/src/r_poly_scene.cpp b/src/r_poly_scene.cpp new file mode 100644 index 000000000..7304000a6 --- /dev/null +++ b/src/r_poly_scene.cpp @@ -0,0 +1,341 @@ +/* +** 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 +#include "templates.h" +#include "doomdef.h" +#include "p_maputl.h" +#include "sbar.h" +#include "r_data/r_translate.h" +#include "r_poly_scene.h" +#include "r_poly.h" +#include "gl/data/gl_data.h" + +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; +} + +void RenderPolyScene::Render(int portalDepth) +{ + ClearBuffers(); + Cull.CullScene(WorldToClip, PortalPlane); + 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()) + { + RenderPolyPlane::RenderPlanes(WorldToClip, PortalPlane, sub, subsectorDepth, StencilValue, Cull.MaxCeilingHeight, Cull.MinFloorHeight, SectorPortals); + } + + 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) + RenderSprite(thing, sortDistance, left, right, 0.0, 1.0, subsectors); + else + RenderSprite(thing, sortDistance, left, right, 0.0, 1.0, nodes + numnodes - 1); // The head node is the last node output. +} + +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]; + } + + subsector_t *sub = (subsector_t *)((BYTE *)node - 1); + + 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) +{ + // Reject lines not facing viewer + DVector2 pt1 = line->v1->fPos() - ViewPos; + DVector2 pt2 = line->v2->fPos() - ViewPos; + 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 + if (!swrenderer::r_dontmaplines && line->linedef && segmentRange != LineSegmentRange::AlwaysVisible) + { + 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; + RenderPolyWall::Render3DFloorLine(WorldToClip, PortalPlane, line, frontsector, subsectorDepth, StencilValue, fakeFloor, TranslucentObjects); + } + } + + // Render wall, and update culling info if its an occlusion blocker + if (RenderPolyWall::RenderLine(WorldToClip, PortalPlane, line, frontsector, subsectorDepth, StencilValue, TranslucentObjects, LinePortals)) + { + if (segmentRange == LineSegmentRange::HasSegment) + Cull.MarkSegmentCulled(sx1, sx2); + } +} + +void RenderPolyScene::RenderPortals(int portalDepth) +{ + 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; + 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; + PolyTriangleDrawer::draw(args, TriDrawVariant::FillNormal, TriBlendMode::Copy); + } + } + + 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; + PolyTriangleDrawer::draw(args, TriDrawVariant::FillNormal, TriBlendMode::Copy); + } + } + } +} + +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; + args.stencilwritevalue = StencilValue; + 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; + PolyTriangleDrawer::draw(args, TriDrawVariant::StencilClose, TriBlendMode::Copy); + } + } + + 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; + args.stencilwritevalue = StencilValue; + 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; + PolyTriangleDrawer::draw(args, TriDrawVariant::StencilClose, TriBlendMode::Copy); + } + } + } + + 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; + double distanceSquared = (thing->Pos() - ViewPos).LengthSquared(); + 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) + { + obj.wall.Render(WorldToClip, PortalPlane); + } + 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); + } + } +} diff --git a/src/r_poly_scene.h b/src/r_poly_scene.h new file mode 100644 index 000000000..1e3037b12 --- /dev/null +++ b/src/r_poly_scene.h @@ -0,0 +1,103 @@ +/* +** 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. +** +*/ + +#pragma once + +#include +#include +#include +#include +#include "doomdata.h" +#include "r_utility.h" +#include "r_main.h" +#include "r_poly_triangle.h" +#include "r_poly_intersection.h" +#include "r_poly_wall.h" +#include "r_poly_sprite.h" +#include "r_poly_wallsprite.h" +#include "r_poly_playersprite.h" +#include "r_poly_particle.h" +#include "r_poly_plane.h" +#include "r_poly_cull.h" +#include +#include + +class PolyTranslucentObject +{ +public: + PolyTranslucentObject(particle_t *particle, subsector_t *sub, uint32_t subsectorDepth) : particle(particle), sub(sub), subsectorDepth(subsectorDepth) { } + PolyTranslucentObject(AActor *thing, subsector_t *sub, uint32_t subsectorDepth, double dist, float t1, float t2) : thing(thing), sub(sub), subsectorDepth(subsectorDepth), DistanceSquared(dist), SpriteLeft(t1), SpriteRight(t2) { } + PolyTranslucentObject(RenderPolyWall wall) : wall(wall), subsectorDepth(wall.SubsectorDepth), DistanceSquared(1.e6) { } + + bool operator<(const PolyTranslucentObject &other) const + { + return subsectorDepth != other.subsectorDepth ? subsectorDepth < other.subsectorDepth : DistanceSquared < other.DistanceSquared; + } + + particle_t *particle = nullptr; + AActor *thing = nullptr; + subsector_t *sub = nullptr; + + RenderPolyWall wall; + + uint32_t subsectorDepth = 0; + double DistanceSquared = 0.0; + + float SpriteLeft = 0.0f, SpriteRight = 1.0f; +}; + +class PolyDrawSectorPortal; +class PolyDrawLinePortal; + +// Renders everything from a specific viewpoint +class RenderPolyScene +{ +public: + RenderPolyScene(); + ~RenderPolyScene(); + void SetViewpoint(const TriMatrix &worldToClip, const Vec4f &portalPlane, uint32_t stencilValue); + void Render(int portalDepth); + void RenderTranslucent(int portalDepth); + + static const uint32_t SkySubsectorDepth = 0x7fffffff; + +private: + void ClearBuffers(); + void RenderPortals(int portalDepth); + void RenderSectors(); + void RenderSubsector(subsector_t *sub); + void RenderLine(subsector_t *sub, seg_t *line, sector_t *frontsector, uint32_t subsectorDepth); + void RenderSprite(AActor *thing, double sortDistance, const DVector2 &left, const DVector2 &right); + void RenderSprite(AActor *thing, double sortDistance, DVector2 left, DVector2 right, double t1, double t2, void *node); + + TriMatrix WorldToClip; + Vec4f PortalPlane; + uint32_t StencilValue = 0; + PolyCull Cull; + uint32_t NextSubsectorDepth = 0; + std::set SeenSectors; + std::unordered_map SubsectorDepths; + std::vector TranslucentObjects; + + std::vector> SectorPortals; + std::vector> LinePortals; +}; diff --git a/src/r_poly_sky.cpp b/src/r_poly_sky.cpp index 2331548b3..dafe1f7ec 100644 --- a/src/r_poly_sky.cpp +++ b/src/r_poly_sky.cpp @@ -55,7 +55,7 @@ void PolySkyDome::Render(const TriMatrix &worldToClip) PolyDrawArgs args; args.uniforms.light = 256; args.uniforms.flags = 0; - args.uniforms.subsectorDepth = RenderPolyPortal::SkySubsectorDepth; + args.uniforms.subsectorDepth = RenderPolyScene::SkySubsectorDepth; args.objectToClip = &objectToClip; args.stenciltestvalue = 255; args.stencilwritevalue = 255; diff --git a/src/r_swrenderer.cpp b/src/r_swrenderer.cpp index 56820e536..0c4925454 100644 --- a/src/r_swrenderer.cpp +++ b/src/r_swrenderer.cpp @@ -195,7 +195,7 @@ void FSoftwareRenderer::RenderView(player_t *player) bool saved_swtruecolor = r_swtruecolor; r_swtruecolor = screen->IsBgra(); - RenderPolyScene::Instance()->RenderActorView(player->mo, false); + PolyRenderer::Instance()->RenderActorView(player->mo, false); FCanvasTextureInfo::UpdateAll(); // Apply special colormap if the target cannot do it @@ -260,7 +260,7 @@ void FSoftwareRenderer::WriteSavePic (player_t *player, FileWriter *file, int wi pic->ObjectFlags |= OF_Fixed; pic->Lock (); if (r_polyrenderer) - RenderPolyScene::Instance()->RenderViewToCanvas(player->mo, pic, 0, 0, width, height, true); + PolyRenderer::Instance()->RenderViewToCanvas(player->mo, pic, 0, 0, width, height, true); else R_RenderViewToCanvas (player->mo, pic, 0, 0, width, height); screen->GetFlashedPalette (palette); @@ -285,7 +285,7 @@ void FSoftwareRenderer::DrawRemainingPlayerSprites() } else { - RenderPolyScene::Instance()->RenderRemainingPlayerSprites(); + PolyRenderer::Instance()->RenderRemainingPlayerSprites(); } } @@ -413,7 +413,7 @@ void FSoftwareRenderer::RenderTextureView (FCanvasTexture *tex, AActor *viewpoin DAngle savedfov = FieldOfView; R_SetFOV ((double)fov); if (r_polyrenderer) - RenderPolyScene::Instance()->RenderViewToCanvas(viewpoint, Canvas, 0, 0, tex->GetWidth(), tex->GetHeight(), tex->bFirstUpdate); + PolyRenderer::Instance()->RenderViewToCanvas(viewpoint, Canvas, 0, 0, tex->GetWidth(), tex->GetHeight(), tex->bFirstUpdate); else R_RenderViewToCanvas (viewpoint, Canvas, 0, 0, tex->GetWidth(), tex->GetHeight(), tex->bFirstUpdate); R_SetFOV (savedfov);