How does ray tracing work?
Ray tracing follows straight-line light rays backwards from the camera through each pixel, then applies the laws of optics where they hit to decide the pixel's colour.
How does ray tracing work?
Ray tracing makes an image by sending one straight-line ray from the camera through every pixel and asking what it hits first. At that point, the laws of geometric optics — Lambert's cosine law, the inverse-square law, shadows and the law of reflection — decide how much light travels back along the ray, and that becomes the pixel's colour.
Why can light be modelled as rays?
Light is an electromagnetic wave, but when every object in the scene is much larger than its wavelength (about – for visible light) diffraction is negligible and light travels in straight lines. This is geometric optics: energy flows along rays, and rays only change direction where they meet a surface. Every step of a ray tracer is a statement from geometric optics.
Real lamps emit rays in every direction and only a tiny fraction ever reaches a camera. Simulating them forwards would waste nearly all the work, so a ray tracer starts at the camera instead. This is allowed by the Helmholtz reciprocity principle: a light path from A to B behaves the same as the path from B to A. The method of tracing backwards with shadow and mirror rays is called Whitted-style ray tracing, after Turner Whitted (1980).
How is a ray sent through a pixel?
The camera sits at and looks along , with pointing right and up. A pixel is a point on an image plane one unit in front of the camera, spread across . The ray through it is:
Because is a unit vector, is simply the distance travelled in metres. Notice that nothing here depends on the objects or the light — move them and this step does not change.
How do you find where a ray hits a sphere?
A sphere of centre and radius is every point at distance from . Substituting the ray into that condition gives a quadratic equation in :
| Discriminant | Roots | What the ray does |
|---|---|---|
| none | misses the sphere | |
| one | grazes the edge (a tangent line) | |
| two | enters at and leaves at |
The visible surface is the smallest positive root, . A negative root means the intersection is behind the camera. The flat floor is even simpler: the plane is hit when , so . Whichever object gives the smaller positive is the one the pixel shows.
At the hit point the ray tracer needs the surface normal — the unit vector sticking straight out of the surface. For a sphere it points from the centre through : . Every lighting law below measures angles from .
How bright is the surface where the ray lands?
The inverse-square law
A point light of radiant intensity (watts per steradian) spreads its power over spheres of area , so the irradiance it delivers at distance falls as the square of the distance. Doubling the distance to the light cuts the light arriving to a quarter.
Lambert's cosine law
A beam arriving at angle from the normal is spread over a patch times larger than when it arrives head-on, so the energy per unit area falls by . With unit vectors, , and a matte (diffuse) surface of reflectance looks this bright:
The removes surfaces facing away from the light. This single dot product is why a sphere looks round: its brightness fades smoothly from the point facing the lamp to the terminator, where . It is also why winter sunlight, arriving at a slant, heats the ground less.
Shiny highlights
Glossy surfaces also reflect light mostly in the mirror direction. The Phong model mirrors about the normal to get , then checks how closely it points at the viewer . The exponent (shininess) sets how tight the highlight is:
How does a ray tracer make shadows?
A point is in shadow when something blocks the straight line between it and the light. So the tracer casts a second ray, the shadow ray, from toward the light. If it hits an object at a distance smaller than , the direct light is removed and only the ambient term remains.
With one point light, shadows have perfectly sharp edges. Real lamps have a size, so points near the edge see part of the lamp: that partial shadow is the penumbra, which a point-light tracer cannot produce.
How are reflections traced?
At a mirror, the angle of incidence equals the angle of reflection, and the incoming ray, the normal and the outgoing ray lie in one plane. Subtracting twice the component of along the normal flips it and gives exactly that direction:
The reflected ray is then traced with exactly the same steps — intersection, lighting, shadows — which makes ray tracing recursive. The pixel mixes the surface's own colour with what the mirror ray sees, weighted by the reflectivity : . Recursion stops after a fixed number of bounces.
What does simple ray tracing leave out?
- Refraction — transparent materials bend rays by Snell's law, ; a tracer adds a refracted ray alongside the reflected one.
- Soft shadows — sampling many points over an area light produces the penumbra.
- Global illumination — light bouncing between diffuse surfaces (colour bleeding). Path tracing handles it by averaging many random bounces, a Monte Carlo solution of the rendering equation.
- Wave optics — diffraction, interference and polarisation cannot be described by rays at all.
Frequently asked questions
Why does ray tracing start at the camera instead of the light?
What does the discriminant tell you in ray–sphere intersection?
Why does brightness depend on the cosine of the angle?
Is ray tracing physically accurate?
What is the difference between ray tracing and path tracing?
Keep exploring
- Follow a ray and move the camera, sphere and light in the Ray Tracing simulation.
- Every step above is vector algebra: Vectors: components, addition, dot and cross products.
- Another question geometric optics answers — light scattering off air molecules: Why is the sky blue?.
- Angles, cosines and radians from the ground up: What is the unit circle?.
- The same reflection rule for a moving ball — the velocity component along the normal flips: How does a bouncing ball work?.