2021-03-19 22:11:23 +00:00
|
|
|
/*
|
|
|
|
** sectorgeometry.cpp
|
|
|
|
**
|
|
|
|
** caches the triangle meshes used for rendering sector planes.
|
|
|
|
**
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
** Copyright 2021 Christoph Oelckers
|
|
|
|
** All rights reserved.
|
|
|
|
**
|
|
|
|
** Redistribution and use in source and binary forms, with or without
|
|
|
|
** modification, are permitted provided that the following conditions
|
|
|
|
** are met:
|
|
|
|
**
|
|
|
|
** 1. Redistributions of source code must retain the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer.
|
|
|
|
** 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
** notice, this list of conditions and the following disclaimer in the
|
|
|
|
** documentation and/or other materials provided with the distribution.
|
|
|
|
** 3. The name of the author may not be used to endorse or promote products
|
|
|
|
** derived from this software without specific prior written permission.
|
|
|
|
**
|
|
|
|
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
**---------------------------------------------------------------------------
|
|
|
|
**
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sectorgeometry.h"
|
|
|
|
#include "build.h"
|
|
|
|
#include "gamefuncs.h"
|
|
|
|
#include "texturemanager.h"
|
|
|
|
#include "earcut.hpp"
|
2021-12-16 05:10:59 +00:00
|
|
|
#include "hw_sections.h"
|
2021-12-11 22:00:38 +00:00
|
|
|
#include "tesselator.h"
|
2021-03-19 22:11:23 +00:00
|
|
|
|
2021-12-12 17:07:19 +00:00
|
|
|
SectionGeometry sectionGeometry;
|
2021-03-19 22:11:23 +00:00
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// CalcPlane fixme - this should be stored in the sector, not be recalculated each frame.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
static FVector3 CalcNormal(sectortype* sector, int plane)
|
|
|
|
{
|
|
|
|
FVector3 pt[3];
|
|
|
|
|
2021-12-06 17:17:45 +00:00
|
|
|
auto wal = sector->firstWall();
|
2021-11-26 19:55:13 +00:00
|
|
|
auto wal2 = wal->point2Wall();
|
2021-03-19 22:11:23 +00:00
|
|
|
|
|
|
|
pt[0] = { (float)WallStartX(wal), (float)WallStartY(wal), 0 };
|
|
|
|
pt[1] = { (float)WallEndX(wal), (float)WallEndY(wal), 0 };
|
2021-12-22 10:06:32 +00:00
|
|
|
PlanesAtPoint(sector, wal->pos.X, wal->pos.Y, plane ? &pt[0].Z : nullptr, plane? nullptr : &pt[0].Z);
|
|
|
|
PlanesAtPoint(sector, wal2->pos.X, wal2->pos.Y, plane ? &pt[1].Z : nullptr, plane ? nullptr : &pt[1].Z);
|
2021-03-19 22:11:23 +00:00
|
|
|
|
|
|
|
if (pt[0].X == pt[1].X)
|
|
|
|
{
|
|
|
|
if (pt[0].Y == pt[1].Y) return { 0.f, 0.f, plane ? -1.f : 1.f };
|
|
|
|
pt[2].X = pt[0].X + 4;
|
|
|
|
pt[2].Y = pt[0].Y;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pt[2].X = pt[0].X;
|
|
|
|
pt[2].Y = pt[0].Y + 4;
|
|
|
|
}
|
|
|
|
PlanesAtPoint(sector, pt[2].X * 16, pt[2].Y * 16, plane ? &pt[2].Z : nullptr, plane ? nullptr : &pt[2].Z);
|
|
|
|
|
|
|
|
auto normal = (pt[2] - pt[0]) ^ (pt[1] - pt[0]);
|
|
|
|
|
|
|
|
if ((pt[2].Z < 0 && !plane) || (pt[2].Z > 0 && plane)) return -pt[2];
|
|
|
|
return pt[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// The math used here to calculate texture positioning was derived from
|
|
|
|
// Polymer but required several fixes for correctness.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
class UVCalculator
|
|
|
|
{
|
|
|
|
sectortype* sect;
|
|
|
|
int myplane;
|
|
|
|
int stat;
|
|
|
|
float z1;
|
|
|
|
int ix1;
|
|
|
|
int iy1;
|
|
|
|
int ix2;
|
|
|
|
int iy2;
|
|
|
|
float sinalign, cosalign;
|
|
|
|
FGameTexture* tex;
|
|
|
|
float xpanning, ypanning;
|
|
|
|
float xscaled, yscaled;
|
2021-04-02 20:52:46 +00:00
|
|
|
FVector2 offset;
|
2021-03-19 22:11:23 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2021-04-02 20:52:46 +00:00
|
|
|
UVCalculator(sectortype* sec, int plane, FGameTexture* tx, const FVector2& off)
|
2021-03-19 22:11:23 +00:00
|
|
|
{
|
|
|
|
float xpan, ypan;
|
|
|
|
|
|
|
|
sect = sec;
|
|
|
|
tex = tx;
|
|
|
|
myplane = plane;
|
2021-04-02 20:52:46 +00:00
|
|
|
offset = off;
|
2021-03-19 22:11:23 +00:00
|
|
|
|
2021-11-26 19:55:13 +00:00
|
|
|
auto firstwall = sec->firstWall();
|
2021-12-22 09:59:50 +00:00
|
|
|
ix1 = firstwall->pos.X;
|
2021-12-22 10:06:32 +00:00
|
|
|
iy1 = firstwall->pos.Y;
|
2021-12-22 09:59:50 +00:00
|
|
|
ix2 = firstwall->point2Wall()->pos.X;
|
2021-12-22 10:06:32 +00:00
|
|
|
iy2 = firstwall->point2Wall()->pos.Y;
|
2021-03-19 22:11:23 +00:00
|
|
|
|
|
|
|
if (plane == 0)
|
|
|
|
{
|
|
|
|
stat = sec->floorstat;
|
|
|
|
xpan = sec->floorxpan_;
|
|
|
|
ypan = sec->floorypan_;
|
|
|
|
PlanesAtPoint(sec, ix1, iy1, nullptr, &z1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stat = sec->ceilingstat;
|
|
|
|
xpan = sec->ceilingxpan_;
|
|
|
|
ypan = sec->ceilingypan_;
|
|
|
|
PlanesAtPoint(sec, ix1, iy1, &z1, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
DVector2 dv = { double(ix2 - ix1), -double(iy2 - iy1) };
|
|
|
|
auto vang = dv.Angle() - 90.;
|
|
|
|
|
2021-05-11 23:58:42 +00:00
|
|
|
cosalign = float(vang.Cos());
|
|
|
|
sinalign = float(vang.Sin());
|
2021-03-19 22:11:23 +00:00
|
|
|
|
2021-03-28 16:22:30 +00:00
|
|
|
int pow2width = 1 << sizeToBits((int)tx->GetDisplayWidth());
|
|
|
|
int pow2height = 1 << sizeToBits((int)tx->GetDisplayHeight());
|
2021-03-19 22:11:23 +00:00
|
|
|
|
2021-12-10 23:39:13 +00:00
|
|
|
xpanning = xpan / 256.f;
|
|
|
|
ypanning = ypan / 256.f;
|
2021-03-19 22:11:23 +00:00
|
|
|
|
|
|
|
float scalefactor = (stat & CSTAT_SECTOR_TEXHALF) ? 8.0f : 16.0f;
|
|
|
|
|
|
|
|
if ((stat & (CSTAT_SECTOR_SLOPE | CSTAT_SECTOR_ALIGN)) == (CSTAT_SECTOR_ALIGN))
|
|
|
|
{
|
|
|
|
// This is necessary to adjust for some imprecisions in the math.
|
|
|
|
// To calculate the inverse Build performs an integer division with significant loss of precision
|
|
|
|
// that can cause the texture to be shifted by multiple pixels.
|
|
|
|
// The code below calculates the amount of this deviation so that it can be added back to the formula.
|
|
|
|
int len = ksqrt(uhypsq(ix2 - ix1, iy2 - iy1));
|
|
|
|
if (len != 0)
|
|
|
|
{
|
|
|
|
int i = 1048576 / len;
|
|
|
|
scalefactor *= 1048576.f / (i * len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 23:39:13 +00:00
|
|
|
xscaled = scalefactor * pow2width;
|
|
|
|
yscaled = scalefactor * pow2height;
|
2021-03-19 22:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FVector2 GetUV(int x, int y, float z)
|
|
|
|
{
|
|
|
|
float tv, tu;
|
|
|
|
|
|
|
|
if (stat & CSTAT_SECTOR_ALIGN)
|
|
|
|
{
|
|
|
|
float dx = (float)(x - ix1);
|
|
|
|
float dy = (float)(y - iy1);
|
|
|
|
|
|
|
|
tu = -(dx * sinalign + dy * cosalign);
|
|
|
|
tv = (dx * cosalign - dy * sinalign);
|
|
|
|
|
|
|
|
if (stat & CSTAT_SECTOR_SLOPE)
|
|
|
|
{
|
|
|
|
float dz = (z - z1) * 16;
|
|
|
|
float newtv = sqrt(tv * tv + dz * dz);
|
|
|
|
tv = tv < 0 ? -newtv : newtv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-02 20:52:46 +00:00
|
|
|
tu = x - offset.X;
|
|
|
|
tv = -y - offset.Y;
|
2021-03-19 22:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stat & CSTAT_SECTOR_SWAPXY)
|
|
|
|
std::swap(tu, tv);
|
|
|
|
|
|
|
|
if (stat & CSTAT_SECTOR_XFLIP) tu = -tu;
|
|
|
|
if (stat & CSTAT_SECTOR_YFLIP) tv = -tv;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { tu / xscaled + xpanning, tv / yscaled + ypanning };
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-12-11 22:00:38 +00:00
|
|
|
enum class ETriangulateResult
|
|
|
|
{
|
|
|
|
Ok = 0,
|
|
|
|
Failed = 1, // unable to triangulate
|
|
|
|
Invalid = 2, // input data invalid.
|
|
|
|
};
|
|
|
|
|
2021-03-19 22:11:23 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
2021-12-11 22:00:38 +00:00
|
|
|
// Convert the outline to render coordinates.
|
2021-03-19 22:11:23 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2021-12-11 22:00:38 +00:00
|
|
|
static int OutlineToFloat(Outline& outl, FOutline& polygon)
|
2021-03-19 22:11:23 +00:00
|
|
|
{
|
2021-12-11 22:00:38 +00:00
|
|
|
polygon.resize(outl.Size());
|
|
|
|
unsigned count = 0;
|
|
|
|
for (unsigned i = 0; i < outl.Size(); i++)
|
2021-03-31 19:26:57 +00:00
|
|
|
{
|
2021-12-11 22:00:38 +00:00
|
|
|
polygon[i].resize(outl[i].Size());
|
|
|
|
count += outl[i].Size();
|
|
|
|
for (unsigned j = 0; j < outl[i].Size(); j++)
|
2021-03-19 22:11:23 +00:00
|
|
|
{
|
2021-12-22 09:26:51 +00:00
|
|
|
float X = RenderX(outl[i][j].X);
|
2021-12-22 09:28:51 +00:00
|
|
|
float Y = RenderY(outl[i][j].Y);
|
2021-12-14 16:40:03 +00:00
|
|
|
if (fabs(X) > 32768.f || fabs(Y) > 32768.f)
|
|
|
|
{
|
|
|
|
// If we get here there's some fuckery going around with the coordinates. Let's better abort and wait for things to realign.
|
|
|
|
// Do not try alternative methods if this happens.
|
2021-12-11 22:00:38 +00:00
|
|
|
return -1;
|
2021-12-14 16:40:03 +00:00
|
|
|
}
|
2021-12-11 22:00:38 +00:00
|
|
|
polygon[i][j] = { X, Y };
|
2021-03-19 22:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-11 22:00:38 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// Try to triangulate a given outline with Earcut.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
|
|
|
ETriangulateResult TriangulateOutlineEarcut(const FOutline& polygon, int count, TArray<FVector2>& points, TArray<int>& indicesOut)
|
|
|
|
{
|
|
|
|
// Sections are already validated so we can assume that the data is well defined here.
|
|
|
|
|
2021-03-19 22:11:23 +00:00
|
|
|
auto indices = mapbox::earcut(polygon);
|
2021-12-14 16:40:03 +00:00
|
|
|
size_t numtriangles = count + (polygon.size() - 1) * 2 - 2; // accout for the extra connections needed to turn the polygon into s single loop.
|
|
|
|
if (indices.size() < numtriangles * 3)
|
2021-04-03 10:44:30 +00:00
|
|
|
{
|
|
|
|
// this means that full triangulation failed.
|
2021-12-11 22:00:38 +00:00
|
|
|
return ETriangulateResult::Failed;
|
2021-04-03 10:44:30 +00:00
|
|
|
}
|
2021-12-11 22:00:38 +00:00
|
|
|
points.Resize(count);
|
2021-03-19 22:11:23 +00:00
|
|
|
int p = 0;
|
|
|
|
for (size_t a = 0; a < polygon.size(); a++)
|
|
|
|
{
|
|
|
|
for (auto& pt : polygon[a])
|
|
|
|
{
|
2021-12-11 22:00:38 +00:00
|
|
|
points[p++] = { pt.first, pt.second };
|
2021-03-19 22:11:23 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-14 08:03:19 +00:00
|
|
|
indicesOut.Resize((unsigned)indices.size());
|
2021-12-11 22:00:38 +00:00
|
|
|
for (unsigned i = 0; i < indices.size(); i++)
|
2021-03-19 22:11:23 +00:00
|
|
|
{
|
2021-12-11 22:00:38 +00:00
|
|
|
indicesOut[i] = indices[i];
|
2021-03-19 22:11:23 +00:00
|
|
|
}
|
2021-12-11 22:00:38 +00:00
|
|
|
return ETriangulateResult::Ok;
|
2021-04-03 10:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
2021-12-11 22:00:38 +00:00
|
|
|
// Try to triangulate a given outline with libtess2.
|
2021-04-03 10:44:30 +00:00
|
|
|
//
|
|
|
|
//==========================================================================
|
2021-12-14 16:40:03 +00:00
|
|
|
FMemArena tessArena(100000);
|
2021-04-03 10:44:30 +00:00
|
|
|
|
2021-12-11 22:00:38 +00:00
|
|
|
ETriangulateResult TriangulateOutlineLibtess(const FOutline& polygon, int count, TArray<FVector2>& points, TArray<int>& indicesOut)
|
2021-04-03 10:44:30 +00:00
|
|
|
{
|
2021-12-14 16:40:03 +00:00
|
|
|
tessArena.FreeAll();
|
2021-04-03 10:44:30 +00:00
|
|
|
|
2021-12-11 22:00:38 +00:00
|
|
|
auto poolAlloc = [](void* userData, unsigned int size) -> void*
|
|
|
|
{
|
|
|
|
FMemArena* pool = (FMemArena*)userData;
|
|
|
|
return pool->Alloc(size);
|
|
|
|
};
|
2021-05-21 10:57:12 +00:00
|
|
|
|
2021-12-11 22:00:38 +00:00
|
|
|
auto poolFree = [](void*, void*)
|
2021-04-03 10:44:30 +00:00
|
|
|
{
|
2021-12-11 22:00:38 +00:00
|
|
|
};
|
2021-05-21 10:57:12 +00:00
|
|
|
|
2021-12-11 22:00:38 +00:00
|
|
|
TESSalloc ma{};
|
|
|
|
ma.memalloc = poolAlloc;
|
|
|
|
ma.memfree = poolFree;
|
|
|
|
ma.userData = (void*)&tessArena;
|
|
|
|
ma.extraVertices = 256; // realloc not provided, allow 256 extra vertices.
|
2021-05-21 10:57:12 +00:00
|
|
|
|
2021-12-11 22:00:38 +00:00
|
|
|
auto tess = tessNewTess(&ma);
|
|
|
|
if (!tess)
|
|
|
|
return ETriangulateResult::Failed;
|
2021-05-29 11:19:44 +00:00
|
|
|
|
2021-12-14 16:40:03 +00:00
|
|
|
tessSetOption(tess, TESS_CONSTRAINED_DELAUNAY_TRIANGULATION, 0);
|
|
|
|
tessSetOption(tess, TESS_REVERSE_CONTOURS, 1);
|
2021-12-11 23:22:07 +00:00
|
|
|
|
2021-12-11 22:00:38 +00:00
|
|
|
// Add contours.
|
|
|
|
for (auto& loop : polygon)
|
2021-12-14 08:03:19 +00:00
|
|
|
tessAddContour(tess, 2, &loop.data()->first, (int)sizeof(*loop.data()), (int)loop.size());
|
2021-05-23 07:15:53 +00:00
|
|
|
|
2021-12-14 16:40:03 +00:00
|
|
|
int result = tessTesselate(tess, TESS_WINDING_POSITIVE, TESS_POLYGONS, 3, 2, nullptr);
|
2021-12-11 22:00:38 +00:00
|
|
|
if (!result)
|
2021-12-12 17:07:19 +00:00
|
|
|
{
|
2021-12-11 22:00:38 +00:00
|
|
|
tessDeleteTess(tess);
|
|
|
|
return ETriangulateResult::Failed;
|
2021-12-12 17:07:19 +00:00
|
|
|
}
|
2021-05-23 07:15:53 +00:00
|
|
|
|
2021-12-11 22:00:38 +00:00
|
|
|
const float* verts = tessGetVertices(tess);
|
|
|
|
const int* vinds = tessGetVertexIndices(tess);
|
|
|
|
const int* elems = tessGetElements(tess);
|
|
|
|
const int nverts = tessGetVertexCount(tess);
|
2021-12-14 16:40:03 +00:00
|
|
|
const int nelems = tessGetElementCount(tess) * 3; // an 'element' here is a full triangle, not a single vertex like in OpenGL...
|
2021-05-21 10:57:12 +00:00
|
|
|
|
2021-12-11 22:00:38 +00:00
|
|
|
points.Resize(nverts);
|
|
|
|
indicesOut.Resize(nelems);
|
|
|
|
for (int i = 0; i < nverts; i++)
|
2021-12-14 16:40:03 +00:00
|
|
|
{
|
2021-12-11 22:00:38 +00:00
|
|
|
points[i] = { verts[i * 2], verts[i * 2 + 1] };
|
2021-12-14 16:40:03 +00:00
|
|
|
}
|
2021-12-11 22:00:38 +00:00
|
|
|
for (int i = 0; i < nelems; i++)
|
2021-12-14 16:40:03 +00:00
|
|
|
{
|
2021-12-11 22:00:38 +00:00
|
|
|
indicesOut[i] = elems[i];
|
|
|
|
}
|
|
|
|
return ETriangulateResult::Ok;
|
|
|
|
}
|
|
|
|
|
2021-03-19 22:11:23 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2021-12-15 11:01:14 +00:00
|
|
|
bool SectionGeometry::ValidateSection(Section* section, int plane)
|
2021-03-19 22:11:23 +00:00
|
|
|
{
|
2021-12-15 10:12:31 +00:00
|
|
|
auto sec = §or[section->sector];
|
2021-12-11 22:00:38 +00:00
|
|
|
auto& sdata = data[section->index];
|
2021-05-02 22:04:36 +00:00
|
|
|
|
2021-12-11 22:00:38 +00:00
|
|
|
auto compare = &sdata.compare[plane];
|
2021-03-19 22:11:23 +00:00
|
|
|
if (plane == 0)
|
|
|
|
{
|
|
|
|
if (sec->floorheinum == compare->floorheinum &&
|
|
|
|
sec->floorpicnum == compare->floorpicnum &&
|
|
|
|
((sec->floorstat ^ compare->floorstat) & (CSTAT_SECTOR_ALIGN | CSTAT_SECTOR_YFLIP | CSTAT_SECTOR_XFLIP | CSTAT_SECTOR_TEXHALF | CSTAT_SECTOR_SWAPXY)) == 0 &&
|
|
|
|
sec->floorxpan_ == compare->floorxpan_ &&
|
|
|
|
sec->floorypan_ == compare->floorypan_ &&
|
2021-12-11 22:00:38 +00:00
|
|
|
sec->firstWall()->pos == sdata.poscompare[0] &&
|
|
|
|
sec->firstWall()->point2Wall()->pos == sdata.poscompare2[0] &&
|
2021-12-14 16:40:03 +00:00
|
|
|
!(section->dirty & EDirty::FloorDirty) && sdata.planes[plane].vertices.Size() ) return true;
|
2021-03-19 22:11:23 +00:00
|
|
|
|
2021-12-14 16:40:03 +00:00
|
|
|
section->dirty &= EDirty::FloorDirty;
|
2021-03-19 22:11:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (sec->ceilingheinum == compare->ceilingheinum &&
|
|
|
|
sec->ceilingpicnum == compare->ceilingpicnum &&
|
|
|
|
((sec->ceilingstat ^ compare->ceilingstat) & (CSTAT_SECTOR_ALIGN | CSTAT_SECTOR_YFLIP | CSTAT_SECTOR_XFLIP | CSTAT_SECTOR_TEXHALF | CSTAT_SECTOR_SWAPXY)) == 0 &&
|
|
|
|
sec->ceilingxpan_ == compare->ceilingxpan_ &&
|
|
|
|
sec->ceilingypan_ == compare->ceilingypan_ &&
|
2021-12-11 22:00:38 +00:00
|
|
|
sec->firstWall()->pos == sdata.poscompare[1] &&
|
|
|
|
sec->firstWall()->point2Wall()->pos == sdata.poscompare2[1] &&
|
2021-12-14 16:40:03 +00:00
|
|
|
!(section->dirty & EDirty::CeilingDirty) && sdata.planes[1].vertices.Size()) return true;
|
2021-03-19 22:11:23 +00:00
|
|
|
|
2021-12-14 16:40:03 +00:00
|
|
|
section->dirty &= ~EDirty::CeilingDirty;
|
2021-03-19 22:11:23 +00:00
|
|
|
}
|
2021-12-27 22:14:16 +00:00
|
|
|
compare->copy(sec);
|
2021-12-11 22:00:38 +00:00
|
|
|
sdata.poscompare[plane] = sec->firstWall()->pos;
|
|
|
|
sdata.poscompare2[plane] = sec->firstWall()->point2Wall()->pos;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-12 17:07:19 +00:00
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2021-12-15 11:01:14 +00:00
|
|
|
bool SectionGeometry::CreateMesh(Section* section)
|
2021-12-12 17:07:19 +00:00
|
|
|
{
|
|
|
|
auto outline = BuildOutline(section);
|
|
|
|
FOutline foutline;
|
|
|
|
int count = OutlineToFloat(outline, foutline);
|
|
|
|
if (count == -1) return false; // gotta wait...
|
|
|
|
TArray<FVector2> meshVertices;
|
|
|
|
TArray<int> meshIndices;
|
|
|
|
ETriangulateResult result = ETriangulateResult::Failed;
|
|
|
|
|
|
|
|
auto& sdata = data[section->index];
|
|
|
|
|
|
|
|
if (!(section->flags & NoEarcut))
|
|
|
|
{
|
|
|
|
result = TriangulateOutlineEarcut(foutline, count, sdata.meshVertices, sdata.meshIndices);
|
|
|
|
}
|
2021-12-14 16:40:03 +00:00
|
|
|
if (result == ETriangulateResult::Failed && !(section->geomflags & NoLibtess))
|
2021-12-12 17:07:19 +00:00
|
|
|
{
|
2021-12-14 16:40:03 +00:00
|
|
|
section->geomflags |= NoEarcut;
|
2021-12-12 17:07:19 +00:00
|
|
|
result = TriangulateOutlineLibtess(foutline, count, sdata.meshVertices, sdata.meshIndices);
|
|
|
|
}
|
2021-12-14 16:40:03 +00:00
|
|
|
|
2021-12-15 11:17:56 +00:00
|
|
|
sdata.planes[0].vertices.Clear();
|
|
|
|
sdata.planes[1].vertices.Clear();
|
2021-12-14 16:40:03 +00:00
|
|
|
section->dirty &= ~EDirty::GeometryDirty;
|
2021-12-15 11:17:56 +00:00
|
|
|
section->dirty |= EDirty::FloorDirty | EDirty::CeilingDirty;
|
2021-12-12 17:07:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
// assumes that the geometry has already been validated.
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2021-12-15 11:01:14 +00:00
|
|
|
void SectionGeometry::CreatePlaneMesh(Section* section, int plane, const FVector2& offset)
|
2021-12-12 17:07:19 +00:00
|
|
|
{
|
2021-12-15 10:12:31 +00:00
|
|
|
auto sectorp = §or[section->sector];
|
2021-12-12 17:07:19 +00:00
|
|
|
// calculate the rest.
|
|
|
|
auto texture = tileGetTexture(plane ? sectorp->ceilingpicnum : sectorp->floorpicnum);
|
|
|
|
auto& sdata = data[section->index];
|
|
|
|
auto& entry = sdata.planes[plane];
|
|
|
|
int fz = sectorp->floorz, cz = sectorp->ceilingz;
|
2021-12-27 21:32:26 +00:00
|
|
|
sectorp->setfloorz(0, true);
|
|
|
|
sectorp->setceilingz(0, true);
|
2021-12-12 17:07:19 +00:00
|
|
|
|
|
|
|
UVCalculator uvcalc(sectorp, plane, texture, offset);
|
|
|
|
|
|
|
|
entry.vertices.Resize(sdata.meshVertices.Size());
|
|
|
|
entry.texcoords.Resize(entry.vertices.Size());
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < entry.vertices.Size(); i++)
|
|
|
|
{
|
|
|
|
auto& org = sdata.meshVertices[i];
|
|
|
|
auto& pt = entry.vertices[i];
|
|
|
|
auto& tc = entry.texcoords[i];
|
|
|
|
|
2021-12-14 16:40:03 +00:00
|
|
|
pt.X = org.X; pt.Y = org.Y;
|
2021-12-12 17:07:19 +00:00
|
|
|
PlanesAtPoint(sectorp, (pt.X * 16), (pt.Y * -16), plane ? &pt.Z : nullptr, !plane ? &pt.Z : nullptr);
|
|
|
|
tc = uvcalc.GetUV(int(pt.X * 16.), int(pt.Y * -16.), pt.Z);
|
|
|
|
}
|
|
|
|
entry.normal = CalcNormal(sectorp, plane);
|
2021-12-27 21:32:26 +00:00
|
|
|
sectorp->setfloorz(fz, true);
|
|
|
|
sectorp->setceilingz(cz, true);
|
2021-12-12 17:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2021-12-11 22:00:38 +00:00
|
|
|
void SectionGeometry::MarkDirty(sectortype* sector)
|
|
|
|
{
|
2021-12-15 12:08:09 +00:00
|
|
|
for (auto section : sectionsPerSector[sectnum(sector)])
|
2021-04-03 10:44:30 +00:00
|
|
|
{
|
2021-12-14 16:40:03 +00:00
|
|
|
section->dirty = sector->dirty;
|
2021-04-03 10:44:30 +00:00
|
|
|
}
|
2021-12-12 17:07:19 +00:00
|
|
|
sector->dirty = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//==========================================================================
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//==========================================================================
|
|
|
|
|
2021-12-15 11:01:14 +00:00
|
|
|
SectionGeometryPlane* SectionGeometry::get(Section* section, int plane, const FVector2& offset, TArray<int>** pIndices)
|
2021-12-12 17:07:19 +00:00
|
|
|
{
|
|
|
|
if (!section || section->index >= data.Size()) return nullptr;
|
2021-12-15 10:12:31 +00:00
|
|
|
auto sectp = §or[section->sector];
|
2021-12-12 17:07:19 +00:00
|
|
|
if (sectp->dirty) MarkDirty(sectp);
|
2021-12-14 16:40:03 +00:00
|
|
|
if (section->dirty & EDirty::GeometryDirty)
|
2021-12-12 17:07:19 +00:00
|
|
|
{
|
|
|
|
bool res = CreateMesh(section);
|
|
|
|
if (!res)
|
|
|
|
{
|
2021-12-14 16:40:03 +00:00
|
|
|
section->dirty &= ~EDirty::GeometryDirty; // sector is in an invalid state, so pretend the old setup is still valid. Happens in some SW maps.
|
2021-12-12 17:07:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ValidateSection(section, plane))
|
|
|
|
{
|
|
|
|
CreatePlaneMesh(section, plane, offset);
|
|
|
|
}
|
|
|
|
*pIndices = &data[section->index].meshIndices;
|
|
|
|
return &data[section->index].planes[plane];
|
2021-03-19 22:11:23 +00:00
|
|
|
}
|