Making a Cornell Box Path Tracer from Scratch

Problem

I implement a small serial Monte Carlo path tracer in C++17. The target scene is the classic Cornell Box:

The renderer outputs a PPM image. The goal is to build a clean serial baseline that can be parallelized later.

How the algorithm/baseline code works

1) Rendering equation and Lambertian BRDF

For outgoing radiance LoL_o:

Lo(x,ωo)=Le(x,ωo)+∫ℋ2fr(x,ωi,ωo)Li(x,ωi)(n⋅ωi)dωi L_o(x, \omega_o) = L_e(x, \omega_o) + \int_{\mathcal{H}^2} f_r(x, \omega_i, \omega_o)\,L_i(x, \omega_i)\,(n\cdot \omega_i)\,d\omega_i

For Lambertian surfaces:

fr=ρπ f_r = \frac{\rho}{\pi}

where ρ\rho is albedo.

2) Direct lighting (Next Event Estimation)

At each diffuse hit point, I sample one random point on the rectangular area light. If the sampled point is visible (shadow ray is not blocked), direct light is added using area-domain Monte Carlo:

Ldirect≈frLeG(x,xl)pA(xl),G(x,xl)=max⁡(0,n⋅ωi)max⁡(0,nl⋅(−ωi))∥xl−x∥2 L_{\text{direct}} \approx \frac{f_r\,L_e\,G(x,x_l)}{p_A(x_l)}, \quad G(x,x_l)=\frac{\max(0,n\cdot \omega_i)\,\max(0,n_l\cdot (-\omega_i))}{\|x_l-x\|^2}

For uniform sampling on the light, pA=1/Ap_A = 1/A where AA is light area.

3) Indirect lighting (recursive path tracing)

Indirect bounce directions are sampled with cosine-weighted hemisphere sampling:

p(ωi)=cos⁡θπ p(\omega_i)=\frac{\cos\theta}{\pi}

The recursive Monte Carlo contribution for one sample is:

Lindirect∼frLi(n⋅ωi)p(ωi) L_{\text{indirect}} \sim \frac{f_r\,L_i\,(n\cdot \omega_i)}{p(\omega_i)}

Russian Roulette terminates long paths probabilistically after a minimum depth.

Where is parallelism

This baseline has many independent computations:

So the main parallel decomposition is data parallelism over image pixels (CPU threads or GPU threads), with reductions for sample accumulation.