# Why does _ray_ tracing work?

Perspective projection (or at least non-orthogonal) is by far the most common in photo-realistic graphics. When we use perspective projection, the (equally thick) slices of volume visualized by each pixel get larger with the distance. But with ray tracing we normally cast cylinder-like rays instead of cones. It seems to me that this must make quite a difference to the results: consider the two fields of view of pixels: cone-like one at the LHS, and cylinder-like one at the RHS, the pixels are at the bottom:

The colored lines here represent walls of randomly-distributed light sources with the same density in each wall. If we assume that the intensities of the light sources are the same, so that one of the pixels would "see" white average color, the other must necessarily have a color tint, because the left one samples more red compared to blue than the right one.

Am I right in this? If so, why does then ray tracing work? Why don't we do "frustum tracing" or "cone tracing" to get the colors/illuminances right? And if I'm wrong, then what is my mistake?

• Keep in mind that light intensity falls off with the square of distance, so neither of them would generate white, unless the intensities were set appropriately. – user1118321 Jul 10 '18 at 2:17
• I wouldn't call the cylinder rays, more like infinitely thin rays that either fully hit or fully miss geometry. – PaulHK Jul 10 '18 at 2:25

But with ray tracing we normally cast cylinder-like rays instead of cones.

Rays aren't really cylinders: they're infinitely thin. This infinitely thin ray can be an approximation to the cone just as much as it can be an approximation to the cylinder.

If we assume that the intensities of the light sources are the same, so that one of the pixels would "see" white average color, the other must necessarily have a color tint ... Am I right in this?

Sort of. Your left-hand diagram doesn't have a colour tint. Although it covers more of the red wall than the blue wall, both of them subtend the same solid angle from the camera's point of view. The same $\tfrac{1}{r^2}$ ratio between the amount of light included in the cone is exactly countered by the $\tfrac{1}{r^2}$ falloff of the light. If the world worked the way you describe, you could make anything brighter by moving it further away from your eye. The right-hand diagram would have a blue colour tint, because the blue light is closer, but this diagram is not how ray-tracing works.

There has been research in a few variants of beam tracing, where instead of tracing an infinitely thin ray, you trace a cylinder, or cone tracing, where you trace a cone or frustum as a simple approximation to the irregular solid that contributes to the pixel. The maths (and therefore the algorithm) are somewhat more complex (and therefore slower) than simple ray tracing, and then you have to deal with what happens when the cone partially intersects an object, and you have to use the same shape to approximate the continued path of the cone. These approaches never caught on.

Instead, supersampling with rays offers the same advantages as cone tracing but with the performance advantage of doing the same simple algorithm many times. You don't supersample by moving the origin of the rays, which would make a cylinder shape, but by varying the directions, which makes a cone shape. Instead of approximating that cone by a single ray along its axis, you're approximating it by (say) 32 rays spread throughout its volume.

• " but with the performance advantage of doing the same simple algorithm many times." ... and the performance disadvantage of doing more rays ;-) – Simon F Jul 10 '18 at 9:14
• @SimonF I'd rather do four simple rays with no edge cases than one complex cone with special handling for partial occlusion; more if I have a GPU to do them on. – Dan Hulme Jul 10 '18 at 9:16
• No worries :-) It was a tongue-in-cheek remark. FWIW 30 years(!) ago I was developing an Amantides-style cone ray tracer (dl.acm.org/citation.cfm?id=808589). There are certainly pros and cons to it compared to simple rays. – Simon F Jul 10 '18 at 10:39
• @SimonF Oh yeah, that trade-off was a lot different before SIMD and then GPUs made it cheap to trace a zillion coherent supersampling rays. – Dan Hulme Jul 10 '18 at 11:55

It's useful to think of rays not as "tubes" or "cones" of vision, but as the units of bookkeeping of radiant power. This notion resembles that of photons: they also are emitted and detected as a whole, and, for a given frequency, light intensity is represented by density of these units, while the energy per unit doesn't change as the unit propagates.

Consider, for example, the well-known inverse-square law for intensity of light emitted by an isotropic source. Starting with $N$ rays emitted by a surface of area $A$, you'll see that these rays, propagating outwards, have more and more space between them – their density reduces, while number of them, $N$, remains the same (in empty space). Calculate average number of rays per unit surface at distance $r$, and you'll see that it's indeed proportional to $r^{-2}$.

Now, the above example was about forward ray tracing. Backward ray tracing is actually exactly the same, but the question of each ray is not "what object might absorb me?" – instead, it's "where could this unit of light come from?".