Motivation
Rasterization begins with projected triangles and asks which image samples they cover. Ray casting reverses that question: for one sample, construct a ray and find the first surface it meets. Both methods can describe visibility of the same scene, but their computations and numerical failure modes differ. This chapter derives intersection tests in exact arithmetic, then states the extra conventions needed in a renderer.
Rays and valid hits
Definition 4.1.1
(Ray and parameter interval)
A ray has origin and nonzero direction :
A hit is valid only if its parameter is in the interval declared for that ray. If , is distance; otherwise distance is . For a fixed ray, the nearest valid hit is the one with the smallest admissible .
A primary camera ray commonly seeks . A finite shadow ray must stop at its light. Using a unit direction is convenient, but the intersection formulas do not require it. An interval test belongs to the geometry query: a root behind the origin is not made visible by the fact that the quadratic equation has a real solution.
Ray-sphere intersection
Theorem 4.1.2
(Solving for a sphere hit)
For a sphere with and a ray with , put , , , and . Intersection parameters satisfy
If there is no real line intersection; if , test against the ray's permitted interval and choose the least valid one.
Proof
(Substituting the ray into the sphere equation)
A point lies on the sphere exactly when , where . Expanding,
so the condition is with the stated , , . Since , and the equation is a genuine quadratic. It has a real solution exactly when , and then its solutions are (one repeated root when ). Each real root is the parameter of a point of the line on the sphere; the ray's hits are those roots in the permitted interval, and the least such root is the nearest hit.
Proof X-ray
Where the sphere proof can mislead
The key move is to restrict the sphere equation to the line, which turns a three-dimensional question into one scalar quadratic in . The hypothesis is used exactly there: it makes , so the quadratic formula divides by a nonzero number. The proof says nothing about the ray's interval; it describes the entire line, so interval filtering is a separate logical step.
The signs of the roots carry the geometry. Their product is , so (origin inside the sphere) gives one negative and one positive root, and the positive one is the exit. If the origin is outside, and the roots share a sign; their sum is , so when (the ray moves away from the center) any real roots are both negative and the ray misses the sphere. Without checking those signs, a real discriminant would be mistaken for a visible hit.
Worked example 4.1.3
(A sphere in front of the camera)
Let , , , and . Then , , and . The roots are and ; the first visible hit is . The outward unit normal there is . If the ray starts at with the same direction, its roots are and : the positive exit remains valid.
Concept lensGeometric
Closest approach and half-chord
For a unit direction (), let be the parameter of the point of the line nearest the center, and the distance from to the line, so . Then
The line meets the sphere exactly when , and the two hits lie a half-chord on either side of the closest point. In the example , and the half-chord is , giving and .
At a tangent there is one repeated line root, which still needs interval testing. In floating-point arithmetic, cancellation in the usual quadratic formula can lose accuracy for one root; robust implementations use a stable root calculation and check their error bounds. A mathematically zero discriminant is not necessarily represented as exactly zero by a machine.
Ray-triangle intersection
For vertices , put and . Any point of the closed nondegenerate triangle has the form
To find an intersection solve . A valid triangle hit also satisfies the ray interval. The boundary inequalities deliberately include edges; a real implementation needs a consistent shared-edge rule if it must avoid gaps or double counting.
Set , , , and .
Proposition 4.1.4
(Ray-triangle solution by Cramer's rule)
Let be linearly independent. If , the line meets the plane of the triangle in exactly one point, and the parameters of are
The ray hits the closed triangle exactly when , , and lies in the permitted interval. If , the direction is parallel to the plane, so the line either misses the plane or lies in it.
Proof
(Three scalar triple products)
The equation is equivalent to , a linear system for whose matrix has columns . Its determinant is . If there is exactly one solution, and Cramer's rule writes each unknown as a ratio of determinants with in place of one column:
where the last two equalities use the cyclic invariance of the scalar triple product. Dividing by gives the displayed , , . Because are independent, the coordinates of a point of the plane are unique, so the point lies in the closed triangle exactly when the three inequalities hold. If the three columns are dependent; since are independent, lies in their span, that is, is parallel to the plane.
If , the formulas divide by zero: the ray is parallel to the triangle plane, or the triangle is degenerate. A coplanar ray can intersect the triangle in an interval and requires a separate policy; it is not automatically a miss. Near-zero determinants require a precision-aware implementation rather than an exact-equality assumption.
Worked example 4.1.5
(A central triangle hit)
Take , , and . Here , , , , and . Thus . The three vertex weights are , all nonnegative, and the hit is .
Visibility and shadow rays
For opaque geometry, find all candidate hits on the primary ray's interval and retain the smallest ; objects need not be processed in distance order. Spatial acceleration structures can reduce the number of tested primitives, but their bounds must be conservative or they can discard a real hit. This is a nearest-hit statement for a sample, not a guarantee against image aliasing or a proof that transparent layers can be discarded.
Given a hit point and a distinct point light , let . The shadow segment is for . A blocker on this open segment occludes the point light under an opaque hard-shadow model. A surface behind the light is not a blocker for that light. In practice, self-intersection handling must account for surface orientation, precision and scene scale; an arbitrary fixed offset may erase a nearby genuine blocker. Area lights, transparency and indirect light need additional models and are outside this test.
Worked example 4.1.6
(A blocker before the light)
Use and . Then and the light is units away. Put an opaque sphere of radius at . Its center is on the shadow ray at , so the entry and exit are and . Both lie strictly between and ; this sphere blocks the point light.
Counterexample mode
An infinite shadow ray invents a blocker
Let , and consider an opaque sphere centered at with radius . Its first ray intersection is , but the light is at . Testing the entire infinite ray would falsely report a shadow. Restrict the test to the point-to-light segment.
Common mistakes
Common mistake
(Comparing a parameter with a distance)
The shadow segment is only for the unit direction . If the unnormalized direction of the blocker example is used instead, the same segment is , and the blocker's entry and exit are . Testing those values against would let objects up to about units from count as blockers, far beyond a light at distance . The upper limit must be written in the same units as the direction vector.
Common mistake
(Treating a zero determinant as a miss)
says only that is parallel to the triangle's plane; the formulas then do not apply, but the ray may still meet the triangle. Take the central triangle and the ray , . Then , so . Yet the ray lies in the plane and, at , the triangle occupies , so the ray meets it for . A renderer must decide by policy whether to report such grazing contact; it must not claim the exact test proved a miss.
Exercises
Quick check
(With , and a sphere of center and radius , compute both parameters and the physical distance to the first hit.)
Find , and for this non-unit direction.
Solution · Answer
Here , , and , so . The same points occur at half the earlier parameters because . The first hit is at , a distance from the origin.
Quick check
(For the central triangle example, change the ray origin to and keep . Find the plane intersection and decide whether it lies in the triangle.)
Recompute and , then apply the formulas for , and .
Solution · Answer
With , , and we get , and . The plane is reached at , but (and ), so the point is outside the triangle even though the plane is hit.
Quick check
(From toward the light , does a sphere centered at with radius block the point light? Explain using an interval.)
Use the unit shadow direction and the open segment.
Solution · Answer
The unit shadow direction is and the light is at . The sphere intersections are and , both beyond the open segment , so there is no blockage.
Quick check
(A ray starts inside a sphere. Why is selecting the smaller algebraic root without checking its sign incorrect? Give an example.)
What is the sign of when the origin is inside the sphere?
Solution · Answer
Take the origin at the center of a unit sphere and a unit direction: , , , so the roots are and . Because , the roots have opposite signs for every ray from an interior point. The smaller root lies behind the origin; the positive root is the exit.
Further study
Bounding-volume hierarchies, watertight triangle tests, Monte Carlo light transport and material-dependent reflection require additional analysis. This chapter does not provide a complete ray tracer, a physical transparency model, or a verified graphics API implementation.