# Ray tracing with thin lens camera

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.

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?

• I was writing ray-tracing instead of raytracing, that was the error – arcollector Feb 14 '16 at 0:52

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.

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.

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.

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.

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.

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

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 (In the code, I assume the focalDist is 1, so it cancels out)

• Where the lens are located in your model? Onto the screen plane? Where is the aperture size? – arcollector Feb 17 '16 at 18:15

Instead of a screen plane in front of the eye, it describes a film plane, where the image is projected, to explicitly model camera optics. You don't need to compute the focal point in doing ray tracing - it's just a way to find the plane of focus for depth of field effects.

For depth of field effects I use a standard perspective projection but jitter the position of the eye on a circle parallel to the focal plane - and I make sure all my rays for a given pixel go through the same spot on the focal plane making it sharp while stuff in front of or behind that plane ends up blurry. It's a simple model of something like an aperture, and gives pretty good results.