>>11658899deriving the perspective projection is a little inspired. there are multiple ways you could define this transform, but i'll motivate the one used in opengl the way i see it.
what we want out of a perspective projection is for objects that are far away from the eye to become smaller. referring to the figure, assuming our eye is at the origin looking down the z-axis and the screen/near clip plane is at z = n, what we want is given a point in eye space (xe,ze) to find what point on the screen (xs,n) it maps to. this is easy if we know (xe,ze) and n, we just use the geometry of similar triangles and derive the formula
thus, after a perspective transformation we want xs = n * xe / ze. but linear algebra doesn't allow us to divide; we can't form a matrix that takes (xe,ze) as input and outputs a 1/ze term. but we can use the equivalence of homogeneous vectors to fuck with the h coordinate, and then at the end of the day we can divide by the h coordinate to recover the standard form of homogeneous vectors where h = 1.
following this logic, what we want after transformation is
where A, B, and C are some unknown matrix coefficients, that way after homogeneous division we get
transformed depth shouldn't depend on cross-range xe, so A = 0. what should B and C equal? well, it would be desirable if points on the screen/near clip plane z = n don't move after transformation and division. we could introduce a far clip plane z = f too with the same constraint. therefore we want the equation for z_s to satisfy
using these equations, we can solve for A and B in terms of n and f: A = n + f, B = -nf (do the algebra yourself)