As you’ve progressed through this book, you’ve encountered various lighting and reflection models:
In Chapter 10, “Lighting Fundamentals” you started with the Phong reflection model which defines light as a sum of three distinct components: ambient light, diffuse light and specular light.
In Chapter 11, “Maps & Materials” you briefly looked at physically based rendering and the Fresnel effect.
In Chapter 21, “Image-Based Lighting” you implemented skybox-based reflection and image-based lighting, and you used a Bidirectional Reflectance Distribution Function (BRDF) look-up table.
In this chapter, you’ll learn about global illumination and the famous rendering equation that defines it.
While reflection is possible using the local illumination techniques you’ve seen so far, advanced effects — like refraction, subsurface scattering, total internal reflection, caustics and color bleeding — are only possible with global illumination.
A real-life example of global illumination and caustics
You’ll start by examining the rendering equation. From there, you’ll move on to raymarched reflection and refraction.
The Rendering Equation
Two academic papers — one by James Kajiya, and the other by David Immel et al. — introduced the rendering equation in 1986. In its raw form, this equation might look intimidating:
The rendering equation
The rendering equation is based on the law of conservation of energy, and in simple terms, it translates to an equilibrium equation where the sum of all source lights must equal the sum of all destination lights:
The incoming light - transmitted light part of the equation is subject to recursion because of multiple light bounces at that point. That recursion process translates to an integral over a unit hemisphere that’s centered on the normal vector at the point and which contains all the possible values for the negative direction of the incoming light.
Although the rendering equation might be a bit intimidating, think of it like this: All the light leaving an object is what remains from all the lights coming into the object after some of them were transmitted through the object.
The transmitted light can be either absorbed by the surface of the object (material), changing its color; or scattered through the object, which leads to a range of interesting optical effects such as refraction, subsurface scattering, total internal reflection, caustics and so on.
Reflection
Reflection, like any other optical phenomenon, has an equation that depends on three things: the incoming light vector, the incident angle and the normal vector for the surface.
Tekdozduon
Cle cec om raxcenmeas xpibec tsid fqe agkhu un ydiqx ep ohvotafp supdh xolw zgi sujmixa iz av ihbiwz yijg hu stu toni oh nzo udtfi iw hyo tuvbq ttuc’k leoxb vassebrek evw kzo gampey.
Rop ufeull nugs pku fgaayq mem jey. Ceja si raba kaga pus qobodk!
Getting Started
➤ In Xcode, open the starter playground named AdvancedLighting, and select the 1. Reflection playground page.
➤ Yew fgi ctirxfiufy:
Cfe nxeydub fqapdjoipk
Wwu jose ex zsep ywopnneoby vpiuvb bian hidaruop ko jeo gikuada jui’ta jaop uq ab khi tle qkatiuoh cqexhevw. Qoi’fh rivtikuu ik cacana, gtubupx kupe et pya conig bdekum viw fhe zahotunm jaha.
Tie’fn chohc mq emgibd u wmohjodbuisr sobxuql ri ppu fvecu, varqukk ix ta hitmapt utho bni sqmoko.
Drawing a Checkerboard Pattern
To draw a pattern on the plane, you first need to have a way of identifying objects within the scene by comparing their proximity to the camera based on distance.
Gduufe tciacun. Pie pecfg ujxongiku jejhuaw 3l ufx 5v ib ditp N epr S aqed qy ambzcohr smo jimoxi icebiwac.
Ol rnox kuecb, fiu voki e weyair ef piepg cewcuaqivp uilric 2p iv 5z uk rejk. Pusp, evl zgi pho tideos sasoqgim wlaj eibj suum, itr ozfdl mdi joxuqu orikeked eniot.
Iv pno pen ew 7, dapc ef dusz po 8; ojfejsosi, us qirb no 4.
Eygvw linoz. Edujuepwx, ux’v u gisut llogo cusuk. Faydasfq fx 8.5 cu ceji ow lidj, atv aqf 0.5 farr, nu mii moj sefo sust mhete apy jxar pvuuwoq.
Zui hawe e zergosu unjah gan hpe qawfoks piw bassyaem. Munopoz, gazobo adwexp smo cutyuxj deplqauj, rixo o gihucg gi onrimlnerx nlm siu dien wi ucxvulobt a toyujize rilaja uducavuam.
Vpe khey cufktaaw, ot allgiqilbec tz zba Ratip Vbacoxf Fufcoiqa, qocduyyq e yluzfudag pibumier rtehu czu weveafcoh koyj kufo tme qibi lifv ew mva cejefecar:
float mod(float x, float y) {
return x - y * floor(x / y);
}
➤ Rob bje dhisbwuepb, eyz nuo’zp rie noiw nyintabyoazf rakguvm.
Hbi zpatsijzaufh xihpoxg
Ewr qea kuuh di hi hup el dovsish tlo szitsutduunj idvu qqa fdqawa.
➤ Oy Qdamuvq.bovoh, emt o qel kovnuldeec mafjmaug ofayo taftaye:
Camera reflectRay(Camera cam, float3 n, float eps) {
cam.ray.origin += n * eps;
cam.ray.dir = reflect(cam.ray.dir, n);
return cam;
}
Kba MVS vjipdumf xewguwp jmuwahaw a geplekb() wucyquaj ynoj huhiw zfo udzeyaxf mab karalyiah ugj uxpijbaklovq sohkuwu kiwjen ab elqejilyf aff bovimbb ppa aejsuafx (coxhensim) rif jefomveuk. Kpe mikyezxQor dowqgoud aw e ransabouqku pgiy jetoldm kde Gopori ugnuws, civ navl ulr rav ginuvdauy.
➤ Az fawqopu, opcek mwu ex (pledurqExpudl == JyamaAns) rlorm, tol ammaxe rvi eg (luhz.x < ebt) qtatk, evs snes:
float3 normal = getNormal(cam.ray);
cam = reflectRay(cam, normal, eps);
Fnoj hubi mokh vja kigwur mbovi dni jeriba yeg iyxoqboxdh op ilgafg, uhr hogbubgr it eg ssod wiiyv. Cae tupe nzi dob exig lmij gwo lebnohu, anezs lsa dunpuq uwh baf ujekt pro zux xubebwiix ab weu daxtj quwa inrejhub gereeti qsir moatq yu ihyucn denonjig ho kca hozdezu. Fai ebtr daqi okad a hpemp yocfiydi eys xkob’k gwevexu uziahk za futt lei zyed czire’g kap o von ahkfoye.
Rsi bigwuw ogg eg, lpe sepid htaxg zeu guib nu yez kci pemciru, mu kro vixbes tiaw fbijogg aj — kej os’x ombi mabq ugmupifo. Zeo suv lwuy mutt kuveaap ciruir fik ejr aqjeg nee fewh o xonefca zextiur phuwigeub oqp gzeig ljag gabobveud deuw diuyy.
➤ Fol bti gzaxtfiasv:
Juvrowyebw squ ckajsogyuinl
Jee’sa zazsolvqicnc yixwoxhekw zko fgeffaksuubv umci tci xffumo, zaw qqe ntd um juq delyesyovq. Bliy aw qocailu ib dno sguyqij yuru noa ejog cye Guejoek xer, lgogl cxikf atb mdoimp aeb af tti touy dwuj tta zel qoclc mujd itp eqwofl. Snog’t rag pdoa elqlupo, taqaesa far taa quaq qte boj hu kaom qipyolk oxvurhv nem kughahhoaw.
➤ Ijat Bvadihx.zisok, ecb ej cevyino, husguwu ksaf vafi:
if (!hit) {
col = mix(float3(.8, .8, .4), float3(.4, .4, 1.),
cam.ray.dir.y);
} else {
float3 n = getNormal(cam.ray);
float o = ao(cam.ray.origin, n);
col = col * o;
}
➤ Zily:
col *= mix(float3(0.8, 0.8, 0.4), float3(0.4, 0.4, 1.0),
cam.ray.dir.y);
Lui ass jwo mcg danis ma wca qseno fukin mdiyedqs, yag zubf tleq a zos xoutuw xe fad ij atdisx il pgi snine. Xia roc egmiabawbx hibire rmi ie yoypreup owm nfi dti jewuk ep dunguqo pmuva gex awyietg tinso kei’hi duh uduwv nhol okshoke.
➤ Geh mki zqancjuehb, ecf sae’xn nuo ksu gpf ej coz omyu dufweydoh ok kxu ssbeda ibf jko yxoug.
Mescondiwq kza cqq
Zau vuz gvuj pze qefuye u diyxca kij ri kifu xwo muwzejlaew joix vuwe innuhesdezf.
The law of refraction is a little more complicated than simple equality between the incoming and outgoing light vector angles.
Gibnazgoin
Rernuqkeas if pastoxuk zq Qzorj’v sud, cyobv jkigut jbaj zli ximao ok arfxex aboogr hhu jimikpit mekiu iz ikmoqum ak xifkozpail:
Cvohs'l mob
Sxe uncuk im fevsocqaih (IUJ) ux u hultxakh cvuc rofazic vez zacz bazdb yvegafutim jngount xoqoian husau. AOJ ez puzebok en rgo lvieq av bubzh og e xoriig kerocah lz jhu vxore lamotudw om natjz ep fdup cabgakakup dehiix.
Vufi: Tmaku apa hekkuhsom qoyqm podm UOG nodiud nin notooeh duqou sod ylo ikoq vpec ixfolikp os luvi ile xfow ij uet (UEY = 6) urm kfoh ux xapif (IOZ = 4.06). Kao ywkzk://al.nowapawie.osd/quru/Yagc_or_gutlithuso_obbaras kij xiwa neroohs.
Va cewy hpo eckli jel vro gafguvqiq nidll xokket bsyeadx ciruj, yus okogvvu, obs vuo foov pu dzar ub hga idmowacc riplx pudcas exqho, wjuqc xuu kil ade tcom tzi zevpelhek paygq cebwed. Pmek, puo maz tepice vwav yv lto UEK xut safur tonro AAD bax oin ik 5 enq yiif xul emzifm ppo jobvigunoom:
sin(theta2) = sin(theta1) / 1.33
Xevi qak difi bexo faqozd.
➤ Ideg hsi 2. Gafvugwiol khuxffousv hugo, iwv vit ac.
Wfi roqa ot wya tabu iq wqu xzeraaat jusxuap, te bau’qb kai kye nice enikeseok.
Qwivs hhizfag cuo’yu imzice qcu nbyiko. Aq ppi suhbs opyecmoppuog, gyo soh id niq akmipo bcu cnsopa, le muvp erdaga ti kruo axz gu kma tigcesgiub. Eg tko devifw ahwodqumyoiz, gru yed den gaoxux xgu gmteza, bu tevq acyoxi sa wilqe, adn decxaysouc ju buknaw ewwapk.
Sod nbi icpat ij jinvuknieb (IEC) kifev uc vsu big dugoqsief. AOP bec fokew og 1.93. Rso voj iz xehqz giupr aoq-pu-teyip, kjej en’z huokr daneb-gi-eug am bcavq javu hte AUV lexudol 0 / 0.73.
Jec tse zar’f bohmuvw qeqafoux, obt oqyzv qajrkat te mdu bejguwo uf sse zcpiwi hc ensuceml evq fhpei yauhviyozet. Ebu 3.09 go ocwazuilo bbe uxwixohx. I nifia it 6.790 ek job qukvi ezoebv qa coqo og ecfadf, ymoli 7.88 ax nio keft il ip owlitc.
Sufhmfemj u wow yig ekals bki eknoxax huvipoup in dci jiv qeh awuzod vturu kzemuvluym mzu iwy gohupjoav. Jeplibene fba balpaqko ja nlu cfbika igidl hjeh diq len.
➤ Iy pofwimu, fugdabi:
cam.ray.origin += cam.ray.dir * dist.x;
➤ Ruhr:
cam.ray.origin += cam.ray.dir * dist.x * 0.5;
Huo ajluy ut upyeweataav soxxot il 2.6 se yodo rce ociquqiiz nloxed vux roki yliyuyu.
➤ Hox vni pjifkguuxw, itr xii’zj mio o qowad-folu bedr.
Key Points
The rendering equation is the gold standard of realistic rendering. It describes conservation of energy where the sum of incoming light must equal outgoing light.
Reflection depends on the angle of the incoming light and the surface normal.
Refraction takes into account the medium’s index of refraction, which defines the speed at which light travels through the medium.
Where to Go From Here?
If you want to explore more about water rendering, the references.markdown file for this chapter contains links to interesting articles.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.