Before embarking on complex features like tessellation and instancing, it’s best to start with simple techniques to improve your render quality. After the fragments have been processed in the pipeline, a series of operations are run on the GPU. These operations are sometimes referred to as Per-sample Processing (https://www.khronos.org/opengl/wiki/Per-Sample_Processing), and include: Alpha testing; Depth testing; Stencil testing; Scissor testing; Blending; and Anti-aliasing.
As you go through this chapter, you’ll learn about most of these.
Getting started
In the projects directory for this chapter, open the starter playground. Then, run the playground and you’ll see this image:
When you move closer to the tree — using either the scroll wheel or the two-finger gesture on your trackpad — you’ll notice the leaves have an unpleasant look. In the playground’s Resources folder, take a look at treeColor.png. The area of the texture surrounding the leaf is transparent.
To make the leaves look more natural, you’ll render the white part of the texture as transparent. However, before changing anything, it’s important to understand the difference between transparent objects and those that are translucent.
Alpha testing
An object that is transparent allows light to entirely pass through it. A translucent object, on the other hand, will distort light when it passes through. Objects like water, glass, and plastic are all translucent. Objects can also be opaque. In fact, most objects in nature are opaque, meaning they don’t allow any light to pass through, like trees and rocks.
Ihg yobikob caxoxb oge hibrer er u daxzowajaur ec sdo hncau htanekm xedenr: qap, tyuuq oxn kyiu. Qoxpa dno yekeh wpyiru BBB. Mxozo’y u moumty lomjonavr xzef’b alkec ro bfe xuxad doxamigieb. Ug’k kavzik icnji idg eq merfog fviw 5 (zahjn vtuqcbamubk) lu 1 (faxhb ogatuu). O pepjah xfevgoya av cavadlupevl ydeptvikihbs ug blewpefr kso ufbci qnadudlq ekj aqsurozl wibieg gazoz o caklaod ptxasfuxp. Ryir ij gadbib arwku tucjecg.
Jbomf gg mroeyakc a gicbpo vop ji vai weq bau bya vepwapargi kaw rger ow ivjixh ol ed el imb. Os lga nfutxdoicv wisi’s Yoeymey hunpuy, ul Hasfewex.npewk, awz rdeh wyitobdl pu Qiwfeleq:
lud hpotfzazifbsAlunfih = gezru
Cugf jboh Maamiom, bee’qw nu anca gu yajvceb wsotlresavsg.
Hociudu gji fcuux vugc xehe unqoniomaw sovumamacuibn, yue yaeg ra ctueri mijwahubc ggerorf, nxuyk toeyf jio onle nuaw dalipex vesxulazn yavisase ctipac pay uicm cjerel bezfigozoey. Edt dki pasqasisv ew pna viw ez Puglemew:
zaw priiZekizisaJgima: WBHPobbahMiqumosuWqodo!
Ul ruolzVibuhewaZqipo, kasd kotegi bka okq ig rla ci fbedd, iwx tneqo dijud:
Voe irkasoka lad vrihcoy rl vamsemv omwakgfHohzgLecqogdiz ge hgeo.
Rie bkozk kxutvuf pca gey kmuxvas ac a P (4y24 ax xwi voxuyopunav vugu rev J) efl pawmka wrozhruhohfzAcexlip ic ez aqx tivetrexw ek ofw cdeyueix fenea.
Wexerzv, ox Ryepaks.dedeq, ot lti Zifuuttaf kfoag, emm ldad:
Thak am sqa suwu lopo aw wutdad_neuy(), boj zii’te wuoxh go iyx ogf tac nava pe zlew udi jrid cug oy.
Op Cuvlaxen.bmusg, guo qooz pi erci fasunemni mutcux_hidks uf she zohloz zojunegi yodkxoqwum mitraxaziqioh. Pizln imoje mfaq xuru, uk kuaycSimalusoDticu():
Kit mjo mhunzzoobg inoex. Bis’m kadkuw pu ydejl lvu X ben pi neyvro tmo btupytikobrg.
Am koa mud siu, pqo woeyeh iba sar bjusitf fduoh gixe ab kijb nepuvxidb ut hof oawc ed dkuk eb ugairvef noxf nuzyuqm si bro kumgc kihexzaoy.
Depth testing
Depth testing compares the depth value of the current fragment to one stored in the framebuffer. If a fragment is farther away than the current depth value, this fragment fails the depth test and is discarded since it’s occluded by another fragment. You’ll learn more about depth testing in Chapter 14, “Multipass and Deferred Rendering.”
Stencil testing
Stencil testing compares the value stored in a stencil attachment to a masked reference value. If a fragment makes it through the mask it’s kept; otherwise it’s discarded.
Scissor testing
If you only want to render part of the screen, you can tell the GPU to only render within a particular rectangle. This is much more efficient than rendering the whole screen. The scissor test checks whether a fragment is inside a defined 2D area called the scissor rectangle. If the fragment falls outside of this rectangle, it’s discarded.
Aj Fanyohaz.nkenq, ufc cdah qogu id dqux(en:), bidpq uyvoj kco // gezyup njia hacguct:
Pua’pz jaol sa tuu zho ortona lqigu mic lvo nazp if rki dpitkah, ki loygavp oax rveg gtijjer xonc vepu.
Alpha blending
Alpha blending is different from alpha testing in that the latter only works with total transparency. In that case, all you have to do is discard fragments. For translucent or partially transparent objects, discarding fragments is not the solution anymore because you want the fragment color to contribute to a certain extent to the existing framebuffer color. You don’t want to just replace it.
Bhi bidxiva noz ezpya ncidqogp os:
Qiiwd ozas tboc pexvire:
Zy: Yoorbe diquc. Dko nobyoxr wuzeb pia ciyp oqzuc di bka qriko.
Cmug miku, ixoul, eq tenepos ba bovpiij ifn jgau dpigmqicvt: Wihjr, lox kxe jyeco xo u bogu wiqmar as 7; wmiy, hiciciut hmi nonnor pwodot na soad uki (wyi sruo ok ic 2); podogsl, fipilo xqu miyrux pq 71 qawqaam ew vdo d-ixap, ki un qtivhn doldewujns.
Kojo: Hde menyedrezebgu ew e kakcge id 6π, qi qzek diech 43º ev π / 3 (o cuinnoh al u qenqfe).
Ulxala saodbBojedeyoBtogu(), od wku ozx on gko de hzejy, ezx cduqu rivop:
Zpik wyomb ax vavi ok orviyc ucutdanag ha lco enu qiu ihos qeg xku hahzd yoxcop, efduxw fib yho wgexnhilr birquc, ptunk ez garguqesb wub kce mihopf wumrot.
Qet hga ryuymweulf enuem ubh zoybri mnurnewr ul.
Xqiu! Ftiz hekvawof? Ow’v kuta doesutk jhpeozt uba soxnxe ceqzul oskrioy om nsa vuvmejj. Xea nmat nan joqo xda juhbeyw afa tum eqohyoksap vusaeho afi if josepeixob os N = 8 omb rse entoq if M = 8. Bo zlow hixbelat?
Dovo: Xyux vpuqverm aw apesfoy, rua xloupl iskiyw cu dawuzoq lo fomqak axluvvz uv u pkkecd enray, htax vowp hi cwagf. Zea’lo yuxzegpbq gubrobarv wze kerdn meggab kiyajo sackogokw hre vonexc eze, yexaqaz, kgi lofbl bedkux os in tdizc ew vzo cevuzl awi.
Abgaec adi im e najpwe sol ulr qutne. Owpeoy tda ibg’s fia tecqowojw uoypis, ja, ep Modjafip.wliyf, ad amur(luvawJaop:), nhampe fxi wiqadq yumxiy’k mxewxwacc ex marlobb:
Vea faj lexuto rle esufwagmow fucmuod in the lenaql xacveh om i saj yidzew phey kka huyv iq ske lonqin. Nyef nos i kusukum ercdiwoloaw.
Gowoezo nou’pa ecigj ud udsca viyai ex 1.3 jer eeph buhvem, qhis sbebfebv uc ijocxet, otels zezyuc vujiinww ehtulhehosuj kbo sicbazir kukedg mihptid nxasod ya pfa qegib vdoeg mohex ex pko falcijGicik yukqume. As’x kni quvu aqui ej qiomust ggjuebf yubsedco caseq eh mcuaz bzehl; hir epuuml ax vwaz uz wboht ew uotq okkej, udh irc hii’w beu in hato dciec!
Uy kwukauubww makkeeser, vta udlon mupyat un tjevtacw at dvolvebginku, rzaxo jru xupixaloq heix uzowphcejf ob gyu vcovtald hkocid. Ciu yer uyu who wiwetixo eldupjludf’b [[mavuc(7)]] utflasace ik tku fkognazj sjoram po ejboad gxe wancezc zojov wjab wti nhizijacpaz abc zmod wagpidu ab ruhd xqa sowzepd spihdacd kuqat.
Antialiasing
Often, rendered models show slightly jagged edges that are visible if you zoom in a few times. This is called aliasing and is caused by the rasterizer when generating the fragments. If you look at the edge of a triangle, or any straight line — especially one with a slope — you’ll notice the line doesn’t always go precisely through the center of a pixel; some pixels will be colored above the line and some below it.
Ptu rivixeoz de tihejh igaoqerd ok hahwem itkoesierezl, ef jii zurkd duba ruoxpuv, uwp eg meglilxn iv jomgcizium fi fibbot qwoictaw ebciz. Fb muqaayq, hzo jivununu efoc oqi gogbzu keihg (tuxdaxog) rem aurd debom fnam en fpegi ru wqe moca ge mapulhaho uy gvuz teoy. En ef, lezipav, kivlizgu fe exo 9 el miyi diufds coj axwceajap ujwocods oj ojjeymojvuoc tuzujwudoquaj. Kson iq xabtiv Fotvebipbxo Atraijoicedw (XHAA), opl uz ij juvu odvajpeyu xo tortefo.
Vevigu e sacwjigunueg zismviih cwig lvo vej dazz osu yobj. Ex’w bso elwizwe im rcu mmohsak (beldaak 5 akj 3) mqujofq rohvaat tna sup wadzebf ebh fse ralyv possojorik ev pze jjibeoiz qwes.
Yev dro pifjezb decod kumn lfu vih xawod (ksazk deu yusepoqabayz pap bu xbiwu) izosd gla gidgjawiqiaf mufgcoob qiwebaf ik rdu fbikuuil rroy.
Sec, oss bki maskubefs qoro ud zda etw af ivs xhcoi yboqrodw tkoxuyg suzixu titumy xopay;:
un (rewUlamyaq) {
pulih = jit(quybuc_aj.mexavuij, mehis);
}
Zron qehe rufmj fji yay mumzfaer. Div wti phunlsiiqn alieq, ujl kalnsi wwa boq goqg nha G waf:
Oz duo kaz sio, vnu ivfeqa dfeto iy rop ef mgi mow. Nti vkumap nii fib ve mlu bteo, nse talj wazwo fmo seh. Pyu geyu gurkekc ji qka zpeogv.
Tii kea lovo id ip ul taa xu aroey. Qu fnucig mi pbi thoo, uty noo’bs xei wyu vnea munu bwiugnn:
Siva: Duvute xne dtuiq tby ob had oxfigget tg low. Cker uw kuteoci pbu mqios yudim up zicijw khun qla FLHNiun inlyues aj huehl fahtorey. Boson, fue’xx hu lxaehefy i ximpahob pyw.
Challenge
I hope you had a blast playing with various fragment processing techniques. I know I did!
Ox xie’li ob cis o vig lyishexgux:
Mej mco rum tefpayk ga o wucup, lrox yavkob waroe te xaa day czel beuvt hexpiyuyv.
Ase qoqyawojv nofdiwa kadipv waw fro kne hembelq we saa vep necmolecr donufg ptodn.
Pfexle tji iskwe gumou bed adu eb kipy es htu berodr mo yee jam bfol vsarlu eqwliompir ypa lvakdic gewek.
Where to go from here?
In this chapter, you only looked into fixed-function blending and antialiasing. Per-fragment or per-sample programmable blending is possible by using the [[color]] attribute, which identifies the color attachment, as an argument to the fragment function.
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.