<p>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</p>

<p><a href="https://i.sstatic.net/WmmPi.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/WmmPi.png" alt="enter image description here"></a></p>

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

<p>$Y_p / Y = D/-Z$</p>

<p>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.</p>

<p>Hence we have,</p>

<p>$Y_p = (Y*D)/-Z$</p>

<p>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. </p>

<p>$A = Width\ of\ rectangle/Height\ of\ rectangle$</p>

<p>Similarly for $X_p$ and $Z_p$ we have,</p>

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

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

<p>$Z_p = D$</p>

<p>We can easily see see that</p>

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

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

<p>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$.</p>

<p>Hence one way to write $X_p$ and $Y_p$ is</p>

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

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

<p>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$.</p>

<p>For more information you can check this site <a href="https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/opengl-perspective-projection-matrix" rel="nofollow noreferrer">here</a></p>

<p>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.</p>

CC BY-SA 3.0
2018-03-04T21:25:19.617Z
2018-03-05T09:49:05.723Z
2018-03-05T09:49:05.723Z
gallickgunner
<p>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:</p>

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

<p><a href="https://github.com/rayryeng/equi2cubic/blob/master/equi2cubic.m" rel="noreferrer">Source</a></p>

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

<p><code>tan(vfov/2)*(output_width/output_height)</code> instead of</p>

<pre><code>1 / tan(vfov/2)*(output_width/output_height)
</code></pre>

<p>Here is the Perspective Matrix I found online. </p>

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

<p><a href="https://i.sstatic.net/C9PST.jpg" rel="noreferrer"><img src="https://i.sstatic.net/C9PST.jpg" alt="Perspective Matrix"></a></p>

CC BY-SA 3.0
2018-03-17T14:46:54.870Z
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
CC BY-SA 3.0
2018-03-19T13:08:22.667Z
is it an inverse mapping?
BossShell
<c++><matrix><perspective><camera-matrix>
Inverse value in a Perspective Matrix
BossShell
# Inverse value in a Perspective Matrix

(Asked by: username_0 on 2018-03-04)

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](https://github.com/rayryeng/equi2cubic/blob/master/equi2cubic.m)

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](https://i.sstatic.net/C9PST.jpg)](https://i.sstatic.net/C9PST.jpg)

**Tags:** c++, matrix, perspective, camera-matrix

> **Comments:**
> **username_1**: 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?  
> **username_0**: is it an inverse mapping?  

---

## 1 answers

### Answer by username_2 on 2018-03-04. Score: 2 | Accepted 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

[![enter image description here](https://i.sstatic.net/WmmPi.png)](https://i.sstatic.net/WmmPi.png)

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](https://www.scratchapixel.com/lessons/3d-basic-rendering/perspective-and-orthographic-projection-matrix/opengl-perspective-projection-matrix)

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.


---

