# how to trace light after refraction by a camera lens?

I am a programmer and I am doing a camera simulation, I am stuck in a matter of how to know where arrives every ray of light after traveling through the lens and being refracted. Every point of the object gives an infinite number of rays, but in my simulation I will take five random rays to trace from every point from the object. The rays from one point of the object should also come to one point on the film. How can I know this specific point on the film for each point from the object? Hope to find some help here about this problem.

This is classic raytracing problem with well known pitfalls.

If you trace from the camera to the scene (backward raytracing), you can get most of the things right. Having a physical camera with real lenses is not a problem, just consider the lens system as part of the scene. For a simple pinhole sensor, this works fairy well, but requires special treatment for phenomena that can only be traced from the light source (forward raytracing): caustics, for instance, requires photon mapping. If you want to simulate a physical film, you may still need to shoot many samples from each pixel on the film to account for rays refracting through each part of the lens (this gives you correct depth of view).

If you trace from the light sources (to object, to camera), it's much much worse, because most of the rays are wasted. This, however, is equivalent to what happens in real life and thus simulates everything -- the sampling has to be random (random rays in random directions from each light, and random scattering on all diffuse surfaces and so on). This even gives you correct statistical noise at low intensities. In this case, physical film is better than a pinhole camera (because point camera is impossible to hit, you need to do the last part of the path not randomly but just go directly to the camera and use analytical expressions for the amount of incident light at that angle).

There are advanced techniques to improve the efficiency of this algorithm. In Metropolis light transport, "good" light paths are reused and only slightly perturbed many times.

To sum up: the best idea would be to shoot a few samples from the film to the lens, and see where it gets you. If you insist on starting at the light source, you need to shoot many rays (500 rays would be a reasonable starting point) towards the camera and hope it gets all the way to the film.

Shooting from the object instead of light source removes one step, but can get you into big trouble: you have to ensure that the light distribution is correct. Knowing the brightness is not enough, the same average number of rays have to be shot per unit area of the object.

EDIT:

With raytracing, especially from light to the camera, refractions and reflections are the easiest thing to do (the hard part is the scattering on diffuse surfaces). Geometry is pretty basic, for a ray direction vector $\vec{s}$ you can get its perpendicular component $(\vec{n}\cdot\vec{s})\vec{n}$ to the surface with normal $\vec{n}$ and the rest is transverse component ($\vec{s}-(\vec{n}\cdot\vec{s})\vec{n}$). Then you just use Snell's law to add these to vectors with different weights. If you want to be exact, you must also take care of the intensity: the percentage of refracted light depends on the index of refraction and angle of incidence (and some light is reflected, so in principle you have to split the ray in two). In essence, just stop at the surface, compute the new direction and intensity, and carry on. The details are here: http://graphics.stanford.edu/courses/cs148-10-summer/docs/2006--degreve--reflection_refraction.pdf

Just to stress the main points of raytracing here: 1) You should sample rays randomly, this way you get progressively better image the longer you wait, and it's actually what happens in real life. 2) The distribution of random sampling is essential, it dictates the brightness profile of illumination. A uniform point light source shoult shoot rays into all directions equally, but that's wasteful: you can smartly limit to rays that you know have a chance of hitting the object, but within these, they must still prefer all directions equally. A crude example is to shoot only into the half-space where the object is. 3) You should shoot rays from the light -- shooting from the object will cause many problems and will be very hard to get right (you miss shadows, you don't have the right distribution of brightness across the surface...). However, it is possible to compute the illumination of the object by "burning" the texture with non-raytracing methods (much faster because you don't lose a huge proportion of rays from the light that miss the object). If you do this (and if you have triangulation), the color of the ray is read from the texture, the number of rays should be proportional to the surface of the triangle. 4) When you hit a diffuse object, the intensity of the ray is inversely proportional to the square of the distance from the point light source. 5) The ray that hits the object should scatter into a new random direction (away from the surface), with perpendicular directions being more frequent (density proportional to $\cos\theta$ -- Lambert law). This step is why you need a lot of rays. The ray color is multiplied by the color of the surface. If you shoot directly from the surface, you must also shoot proportionally to the cosine. 6) The new ray that starts at the surface can bounce around further -- but it makes sense to stop the recursion after a few. 7) Refractions and reflections are nice special cases: instead of redirecting the ray into random directions like with diffuse surface, the direction of the new ray is determined exactly. 8) When the ray lands on the sensor plate, you record it (add the color to the sensor). The rest of the runaway rays are wasted.

The longer you wait, the better the picture is, just like a longer exposure time has less noise because you get more photons on the sensor.

• thank you for your post, but actually i didn't find the answer which i really need, I am doing the way you said to trace from object to lens because I need it to be just like real life, so how to trace them after refraction, I didn't get this point until know. @orion – user3159060 Jan 9 '15 at 21:34

I am sorry, but it is not possible to tell you where this point will appear on the film, because one cannot know whether the fascicle of rays emerging from one point of the object would converge to one point on the film. You have to "play" with the distance, i.e. bring the camera closer to or more distant from the film, until you get one point on the film for one point on the object.

• While one point on the object does not necessarily correspond to one point on the film (if it is out of focus), a ray originating from that point will reach a well-defined point on the film. This is the principle behind ray optics. The problem is that you need many rays to build up an image. – lionelbrits Jan 8 '15 at 12:31