


Y = mat * p.x + mat * p.y + mat * p.z + mat * 1

X = mat * p.x + mat * p.y + mat * p.z + mat * 1 We need to actually implement the projection (which is basically a matrix multiplication): def project(p, mat): We need to start with some matrix, identity matrix seems a natural choice: mat = [ # 3D coordinates corresponding to i0, i1, i2, i3 Let's define our points: # Known 2D coordinates of our rectangle If our random perturbations cause the projected 2D points to get closer to the ones we marked above, then we keep that matrix as an improvement over our initial (or previous) guess. So what we're going to do is project each of the four 3D points and see how close we get to the 2D points we wanted. Remember, the algorithm is to start with some projection matrix and randomly perturb it until it gives the projection that we want. It is very handy to look at the gluProject() documentation because I actually want a solution that works for OpenGL, but beware that the documentation is missing the division by z in the formula. This is more or less what gluProject() does, except gluProject() also takes the current viewport into account and takes a separate modelview matrix into account (we can just assume the modelview matrix is the identity matrix). Projecting each of these 3D coordinates into 2D is done by multiplying the 4D vector with a 4x4 projection matrix, then dividing the x and y components by z to actually get the perspective correction. In other words, a unit square in the y=0 plane. Let's say we use GIMP to find the 2D coordinates of what we think is a square on the ground plane (whether or not it is really a square depends on your judgment of the depth): Here is a still photo from Thomas the Tank Engine: Very simply put, the algorithm is as follows: Randomly perturb your projection matrix until it projects your known 3D coordinates to your known 2D coordinates. Alright, I came here looking for an answer and didn't find something simple and straightforward, so I went ahead and did the dumb but effective (and relatively simple) thing: Monte Carlo optimisation.
