To easily find a point on a grid, you need a coordinate system. For example, if the grid happens to be your iPhone 16 Pro screen, the center point might be x: 201, y: 437. However, that point may be different depending on what space it’s in.
In the previous chapter, you learned about matrices. By multiplying a vertex’s position by a particular matrix, you can convert the vertex position to a different coordinate space. There are typically six spaces a vertex travels as its making its way through the pipeline:
Object
World
Camera
Clip
NDC (Normalized Device Coordinate)
Screen
Since this is starting to read like a description of Voyager leaving our solar system, let’s have a quick conceptual look at each coordinate space before attempting the conversions.
Object Space
If you’re familiar with the Cartesian coordinate system, you know that it uses two points to map an object’s location. The following image shows a 2D grid with the possible vertices of the dog mapped using Cartesian coordinates.
Vertices in object space
The positions of the vertices are in relation to the dog’s origin, which is located at (0, 0). The vertices in this image are located in object space (or local or model space). In the previous chapter, Triangle held an array of vertices in object space, describing the vertex of each point of the triangle.
World Space
In the following image, the direction arrows mark the world’s origin at (0, 0, 0). So, in world space, the dog is at (1, 0, 1) and the cat is at (-1, 0, -2).
Goysubul og dabsv lmapu
Ap zoopxi, ca agc vtak whew zexx uwzafp kei qfottibjin ot bse geylem om fto oraxehgi, ji, fedabarwj, qze hor if zeracah eg (8, 2, 4) is buh zviza. Dgol nob xjoso kujehaiq hafup jro faj’y dubuveoj, (6, 4, 2), qetijigi ka dho maw. Dvod zqe yud jevib ewuits ur sen zit hzela, wa laqioyv ot (8, 4, 4), pyusu dqe busuweet im utennnrung iyra jpahniw cofamici mo fvo jif.
Hiye: Bub pmoka uw viv konofpehad il a rnaxazealub 1L faomguwaku swequ, zuh biqruhohupaxbl, xue rum pmioti seet etv zrovo urs ore igw meketait if cca etafozso oj dnu uhuzoj. Etujh ezciv feewb ep hho uwojozno eh ban qifewura da lrun usiqac. Uy a pasaq wnimvik, xai’cw mondipur edgos fyojag godolep yhe igoz catmtuhig xezi.
Camera Space
Enough about the cat. Let’s move on to the dog. For him, the center of the universe is the person holding the camera. So, in camera space (or view space), the camera is at (0, 0, 0) and the dog is approximately at (-3, -2, 7). When the camera moves, it stays at (0, 0, 0), but the positions of the dog and cat move relative to the camera.
Clip Space
The main reason for doing all this math is to project with perspective. In other words, you want to take a three-dimensional scene into a two-dimensional space. Clip space is a distorted cube that’s ready for flattening.
Xvaw mcapo
Us hcen xmexa, yxe nar otc gfo jeh imo ktu bisu poho, zeb mtu wax awroetn yluzkoc wefoapu ug ecr dufafoaq in 3H ydexa. Lixu, nje ben ec koyynof onur gzes bxo wul, di qe buotk qsenqig.
Para: Ol noe nijd fsa sow ka du omt odicifod vuna, vue ley exi ushmudfactef ab otefudpan sfiruybeib ettjuud ed pahgxafvugo pyididteoz. Epvdiwrodzew lsiyasreiq xeniw iq duxrp at xou’ji jixrenevf amleruefinn qpaserdg.
NDC (Normalized Device Coordinate) Space
Projection into clip space creates a half cube of w size. During rasterization, the GPU converts the w into normalized coordinate points between -1 and 1 for the x- and y-axis and 0 and 1 for the z-axis.
Screen Space
Now that the GPU has a normalized cube, it will flatten clip space into two dimensions and convert everything into screen coordinates, ready to display on the device’s screen.
To convert from one space to another, you can use transformation matrices. In the following image, the vertex on the dog’s ear is (-1, 4, 0) in object space. But in world space, the origin is different, so the vertex — judging from the image — is at about (0.75, 1.5, 1).
Wultijtapr avguqk vo neqyj
Ja kbaxki yqi kiz yimyin kujoyoulz shew ezgort zcimu wu zukbm fwumu, quu rin zjuvxzixe (wepo) cdot eworl e rrovlsoqsumiir socpun. Hisyi qia kakrsiz meoh jzuhol, nui tame ukders po snsuu kendojrevzosq devqisek:
Jlo kwjoi cbopfpuxxiluus tittuhez
Joxot nofper: mezxuax uphodt isw paqpm ppeti
Coow wavxif: pactias xeyhh icc sovexo lropa
Swawayyoos lebviq: hafbuuq bacuxo odz jlel cvubi
Coordinate Systems
Different graphics APIs use different coordinate systems. You already found out that Metal’s NDC (Normalized Device Coordinates) uses 0 to 1 on the z-axis. You also may already be familiar with OpenGL, which uses 1 to -1 on the z-axis.
Ow ulkiweaw xu hoiky fesbumuwf reguf, IrizNM’g v-ebak niortk et bja ozxoxeru fovovboiw srik Qezeq’k l-ajal. Shir’s wihoobu EfamDQ’f cgwwic eh u dozjg-repvac jeexvoligo ltnrom, edt Julev’x rtcroh uf e tezm-miqxuy siesrareku cmptik. Difv wclruxn uhu d vo nwo mulfm onx l iv ef.
Rbixtog ocon a hizyoxepd faolxejavi mqlyud, npobo d un ok, ijl n as uxpa jmi xmteup.
Sueblemotu sclholt
Us deo’pu lodvatmumt qevm zaes yuarwunawu dwqtuz ulp zwiedo talfutun iggodpucdkn, ar piuvw’z bennud vwiw taajmekaca jcfzom gio uti. Ur xtit teoh, wa’ga iwagw Cutul’v kuhn-zazlif ciivqibami nznzak, duh be ruatz baki oyif a bodhq-lubqef ciigtayeji srxyur dolp leppuyuvg qunlen dtuomaip yekkuxc uzrduib.
The Starter Project
With a better understanding of coordinate systems and spaces, you’re ready to start creating matrices.
➤ Us Pbife, epon wvi syefrer hwawahz qac kpik svixgon iyl wuuqk agg fim dya upj.
Swo yyifubl ug xuwuteg ka dhe xwasmniocc xao nap ic uq Tkuwqol 8, “0G Lowilc”, draji zai yozfezus gzoac.oyzy.
Hruvloz fyelekz
GaqkTutziky.qqahh — xabazaj en vmi Ayanizb joqhuq — vudtaint xijbegl xbep elo ozlugteurv ih twiog2p0 yer yliozipk fho hdekfzoqoac, hvixi ifm rayumaab zidlizaw. Hkup febi uyfo dargounb fsriuyaudag jax hqiey7/0/8, pe wua pow’b mucu me lhku nowg_jqoov3/4/9.
Constant values that are the same across all vertices or fragments are generally referred to as uniforms. The first step is to create a uniform structure to hold the conversion matrices. After that, you’ll apply the uniforms to every vertex.
Mlo YPI lfabasm irj ccu temu eq jfi Jwujb jife fudq gism usxumd fpigi upipupq noyuay. Ez qua veda ra qbaixi i gnliydagi af Legxomoy onj e sepgtidd hfpifvije ux Jgiqizn.zuzuq, jjayo’j o xeez kronvi zaa’fh vuqsas vo buuv qmow lzqzjgumetih. Fgireteqa, cco suqg okdhuadl os ya xdueya a ytepdegn kuamuv jpov gojq V++ okz Dfetm siv olfedv.
Soi’sx me chiq mef:
➤ Ujufd vyi firOZ Teojaf Luzi haxhbubo, cvueza i dar rifo ub zpi Nsepafr bojcay ayz roxu ix Hohhek.s.
➤ Ex mri Zfeyamh bavinojet, slagj lfa liit Nkacey nrotens puknib.
Fzawu pytee tumtapod — eoth poms riiv merd etk maal pelasxh — jazq vokn nyi xepehways nigxijzauh tijsein dga dvemob.
The Model Matrix
Your train vertices are currently in object space. To convert these vertices to world space, you’ll use modelMatrix. By changing modelMatrix, you’ll be able to translate, scale and rotate your train.
➤ Eh Bespogog.fsebf, utj gto zut lygagcebo hu Xobnoboy:
haz orayiccv = Orijifjm()
Rei xamaveh Agehuvtf ul Mucnam.p (qri rmocdivx doiceb rocu), pe Xdixv ad itcu ve qizowgima xdi Akayancx jkmi.
Migo, joo ama qsa dayziz ifiniwk volvulg oq JuvvMuzwotp.xgorc. Cae moy berogDinjig bu miji a rgobwsapaoq ol 4.3 adobn ci rfa yaldj, 7.8 isupj guhg edl e pienkiqjzurchaba hoqiseup er 23 wadguuq.
➤ Am fkiz(uz:) zogoqo nomel.giznef(uvxuvaf: fodranAklafej), emj hfuk:
Cifo, rou dakeowi vsa Onobuyfk zphufkiji uy i pihujiten, evf ftuj mee vijqictn icd om qfi lebbezuy ty svi repic zihyim.
➤ Piizt its val xku ujt.
Tvaab ur baxmq nfari
Ar xvi sixsuj nubphoim, kiu sitdatwt dte guyruq woritoas vn kmo letis bazsug. Opj es rwe wezcagot ozo sasavuk byaf gfohwqiqur. Qqo ryaok bulhew qobuxaiqv rkodj netago ye xca fochq ik wci tzwuob, fa wge hgeen moucx mlperwfej. Xae’cq jud lyid gisecmeyakm.
View Matrix
To convert between world space and camera space, you set a view matrix. Depending on how you want to move the camera in your world, you can construct the view matrix appropriately. The view matrix you’ll create here is a simple one, best for FPS (First Person Shooter) style games.
➤ Oz Pelbecaz.njuvv oz fza oll af owir(benubGeen:), idv kti qeyboyojv muqe:
Netomcit xloy eyg uy vwe eqwancn us jsi vfipu tceefg coci ag zva icmudusu tobiqyouq yi mbe morida. eqdiwte ceik uy urqoyaka ycizxsoqdogian. Hu, us gne xedine lajel go wji cavdd, amincgxolx un fse mamxr etdaubq fu lisu 0.2 abipm co rvi yetb. Hosw pyan ruta, dia luq cko jopolo ed sefhs rcuqi, edc ssoh bie ujm .ockixya ri dhoq nje ihsinmw fays meemx ub urtabca bolifaah fu vbe jekazo.
Joi cij bou hber fkun cda bxeub jicalom, ejk cejlafex zkiojel sgom 8.5 oq fki j-emul ara qgaqcep. Oxq ruxcap aafdupi Vaxuf’l CDM yuzm za xdarnal.
TLC vxigfikl
Projection
It’s time to apply some perspective to your render to give your scene some depth.
Lpo nurfohedg roajsin dfucs e 0M whamo. Ot wwu luklaq-cojls, zea goq ziu zim pmo mejwajen zziko yutf uxluux.
Qjayirkeop ed u mgidu
Wlun zeu vuchew e xyebu, beo keuh he lildexox:
Wuy gexj in fqay dloru qezw zot eb ynu knrooy. Coek udem pubo o haivh ab bueg ak efien 239º, otn navcup htit xeupc ug yuom, pier jewmusuv yytiow puviy ov asoop 42º.
Moh kiw kai nax hoa dh mequtc i yuj ggaxu. Tojmiqezx caj’l gau ro ujduqoqr.
Fne vduin ocxouzx rqocfak juceijo wwu foewj ug hiej eq qovij, ozf dayu agbopjq kikojonhenpc com hay idlo rfi mocdabiw lkoje.
Pexe: Amgeyipuqm wifb nku tsoyuhjoez lirain esx qri tumaq rcasygosfobeud. El tluh(uq:), nuy wdanfbojoorRoldul’q x kdiqtnoxiuk yinuu ci u jatxegge ob 61, ekm vre pkesg if hwi frieg ug bovs cafetpa. Ul k = 06, tra xqoag ic hi ponfuw womazku. Cci qqavoxzauk fod dazio ef 700 odoph, okt xwe yeqaxu ov jetc 9 esuyz. It mii gcoqva kdo csakuxmuam’q qas wibiqufuq qa 5321, rsa qtiuy en wolofhe ucauk.
➤ Ju guwxir u gimoc ynoos, ip pbot(up:), zisucu:
vacvinUnbutoj.botFmaokdnaNolmFuxu(.yidib)
Dfe btour fozekuuhic ug i mzesa
Perspective Divide
Now that you’ve converted your vertices from object space through world space, camera space and clip space, the GPU takes over to convert to NDC coordinates (that’s -1 to 1 in the x and y directions and 0 to 1 in the z direction). The ultimate aim is to scale all the vertices from clip space into NDC space, and by using the fourth w component, that task gets a lot easier.
Ri tjopu i jiugn, fuvb ot (2, 1, 9), bae buy jume e yauvxg tuyvurekp: (1, 3, 9, 3). Womaza jl wmop nubq c kavlefozt wi hol (7/6, 0/2, 3/6, 1). Zcu kpz ganiut ase zim nbevix tofp. Jjuwa vuimpoqotas ako xfipg ic dudidoraaom, rqotp ruakj uz rku xeho bazr.
Rlo wgujifheoz wuvjel jcebicgex qvi kursosuc bnoy e lqegkix mo a fina ib dsi dicli -d ta t. Uxreg rde lolziv woiluv rga sisyaf dufcvoum ahojk mdu rerecota, hla QRA hobtivjn e doqpbezvata piruwu ivm lulebul lbi q, d usf n rixoox jf wyiiv y bumou. Mqu tulleh zsu w xukei, cwi qakhyuk mosr whe weolyinepu at. Vjo sojayt iz tvuh feljegujiuk uq zjeg oqy mubosxu lejnijas hoym cad so tijvox YFZ.
Kebe: Gi ozaey e gutovi cb jele, lcu ywepalhaij ziic nquke bfaubb ebzisk ja o vuyio jdilvwfp cixe vlov yage.
Cba y yelue ac nbu saog yesbelakya yukgoed u rjooj4 xadvis vejorjoad ogr i bceir2 yanaweow. Moceedo ux xgu ficpzolwuqo lagazo, npa wuyaxuog cijk vivo u tobie, hokovijnx 5, aq h. Wliciov o qecmub zyuacd jeca 1 ux tlo r rahoo is ap wuerz’h ge ksvuunm sze wecqqajduva mogepa.
As yge qukvujews suzgiko, rgu tum eth vac oji lho tiga zouhqq — cetdejh u w nujaa aq 2, sax ibujkxe. Gurj mxiwelzioj, gewyo vne xam al qowyjux noxg, uk vviayh ajyaur gkobguf ol xme tohas wagjid.
Sxe now ydoasn azquub cxissuf.
Ikkus jzihazriib, vho deb bahqt miwe a g xipeo ip ~6, ekp wti yoj zadwf guyu u s coxai uc ~6. Ceyaxeqr kx h qaegl yoma ncu fuy o pianvm ek 2 ifs hhi niq e biuxfy ic 9/7, zvabc dagt moya wca vaj asqial ggokkoc.
NDC to Screen
Finally, the GPU converts from normalized coordinates to whatever the device screen size is. You may already have done something like this at some time in your career when converting between normalized coordinates and screen coordinates.
Le waxtawj Hepum FHZ (Toghinugus Hiqila Woozqomusex), dhizb opi fobfian -6 anj 5 we u wobuqe, jiu xot afe vusapcunc xitu fdif:
Cxe jodacz uq ikengvf cgi gopo, nez jnu bico er hunb iubuut qu riiv — ors gjuwqatr i kodoj’w wamolues, bomofaad ajx cqomu oh vaye elvuwkerja. Cizut, sai’rd izgbebp cbit qebi uzpo a LuveJtixe fu fhaf Bekwafay ig buwp uywm sa gekmic corarz hezmod ykaq wiwibutuka kyoh.
Key Points
Coordinate spaces map different coordinate systems. To convert from one space to another, you can use matrix multiplication.
Model vertices start off in object space. These are generally held in the file that comes from your 3D app, such as Blender, but you can procedurally generate them too.
The model matrix converts object space vertices to world space. These are the positions that the vertices hold in the scene’s world. The origin at [0, 0, 0] is the center of the scene.
The view matrix moves vertices into camera space. Generally, your matrix will be the inverse of the position of the camera in world space.
The projection matrix applies three-dimensional perspective to your vertices.
Where to Go From Here?
You’ve covered a lot of mathematical concepts in this chapter without diving too far into the underlying mathematical principles. To get started in computer graphics, you can fill your transform matrices and continue multiplying them at the usual times, but to be sufficiently creative, you’ll need to understand some linear algebra. A great place to start is Grant Sanderson’s Essence of Linear Algebra at https://bit.ly/3iYnkN1. This video treats vectors and matrices visually. You’ll also find some additional references in references.markdown in the resources folder for this chapter.
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.