godot-lportal/lportal.cpp

414 lines
9.1 KiB
C++
Raw Normal View History

// Copyright (c) 2019 Lawnjelly
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "lportal.h"
#include "core/engine.h"
#include "lroom.h"
#include "ldebug.h"
#include "lroom_manager.h"
2019-11-19 11:29:53 +01:00
/////////////////////////////////////////////////////////////////////
2019-10-09 12:33:19 +02:00
2019-12-03 16:50:30 +01:00
bool LPortal::NameStartsWith(const Node * pNode, String szSearch)
{
int sl = szSearch.length();
String name = pNode->get_name();
int l = name.length();
if (l < sl)
return false;
2019-09-12 12:07:43 +02:00
String szStart = name.substr(0, sl);
//print_line("\t\tNameStartsWith szStart is " + szStart);
if (szStart == szSearch)
return true;
return false;
}
2019-09-12 12:07:43 +02:00
String LPortal::FindNameAfter(Node * pNode, String szStart)
{
String szRes;
String name = pNode->get_name();
2019-09-12 12:07:43 +02:00
szRes = name.substr(szStart.length());
2019-09-16 15:23:10 +02:00
// because godot doesn't support multiple nodes with the same name, we will strip e.g. a number
// after an @ on the end of the name...
// e.g. portal_kitchen@2
for (int c=0; c<szRes.length(); c++)
{
if (szRes[c] == '*')
{
// remove everything after and including this character
szRes = szRes.substr(0, c);
break;
}
}
//print("\t\tNameAfter is " + szRes);
return szRes;
}
//////////////////////////////////////////////////////////
2019-10-09 12:33:19 +02:00
void LPortal::Debug_CheckPlaneValidity(const Plane &p) const
{
assert (p.distance_to(m_ptCentre) < 0.0f);
}
2019-09-26 19:33:40 +02:00
// preprocess
2019-10-09 12:33:19 +02:00
void LPortal::AddLightPlanes(LRoomManager &manager, const LLight &light, LVector<Plane> &planes, bool bReverse) const
2019-09-26 19:33:40 +02:00
{
const Vector<Vector3> &pts = m_ptsWorld;
int nPoints = pts.size();
ERR_FAIL_COND(nPoints < 3);
2019-11-19 11:29:53 +01:00
if (light.m_Source.m_eType == LSource::ST_DIRECTIONAL)
2019-10-09 12:33:19 +02:00
{
// assuming ortho light
const int max_points = 32;
Vector3 pushed_pts[max_points];
if (nPoints > max_points)
nPoints = max_points;
2019-09-26 19:33:40 +02:00
2019-10-09 12:33:19 +02:00
// transform pushed points
2019-11-19 11:29:53 +01:00
Vector3 ptPush = light.m_Source.m_ptDir * 2.0;
2019-09-26 19:33:40 +02:00
2019-10-09 12:33:19 +02:00
for (int n=0; n<nPoints; n++)
{
pushed_pts[n] = pts[n] + ptPush;
}
Plane p;
for (int n=0; n<nPoints; n++)
{
int nPLUS = (n + 1) % nPoints;
p = Plane(pts[n], pts[nPLUS], pushed_pts[n]);
if (bReverse) p = -p;
planes.push_back(p);
Debug_CheckPlaneValidity(p);
}
// first and last
// p = Plane(pts[nPoints-1], pts[0], pushed_pts[0]);
// if (bReverse) p = -p;
// planes.push_back(p);
// Debug_CheckPlaneValidity(p);
// debug
if (manager.m_bDebugLights)
{
for (int n=1; n<nPoints; n++)
{
if (n == 2)
continue;
manager.m_DebugPortalLightPlanes.push_back(pts[n-1]);
manager.m_DebugPortalLightPlanes.push_back(pts[n]);
manager.m_DebugPortalLightPlanes.push_back(pushed_pts[n]);
manager.m_DebugPortalLightPlanes.push_back(pushed_pts[n]);
manager.m_DebugPortalLightPlanes.push_back(pushed_pts[n-1]);
manager.m_DebugPortalLightPlanes.push_back(pts[n-1]);
}
}
return;
2019-09-26 19:33:40 +02:00
}
2019-10-09 12:33:19 +02:00
// use a point for the light for omni and spotlight
2019-09-26 19:33:40 +02:00
Plane p;
2019-10-09 12:33:19 +02:00
for (int n=0; n<nPoints; n++)
2019-09-26 19:33:40 +02:00
{
2019-10-09 12:33:19 +02:00
int nPLUS = (n + 1) % nPoints;
2019-11-19 11:29:53 +01:00
p = Plane(pts[nPLUS], pts[n], light.m_Source.m_ptPos);
2019-10-09 12:33:19 +02:00
if (bReverse) p = -p;
2019-09-26 19:33:40 +02:00
planes.push_back(p);
2019-10-09 12:33:19 +02:00
Debug_CheckPlaneValidity(p);
2019-09-26 19:33:40 +02:00
}
// first and last
2019-10-09 12:33:19 +02:00
// p = Plane(pts[0], pts[nPoints-1], light.m_ptPos);
// if (bReverse) p = -p;
// planes.push_back(p);
// Debug_CheckPlaneValidity(p);
2019-09-26 19:33:40 +02:00
}
2019-09-12 16:51:33 +02:00
// add clipping planes to the vector formed by each portal edge and the camera
void LPortal::AddPlanes(LRoomManager &manager, const Vector3 &ptCam, LVector<Plane> &planes) const
2019-09-12 16:51:33 +02:00
{
// short version
const Vector<Vector3> &pts = m_ptsWorld;
int nPoints = pts.size();
ERR_FAIL_COND(nPoints < 3);
2019-09-12 16:51:33 +02:00
Plane p;
for (int n=1; n<nPoints; n++)
{
p = Plane(ptCam, pts[n], pts[n-1]);
// detect null plane
// if (p.normal.length_squared() < 0.1f)
// {
// print("NULL plane detected from points : ");
// print(ptCam + pts[n] + pts[n-1]);
// }
2019-09-12 16:51:33 +02:00
planes.push_back(p);
2019-10-09 12:33:19 +02:00
Debug_CheckPlaneValidity(p);
2019-09-12 16:51:33 +02:00
}
// first and last
p = Plane(ptCam, pts[0], pts[nPoints-1]);
planes.push_back(p);
2019-10-09 12:33:19 +02:00
Debug_CheckPlaneValidity(p);
// debug
if (!manager.m_bDebugPlanes)
return;
for (int n=0; n<nPoints; n++)
{
manager.m_DebugPlanes.push_back(pts[n]);
}
2019-09-12 16:51:33 +02:00
}
LPortal::eClipResult LPortal::ClipWithPlane(const Plane &p) const
{
int nOutside = 0;
int nPoints = m_ptsWorld.size();
for (int n=0; n<nPoints; n++)
{
float d = p.distance_to(m_ptsWorld[n]);
if (d >= 0.0f)
nOutside++;
}
if (nOutside == nPoints)
{
LPRINT_RUN(2, "\t\tOutside plane " + p);
return CLIP_OUTSIDE;
}
if (nOutside == 0)
return CLIP_INSIDE;
return CLIP_PARTIAL;
}
2020-01-08 20:18:06 +01:00
void LPortal::CreateGeometry(PoolVector<Vector3> p_vertices, const Transform &trans, bool bPortalPlane_Convention)
{
int nPoints = p_vertices.size();
ERR_FAIL_COND(nPoints < 3);
m_ptsWorld.clear();
2019-09-16 15:23:10 +02:00
//print("\t\t\tLPortal::CreateGeometry nPoints : " + itos(nPoints));
2019-09-12 12:07:43 +02:00
2020-01-08 20:18:06 +01:00
//Vector3 ptFirstThree[3];
for (int n=0; n<nPoints; n++)
{
Vector3 ptWorld = trans.xform(p_vertices[n]);
2020-01-08 20:18:06 +01:00
// store the first three to make the plane, irrespective of duplicates
// if (n < 3)
// ptFirstThree[n] = ptWorld;
// new!! test for duplicates. Some geometry may contain duplicate verts in portals which will muck up
// the winding etc...
bool bDuplicate = false;
for (int m=0; m<m_ptsWorld.size(); m++)
{
Vector3 ptDiff = ptWorld - m_ptsWorld[m];
if (ptDiff.length() < 0.001f)
{
bDuplicate = true;
break;
}
}
if (!bDuplicate)
{
m_ptsWorld.push_back(ptWorld);
m_ptCentre += ptWorld;
}
2019-09-16 15:23:10 +02:00
//print("\t\t\t\t" + itos(n) + "\tLocal : " + Variant(p_vertices[n]) + "\tWorld : " + ptWorld);
}
2020-01-08 20:18:06 +01:00
// Plane portal_plane = Plane(ptFirstThree[0], ptFirstThree[1], ptFirstThree[2]);
SortVertsClockwise(bPortalPlane_Convention);
PlaneFromPoints();
}
2020-01-08 20:18:06 +01:00
void LPortal::SortVertsClockwise(bool bPortalPlane_Convention)
2019-09-12 12:07:43 +02:00
{
Vector<Vector3> &verts = m_ptsWorld;
2019-09-12 12:07:43 +02:00
2020-01-08 20:18:06 +01:00
// We first assumed first 3 determine the desired normal
2019-09-12 12:07:43 +02:00
// find normal
2020-01-08 20:18:06 +01:00
Plane plane;
if (bPortalPlane_Convention)
plane = Plane(verts[0], verts[2], verts[1]);
else
plane = Plane(verts[0], verts[1], verts[2]);
2019-09-12 12:07:43 +02:00
Vector3 ptNormal = plane.normal;
// find centroid
int nPoints = verts.size();
Vector3 ptCentre = Vector3(0, 0, 0);
for (int n=0; n<nPoints; n++)
{
ptCentre += verts[n];
}
ptCentre /= nPoints;
m_ptCentre = ptCentre;
2019-09-12 12:07:43 +02:00
// now algorithm
for (int n=0; n<nPoints-2; n++)
{
Vector3 a = verts[n] - ptCentre;
a.normalize();
Plane p = Plane(verts[n], ptCentre, ptCentre + ptNormal);
double SmallestAngle = -1;
int Smallest = -1;
for (int m=n+1; m<nPoints; m++)
2019-09-12 12:07:43 +02:00
{
if (p.distance_to(verts[m]) > 0.0f)
{
Vector3 b = verts[m] - ptCentre;
2019-09-12 12:07:43 +02:00
b.normalize();
double Angle = a.dot(b);
if (Angle > SmallestAngle)
{
SmallestAngle = Angle;
Smallest = m;
}
} // which side
} // for m
// swap smallest and n+1 vert
if (Smallest != -1)
{
Vector3 temp = verts[Smallest];
verts.set(Smallest, verts[n+1]);
verts.set(n+1, temp);
}
} // for n
// the vertices are now sorted, but may be in the opposite order to that wanted.
// we detect this by calculating the normal of the poly, then flipping the order if the normal is pointing
// the wrong way.
plane = Plane(verts[0], verts[1], verts[2]);
if (ptNormal.dot(plane.normal) < 0.0f)
{
// reverse order of verts
ReverseWindingOrder();
}
}
void LPortal::ReverseWindingOrder()
{
Vector<Vector3> &verts = m_ptsWorld;
2019-09-12 12:07:43 +02:00
Vector<Vector3> copy = verts;
for (int n=0; n<verts.size(); n++)
{
verts.set(n, copy[verts.size() - n - 1]);
}
}
void LPortal::CopyReversedGeometry(const LPortal &source)
{
2019-09-16 15:23:10 +02:00
//print("\t\t\tCopyReversedGeometry");
// points are the same but reverse winding order
int nPoints = source.m_ptsWorld.size();
m_ptCentre = source.m_ptCentre;
2019-09-12 12:07:43 +02:00
m_ptsWorld.resize(nPoints);
for (int n=0; n<nPoints; n++)
{
m_ptsWorld.set(n, source.m_ptsWorld[nPoints - n - 1]);
2019-09-16 15:23:10 +02:00
//print("\t\t\t\t" + itos(n) + "\t: " + Variant(m_ptsWorld[n]));
}
PlaneFromPoints();
}
void LPortal::PlaneFromPoints()
{
if (m_ptsWorld.size() < 3)
{
WARN_PRINT("Portal must have at least 3 vertices");
return;
}
// create plane from points
m_Plane = Plane(m_ptsWorld[0], m_ptsWorld[1], m_ptsWorld[2]);
2019-09-12 12:07:43 +02:00
2019-09-16 15:23:10 +02:00
//print("\t\t\t\t\tPlane normal world space : " + m_Plane);
2019-09-12 12:07:43 +02:00
}
LPortal::LPortal() {
// unset
m_iRoomNum = -1;
2019-09-16 15:23:10 +02:00
m_bMirror = false;
// m_uiFrameTouched_Blocked = 0;
2019-09-12 12:07:43 +02:00
}