shade(p, wo) //计算p点向wo方向发出的光 Randomly choose N directions wi~pdf Lo = 0.0 For each wi Trace a ray r(p, wi) If ray r hit the light Lo += (1 / N) * L_i * f_r * cosine / pdf(wi) Return Lo
通过递归加入全局光照(Global Illumination)
1 2 3 4 5 6 7 8 9 10
shade(p, wo) Randomly choose N directions wi~pdf Lo = 0.0 For each wi Trace a ray r(p, wi) If ray r hit the light Lo += (1 / N) * L_i * f_r * cosine / pdf(wi) Else If ray r hit an object at q //考虑p点接受的反射光 Lo += (1 / N) * shade(q, -wi) * f_r * cosine / pdf(wi) Return Lo
问题1:光线数目指数爆炸
因此只能保留一根光线:
1 2 3 4 5 6 7
shade(p, wo) Randomly choose ONE direction wi~pdf(w) Trace a ray r(p, wi) If ray r hit the light Return L_i * f_r * cosine / pdf(wi) Else If ray r hit an object at q Return shade(q, -wi) * f_r * cosine / pdf(wi)
ray_generation(camPos, pixel) Uniformly choose N sample positions within the pixel pixel_radiance = 0.0 For each sample in the pixel Shoot a ray r(camPos, cam_to_sample) If ray r hit the scene at p pixel_radiance += 1 / N * shade(p, sample_to_cam) Return pixel_radiance
shade(p, wo) Manually specify a probability P_RR Randomly select ksi in a uniform dist. in [0, 1] If (ksi > P_RR) return0.0;
Randomly choose ONE direction wi~pdf(w) Trace a ray r(p, wi) If ray r hit the light Return L_i * f_r * cosine / pdf(wi) / P_RR Else If ray r hit an object at q Return shade(q, -wi) * f_r * cosine / pdf(wi) / P_RR
1. light source (direct, no need to have RR) 2. other reflectors (indirect, RR)
Final
1 2 3 4 5 6 7 8 9 10 11 12 13 14
shade(p, wo) # Contribution from the light source. Uniformly sample the light at x’ (pdf_light = 1 / A) L_dir = L_i * f_r * cos θ * cos θ’ / |x’ - p|^2 / pdf_light
# Contribution from other reflectors. L_indir = 0.0 Test Russian Roulette with probability P_RR Uniformly sample the hemisphere toward wi (pdf_hemi = 1 / 2pi) Trace a ray r(p, wi) If ray r hit a non-emitting object at q L_indir = shade(q, -wi) * f_r * cos θ / pdf_hemi / P_RR
Return L_dir + L_indir
NOTES:
1.在前述进行积分域变换时,需要考虑光源与物体之间是否有遮挡情况
修改代码:
1 2 3 4 5 6
# Contribution from the light source. L_dir = 0.0 Uniformly sample the light at x’ (pdf_light = 1 / A) Shoot a ray from p to x’ If the ray is not blocked in the middle L_dir = …
2.Pah Tracing 难以处理点光源
3.Path Tracing 的正确性
可以实现PHOTO-REALISTIC
4.现代光线追踪介绍
1 2 3 4 5 6 7 8
• Previous - Ray tracing == Whitted-style ray tracing • Modern - The general solution of light transport, including - (Unidirectional & bidirectional) path tracing - Photon mapping - Metropolis light transport - VCM / UPBP…