Given 4 points in eye coordinates (call them ABCD), all of which are in front of the camera (Z<0), how can I calculate a 4x4 projection matrix that will project points ABCD exactly onto the 4 corners of the viewport?
The goal is to make an orthogonal rendering of a rectangular quad which exists somewhere in the scene (with vertices ABCD) as if it was viewed square-on, but where the rendering also includes any objects that block line of sight to the quad from the actual camera location.
I think the answer may involve a homography which maps eye coords to NDC, and I've used OpenCV's cv2.getPerspectiveTransform() to calculate one. But converting the 3x3 homography to a 4x4 projection matrix is proving a challenge.
I can correctly map X and Y from eye coords to NDC using this matrix, but only when A,B,C,D have the same Z coordinate:
H[0,0] H[0,1] 0 H[0,2]
H[1,0] H[1,1] 0 H[1,2]
0 0 0 0
H[2,0] H[2,1] 0 1
I can't work out how to compute Z while keeping X and Y. This handy guide to projection matrices shows that to correctly map Z from [-near,-far] to [-1,1], the bottom rows of the projection matrix should be:
0 0 -(f+n)/(f-n) -2fn/(f-n)
0 0 -1 0
I feel like I'm holding all the pieces of the puzzle but can't work out how to put them together.