UniversityCorePhysicsOpticsVectors

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.

Key fact
A ray tracer runs light paths in reverse: from the eye into the scene. Because light paths are reversible, the path from the eye to a lamp carries light exactly like the path from the lamp to the eye — and almost no computation is wasted on light nobody sees.

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 400400–700 nm700\ \text{nm} 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 O⃗\vec{O} and looks along −w^-\hat{w}, with u^\hat{u} pointing right and v^\hat{v} up. A pixel is a point (sx,sy)(s_x, s_y) on an image plane one unit in front of the camera, spread across ±tan⁡(fov/2)\pm\tan(\text{fov}/2). The ray through it is:

P⃗(t)=O⃗+t D^,D^=normalize⁡ ⁣(sxu^+syv^−w^)\vec{P}(t) = \vec{O} + t\,\hat{D},\qquad \hat{D} = \operatorname{normalize}\!\left(s_x\hat{u} + s_y\hat{v} - \hat{w}\right)
Parametric ray · formula card →

Because D^\hat{D} is a unit vector, tt 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 C⃗\vec{C} and radius rr is every point at distance rr from C⃗\vec{C}. Substituting the ray into that condition gives a quadratic equation in tt:

∣O⃗+tD^−C⃗∣2=r2  ⟹  at2+bt+c=0|\vec{O} + t\hat{D} - \vec{C}|^2 = r^2 \;\Longrightarrow\; a t^2 + b t + c = 0
a=D^⋅D^,b=2 D^⋅(O⃗−C⃗),c=∣O⃗−C⃗∣2−r2a = \hat{D}\cdot\hat{D},\quad b = 2\,\hat{D}\cdot(\vec{O}-\vec{C}),\quad c = |\vec{O}-\vec{C}|^2 - r^2
Ray–sphere intersection · formula card →
DiscriminantRootsWhat the ray does
Δ<0\Delta < 0nonemisses the sphere
Δ=0\Delta = 0onegrazes the edge (a tangent line)
Δ>0\Delta > 0twoenters at t0t_0 and leaves at t1t_1

The visible surface is the smallest positive root, t=(−b−Δ)/2at = (-b - \sqrt{\Delta})/2a. A negative root means the intersection is behind the camera. The flat floor is even simpler: the plane y=0y = 0 is hit when Oy+tDy=0O_y + tD_y = 0, so t=−Oy/Dyt = -O_y/D_y. Whichever object gives the smaller positive tt is the one the pixel shows.

At the hit point P⃗=O⃗+tD^\vec{P} = \vec{O} + t\hat{D} 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 P⃗\vec{P}: N^=(P⃗−C⃗)/r\hat{N} = (\vec{P}-\vec{C})/r. Every lighting law below measures angles from N^\hat{N}.

How bright is the surface where the ray lands?

The inverse-square law

A point light of radiant intensity I0I_0 (watts per steradian) spreads its power over spheres of area 4πd24\pi d^2, so the irradiance it delivers at distance dd falls as the square of the distance. Doubling the distance to the light cuts the light arriving to a quarter.

L⃗=S⃗−P⃗,d=∣L⃗∣,L^=L⃗/d,E=I0d2\vec{L} = \vec{S} - \vec{P},\quad d = |\vec{L}|,\quad \hat{L} = \vec{L}/d,\qquad E = \frac{I_0}{d^2}
Inverse-square law of light · formula card →

Lambert's cosine law

A beam arriving at angle θi\theta_i from the normal is spread over a patch 1/cos⁡θi1/\cos\theta_i times larger than when it arrives head-on, so the energy per unit area falls by cos⁡θi\cos\theta_i. With unit vectors, cos⁡θi=N^⋅L^\cos\theta_i = \hat{N}\cdot\hat{L}, and a matte (diffuse) surface of reflectance kdk_d looks this bright:

Id=kd E max⁡(0, N^⋅L^)I_d = k_d\,E\,\max(0,\ \hat{N}\cdot\hat{L})
Lambert's cosine law · formula card →

The max⁡(0,⋅)\max(0,\cdot) 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 N^⋅L^=0\hat{N}\cdot\hat{L} = 0. 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 L^\hat{L} about the normal to get R^=2(N^⋅L^)N^−L^\hat{R} = 2(\hat{N}\cdot\hat{L})\hat{N} - \hat{L}, then checks how closely it points at the viewer V^=−D^\hat{V} = -\hat{D}. The exponent nn (shininess) sets how tight the highlight is:

Is=ks E max⁡(0, R^⋅V^) n,I=ka+Id+IsI_s = k_s\,E\,\max(0,\ \hat{R}\cdot\hat{V})^{\,n},\qquad I = k_a + I_d + I_s
An empirical model
Lambert's law is real physics for ideal matte surfaces, but the Phong highlight is an empirical fit, not a law of nature. Physically based renderers replace it with microfacet models that conserve energy. The small ambient term kak_a stands in for all the light bouncing around the room, which this simple tracer does not follow.

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 P⃗\vec{P} toward the light. If it hits an object at a distance ss smaller than dd, the direct light is removed and only the ambient term remains.

P⃗+εN^+s L^,in shadow if 0<s<d\vec{P} + \varepsilon\hat{N} + s\,\hat{L},\qquad \text{in shadow if } 0 < s < d
Shadow acne
The shadow ray starts a tiny distance ε\varepsilon above the surface. Without that offset, rounding errors make the ray hit the very surface it left, and the object covers itself in speckled false shadows.

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 D^\hat{D} along the normal flips it and gives exactly that direction:

D^r=D^−2(D^⋅N^) N^,θi=θr\hat{D}_r = \hat{D} - 2(\hat{D}\cdot\hat{N})\,\hat{N},\qquad \theta_i = \theta_r
Law of reflection · formula card →

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 ρ\rho: colour=(1−ρ) local+ρ reflected\text{colour} = (1-\rho)\,\text{local} + \rho\,\text{reflected}. Recursion stops after a fixed number of bounces.

Try it
In the simulation, drag only the light and watch the live equations: the ray, the discriminant and the reflection angle θr\theta_r stay fixed, while EE, N^⋅L^\hat{N}\cdot\hat{L} and the highlight change. The mirror direction depends on the viewer, not on the light.

What does simple ray tracing leave out?

  • Refraction — transparent materials bend rays by Snell's law, n1sin⁡θ1=n2sin⁡θ2n_1\sin\theta_1 = n_2\sin\theta_2; 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?

Almost none of the light leaving a lamp reaches the camera, so tracing it forwards wastes nearly all the work. Light paths are reversible (Helmholtz reciprocity), so following them backwards from the eye gives the same answer while only computing rays that end up in the image.

What does the discriminant tell you in ray–sphere intersection?

Substituting the ray into the sphere's equation gives a quadratic in the distance tt. If Δ=b2−4ac\Delta = b^2 - 4ac is negative the ray misses, if it is zero the ray just touches the sphere, and if it is positive the ray passes through, entering at the smaller root.

Why does brightness depend on the cosine of the angle?

A beam hitting a surface at angle θ\theta from the normal spreads the same power over an area 1/cos⁡θ1/\cos\theta times larger, so the power per unit area falls by cos⁡θ\cos\theta. That is Lambert's cosine law, and it is computed as the dot product N^⋅L^\hat{N}\cdot\hat{L}.

Is ray tracing physically accurate?

Its geometry is: straight rays, the law of reflection, the inverse-square and cosine laws all come from geometric optics. The simple version here uses an empirical highlight model and ignores light bouncing between diffuse surfaces; path tracing and physically based materials close most of that gap.

What is the difference between ray tracing and path tracing?

Whitted ray tracing follows one deterministic ray per pixel plus shadow and mirror rays. Path tracing sends many rays per pixel and lets each bounce in a random direction, averaging the results to capture soft shadows, indirect light and colour bleeding.

Keep exploring