0

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.

Roofus
  • 584
  • 6
  • 17
  • Are the four points the corners of a planar quadrilateral / rectangle? Or can they really be arbitrary? Do you need to keep the camera location or can this be changed? Also, if you want to view a quad, the distance is variable (as you can increase the distance while reducing the field of view). – Nico Schertler May 24 '18 at 16:20
  • @NicoSchertler In my case the points will be coplanar. Even in a more general case, I expect they can be projected towards the camera onto a plane without changing the visible result. Also assume degenerate cases such as colinear points don't occur or are filtered out in advance. – Roofus May 28 '18 at 01:12
  • The camera location can't be changed, as the rendering should include objects that block line of sight to the quad from the actual camera location. – Roofus May 28 '18 at 01:14

0 Answers0