Rendering
Five original substantive sections, three fully worked problems, two bounded interactive numerical simulations; editorial review outstanding.
§5.1 Visibility and the meaning of a rendered pixel
The visibility problem comes before the shading problem
A three-dimensional scene may contain millions of surfaces, but a camera pixel records contributions only from paths that can reach the sensor. Visibility determines which surfaces or participating media are encountered along those paths. A triangle behind a wall may have a perfectly valid material and a strong light source yet contribute nothing to the direct camera image. Rendering therefore combines two distinct questions: which geometric interactions are possible, and what radiance follows from those interactions? Mixing these questions can produce misleading explanations of both rasterization and ray tracing.
In a conventional triangle rasterizer, each projected triangle covers candidate pixel samples. A depth buffer retains the depth of the nearest accepted fragment according to a selected depth convention. The depth test is a local visibility approximation for opaque surfaces under a single camera projection. It does not by itself compute reflections, refractions, or shadows from other viewpoints. Transparency is also more complicated: a partly transmitting surface may need to combine contributions from several depth layers, and the order of compositing matters. A single nearest-depth value is insufficient to describe every possible light path.
Depth-buffer precision is finite. Under a typical perspective projection, the mapping from camera-space depth to stored depth is nonlinear, often allocating more precision near the near clipping plane. Setting the near plane unnecessarily close to the camera can make distant depth discrimination poor, leading to z-fighting between nearly coplanar surfaces. Reversed-depth conventions with floating-point depth buffers can improve practical precision when implemented consistently, but they do not eliminate geometric ambiguity. A useful diagnostic is to render a depth visualization with known plane distances and verify that the ordering and clipping behavior match the camera model.
Back-face culling can reject triangles whose projected orientation indicates that their outward face points away from the camera. This is an optimization under assumptions about closed opaque surfaces and consistent winding. It is not a universal visibility rule. A leaf, sheet of paper, or thin cloth may be intentionally double-sided. A mesh with inverted winding may disappear despite occupying a visible location. Frustum culling tests whether an object's bounds lie outside the camera's viewing volume, while occlusion culling estimates whether other geometry hides it. A conservative culling system may draw some invisible objects, but it must not reject objects that could contribute visibly; otherwise the optimization changes the image incorrectly.
Ray tracing approaches visibility by intersecting rays with geometry. For a camera ray r(t)=o+t d, t must lie in a valid interval, usually beginning slightly beyond the ray origin and ending at the nearest known hit. For a triangle, intersection tests commonly solve for a distance and barycentric coordinates. A correct implementation must handle parallel rays, degenerate triangles, edge hits, and numerical tolerance. A ray that hits the back of a triangle may be accepted or rejected depending on whether the geometry is one-sided. The intersection result is not just a yes/no answer: position, normal, material coordinates, and instance identity are needed to evaluate subsequent light transport.
Acceleration structures reduce the number of primitive tests. A bounding volume hierarchy groups objects in nested spatial bounds. A ray first tests a cheap bounding box and traverses into children only when their bounds may intersect. The performance depends on the quality of the hierarchy, ray coherence, scene motion, and the cost of updates. A static architectural scene can justify an expensive high-quality build, while a deforming character scene may need efficient refitting or rebuilding. An acceleration structure changes computational cost, not the geometric meaning of a ray hit. Its traversal must preserve the same nearest intersection as brute-force testing within the chosen numerical tolerances.
Transparent surfaces and compositing
Alpha compositing is frequently confused with physical transmission. In a common over operator using premultiplied colors, the result is C_out=C_front+(1-alpha_front)C_back and alpha_out=alpha_front+(1-alpha_front)alpha_back. Here the color values are already multiplied by their alpha. This operation is associative under consistent conventions, which is useful for compositing layers, but changing layer order generally changes the result. Sorting transparent fragments is difficult when polygons intersect or overlap cyclically. Weighted blended approximations can trade physical correctness for order independence and speed, and their limitations should be visible in tests involving crossing transparent surfaces.
A glass object is not generally modeled by a constant alpha. Light may refract through it, reflect from both surfaces, and be attenuated inside the medium. Alpha is still useful for illustration, user interfaces, and certain particle effects, but an image compositor should not claim that a 50-percent alpha layer is an optically accurate pane of glass. The right representation depends on the required fidelity and whether the viewer will see the object from changing angles.
§5.2 Ray tracing and camera sampling
Rays connect camera geometry to material response
A pinhole camera maps a sample location on an image plane to a direction in world space. For each pixel, the renderer can launch one or more primary rays through chosen subpixel positions. If a ray hits a surface, the outgoing radiance toward the camera depends on emitted light and incoming illumination scattered by the material. Secondary rays may sample reflections, transmission, and shadows. A recursive implementation is intuitive for a small teaching renderer, but real scenes can create enormous branching if every bounce spawns many deterministic rays. Monte Carlo sampling instead estimates integrals by tracing a manageable set of stochastic paths.
Consider a pinhole camera whose optical axis points along negative z. With a focal distance f and image-plane coordinates (x,y), a direction proportional to (x,y,-f) can be normalized to produce a ray. The camera coordinate convention must be stated because some engines point the camera along positive z. The field of view determines how image-plane distances map to ray angles. If the sensor width is s and focal length is f, the horizontal field of view is 2 arctan(s/(2f)) in the simple pinhole model. Changing the sensor or focal length changes the projection; changing image resolution alone does not necessarily change the field of view.
Depth of field requires a finite aperture rather than a single pinhole. A thin-lens camera samples an origin on the lens and aims rays toward a focal plane or focal point. Objects at the focus distance converge approximately to a sharp image, while others spread over a circle of confusion. The implementation must keep units and lens conventions consistent. A stochastic aperture sampler may create visually plausible blur even if the lens is simplified; it should not be presented as a complete physical camera without accounting for aberrations, vignetting, sensor response, and other effects.
The intersection calculation also contains a numerical problem. If a reflected or shadow ray begins at exactly the floating-point surface hit, rounding can make it intersect the same triangle again, causing acne-like dark artifacts or infinite self-intersections. A small ray-origin offset or robust intersection formulation is needed. The offset should be related to scene scale and numerical error rather than a single arbitrary world-space constant. An excessive offset makes contact shadows disappear and can allow rays to pass through thin surfaces. Tests should include tiny and very large geometry to expose scale sensitivity.
Direct lighting and shadow rays
A direct-light estimator samples a light source and checks whether the segment from the surface to the sampled light point is unoccluded. For a point light, a simple model includes inverse-square falloff with distance when the light intensity is defined appropriately. For an area light, each sample represents a finite patch and includes geometric factors and a probability density. A shadow ray that encounters an opaque object before the light sample contributes zero for that path. For transparent or volumetric objects, the situation is more complex because the ray may be attenuated rather than completely blocked.
A common bug is to sample an area light uniformly in its surface coordinates but then divide by a solid-angle probability density as though the sampling measure were already transformed. The relationship between area and solid angle involves distance squared and the cosine between the light's surface normal and the connecting direction. Probability densities must be expressed with respect to the same measure as the integrand. This is a central principle of Monte Carlo rendering, not a minor implementation detail.
§5.3 Global illumination and path tracing
Indirect light is a chain of interactions
Direct lighting describes light arriving from a source along a visible path to a surface. Indirect lighting includes light reflected or transmitted by other surfaces before reaching the point. A white wall can brighten a shaded corner by reflecting sunlight, and a colored wall can tint nearby objects through color bleeding. Such effects are natural consequences of repeated application of the rendering equation. A renderer that includes only direct lighting may create an overly dark interior even if all local material parameters are physically plausible.
Path tracing estimates light transport by constructing random paths through a sequence of surface interactions. At each bounce, it samples a new direction from a probability distribution, evaluates the material response and geometric cosine, and updates a throughput factor. In a simplified notation, a sample contribution is weighted by f_r cos(theta)/p(w), where p(w) is the probability density of the sampled direction with respect to solid angle. When the sampling distribution resembles the integrand, variance can be reduced. A cosine-weighted hemisphere sampler is effective for ideal diffuse reflection because its density is cos(theta)/pi, matching the angular structure of Lambertian scattering.
The estimator is unbiased under appropriate assumptions when the sampled paths cover the contributing domain and weights use the correct densities. Unbiased does not mean noise-free. With a finite number of samples, an unbiased estimator can still have high variance and produce bright outliers, especially when rare paths carry large contributions. Bias can be introduced deliberately through path-depth limits, clamping, approximate visibility, or denoising. These methods may produce better images under a time budget but should be described honestly. In scientific image synthesis, bias can change the quantity being estimated; in entertainment rendering, a small controlled bias may be acceptable.
Russian roulette is a probabilistic path-termination technique. After several bounces, a path survives with probability q and its contribution is divided by q when it survives. If q is chosen appropriately and the weighting is correct, this maintains the expectation while reducing average path length. Terminating all paths after a fixed number of bounces without compensating for the omitted contributions generally introduces bias. A practical renderer may combine a modest minimum depth with Russian roulette and an upper safety limit, recording these settings in a reproducibility manifest.
Multiple importance sampling combines strategies, such as sampling a light directly and sampling a direction from the material. A tiny light is hard to hit by random diffuse directions, while a sharply glossy material may be poorly served by sampling the light uniformly. By weighting contributions according to their sampling probabilities, multiple importance sampling can reduce variance across both cases. The balance heuristic uses weights proportional to the respective densities; the power heuristic emphasizes the strategy that better explains a sample. Both require evaluating densities in compatible measures and treating delta distributions, such as ideal mirrors, carefully.
Participating media and limits of surface-only models
Fog, smoke, clouds, and translucent volumes scatter or absorb light throughout a region, not only at its boundary. A surface-only ray tracer cannot model a beam of sunlight visible through mist without adding a participating-medium model or an artistic approximation. A homogeneous absorbing medium follows the Beer–Lambert law T(d)=exp(-sigma_t d), where sigma_t is an extinction coefficient and d is distance. Scattering introduces angular redistribution described by a phase function. The complete volume rendering problem includes emission, absorption, in-scattering, and out-scattering along a ray.
A scene can combine surface BRDFs and volumetric scattering. The path integral then includes both surface and medium events, with potentially very different sampling distributions. For an introductory project, implementing homogeneous absorption first is reasonable because it has a simple analytic reference. A later extension can sample scattering distances and directions. Validation should compare simulated transmittance against exp(-sigma_t d) over several distances, including zero and a distance large enough that transmission approaches zero.
§5.4 Sampling, aliasing, and variance
Why more samples do not always fix the right problem
A pixel is an integral over a finite sensor footprint, exposure interval, wavelength response, and possibly lens aperture. A point sample at the pixel center is an approximation to that integral. If the scene signal changes rapidly across the footprint, a single sample can miss narrow geometry or turn regular patterns into false low-frequency structures. Spatial antialiasing uses multiple subpixel samples or an analytic coverage estimate. Temporal antialiasing incorporates information from multiple frames but must manage motion, occlusion, and history rejection. Sampling more points helps only when the underlying sampling model represents the quantity one intends to estimate.
In ordinary independent Monte Carlo sampling, estimator standard error decreases approximately as one divided by the square root of sample count when variance is finite. Reducing noise by a factor of two may therefore require roughly four times as many independent samples. This is a powerful budgeting rule. It also explains why a renderer with 64 samples per pixel is not necessarily eight times less noisy than one with 8 samples: the ratio of standard errors is sqrt(8), about 2.83, under equal variance and independence assumptions. Stratification, quasi-Monte Carlo sequences, importance sampling, and denoising can change practical convergence behavior, but each brings its own assumptions.
Aliasing and Monte Carlo noise are different phenomena. Aliasing is structured error caused by insufficient sampling of high-frequency content; noise is random variation in an estimator. A checkerboard sampled always at its cell centers can produce a stable but completely wrong image. Adding more random samples may reveal the correct average, while filtering the signal before sampling can address the underlying bandwidth problem. A path tracer can also produce noise from stochastic indirect illumination even when its primary visibility sampling is excellent. Diagnostic images should distinguish geometric edge aliasing, texture minification, shadow sampling, and indirect-light variance.
Adaptive sampling allocates more work to pixels estimated to have high variance. This can improve efficiency, but a pixel with a rare bright event may initially appear low-variance and be undersampled. A stopping rule based only on a handful of early samples can therefore introduce uneven quality or bias. A safer method uses a minimum sample count, robust variance estimates, and a controlled error threshold. Record how the estimator decides that a pixel is finished; otherwise two renders with the same nominal maximum sample count may not be comparable.
Denoising uses spatial and temporal information to predict a cleaner image from noisy estimates. A denoiser can reduce visible noise dramatically, but it may smear fine texture, invent structure, or erase rare specular features. When a render is used for material evaluation or scientific validation, keep access to the raw estimate and variance information. A visually plausible denoised image is not proof that the underlying integral was estimated accurately. Testing should include scenes with thin bright features, moving objects, and high-frequency detail where hallucinated smoothness is particularly risky.
§5.5 Integrated investigation: a sunlit museum gallery
Compare raster and path-traced claims under controlled conditions
A virtual museum gallery contains a polished floor, diffuse painted walls, framed glass artworks, and sunlight entering through a tall window. The application must provide a smooth interactive walkthrough and also produce high-quality still images for a design review. A single rendering strategy may not meet both budgets. The interactive mode can use rasterization, shadow maps, precomputed indirect lighting, and reflection approximations. The still-image mode can use path tracing with measured or calibrated material parameters. The two modes should share geometry and color conventions so differences can be attributed to light transport rather than inconsistent asset interpretation.
Start by defining a reference camera, sun direction, and exposure. Render a white diffuse calibration patch under direct illumination. Confirm that both pipelines agree approximately on direct diffuse response before comparing complex images. Add one reflective floor region and note that a screen-space reflection technique can only reflect objects visible in the current screen buffer, whereas ray tracing can intersect geometry outside the camera view. The discrepancy is an expected limitation of the approximation, not automatically a bug. If a reflection disappears at the screen edge, document that as a mode-specific artifact.
Next examine a shaded corner. A direct-only raster image may leave it nearly black. A path-traced image may show indirect illumination from the sunlit floor and walls. Compare luminance in a small region while keeping tone mapping and exposure fixed. If the path-traced reference is noisy, increase samples or report uncertainty before concluding that a brightness difference is physically meaningful. A low-sample estimate with one rare bright path can exaggerate the corner's mean; robust statistics and multiple independent seeds can help detect this instability.
For the glass artwork, compare simple alpha compositing with a model that includes Fresnel reflection and refraction. A thin decorative overlay may be acceptable in the walkthrough, but the still render should represent the geometry of the pane if accurate reflections are important. For a highly polished floor, inspect whether the material's roughness and environment sampling produce fireflies. Apply multiple importance sampling or a controlled variance reduction method before resorting to arbitrary intensity clamps. Document any clamp because it can alter the energy of rare bright contributions.
Finally establish acceptance criteria for each mode. The interactive view might require a stable frame time, no major shadow detachment, and plausible appearance while moving. The offline view might require convergence within a target luminance tolerance for selected regions and consistent color management across renders. Record GPU, driver, sampling settings, light units, random seeds, and scene revision. The goal is not to force identical pixels from different methods, but to know which discrepancies follow from deliberate approximations and which indicate implementation defects.
Step-by-Step Programming Fundamentals Solutions
Three original questions connect calculations, definitions and interpretation. Open each solution to follow the reasoning.
A ray starts at o=(0,0,-5) with normalized direction d=(0,0,1). A sphere has center c=(0,0,0) and radius r=2. Find the first positive ray parameter at which the ray intersects the sphere and the outward normal at that point.
The ray is r(t)=o+t d. Points on the sphere satisfy |r(t)-c|²=r². Substituting yields (-5+t)²=4, or t²-10t+21=0.
The roots are t=(10±sqrt(100-84))/2=(10±4)/2, giving t=3 and t=7. Both are positive; the first visible surface hit is at t=3. The hit point is (0,0,-2).
The outward normal is (hit-c)/r=(0,0,-1). Its magnitude is one. A ray starting inside the sphere would have a negative entry root and a positive exit root, so the smallest positive root must be selected rather than always choosing the smaller algebraic root.
First hit t=3 at (0,0,-2), outward unit normal (0,0,-1).
A diffuse surface has rho=0.8 and receives constant incoming radiance Li=5 W/(m² sr) from every direction in the upper hemisphere. Use the rendering equation to compute the outgoing radiance, and show why cosine-weighted hemisphere sampling yields the same contribution for every sample.
The diffuse BRDF is 0.8/pi. The integral of cos(theta) over the hemisphere is pi. Thus Lo=(0.8/pi)×5×pi=4 W/(m² sr), assuming no emission.
A cosine-weighted hemisphere distribution has p(w)=cos(theta)/pi with respect to solid angle. The Monte Carlo sample contribution is fr Li cos(theta)/p(w). Substitute fr=0.8/pi and Li=5: the result is (0.8/pi)×5×cos(theta) divided by (cos(theta)/pi), which simplifies to 4 for any sampled direction with positive cosine.
In this specially uniform environment every sample has the same contribution, so the estimator has zero variance apart from numerical effects. In a scene with an occluder or direction-dependent illumination, samples differ and variance returns. The example demonstrates matched importance sampling, not a general promise of noiseless path tracing.
Outgoing radiance 4 W/(m² sr); each valid cosine-weighted sample contributes 4 under the stated constant-illumination assumptions.
A path-traced image uses 64 independent samples per pixel and exhibits an estimated standard error of 0.08 in a chosen linear-light measurement. Approximately how many samples are required to reduce that error to 0.04 if sample variance remains unchanged? Separately, find transmittance through 3 m of homogeneous medium with extinction coefficient 0.2 m^-1.
Standard error is proportional to 1/sqrt(N). Halving standard error requires four times the sample count, so Nnew=4×64=256. This is an expectation under independence and finite variance, not a guaranteed visual improvement for every pixel.
T(d)=exp(-sigmat d)=exp(-0.2×3)=exp(-0.6), approximately 0.548812. The absorbed or scattered-out fraction along the direct path is 1-T, approximately 0.451188, without accounting for in-scattered light.
The sample-budget estimate ignores changes in variance from different sampling strategies. The transmittance calculation assumes a homogeneous extinction coefficient and straight path; it does not calculate the total radiance seen through scattering fog.
Approximately 256 samples per pixel and direct-path transmittance 0.548812.