# Inverse value in a Perspective Matrix

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 • 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 Answer

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 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.9336825,"math_prob":0.99619335,"size":2347,"snap":"2021-21-2021-25","text_gpt3_token_len":664,"char_repetition_ratio":0.103713185,"word_repetition_ratio":0.004761905,"special_character_ratio":0.29015765,"punctuation_ratio":0.06458333,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99988556,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,7,null,9,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-15T22:27:13Z\",\"WARC-Record-ID\":\"<urn:uuid:2f1ba0ca-a94e-4c6f-9bcf-96904db0d2d0>\",\"Content-Length\":\"169989\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2d0a7553-011b-4122-ac83-cad9e4930618>\",\"WARC-Concurrent-To\":\"<urn:uuid:6d4b5572-d0f8-4047-bde5-5d7b1c5b9157>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://computergraphics.stackexchange.com/questions/6365/inverse-value-in-a-perspective-matrix/6366\",\"WARC-Payload-Digest\":\"sha1:VFCSIDXEIHBY7YRYYBT2LTPJ7PSMQ5FY\",\"WARC-Block-Digest\":\"sha1:DJEE3LEZXGDYWEEUKCGA2JNZYGWMVJ3I\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487621627.41_warc_CC-MAIN-20210615211046-20210616001046-00551.warc.gz\"}"}