diff --git a/src/AcDream.App/Rendering/PortalProjection.cs b/src/AcDream.App/Rendering/PortalProjection.cs new file mode 100644 index 0000000..852d9a1 --- /dev/null +++ b/src/AcDream.App/Rendering/PortalProjection.cs @@ -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 +{ + /// 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. + public static Vector2[] ProjectToNdc(IReadOnlyList localPoly, Matrix4x4 cellToWorld, Matrix4x4 viewProj) + { + if (localPoly == null || localPoly.Count < 3) return System.Array.Empty(); + + Matrix4x4 m = cellToWorld * viewProj; + + // To clip space (keep w). + var clip = new List(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(); + + // 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 ClipAgainstNearPlane(List poly) + { + var result = new List(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); + } +} diff --git a/tests/AcDream.App.Tests/Rendering/PortalProjectionTests.cs b/tests/AcDream.App.Tests/Rendering/PortalProjectionTests.cs new file mode 100644 index 0000000..eeb6c6e --- /dev/null +++ b/tests/AcDream.App.Tests/Rendering/PortalProjectionTests.cs @@ -0,0 +1,87 @@ +using System.Numerics; +using AcDream.App.Rendering; +using Xunit; + +namespace AcDream.App.Tests.Rendering; + +public class PortalProjectionTests +{ + // A simple GL-style perspective looking down -Z, camera at origin. + private static Matrix4x4 ViewProj() + { + var view = Matrix4x4.CreateLookAt(Vector3.Zero, new Vector3(0, 0, -1), Vector3.UnitY); + var proj = Matrix4x4.CreatePerspectiveFieldOfView(1.0f, 1.0f, 0.1f, 1000f); + return view * proj; + } + + [Fact] + public void Project_QuadInFront_ProducesNdcInsideViewport() + { + // A 2x2 quad at z=-5 (in front), cell-local == world (identity transform). + var poly = new[] + { + new Vector3(-1, -1, -5), new Vector3(1, -1, -5), new Vector3(1, 1, -5), new Vector3(-1, 1, -5), + }; + var r = PortalProjection.ProjectToNdc(poly, Matrix4x4.Identity, ViewProj()); + Assert.True(r.Length >= 3); + foreach (var v in r) + { + Assert.InRange(v.X, -1.001f, 1.001f); + Assert.InRange(v.Y, -1.001f, 1.001f); + } + } + + [Fact] + public void Project_QuadFullyBehind_ReturnsEmpty() + { + var poly = new[] + { + new Vector3(-1, -1, 5), new Vector3(1, -1, 5), new Vector3(1, 1, 5), new Vector3(-1, 1, 5), + }; + var r = PortalProjection.ProjectToNdc(poly, Matrix4x4.Identity, ViewProj()); + Assert.True(r.Length < 3); + } + + [Fact] + public void Project_QuadStraddlingCamera_ClipsWithoutInversion() + { + // Spans from behind (z=+2) to in front (z=-5). Must clip to the in-front part, + // never produce a wildly out-of-range inverted vertex. + var poly = new[] + { + new Vector3(-1, -1, 2), new Vector3(1, -1, -5), new Vector3(1, 1, -5), new Vector3(-1, 1, 2), + }; + var r = PortalProjection.ProjectToNdc(poly, Matrix4x4.Identity, ViewProj()); + Assert.True(r.Length >= 3); + foreach (var v in r) + { + // A corner a few cm in front of the eye and ~1 m to the side genuinely + // projects large (~±37 NDC) but finite. ±50 still catches the ±7852 + // perspective-inversion blow-up the old w-clip produced. + Assert.InRange(v.X, -50f, 50f); // bounded — no inversion blow-up + Assert.InRange(v.Y, -50f, 50f); + } + } + + [Fact] + public void Project_QuadStraddlingCamera_DownstreamIntersectionIsValidOnScreen() + { + var poly = new[] + { + new Vector3(-1, -1, 2), new Vector3(1, -1, -5), new Vector3(1, 1, -5), new Vector3(-1, 1, 2), + }; + var projected = PortalProjection.ProjectToNdc(poly, Matrix4x4.Identity, ViewProj()); + Assert.True(projected.Length >= 3); + + // The viewport region (NDC [-1,1]^2), same as CellView.FullScreen()'s single polygon. + var viewport = CellView.FullScreen().Polygons[0].Vertices; + var onScreen = ScreenPolygonClip.Intersect(projected, viewport); + + Assert.True(onScreen.Length >= 3); // a non-empty visible region survives + foreach (var v in onScreen) + { + Assert.InRange(v.X, -1.001f, 1.001f); + Assert.InRange(v.Y, -1.001f, 1.001f); + } + } +}