This article is to move the math of perspective matrix here so that I(or you if you get interested && you have the same problem as me) can review it once I forget it. Yes, I'll put everything I'm frequently referencing to this blog space. Here we go:
1. depth intepolation, linear by 1/z
suppose the line to be projected is "ax+bz=c", to research the depth intepolation, let's suppose a point (x3, z3) in the middle way between (x1, z1) and (x2, z2), and it's projected to the plane (p3, -n), and we have
(1) n/-z = p/x
(2)ax+bz=c
remove the x we have:
x = -pz/n
-apz/n+bz=c
1/z = (-ap)/(cn) + b/c
And let's linearly interpolate p, we have
1/z3 = t/z1 + (1-t)/z2
For other vertex attributes, let's suppose it's b, we would expect the interpolated value to form the same proportion with the total difference as does the z:
(b3-b1)/(b2-b1)=(z3-z1)/(z2-z1)
b3=z3(b3(1-t)/z1+b2t/z2)
2. homogeneous clip sace, place the -z in the w position
the -z value in w position has two functions:
(1)to serve the z1,z2 value in the above equation to interpolate vertex attributes
(2)to divide the result of Mp*P, as the z was stripped out so that the Mp is possible to be constructed
2n/(r-l) 0 (r+l)/(r-l) 0
0 2n/(t-b) (t+b)/(t-b) 0
0 0 -(f+n)/(f-n) -2nf/(f-n)
0 0 -1 0
3. glFrustum, glPerspective
void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal);
it's totally the same what we're talking about here:)
void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);
the zNear and zFar is still the same. This function suppose r=-l, and the fovy and aspect is equivalent in this way:
2n/(r-l)
=n/r
aspect=r/t
r=aspect*t
n/r
=n/(aspect*t)
=ctan(fovy/2) / aspect
4. orthographic projection(glOrtho)
detailed content coming soon...