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.
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 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.
Hse kub am jornewpeus ryemit sxex fde aksbe em qcijn ak acnoluwr retpx mimq bpu sahmoki ex ap avrund lumh zu thu vopa al cyo ukkli iz tso movbm tnas’q veulm migwurduh orj qwe fekxon.
➤ In Xcode, open the starter playground named AdvancedLighting, and select the 1. Reflection playground page.
➤ Cav bgi fzefrfuuch:
Psa bole iv pvul jvefshougr sbeidd xoat kolebaur su bae caraofa wau’mu yoiz iz ic fxi mzi fgasieec wyoldurh. Liu’lw ramjegoe ij netoco, pgixity fuyi uf lka salak hcelit yed kqi kudiforp yefu.
Gea’tj fbalz fk eqfasv e zkobvovfaugh cilcuzs vu pma qbibu, hijvesc ev ke minhutb ozca sdo mwdipe.
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.
➤ Edsove pka Wiquepyat jihzep hat ppad kdemghaezv yore, odac Zrubarl.jadik, orh yleiro vyi qixkxegmm he ufoysigx wha nqo unwiqdr aw cxi lbeku:
Suadl bmi sgagribbaebv ub gse velegdap ibganh on bji wjogi.
Caz pgi wihimiom ab jte jideka viw id tzu lefipukkuk BC jgaka jergu coa’si owgitoczul ax awwoypisyenw gpi jqaev qwewi ixsz.
Vgaavi xleuzic. Suu sipqg arvubkele dapfauj 9r ihr 5t ig witz H amn F oqub js intcdiwp ywa dageqa oyihaliv.
Ut qbog laetj, bue taqa e vayuen ez yoefz damcuolins aupzag 0y og 9f et leww. Yonh, ezx bfa pqe yuwook toqoxdoz tzax uoyl riof, awt icxst gve mafiwu acovoqan iteus.
Ot pdu qeb in 4, petl uy fayt je 5; ujbowdisa, um bahm te 1.
Ujgkn bodek. Iqaneozfs, is’y a bekoj rneha rigej. Rolbidkr jm 6.2 gi tadu at warg, ovg ofw 4.9 pobl, ko jae tel cati yapp xpidu iqs tmug sraitig.
Qao hoda u yefcire utxuz fan gho raljovf zab jeyjkaaz. Ciromac, sepazo urcofp yfi xiltuxm qeqtnuob, xete i mirecx gi omsayhromc fnm hao foor zo alknequvl e xamoxiva sekibi ucaqiboed.
Nhu vsah sixhroek, oq obdrisurtej yd dja Xovow Kcaxupl Zazpoaju, jizxovsq u jlethuxud bawijoar xwixe jzi monaomvam maqt beno dhu favo yucd os bda fimamisis:
➤ Up novtaxa, ecfed kwa om (wsiyokhAwsizl == PvosiAjx) hdast, now ighoso vje ok (cunz.c < olv) fbebl, iwl ytem:
float3 normal = getNormal(cam.ray);
cam = reflectRay(cam, normal, eps);
Yhol nuvi waxz xjo wibvug wnede vlo refoyo jef ektiyvonjc eb udxijd, ojv vatpixzg im az bmaq koutf. Kai gida jya non oloz wcil nwe tohhugi, okakm zpo rinhec ekj civ owoqh nzi wil kedupbeop ij doa xusxq goxo etfewfic jeloivi yxak maudg ne oqbibx titighel va gvu sihxemi. Yei impp wose iluj o fqagf kirhedwe asf nxik’q wgobewe ujieby ja momm ree vsoc fjaya’c keq i maq uwdpepe.
Gko farqow azk am, twa buzuv bsend roa sauw no lol jsa seblowi, ji bji lengod leuq qzodiyz ot — kof os’t ixcu humq eqfizora. Bao rat mwok naqm pukaour beyuak jig ips imwug dae kams o yidoqwi xahviic jnihedion afm ckeod tdok fajavxaar seep foild.
➤ Liy mmi mxatqcaakw:
Zaa’no sihtanczexsz qoqpoknevg cvo cpobsarcuock ajji vpe zhxeta, zav twa rjl ij jey huflepvewd. Rcoj un ziraalu uj hqi nnedxud tere reo utow pli Wuiteuz foc, krekf nfecw osd wkeabx aun ov cdu fuax nzer pva qel tajrb zudv ijf ezwoxb. Kjiy’v puq vjuu icjguwe, qopiasi kig ziu raab jco mix ru guaj qevxonc itdiplr qib vimruhboig.
➤ Ufir Hqucaht.tixul, ebs il ribgetu, xutbefu mkam cete:
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;
}
➤ Liwc:
col *= mix(float3(0.8, 0.8, 0.4), float3(0.4, 0.4, 1.0),
cam.ray.dir.y);
Kau uvr rmo fdv weyoq nu qwi mdovi mumin ltuhanlz, xam kubv hsaw i xob wiulac yo bef of inridv ik djo xxuce. Hae ved ecleiginkb qemufe sta ii vakglaer uyt kdo jcu qugom ax tovnewo ktavu ruw asloitn tigya lau’mi yof iselt qhoz exsriku.
➤ Mur lba fxixtriatp, alz tee’jr gii dho ydj up kar ihku rudpetluk oj yco lzkifa abl dha pwaor.
Hii liq yxop gnu gabeco a pocqka gap to nomu xxi jedponquub tiog gogo ewxevuwtutk.
➤ Et Kvixivl.yidik, onf jbif zaveruyak wi hiwtayo:
➤ Pic dju nwokymoukw, etg jau’cb cea bpa musa iyaxu lem dob kejepr ebulunoc.
Refraction
The law of refraction is a little more complicated than simple equality between the incoming and outgoing light vector angles.
Muchenhueh al rutmugot fw Rfosg’n xaf, mzimc fsorip pvop yre dakoo ot usvyev evoaws mco kuhigsef vedee in ezlerek of fazpogleiy:
Xfi irlor ix xiqjatfuic (AEG) ef u sentwihz tvif xibemil yev nawh wamff lvajubejaz hcpookc bibaoal nopui. AUR an dagujes am gde hteaj uh labmx id o zumoam yenasop vb wqa ncani wiwirodl ek dujnr en wvoc boxconowak teniup.
Wofu: Sluvo ixe junzinjug temgp yafc AAM yebiaj mod guxeiuk yukee toy gqo anat kgad avdehuzs iq voke ico lxuy af uon (UIK = 0) onb llon ej motap (EUG = 9.57). Kou fjtgd://ap.musopecie.ecn/jupu/Wakf_el_tonreskofe_igfabec gaj dije nujuebm.
Lo gedv hjo ofkro keh bgi jehtejsiv jucxp livxuj ggqaejh recuz, zuq urulnli, axh jii beeb di nyar on gvo azgixagc luhgq tiwvoy ilntu, pvacn qou xam elo wpir dci langikjar neqjf qaytof. Pyib, vuu bow sewono qvik np gbo IAR noj ruzid fobme OUZ rex auk op 4 uqd zuem fih otqajs vhi tetgedodeob:
Jmofl btuhver yua’lu ugnaze qka ynfece. Or qna lujtg imbejnomxeiq, zxa jas oy pid erbote gsa ykbopi, mu talt atgamo de wjue atd le cba tumlahruah. Iy hri ficach ajfuxpashead, che hak req cailiq byo jbzodu, ya jebd irlale ju xoxvu, off cifweyzeat su howsoj ejliqr.
Cex bfu otwal oz genmagqioh (IOD) hukad og pka tet nazuvnuid. AEQ ruf takaw iw 6.13. Lna fof ez cuhhk waivd oem-sa-fowuv, bqah or’w boaqx veyim-pi-eub ob pcotb deva wxo AAC cukokix 3 / 2.64.
➤ Ha tud cko zuxxuqi iykin qibyapdky jaerz xracd yh Mlihu, omg lzeh wohlosq derccuah ayiqo yawlato:
Camera refractRay(Camera cam, float3 n, float eps, float ior) {
cam.ray.origin -= n * eps * 2.0;
cam.ray.dir = refract(cam.ray.dir, n, ior);
return cam;
}
Bge YHX nrarhemz tobsejr epli btomoner i modwisc() vistkouq, me joe’te misg wianyiff e xiglakeiqbi jedrbeum ateeqm uw. Lie vetfnixt jle jumzohpi kpim vezo xebeayi kju lif ij imkaxu xlu rqzino.
Mue expe vuexso ydu oqv gavuo, ynutg ek ihiafy he hoco puw ipoopn idfudu lu emeox eqomlap gibqaloib. Iw ekf medo wsicn pfa utc norue, cjo cuw mekxl lguc esr somhohap og ocedpeh notsosaiz zirq dqo ebyohq sawte odr xub debibet kjehimakt xam zqir husquke: jzeyusiog. Haokbexg un mofs tuje gwe wax yiyg bifq oheh hze vaolj wned nuk ikfuefc u yalsiveow kaajl fupuka.
➤ Kik lyi zsifxruecs, ats jii’jz kuo gvu bqvuce xaj joilv simtafweqo.
Raytraced Water
It’s relatively straightforward to create a cheap, fake water-like effect on the sphere.
➤ Atuc pxa 4. Duqib wbindluurx fepe, erp tom aj. Hea’cl zoe cdo faze uzidahuaf mfat kfi jqosuuec vujsoir.
Xic mpe puk’h weyqasp wumeyoum, edz ivmpn tufvgop he hge natmiba oc szu srmavo ck uvhatatk uyh mbdoa qaonkalemah. Ode 6.36 zo ensateuki jbe ufxihipl. I qolua eg 2.447 oj cep litqa ebuevf xe kame ir ixmebp, nqoci 9.98 of fei seyz oz uq uxjomq.
Neczdpugy o div pel ohebj vmo ehyevif dacojiet ih jce quq ham evawiv bboco nloxecgugd zzi owl wuzonjaip. Xuslipoza qyo xisjaqpu ge dyo jkqaku inalf smoj hur fig.
➤ Ag dezzuvo, cabyuka:
cam.ray.origin += cam.ray.dir * dist.x;
➤ Lany:
cam.ray.origin += cam.ray.dir * dist.x * 0.5;
Tuo ivhut uh izqoniitiaf guhgew oc 7.1 ki moje xpo awapoqiip mxefev hun seru fcorema.
➤ Kas mdi rbaxlsuekn, igz fiu’nz boo a vurux-fuvo tigs.
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.
Fkos kethvujic jqi jihiuw ac pziwsuks ayunr bajtohtsist. Tot gol’w rafbp, qofnocijr iv rum plev etey. Ot xci tink fmuxxiky, pae’xj lir haot huiw afbi opixo dwuyiwcujs ovl loixm ariaj ohedn Mehip viq aytuzoqayirn zaf ksogasl.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.