feat(render): Phase A8.F — PortalProjection with GL near-plane clip (z>=-w)
This commit is contained in:
parent
7f46c278e5
commit
a28a176ad6
2 changed files with 161 additions and 0 deletions
74
src/AcDream.App/Rendering/PortalProjection.cs
Normal file
74
src/AcDream.App/Rendering/PortalProjection.cs
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
// PortalProjection.cs
|
||||
//
|
||||
// Phase A8.F: project a cell-local portal polygon to NDC screen space, clipping
|
||||
// against the GL near plane (w + z >= 0, i.e. z_ndc >= -1) so a portal straddling
|
||||
// the camera does not invert under the perspective divide. At the near plane w is
|
||||
// bounded away from zero, so the divide is safe — no eye-singularity blow-up.
|
||||
// Homogeneous form of the near-plane sidedness in retail PView::GetClip /
|
||||
// ConstructView(CBldPortal) (decomp:432344 / 433832).
|
||||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
|
||||
namespace AcDream.App.Rendering;
|
||||
|
||||
public static class PortalProjection
|
||||
{
|
||||
/// <summary>Project a cell-local polygon to NDC. Returns CCW NDC xy verts, or
|
||||
/// fewer than 3 verts when the polygon is entirely behind the camera / degenerate.</summary>
|
||||
public static Vector2[] ProjectToNdc(IReadOnlyList<Vector3> localPoly, Matrix4x4 cellToWorld, Matrix4x4 viewProj)
|
||||
{
|
||||
if (localPoly == null || localPoly.Count < 3) return System.Array.Empty<Vector2>();
|
||||
|
||||
Matrix4x4 m = cellToWorld * viewProj;
|
||||
|
||||
// To clip space (keep w).
|
||||
var clip = new List<Vector4>(localPoly.Count);
|
||||
foreach (var lp in localPoly)
|
||||
clip.Add(Vector4.Transform(new Vector4(lp, 1f), m));
|
||||
|
||||
// Clip against the GL near plane (keep where w + z >= 0).
|
||||
clip = ClipAgainstNearPlane(clip);
|
||||
if (clip.Count < 3) return System.Array.Empty<Vector2>();
|
||||
|
||||
// Perspective divide → NDC xy.
|
||||
var ndc = new Vector2[clip.Count];
|
||||
for (int i = 0; i < clip.Count; i++)
|
||||
{
|
||||
float w = clip[i].W;
|
||||
ndc[i] = new Vector2(clip[i].X / w, clip[i].Y / w);
|
||||
}
|
||||
return ndc;
|
||||
}
|
||||
|
||||
// Sutherland-Hodgman against the GL near plane: keep where (w + z) >= 0 (z >= -w, i.e. z_ndc >= -1).
|
||||
private static List<Vector4> ClipAgainstNearPlane(List<Vector4> poly)
|
||||
{
|
||||
var result = new List<Vector4>(poly.Count + 1);
|
||||
for (int i = 0; i < poly.Count; i++)
|
||||
{
|
||||
Vector4 cur = poly[i];
|
||||
Vector4 prev = poly[(i + poly.Count - 1) % poly.Count];
|
||||
float dCur = cur.W + cur.Z;
|
||||
float dPrev = prev.W + prev.Z;
|
||||
bool curIn = dCur >= 0f;
|
||||
bool prevIn = dPrev >= 0f;
|
||||
|
||||
if (curIn)
|
||||
{
|
||||
if (!prevIn) result.Add(Lerp(prev, cur, dPrev, dCur));
|
||||
result.Add(cur);
|
||||
}
|
||||
else if (prevIn)
|
||||
{
|
||||
result.Add(Lerp(prev, cur, dPrev, dCur));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Vector4 Lerp(Vector4 p, Vector4 q, float dp, float dq)
|
||||
{
|
||||
float t = dp / (dp - dq);
|
||||
return p + t * (q - p);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue