gzdoom/src/r_poly_portal.cpp

297 lines
9 KiB
C++
Raw Normal View History

/*
** 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"
2016-11-25 22:44:55 +00:00
#include "p_maputl.h"
#include "sbar.h"
#include "r_data/r_translate.h"
#include "r_poly_portal.h"
2016-11-25 16:14:26 +00:00
#include "r_poly.h"
#include "gl/data/gl_data.h"
2016-11-25 22:44:55 +00:00
extern bool r_showviewer;
/////////////////////////////////////////////////////////////////////////////
2016-11-24 22:08:36 +00:00
PolyDrawSectorPortal::PolyDrawSectorPortal(FSectorPortal *portal, bool ceiling) : Portal(portal), Ceiling(ceiling)
{
StencilValue = PolyRenderer::Instance()->GetNextStencilValue();
2016-11-24 22:08:36 +00:00
}
2016-11-25 17:15:48 +00:00
void PolyDrawSectorPortal::Render(int portalDepth)
2016-11-24 22:08:36 +00:00
{
2016-11-25 19:19:35 +00:00
if (Portal->mType == PORTS_HORIZON || Portal->mType == PORTS_PLANE)
2016-11-24 22:08:36 +00:00
return;
2016-11-25 00:08:25 +00:00
SaveGlobals();
2016-11-24 22:08:36 +00:00
// To do: get this information from PolyRenderer instead of duplicating the code..
2016-11-24 22:08:36 +00:00
double radPitch = ViewPitch.Normalized180().Radians();
double angx = cos(radPitch);
double angy = sin(radPitch) * glset.pixelstretch;
double alen = sqrt(angx*angx + angy*angy);
float adjustedPitch = (float)asin(angy / alen);
float adjustedViewAngle = (float)(ViewAngle - 90).Radians();
float ratio = WidescreenRatio;
float fovratio = (WidescreenRatio >= 1.3f) ? 1.333333f : ratio;
float fovy = (float)(2 * DAngle::ToDegrees(atan(tan(FieldOfView.Radians() / 2) / fovratio)).Degrees);
TriMatrix worldToView =
TriMatrix::rotate(adjustedPitch, 1.0f, 0.0f, 0.0f) *
TriMatrix::rotate(adjustedViewAngle, 0.0f, -1.0f, 0.0f) *
TriMatrix::scale(1.0f, glset.pixelstretch, 1.0f) *
TriMatrix::swapYZ() *
TriMatrix::translate((float)-ViewPos.X, (float)-ViewPos.Y, (float)-ViewPos.Z);
TriMatrix worldToClip = TriMatrix::perspective(fovy, ratio, 5.0f, 65535.0f) * worldToView;
RenderPortal.SetViewpoint(worldToClip, PortalPlane, StencilValue);
RenderPortal.SetPortalSegments(Segments);
2016-11-25 17:15:48 +00:00
RenderPortal.Render(portalDepth);
2016-11-25 00:08:25 +00:00
RestoreGlobals();
}
2016-11-24 22:08:36 +00:00
2016-11-25 17:15:48 +00:00
void PolyDrawSectorPortal::RenderTranslucent(int portalDepth)
2016-11-25 00:08:25 +00:00
{
2016-11-25 19:19:35 +00:00
if (Portal->mType == PORTS_HORIZON || Portal->mType == PORTS_PLANE)
2016-11-25 00:08:25 +00:00
return;
2016-11-25 19:19:35 +00:00
SaveGlobals();
2016-11-25 00:08:25 +00:00
2016-11-25 17:15:48 +00:00
RenderPortal.RenderTranslucent(portalDepth);
2016-11-25 19:19:35 +00:00
RestoreGlobals();
2016-11-25 00:08:25 +00:00
}
void PolyDrawSectorPortal::SaveGlobals()
{
savedextralight = extralight;
savedpos = ViewPos;
savedangle = ViewAngle;
savedvisibility = swrenderer::R_GetVisibility();
savedcamera = camera;
savedsector = viewsector;
2016-11-25 00:08:25 +00:00
2016-11-25 19:19:35 +00:00
if (Portal->mType == PORTS_SKYVIEWPOINT)
{
// Don't let gun flashes brighten the sky box
ASkyViewpoint *sky = barrier_cast<ASkyViewpoint*>(Portal->mSkybox);
extralight = 0;
swrenderer::R_SetVisibility(sky->args[0] * 0.25f);
2016-11-25 19:19:35 +00:00
ViewPos = sky->InterpolatedPosition(r_TicFracF);
ViewAngle = savedangle + (sky->PrevAngles.Yaw + deltaangle(sky->PrevAngles.Yaw, sky->Angles.Yaw) * r_TicFracF);
}
else //if (Portal->mType == PORTS_STACKEDSECTORTHING || Portal->mType == PORTS_PORTAL || Portal->mType == PORTS_LINKEDPORTAL)
{
//extralight = pl->extralight;
//swrenderer::R_SetVisibility(pl->visibility);
2016-11-25 19:19:35 +00:00
ViewPos.X += Portal->mDisplacement.X;
ViewPos.Y += Portal->mDisplacement.Y;
}
2016-11-25 00:08:25 +00:00
camera = nullptr;
viewsector = Portal->mDestination;
R_SetViewAngle();
2016-11-25 19:19:35 +00:00
Portal->mFlags |= PORTSF_INSKYBOX;
if (Portal->mPartner > 0) sectorPortals[Portal->mPartner].mFlags |= PORTSF_INSKYBOX;
2016-11-25 00:08:25 +00:00
}
void PolyDrawSectorPortal::RestoreGlobals()
{
2016-11-25 19:19:35 +00:00
Portal->mFlags &= ~PORTSF_INSKYBOX;
if (Portal->mPartner > 0) sectorPortals[Portal->mPartner].mFlags &= ~PORTSF_INSKYBOX;
2016-11-24 22:08:36 +00:00
camera = savedcamera;
viewsector = savedsector;
ViewPos = savedpos;
swrenderer::R_SetVisibility(savedvisibility);
2016-11-24 22:08:36 +00:00
extralight = savedextralight;
ViewAngle = savedangle;
R_SetViewAngle();
}
/////////////////////////////////////////////////////////////////////////////
2016-11-25 22:44:55 +00:00
PolyDrawLinePortal::PolyDrawLinePortal(FLinePortal *portal) : Portal(portal)
{
StencilValue = PolyRenderer::Instance()->GetNextStencilValue();
2016-11-25 22:44:55 +00:00
}
PolyDrawLinePortal::PolyDrawLinePortal(line_t *mirror) : Mirror(mirror)
2016-11-24 22:08:36 +00:00
{
StencilValue = PolyRenderer::Instance()->GetNextStencilValue();
2016-11-24 22:08:36 +00:00
}
2016-11-25 17:15:48 +00:00
void PolyDrawLinePortal::Render(int portalDepth)
2016-11-24 22:08:36 +00:00
{
2016-11-25 22:44:55 +00:00
SaveGlobals();
// To do: get this information from PolyRenderer instead of duplicating the code..
2016-11-25 22:44:55 +00:00
double radPitch = ViewPitch.Normalized180().Radians();
double angx = cos(radPitch);
double angy = sin(radPitch) * glset.pixelstretch;
double alen = sqrt(angx*angx + angy*angy);
float adjustedPitch = (float)asin(angy / alen);
float adjustedViewAngle = (float)(ViewAngle - 90).Radians();
float ratio = WidescreenRatio;
float fovratio = (WidescreenRatio >= 1.3f) ? 1.333333f : ratio;
float fovy = (float)(2 * DAngle::ToDegrees(atan(tan(FieldOfView.Radians() / 2) / fovratio)).Degrees);
TriMatrix worldToView =
TriMatrix::rotate(adjustedPitch, 1.0f, 0.0f, 0.0f) *
TriMatrix::rotate(adjustedViewAngle, 0.0f, -1.0f, 0.0f) *
TriMatrix::scale(1.0f, glset.pixelstretch, 1.0f) *
TriMatrix::swapYZ() *
TriMatrix::translate((float)-ViewPos.X, (float)-ViewPos.Y, (float)-ViewPos.Z);
2016-12-07 21:26:18 +00:00
if (Mirror)
worldToView = TriMatrix::scale(-1.0f, 1.0f, 1.0f) * worldToView;
2016-11-25 22:44:55 +00:00
TriMatrix worldToClip = TriMatrix::perspective(fovy, ratio, 5.0f, 65535.0f) * worldToView;
2016-11-26 08:01:58 +00:00
// Calculate plane clipping
2016-12-07 21:26:18 +00:00
line_t *clipLine = Portal ? Portal->mDestination : Mirror;
DVector2 planePos = clipLine->v1->fPos();
DVector2 planeNormal = (clipLine->v2->fPos() - clipLine->v1->fPos()).Rotated90CW();
2016-11-26 08:01:58 +00:00
planeNormal.MakeUnit();
double planeD = -(planeNormal | (planePos + planeNormal * 0.001));
2016-11-26 08:01:58 +00:00
Vec4f portalPlane((float)planeNormal.X, (float)planeNormal.Y, 0.0f, (float)planeD);
RenderPortal.SetViewpoint(worldToClip, portalPlane, StencilValue);
RenderPortal.SetPortalSegments(Segments);
2016-11-25 17:15:48 +00:00
RenderPortal.Render(portalDepth);
2016-11-25 22:44:55 +00:00
RestoreGlobals();
2016-11-24 22:08:36 +00:00
}
2016-11-25 17:15:48 +00:00
void PolyDrawLinePortal::RenderTranslucent(int portalDepth)
2016-11-24 22:08:36 +00:00
{
2016-11-25 22:44:55 +00:00
SaveGlobals();
2016-11-25 17:15:48 +00:00
RenderPortal.RenderTranslucent(portalDepth);
2016-11-25 22:44:55 +00:00
RestoreGlobals();
}
void PolyDrawLinePortal::SaveGlobals()
{
savedextralight = extralight;
savedpos = ViewPos;
savedangle = ViewAngle;
savedcamera = camera;
savedsector = viewsector;
2016-12-07 21:26:18 +00:00
savedinvisibility = camera ? (camera->renderflags & RF_INVISIBLE) == RF_INVISIBLE : false;
2016-11-25 22:44:55 +00:00
savedViewPath[0] = ViewPath[0];
savedViewPath[1] = ViewPath[1];
if (Mirror)
{
DAngle startang = ViewAngle;
DVector3 startpos = ViewPos;
vertex_t *v1 = Mirror->v1;
// Reflect the current view behind the mirror.
if (Mirror->Delta().X == 0)
{ // vertical mirror
ViewPos.X = v1->fX() - startpos.X + v1->fX();
}
else if (Mirror->Delta().Y == 0)
{ // horizontal mirror
ViewPos.Y = v1->fY() - startpos.Y + v1->fY();
}
else
{ // any mirror
vertex_t *v2 = Mirror->v2;
double dx = v2->fX() - v1->fX();
double dy = v2->fY() - v1->fY();
double x1 = v1->fX();
double y1 = v1->fY();
double x = startpos.X;
double y = startpos.Y;
// the above two cases catch len == 0
double r = ((x - x1)*dx + (y - y1)*dy) / (dx*dx + dy*dy);
ViewPos.X = (x1 + r * dx) * 2 - x;
ViewPos.Y = (y1 + r * dy) * 2 - y;
}
ViewAngle = Mirror->Delta().Angle() * 2 - startang;
2016-12-07 21:26:18 +00:00
if (camera)
camera->renderflags &= ~RF_INVISIBLE;
2016-11-25 22:44:55 +00:00
}
else
{
auto src = Portal->mOrigin;
auto dst = Portal->mDestination;
P_TranslatePortalXY(src, ViewPos.X, ViewPos.Y);
P_TranslatePortalZ(src, ViewPos.Z);
P_TranslatePortalAngle(src, ViewAngle);
P_TranslatePortalXY(src, ViewPath[0].X, ViewPath[0].Y);
P_TranslatePortalXY(src, ViewPath[1].X, ViewPath[1].Y);
if (!r_showviewer && camera && P_PointOnLineSidePrecise(ViewPath[0], dst) != P_PointOnLineSidePrecise(ViewPath[1], dst))
{
double distp = (ViewPath[0] - ViewPath[1]).Length();
if (distp > EQUAL_EPSILON)
{
double dist1 = (ViewPos - ViewPath[0]).Length();
double dist2 = (ViewPos - ViewPath[1]).Length();
if (dist1 + dist2 < distp + 1)
{
camera->renderflags |= RF_INVISIBLE;
}
}
}
}
2016-12-07 21:26:18 +00:00
//camera = nullptr;
//viewsector = R_PointInSubsector(ViewPos)->sector;
2016-11-25 22:44:55 +00:00
R_SetViewAngle();
2016-12-07 21:26:18 +00:00
if (Mirror)
PolyTriangleDrawer::toggle_mirror();
2016-11-25 22:44:55 +00:00
}
void PolyDrawLinePortal::RestoreGlobals()
{
2016-12-07 21:26:18 +00:00
if (camera)
{
if (savedinvisibility)
camera->renderflags |= RF_INVISIBLE;
else
camera->renderflags &= ~RF_INVISIBLE;
}
2016-11-25 22:44:55 +00:00
camera = savedcamera;
viewsector = savedsector;
ViewPos = savedpos;
extralight = savedextralight;
ViewAngle = savedangle;
ViewPath[0] = savedViewPath[0];
ViewPath[1] = savedViewPath[1];
R_SetViewAngle();
2016-12-07 21:26:18 +00:00
if (Mirror)
PolyTriangleDrawer::toggle_mirror();
2016-11-24 22:08:36 +00:00
}