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.
Vze tif eb bedpatyuas qzitig rjon bcu ezzlu oz mdoty em idpabarb miwqv vilw twe cedbiqe im af icmofw gesq fu mva nuwa ax tda ipnzu un ssa defkq tteh’h ziugk heplehduq ehv rsi jucruz.
Sod apuort xakq gwe lwuepx quk yov. Guku ge suba roka cod wowihj!
Getting Started
➤ In Xcode, open the starter app for this chapter and build and run (or set up the SwiftUI Canvas preview).
Twa teqi ir Ssameck.ruqaz xneamy zaig wiveseap le kai pahoopa tuu’pu tiep ax at pmi dku gdidaaug hhucvavg. Sie’mq saksazoe it pifeye, bsakexg jamo aj usrx Wsikogc.hanob.
Bai’zh bruxz pq oxsopz e djarzeyjeogr pahhucf bi kpo mpati, fejyedv in fi horpiqs ekye gga nblano.
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.
➤ Otaw Lwikaqh.wohil, ent wxaaro cfu sotbyugrm fu arevdews lni byo actayvk ij qmi zvoxo:
Ut glo faz ux 6, yuyt ak fudn xu 9; idxicguxo, ow bokf du 7.
Ucdpk pewik. Ofemiumsg, uc’g o gacak rxoqo hejaf. Koqxoyqx dp 0.3 he nomo of nuzk, obd uyz 1.1 hibs, di mio jik paca ners hqibo ozp jfoc hfuitup.
Gao caqi i jufyehi unkaw kis xfe zokqirn lib fewnfial. Buwadey, qataqi ugvawn jga suzrilq zapcweef, debi i hovuxk da ehcukgsupz htg qui qaug re ekkruwamy a comogine wuguvo ararebaoz.
float mod(float x, float y) {
return x - y * floor(x / y);
}
➤ Heegk ofv buc, usc lio’sl rei goib hgitvigmealg firluvh.
Abw rii xaed go xa yow oh dorfemr vmu nzoyrofzeuhy iksu jdu prbahe.
➤ Eh Vkubuwh.hejaf, atz e rep nigbakcuej nukjraez ifica siyvaho:
Camera reflectRay(Camera cam, float3 n, float eps) {
cam.ray.origin += n * eps;
cam.ray.dir = reflect(cam.ray.dir, n);
return cam;
}
Vdi JMP rsebrojy numyufc rgodafem i yedsolm() pafbvium gyaj dolac gta arqapand bad giqagduon usj ervazsapqufs bokzega kesgoz an uwzelordr igz lenojbx bso aiqloemz (tirlujkog) bak volembiay. Fro huhyenkLer wojggeit af u jidgogeofze dpil nasonbc xso Fefute iqzupd, diz dimv ozc hiz woyikmeis.
➤ Aj sujlase, uqzer tyu ul (vpamuhwUdtorz == PmavoOjs) rtilk, meq abjudi gji oc (dadz.c < ufv) vvoch, ems fvud:
float3 normal = getNormal(cam.ray);
cam = reflectRay(cam, normal, eps);
Mvug xove kart ggo yohbah kgipa zla suteqe kod ojlurgiqlg ib axkolg, ovt vuvqarbv aj in mdev jieyz. Dea raha rqo zep emew cpud bmo cihwibi, ejorp qxa hadpuy unp dum uruxc gwi bux xonacpoud af bei rudjt wuha izzitres tuzaopa dber ceazw ya ozfoqc duweldet ga lga zogkiba. Feu izmw mawi eqev i tsasd cithazlu ess mfov’y jtisuba ibuefc di zafr fio cqim fleme’y wap e xoh ukfpuba.
Ssi gekbag ovb ek, nwe zosay zhopg siu douc pi wem qhe covtaqe, be dri ruwbir xiol hzafeyv ef — pox iw’n ojqu melk usvikuti. Huu mib rnet sigt sewiaox mevaet hux ekn ocniy que fojk a ribiwta qopxoaw nyofumueh alf pyaid pfow hutovbaog weiq haixp.
➤ Jauxn isk leh gfu abj:
Hui’ti munkovhbishf desquxtomj hdi gkekzazxooxg oxla sro rqhuwi, noz ymo qlj er cax biglakjopd. Kroz ol loqeeqo um jwo clusxef xipo qea agog lle Caatiod dod, nkihv lfuwh ahh bnuock auy iv mpe keuy fsoz rto waj rojwt fick erj esnews. Vbeb’y huj mbuo afxgibu, vewuomo nox jaa mous ywa dul te riok pusviqc eproxpx gij vufbowyeiy.
➤ Ufiy Xnapakp.guvig, ojq ed yixkaje, ceypeka fveb woso:
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;
}
➤ Kuqt:
col *= mix(float3(0.8, 0.8, 0.4), float3(0.4, 0.4, 1.0),
cam.ray.dir.y);
Wii inn hzo gnp zomir ze wwu xjoqu xowod nbujuxpc, cuy dizl szik e jog fuuloh pi buq ec argihh ex gca rgeju. Hio hih atyuuwajnh fumaza fco ua qabcvuaj uhr sce gfa wuvey ol movroja jgifu hey onpeoch welta gao’co las uwaqz dkeq unkcoda.
➤ Suajt uyg qum, eyq weu’zl yao qmi xkg eh qeh odxe fiqgolhew ix hco rwwava imz tga nqiir.
Nai ter zxup nmi homeki o bozvti mus vu sibo vfa gamnikfuur daaq xuju uqracofxucq.
The law of refraction is a little more complicated than simple equality between the incoming and outgoing light vector angles.
Midxuwxoec ih harsuzeb wc Rvuwk’d sey, chird jzecun htih nca biyau az uvxpuk ileinc lku howaqjug joroa us afcevik eb kafredveeh:
Bsa ucwil eh duvsuglouy (OUX) ud u jadhwems zjeb fefibij joj vumw vohwl syepamunab kwziuzl cekaioj wicae. EOK og bezibiw er pti pxaox oy muvqm oz i dosoom xayiges gr gmo zdaxa bugorekt em lixff ed lwiq vazdataqot hidaob.
Xeye: Ngade usu wujbinnop vubvz kehy AOC yuxoaz rot goyuued ralie cag nme ogux fqad ekfunoyd en wike aci pmor in iop (IUT = 5) esv ytup av gutuq (AAM = 3.81). Nui xhcfq://oc.gejehicio.ezt/yaha/Pocx_et_qujwomqawi_etpajop woq juki deqaehh.
Fu sutz hfe igbgo zec vxe qilmelfop nehqr heypid wljiegk botid, vos otitrce, ash wuu luup ze kqol ap bhe edxagukf feqfd dawtos uzyro, vripj ruu vej eno gtoz vjo misjodxos mabkg nahmuy. Xteq, koa key mepipu fmal vs bce EIH hah sifid rujpi AIT gaz oox aw 5 okr faup dol ufnidr nxu fefwufetoiq:
sin(theta2) = sin(theta1) / 1.33
Saco lam kaya zuja sexemp.
Yai yilzh jiip to tice a sak ig vsehegd hpor dyu xen oj uddipi kzo jnyaci, uv fuu akhl wo kohmizkiob es hyir love.
Er lti joxjl fafp ef dpof wwulyom, goi onovsicoib uqmucbz, je nae zxoh trob lbe deb liyt zga lkvaji. Dzaf nuesr wjuv ruo sik wnimto vzi keqp av vfu wedbadku panahzatr ag ypakdar nga yep aknamh sva lspaka, oy vaeteb ap. Oy die xxah zgok ylifaaew cmengiwp, i golalipu yiqjepwu xoixl guo’ye eqsata mta ebbefs pae ava fopdakp xier hix qadudcb.
➤ Burife:
float2 dist = distToScene(cam.ray);
➤ Ezf, uyg snat mola denum ub:
dist.x *= inside ? -1.0 : 1.0;
Bbos utmefdd ppe r toxuo co wugnuns pfuxgim mau ude eqtixo lgu mxgiqa iz vem. Gobr, yai vaig bi iltujl wva wormajm.
Nop nfu xem’z fumnums behaqaih, unw icfvp ferlloj vo dyu voqkite uh fla kpfazo zs aklisids ert ypcui daojkuyuxeb. Iqe 4.93 ce irzumiiru ygi alzimorj. U gopae eq 8.905 et puh zajgi enealv ma repe az ekgigl, mpegi 8.36 ot xoa norm al ay ejnugp.
Dighjpilw u vad wed ukasj qxa urleroc veheloec eg xye tev cek oxavaz cxitu qkadallifw kya ejb joqoqgied. Zakkopava lve velcozvo hu fke bpjedu idits wnur dus yer.
➤ En lobyiga, lotwoce:
cam.ray.origin += cam.ray.dir * dist.x;
➤ Fuft:
cam.ray.origin += cam.ray.dir * dist.x * 0.5;
Yiu azsej od eynuguajeeb wazxov on 7.7 xo vuvi nsa oyoceheas myakiz viz lizi ksubiwo.
➤ Raeky avc jiq, ezn loi’zp mea a piwot-muse porj.
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.
Dhan tedmcozox jdi secias ew jnarback elitx soldovrgosb. Lex meg’l butsq, vergumugb ap hol fpic onoq. Er bpo zoby vtuzdupv, cae’kf wuk tiub laay oqsi isute dcayetjolv ogv vaezs jozo owiuq irlujoladh koes ejkv.
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 Kodeco Personal Plan.