<p>In a traditional camera, the photons from the scene travel through the lens of the camera, then hit the sensor at the focal length. A consequence of the lens is that the image is upside down and backwards.</p>

<p><a href="https://i.sstatic.net/e4pu7.gif" rel="noreferrer"><img src="https://i.sstatic.net/e4pu7.gif" alt="Camera"></a></p>

<p>In ray tracing, we can optimize this by imagining the focal plane is in front of the camera. You can think that the photons from the scene travel towards the camera hole, and resolve on the image plane. </p>

<p><a href="https://i.sstatic.net/iOrm3.png" rel="noreferrer"><img src="https://i.sstatic.net/iOrm3.png" alt="Photons hit image plane"></a></p>

<p>Another optimization in ray tracing to to trace rays from the camera into the scene, rather than tracing them from light sources and hoping they hit the camera. Therefore, the first rays we shoot out are the ones that go from the eye through each pixel on the virtual screen.</p>

<p><a href="https://i.sstatic.net/BW8Vy.png" rel="noreferrer"><img src="https://i.sstatic.net/BW8Vy.png" alt="Shooting a Ray"></a></p>

<p>In order to create the ray, we need to know the distances a and b in world units. Therefore, we need to convert pixel units into world units. To do this we need a define a camera. Let's define an example camera as follows:
$$origin = \begin{bmatrix} 0 &amp; 0 &amp; 0 \end{bmatrix}$$
$$coordinateSystem = \begin{bmatrix} 1 &amp; 0 &amp; 0\\ 0 &amp; 1 &amp; 0\\ 0 &amp; 0 &amp; 1 \end{bmatrix}$$
$$fov_{x} = 90^{\circ}$$
$$fov_{y} = \frac{fov_{x}}{aspectRatio}$$
$$focalDist = 1$$</p>

<p>The field of view, or fov is an indirect way of specifying the ratio of pixel units to view units. Specifically, it is the viewing angle that is seen by the camera.</p>

<p><a href="https://i.sstatic.net/OyYfj.png" rel="noreferrer"><img src="https://i.sstatic.net/OyYfj.png" alt="FOV Eye Plane"></a></p>

<p>The higher the angle, the more of the scene is seen. But remember, changing the fov does not change the size of the screen, it merely squishes more or less of the scene into the same number of pixels.</p>

<p><a href="https://i.sstatic.net/PBEBi.png" rel="noreferrer"><img src="https://i.sstatic.net/PBEBi.png" alt="FOV Explaination"></a></p>

<p>Let's look at the triangle formed by fovx and the x-axis:</p>

<p><a href="https://i.sstatic.net/sSUA1.png" rel="noreferrer"><img src="https://i.sstatic.net/sSUA1.png" alt="Triangle"></a></p>

<p>We can use the definition of tangent to calculate the screenWidth in view units
$$\tan \left (\theta  \right) = \frac{opposite}{adjacent}$$
$$screenWidth_{view}= 2 \: \cdot \: focalDist \: \cdot \: \tan \left (\frac{fov_{x}}{2}  \right)$$</p>

<p>Using that, we can calculate the view units of the pixel.
$$x_{homogenous}= 2 \: \cdot \: \frac{x}{width} \: - \: 1$$
$$x_{view} = focalDist \: \cdot \: x_{homogenous} \: \cdot \: \tan \left (\frac{fov_{x}}{2}  \right)$$</p>

<p>The last thing to do to get the ray is to transform from view space to world space. This boils down to a simple matrix transform. We negate yview because pixel coordinates go from the top left of the screen to the bottom right, but homogeneous coordinates go from (-1, -1) at the bottom left to (1, 1) at the top right.
$$ray_{world}= \begin{bmatrix} x_{view} &amp; -y_{view} &amp; d\end{bmatrix}\begin{bmatrix} &amp;  &amp; \\ &amp; cameraCoordinateSystem &amp; \\ &amp;  &amp; \end{bmatrix}$$</p>

<p>You can see an implementation of this <a href="https://github.com/RichieSams/lantern/blob/669e6c9ccd33f455961a96c5cfe0a5bc3f2af59c/source/scene/camera.cpp#L86" rel="noreferrer">here</a> (In the code, I assume the focalDist is 1, so it cancels out)</p>

CC BY-SA 3.0
2016-02-17T18:15:08.913Z
Where the lens are located in your model? Onto the screen plane? Where is the aperture size?
arcollector
CC BY-SA 3.0
2016-02-16T18:38:54.543Z
2016-02-16T18:38:54.543Z
RichieSams
<p>I'm reading Cook's paper "Stochastic Sampling and Distributed Ray Tracing", I don't understand how the rays are generated. He says:</p>

<blockquote>
  <p>Determine the focal point by constructing a ray from the eye point
  (center of the lens) through the screen location of the ray. The focal
  point is located on this ray so its distance from the eye point is
  equal to the focal distance.</p>
</blockquote>

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

<p>I can't parse this image. Where is my screen plane located according to this image? What is the focal point? If you see some optics picture of a thin lens, the focal point is always in the same place, where all the rays converge. If it is always the same point, why do have I to calculate it for each ray?</p>

CC BY-SA 3.0
2016-02-14T00:52:34.470Z
I was writing ray-tracing instead of raytracing, that was the error
arcollector
<raytracing><vectors>
Ray tracing with thin lens camera
arcollector
# Ray tracing with thin lens camera

(Asked by: username_0 on 2016-02-13)

I'm reading Cook's paper "Stochastic Sampling and Distributed Ray Tracing", I don't understand how the rays are generated. He says:

> Determine the focal point by constructing a ray from the eye point
> (center of the lens) through the screen location of the ray. The focal
> point is located on this ray so its distance from the eye point is
> equal to the focal distance.

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

I can't parse this image. Where is my screen plane located according to this image? What is the focal point? If you see some optics picture of a thin lens, the focal point is always in the same place, where all the rays converge. If it is always the same point, why do have I to calculate it for each ray?

**Tags:** raytracing, vectors

> **Comments:**
> **username_0**: I was writing ray-tracing instead of raytracing, that was the error  

---

## 1 answers

### Answer by username_1 on 2016-02-16. Score: 6

In a traditional camera, the photons from the scene travel through the lens of the camera, then hit the sensor at the focal length. A consequence of the lens is that the image is upside down and backwards.

[![Camera](https://i.sstatic.net/e4pu7.gif)](https://i.sstatic.net/e4pu7.gif)

In ray tracing, we can optimize this by imagining the focal plane is in front of the camera. You can think that the photons from the scene travel towards the camera hole, and resolve on the image plane.

[![Photons hit image plane](https://i.sstatic.net/iOrm3.png)](https://i.sstatic.net/iOrm3.png)

Another optimization in ray tracing to to trace rays from the camera into the scene, rather than tracing them from light sources and hoping they hit the camera. Therefore, the first rays we shoot out are the ones that go from the eye through each pixel on the virtual screen.

[![Shooting a Ray](https://i.sstatic.net/BW8Vy.png)](https://i.sstatic.net/BW8Vy.png)

In order to create the ray, we need to know the distances a and b in world units. Therefore, we need to convert pixel units into world units. To do this we need a define a camera. Let's define an example camera as follows:
$$origin = \begin{bmatrix} 0 & 0 & 0 \end{bmatrix}$$
$$coordinateSystem = \begin{bmatrix} 1 & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 1 \end{bmatrix}$$
$$fov\_{x} = 90^{\circ}$$
$$fov\_{y} = \frac{fov\_{x}}{aspectRatio}$$
$$focalDist = 1$$

The field of view, or fov is an indirect way of specifying the ratio of pixel units to view units. Specifically, it is the viewing angle that is seen by the camera.

[![FOV Eye Plane](https://i.sstatic.net/OyYfj.png)](https://i.sstatic.net/OyYfj.png)

The higher the angle, the more of the scene is seen. But remember, changing the fov does not change the size of the screen, it merely squishes more or less of the scene into the same number of pixels.

[![FOV Explaination](https://i.sstatic.net/PBEBi.png)](https://i.sstatic.net/PBEBi.png)

Let's look at the triangle formed by fovx and the x-axis:

[![Triangle](https://i.sstatic.net/sSUA1.png)](https://i.sstatic.net/sSUA1.png)

We can use the definition of tangent to calculate the screenWidth in view units
$$\tan \left (\theta \right) = \frac{opposite}{adjacent}$$
$$screenWidth\_{view}= 2 \: \cdot \: focalDist \: \cdot \: \tan \left (\frac{fov\_{x}}{2} \right)$$

Using that, we can calculate the view units of the pixel.
$$x\_{homogenous}= 2 \: \cdot \: \frac{x}{width} \: - \: 1$$
$$x\_{view} = focalDist \: \cdot \: x\_{homogenous} \: \cdot \: \tan \left (\frac{fov\_{x}}{2} \right)$$

The last thing to do to get the ray is to transform from view space to world space. This boils down to a simple matrix transform. We negate yview because pixel coordinates go from the top left of the screen to the bottom right, but homogeneous coordinates go from (-1, -1) at the bottom left to (1, 1) at the top right.
$$ray\_{world}= \begin{bmatrix} x\_{view} & -y\_{view} & d\end{bmatrix}\begin{bmatrix} & & \\ & cameraCoordinateSystem & \\ & & \end{bmatrix}$$

You can see an implementation of this [here](https://github.com/username_1/lantern/blob/669e6c9ccd33f455961a96c5cfe0a5bc3f2af59c/source/scene/camera.cpp#L86) (In the code, I assume the focalDist is 1, so it cancels out)

> **Comments:**
> **username_0**: Where the lens are located in your model? Onto the screen plane? Where is the aperture size?  

---

