2016-11-07 08:25:12 +00:00
|
|
|
/*
|
2016-11-14 13:19:48 +00:00
|
|
|
** Polygon Doom software renderer
|
2016-11-07 08:25:12 +00:00
|
|
|
** 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 "sbar.h"
|
|
|
|
#include "r_data/r_translate.h"
|
|
|
|
#include "r_poly.h"
|
2016-11-17 07:50:54 +00:00
|
|
|
#include "gl/data/gl_data.h"
|
2016-11-07 08:25:12 +00:00
|
|
|
|
2016-11-09 00:33:40 +00:00
|
|
|
CVAR(Bool, r_debug_cull, 0, 0)
|
2016-11-17 20:07:00 +00:00
|
|
|
EXTERN_CVAR(Int, screenblocks)
|
2016-11-17 07:50:54 +00:00
|
|
|
void InitGLRMapinfoData();
|
2016-11-09 00:33:40 +00:00
|
|
|
|
2016-11-07 08:25:12 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-11-14 13:19:48 +00:00
|
|
|
void RenderPolyScene::Render()
|
2016-11-07 08:25:12 +00:00
|
|
|
{
|
2016-11-16 10:18:40 +00:00
|
|
|
ClearBuffers();
|
2016-11-18 13:40:53 +00:00
|
|
|
SetSceneViewport();
|
2016-11-16 10:18:40 +00:00
|
|
|
SetupPerspectiveMatrix();
|
|
|
|
Cull.CullScene(WorldToClip);
|
|
|
|
RenderSectors();
|
|
|
|
skydome.Render(WorldToClip);
|
|
|
|
RenderTranslucent();
|
|
|
|
PlayerSprites.Render();
|
|
|
|
|
|
|
|
DrawerCommandQueue::WaitForWorkers();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPolyScene::RenderRemainingPlayerSprites()
|
|
|
|
{
|
|
|
|
PlayerSprites.RenderRemainingSprites();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPolyScene::ClearBuffers()
|
|
|
|
{
|
2016-11-07 08:25:12 +00:00
|
|
|
PolyVertexBuffer::Clear();
|
2016-11-09 10:38:07 +00:00
|
|
|
SectorSpriteRanges.clear();
|
|
|
|
SectorSpriteRanges.resize(numsectors);
|
|
|
|
SortedSprites.clear();
|
2016-11-13 14:16:55 +00:00
|
|
|
TranslucentObjects.clear();
|
2016-11-20 03:06:21 +00:00
|
|
|
PolyStencilBuffer::Instance()->Clear(RenderTarget->GetWidth(), RenderTarget->GetHeight(), 0);
|
|
|
|
PolySubsectorGBuffer::Instance()->Resize(RenderTarget->GetPitch(), RenderTarget->GetHeight());
|
2016-11-10 12:58:03 +00:00
|
|
|
NextSubsectorDepth = 0;
|
2016-11-16 10:18:40 +00:00
|
|
|
}
|
2016-11-09 10:38:07 +00:00
|
|
|
|
2016-11-18 13:40:53 +00:00
|
|
|
void RenderPolyScene::SetSceneViewport()
|
2016-11-16 10:18:40 +00:00
|
|
|
{
|
2016-11-17 20:07:00 +00:00
|
|
|
int height;
|
|
|
|
if (screenblocks >= 10)
|
|
|
|
height = SCREENHEIGHT;
|
|
|
|
else
|
|
|
|
height = (screenblocks*SCREENHEIGHT / 10) & ~7;
|
2016-11-18 00:58:39 +00:00
|
|
|
|
|
|
|
int bottom = SCREENHEIGHT - (height + viewwindowy - ((height - viewheight) / 2));
|
2016-11-20 03:06:21 +00:00
|
|
|
PolyTriangleDrawer::set_viewport(viewwindowx, SCREENHEIGHT - bottom - height, viewwidth, height, RenderTarget);
|
2016-11-18 13:40:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RenderPolyScene::SetupPerspectiveMatrix()
|
|
|
|
{
|
|
|
|
static bool bDidSetup = false;
|
|
|
|
|
|
|
|
if (!bDidSetup)
|
|
|
|
{
|
|
|
|
InitGLRMapinfoData();
|
|
|
|
bDidSetup = true;
|
|
|
|
}
|
2016-11-17 07:50:54 +00:00
|
|
|
|
2016-11-17 21:44:55 +00:00
|
|
|
// Code provided courtesy of Graf Zahl. Now we just have to plug it into the viewmatrix code...
|
|
|
|
// We have to scale the pitch to account for the pixel stretching, because the playsim doesn't know about this and treats it as 1:1.
|
|
|
|
double radPitch = ViewPitch.Normalized180().Radians();
|
|
|
|
double angx = cos(radPitch);
|
|
|
|
double angy = sin(radPitch) * glset.pixelstretch;
|
|
|
|
double alen = sqrt(angx*angx + angy*angy);
|
2016-11-18 13:40:53 +00:00
|
|
|
float adjustedPitch = (float)asin(angy / alen);
|
|
|
|
float adjustedViewAngle = (float)(ViewAngle - 90).Radians();
|
2016-11-17 21:44:55 +00:00
|
|
|
|
2016-11-07 08:25:12 +00:00
|
|
|
float ratio = WidescreenRatio;
|
|
|
|
float fovratio = (WidescreenRatio >= 1.3f) ? 1.333333f : ratio;
|
|
|
|
float fovy = (float)(2 * DAngle::ToDegrees(atan(tan(FieldOfView.Radians() / 2) / fovratio)).Degrees);
|
2016-11-18 13:40:53 +00:00
|
|
|
|
2016-11-07 08:25:12 +00:00
|
|
|
TriMatrix worldToView =
|
2016-11-18 13:40:53 +00:00
|
|
|
TriMatrix::rotate(adjustedPitch, 1.0f, 0.0f, 0.0f) *
|
|
|
|
TriMatrix::rotate(adjustedViewAngle, 0.0f, -1.0f, 0.0f) *
|
2016-11-17 21:44:55 +00:00
|
|
|
TriMatrix::scale(1.0f, glset.pixelstretch, 1.0f) *
|
2016-11-07 08:25:12 +00:00
|
|
|
TriMatrix::swapYZ() *
|
|
|
|
TriMatrix::translate((float)-ViewPos.X, (float)-ViewPos.Y, (float)-ViewPos.Z);
|
2016-11-18 00:58:39 +00:00
|
|
|
|
2016-11-14 13:19:48 +00:00
|
|
|
WorldToClip = TriMatrix::perspective(fovy, ratio, 5.0f, 65535.0f) * worldToView;
|
2016-11-16 10:18:40 +00:00
|
|
|
}
|
2016-11-07 08:25:12 +00:00
|
|
|
|
2016-11-16 10:18:40 +00:00
|
|
|
void RenderPolyScene::RenderSectors()
|
|
|
|
{
|
2016-11-10 09:44:35 +00:00
|
|
|
if (r_debug_cull)
|
2016-11-09 00:33:40 +00:00
|
|
|
{
|
2016-11-14 13:19:48 +00:00
|
|
|
for (auto it = Cull.PvsSectors.rbegin(); it != Cull.PvsSectors.rend(); ++it)
|
2016-11-09 00:33:40 +00:00
|
|
|
RenderSubsector(*it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-14 13:19:48 +00:00
|
|
|
for (auto it = Cull.PvsSectors.begin(); it != Cull.PvsSectors.end(); ++it)
|
2016-11-09 00:33:40 +00:00
|
|
|
RenderSubsector(*it);
|
|
|
|
}
|
2016-11-07 08:25:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 13:19:48 +00:00
|
|
|
void RenderPolyScene::RenderSubsector(subsector_t *sub)
|
2016-11-07 08:25:12 +00:00
|
|
|
{
|
|
|
|
sector_t *frontsector = sub->sector;
|
|
|
|
frontsector->MoreFlags |= SECF_DRAWN;
|
|
|
|
|
2016-11-10 12:58:03 +00:00
|
|
|
uint32_t subsectorDepth = NextSubsectorDepth++;
|
|
|
|
|
2016-11-11 18:26:28 +00:00
|
|
|
if (sub->sector->CenterFloor() != sub->sector->CenterCeiling())
|
|
|
|
{
|
2016-11-16 10:18:40 +00:00
|
|
|
RenderPolyPlane::RenderPlanes(WorldToClip, sub, subsectorDepth, Cull.MaxCeilingHeight, Cull.MinFloorHeight);
|
2016-11-11 18:26:28 +00:00
|
|
|
}
|
|
|
|
|
2016-11-07 08:25:12 +00:00
|
|
|
for (uint32_t i = 0; i < sub->numlines; i++)
|
|
|
|
{
|
|
|
|
seg_t *line = &sub->firstline[i];
|
2016-11-10 04:30:33 +00:00
|
|
|
if (line->sidedef == nullptr || !(line->sidedef->Flags & WALLF_POLYOBJ))
|
2016-11-21 19:50:54 +00:00
|
|
|
{
|
2016-11-15 12:30:30 +00:00
|
|
|
RenderLine(sub, line, frontsector, subsectorDepth);
|
2016-11-21 19:50:54 +00:00
|
|
|
}
|
2016-11-07 08:25:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 00:29:08 +00:00
|
|
|
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 });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 09:15:27 +00:00
|
|
|
SpriteRange sprites = GetSpritesForSector(sub->sector);
|
|
|
|
for (int i = 0; i < sprites.Count; i++)
|
|
|
|
{
|
|
|
|
AActor *thing = SortedSprites[sprites.Start + i].Thing;
|
2016-11-13 14:16:55 +00:00
|
|
|
TranslucentObjects.push_back({ thing, sub, subsectorDepth });
|
2016-11-11 18:26:28 +00:00
|
|
|
}
|
2016-11-13 14:16:55 +00:00
|
|
|
|
2016-11-14 13:19:48 +00:00
|
|
|
TranslucentObjects.insert(TranslucentObjects.end(), SubsectorTranslucentWalls.begin(), SubsectorTranslucentWalls.end());
|
|
|
|
SubsectorTranslucentWalls.clear();
|
2016-11-07 08:25:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 13:19:48 +00:00
|
|
|
SpriteRange RenderPolyScene::GetSpritesForSector(sector_t *sector)
|
2016-11-09 10:38:07 +00:00
|
|
|
{
|
|
|
|
if (SectorSpriteRanges.size() < sector->sectornum || sector->sectornum < 0)
|
|
|
|
return SpriteRange();
|
|
|
|
|
|
|
|
auto &range = SectorSpriteRanges[sector->sectornum];
|
|
|
|
if (range.Start == -1)
|
|
|
|
{
|
|
|
|
range.Start = (int)SortedSprites.size();
|
|
|
|
range.Count = 0;
|
|
|
|
for (AActor *thing = sector->thinglist; thing != nullptr; thing = thing->snext)
|
|
|
|
{
|
|
|
|
SortedSprites.push_back({ thing, (thing->Pos() - ViewPos).LengthSquared() });
|
|
|
|
range.Count++;
|
|
|
|
}
|
|
|
|
std::stable_sort(SortedSprites.begin() + range.Start, SortedSprites.begin() + range.Start + range.Count);
|
|
|
|
}
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
2016-11-15 12:30:30 +00:00
|
|
|
void RenderPolyScene::RenderLine(subsector_t *sub, seg_t *line, sector_t *frontsector, uint32_t subsectorDepth)
|
2016-11-07 08:25:12 +00:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
|
2016-11-13 12:24:36 +00:00
|
|
|
// Cull wall if not visible
|
|
|
|
int sx1, sx2;
|
2016-11-14 13:19:48 +00:00
|
|
|
bool hasSegmentRange = Cull.GetSegmentRangeForLine(line->v1->fX(), line->v1->fY(), line->v2->fX(), line->v2->fY(), sx1, sx2);
|
2016-11-15 14:15:26 +00:00
|
|
|
if (!hasSegmentRange || Cull.IsSegmentCulled(sx1, sx2))
|
2016-11-08 04:17:29 +00:00
|
|
|
return;
|
|
|
|
|
2016-11-15 12:30:30 +00:00
|
|
|
// Tell automap we saw this
|
|
|
|
if (!r_dontmaplines && line->linedef)
|
|
|
|
{
|
|
|
|
line->linedef->flags |= ML_MAPPED;
|
|
|
|
sub->flags |= SSECF_DRAWN;
|
|
|
|
}
|
|
|
|
|
2016-11-21 19:50:54 +00:00
|
|
|
// 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, line, frontsector, subsectorDepth, fakeFloor, SubsectorTranslucentWalls);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-14 13:19:48 +00:00
|
|
|
// Render wall, and update culling info if its an occlusion blocker
|
|
|
|
if (RenderPolyWall::RenderLine(WorldToClip, line, frontsector, subsectorDepth, SubsectorTranslucentWalls))
|
2016-11-08 04:17:29 +00:00
|
|
|
{
|
2016-11-14 13:19:48 +00:00
|
|
|
if (hasSegmentRange)
|
|
|
|
Cull.MarkSegmentCulled(sx1, sx2);
|
2016-11-08 04:17:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-14 13:19:48 +00:00
|
|
|
void RenderPolyScene::RenderTranslucent()
|
2016-11-08 04:17:29 +00:00
|
|
|
{
|
2016-11-14 13:19:48 +00:00
|
|
|
for (auto it = TranslucentObjects.rbegin(); it != TranslucentObjects.rend(); ++it)
|
2016-11-08 04:17:29 +00:00
|
|
|
{
|
2016-11-14 13:19:48 +00:00
|
|
|
auto &obj = *it;
|
2016-11-17 00:29:08 +00:00
|
|
|
if (obj.particle)
|
|
|
|
{
|
|
|
|
RenderPolyParticle spr;
|
|
|
|
spr.Render(WorldToClip, obj.particle, obj.sub, obj.subsectorDepth);
|
|
|
|
}
|
|
|
|
else if (!obj.thing)
|
2016-11-08 04:17:29 +00:00
|
|
|
{
|
2016-11-14 13:19:48 +00:00
|
|
|
obj.wall.Render(WorldToClip);
|
2016-11-08 04:17:29 +00:00
|
|
|
}
|
2016-11-14 13:19:48 +00:00
|
|
|
else if ((obj.thing->renderflags & RF_SPRITETYPEMASK) == RF_WALLSPRITE)
|
2016-11-08 04:17:29 +00:00
|
|
|
{
|
2016-11-14 13:19:48 +00:00
|
|
|
RenderPolyWallSprite wallspr;
|
2016-11-16 21:31:49 +00:00
|
|
|
wallspr.Render(WorldToClip, obj.thing, obj.sub, obj.subsectorDepth);
|
2016-11-08 04:17:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-14 13:19:48 +00:00
|
|
|
RenderPolySprite spr;
|
|
|
|
spr.Render(WorldToClip, obj.thing, obj.sub, obj.subsectorDepth);
|
2016-11-09 00:33:40 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-07 08:25:12 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 13:19:48 +00:00
|
|
|
RenderPolyScene *RenderPolyScene::Instance()
|
2016-11-07 08:25:12 +00:00
|
|
|
{
|
2016-11-14 13:19:48 +00:00
|
|
|
static RenderPolyScene scene;
|
|
|
|
return &scene;
|
2016-11-07 08:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
int NextBufferVertex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TriVertex *PolyVertexBuffer::GetVertices(int count)
|
|
|
|
{
|
|
|
|
enum { VertexBufferSize = 16 * 1024 };
|
|
|
|
static TriVertex Vertex[VertexBufferSize];
|
|
|
|
|
|
|
|
if (NextBufferVertex + count > VertexBufferSize)
|
|
|
|
return nullptr;
|
|
|
|
TriVertex *v = Vertex + NextBufferVertex;
|
|
|
|
NextBufferVertex += count;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PolyVertexBuffer::Clear()
|
|
|
|
{
|
|
|
|
NextBufferVertex = 0;
|
|
|
|
}
|