# How to build a decent lens/camera objective model for path tracing?

I have written a small path-tracer after learning and experimenting on smallpt.

The only thing that I did not write (and understood) myself is how the initial rays are computed and fired from the camera. I got the principle right, but I'm looking for some resources describing how to:

• Compute the initial direction of rays
• Modelize a real lens (as opposed to pinhole camera), supposedly allowing effects like depth of field ?

State of the art maths and physics are not required, but OK if explained thoroughly.

The next step up from a pinhole camera model is a thin lens model, where we model the lens as being an infinitely thin disc. This is still an idealization that pretty far from modeling a real camera, but it will give you basic depth of field effects.

The image above, from panohelp.com, shows the basic idea. For each point on the image, there are multiple rays arriving at that image point, via every point on the 2D lens surface. Therefore, generating an image like this using Monte Carlo will require picking, for each ray, both a 2D sample point on the image plane and an independent 2D sample point on the lens surface.

The user-facing parameters to set will be the lens radius (as a physical radius in scene units), which controls how shallow the focus range is (larger lens = shallower focus range), and the distance at which you want objects to be in focus.

To generate eye rays into the scene, you can calculate the position and direction of rays leaving the lens surface; in this model there's no need to explicitly simulate the image plane and the refraction through the lens. Basically, think of the lens as centered at the camera position and oriented to face the camera direction.

Based on the image location, construct a ray from the camera position (lens center) into the scene, just as you would in the pinhole model; then find its intersection with the focal plane. That's where all the rays from that image location should converge. Now you can offset the starting point of the ray to a randomly chosen point on the lens, and set its direction to be toward the convergence point.

You can generalize this a bit by allowing the focal plane to be something other than a plane, or the lens to be something other than a circular disc, and following the same process. That can produce some interesting if not-quite-physical effects. It's also possible to go beyond this simple model with a more physically realistic simulation of a camera's lens elements—but that's beyond my expertise.

See Kolb, et al., A Realistic Camera Model for Computer Graphics, SIGGRAPH 95.

However, do bear in mind that camera models which mimic real-world cameras aren't necessarily what you want for the rendering phase. In a visual effects/post-production scenario, the more blur/vignetting/distortion that the camera model introduces, the worse it is for the compositor/colour timer. It's often better to do it as a post-pass.