// PortalProjection.cs // // Phase A8.F: project a cell-local portal polygon to NDC screen space, clipping // against the in-front-of-camera half-space (keep where w + z >= 0) so a portal // straddling the camera does not invert under the perspective divide. This crossing // excludes the eye (w = 0) and lands just in front of the near plane, so every kept // vertex has w bounded away from zero and the divide is safe — no eye-singularity // blow-up. The predicate is convention-agnostic: acdream's cameras build projection // with Matrix4x4.CreatePerspectiveFieldOfView (NDC z in [0,1]); under a true GL // [-1,1] matrix w + z = 0 is exactly the near plane. Either way the eye is excluded. // 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, preserving the projected winding of /// the input (NOT normalized to CCW). The caller (PortalVisibilityBuilder) is responsible /// for feeding camera-facing portal polygons (via the portal-side test) so the result is /// CCW for the CCW-only . Returns 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 in-front-of-camera half-space (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 in-front-of-camera half-space: keep where (w + z) >= 0. 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); } }