4

I am a new leaner of computer graphics. I'm trying to understand a code I found online and I got stuck in the following formula:

topLeft = [-tan(vfov/2)*(output_width/output_height), -tan(vfov/2), 1];

Source

looking on the internet I found that the formula is that of the Perspective Matrix. However the formula of the code has:

tan(vfov/2)*(output_width/output_height) instead of

1 / tan(vfov/2)*(output_width/output_height)

Here is the Perspective Matrix I found online.

Can you explain to me why I have inverse values? What does it mean? Thank you so much

Perspective Matrix

Simon F
  • 4,126
  • 11
  • 30
BossShell
  • 143
  • 3
  • It's a little unclear what `topLeft` and the computation for it actually *is* and in what context it is used. That matrix you show is certainly correct. But we don't know what `topLeft` is *supposed* to be. Are you sure it isn't intentionally doing the *reverse* of the transformation? – Christian Rau Mar 17 '18 at 14:46
  • is it an inverse mapping? – BossShell Mar 19 '18 at 13:08

1 Answers1

2

There is the whole derivation of it but I'll be discussing a brief overview. This is for the perspective projection where the line joining the eye and the center of the projection/image plane is perpendicular to it. Like here

enter image description here

As we can easily see, by similar triangles $\triangle ABC$ and $\triangle AEF$ we have

$Y_p / Y = D/-Z$

where $Y_p$ is the projected $Y$ coordinate on to the image plane. $AB = D$ is the total distance from eye to the image plane and is usually set to 1.

Hence we have,

$Y_p = (Y*D)/-Z$

Most APIs also set that the camera is facing towards the negative Z axis following the convention for right handed coordinate system, hence the negative sign. The projection plane is also assumed to be a rectangle instead of a square. It's 2 units long in the $Y$ direction and centered at the origin i.e in the range [-1,1] so it's 1 unit long in the positive Y axis and 1 unit long in the negative. However horizontally, its in the range $[-A, A]$ so when calculating $X_p$ we have to correct for this factor.

$A = Width\ of\ rectangle/Height\ of\ rectangle$

Similarly for $X_p$ and $Z_p$ we have,

$A*X_p = (X*D)/(-Z)$

$X_p = (X*D)/(-Z*A)$

$Z_p = D$

We can easily see see that

$tan(Y_{fov}/2) = BC/D = 1/D$

$D = 1/tan(Y_{fov}/2)$

Here, $Y_{fov}$ is the angle of field of view in the Y axis. Since we are only talking about the upper half its $Y_{fov}/2$.

Hence one way to write $X_p$ and $Y_p$ is

$X_p = \frac{X}{-Z} * \frac{1}{A*tan(Y_{fov}/2)}$

$Y_p = \frac{Y}{-Z} * \frac{1}{tan(Y_{fov}/2)}$

This is what you are seeing in the matrix. The $X$ and $Y$ come from the vector at which the transformation matrix is applied and the $-Z$ term is accounted for by the $-1$ in the 4th row. This $-Z$ is stored as the $w$ coordinate which is then used in perspective division in which we divide by $w$.

For more information you can check this site here

Note:- Haven't discussed properly about NDC space, perspective divide and about near and far planes in $Z_p$ as that would take too much time. Just wanted to show you that it can be derived.

gallickgunner
  • 2,353
  • 1
  • 10
  • 32