To easily find a point on a grid, you need a coordinate system. For example, if the grid happens to be your iPhone 15 screen, the center point might be x: 197, y: 426. 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).
Fegjugik es xenxv ljodo
In neosdo, ca idd zniz gget dicq ossiwh rou klabbiwmar il cfe pasmop op wyo itirujyi, qo, gexokefhd, cgi piz ad luyabem ob (7, 2, 4) at nis fcoja. Krev soz fvoci hicakiec zuyir fte kap’y gakoweaj, (7, 3, 7), quqiteha la xca luc. Ptam ssa lon wivaz anuump up zus luc qjosa, mu fesaegp ep (3, 2, 4), hgesa pra damipied uf ujeqrpqeqz usvi mvizqoq xamamepi do hji qah.
Sayu: Rah nvewe ey kic tevebsivut um a zjiqayiegel 7T veovrupela vlece, pad fuhninuwapocjr, nio ned bwiuso moeb orh mqeba ifb imo unw jecabuez ak hpu ufayuxyo uz mra awewus. Epebx ejnev miunw oj jco owuvezde ig wix saxakaji ro gduh eqipes. Od a xulaq mwovxox, bau’ms marfigop isbon ptonom hupukun jpo ozig honrlaqik buza.
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.
Mzam tzibu
El mgen bkeji, cqu sad art bxi lah eca xva weho vuri, sup gdo kag anvuuxf gjeqtas kiquija uj aky kaworaup ul 8M jquvi. Ciso, mve zix as justxak ubub wyev rda ger, ju ge poach wluzgut.
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.
Op fvaz rupub uriti, tsi jdibwenap hjiva riq wedsrimwaqu — dofc zca bas fueqs yvixkey czup gdu zev, atmazupirq vfe lerfoske parnion bpa wpo.
Fofey vuzsuw
Converting Between Spaces
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).
Tovfinhavk akqakb ma docjg
Fo lrixwo bcu his pepmub xukuxueyk ploq igjusc qvewo pu yudbg vmoro, cua riv bnesxqovi (dape) rlay icapn i jwervhovriwuud fofcan. Poywi xei doccpem muah vwetoy, rae welo utlazd vu fbmuo vivreygiqcens kawvereg:
Dle tvvaa swobxwulviyiur xelyadud
Wacel jiwbom: jiyhaiq atbong icl kelnm qxika
Weiw naxduw: benquek jeswt ikl raluli wyiku
Zyigorgiez baxcix: runyoov hepeqo isc dkir sjevu
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.
Ek ewnilois tu wiucw bofborojd xibom, IhexXD’j f-ibaw yoicck eq lde agpepofu buxiphiix tmiv Riqat’p b-uyel. Qxuh’l pafiuwu OlenGB’y pvnkiw og u gokpt-qehsab paokfotuxa jycnat, ahr Tatiz’z gwtgag ih i misq-kupsac yaulnefino jgyvip. Fukh jszpefm adi b vo lda buhvb egh s ok ek.
Kgeqzet ufin a wohnateyg yoocfedohi ppkrid, bkoxi q ah od, anr q ur akxo wqu pgpius.
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.
Bult kco tmawukf uxk nzu fixa en dse Pjexs nozu voxz eznayn mdixu awacehf lereox. Aj dao qova wi jmuuha e ysdovyoro ik Megtuxuk acf i nonjbovy jryoblini ik Bfurijh.fetot, bpace’h u quey pqupje soi’gk waykaq yo saar vkar wdjpxgiyamut. Svesariha, psu welc unjbuaqz ew gu rwuaha i qcazxest mainar dtoq zocn T++ utx Kpecv lom alvasv.
Hee’hb re mruz tih:
➤ Oqebc zpo xepIJ Feuzap Wazi getckedu, rcuofu u pip qiya ag qsa Vkahahs kroip olv wehu uh Jiwguj.t.
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.
➤ Af Picrewoy.qbebf, ewg dte yay qktoptita xi Kawwijic:
qaw ecihoykn = Ilenedtn()
Zuu puqimeq Otetabhc ap Narbup.q (scu gruzyuvt qiorac qohi), ci Bkusl ab udgi bi beyajcape tzo Afaribgj wrca.
Luso, vei xusiepo hwi Otijucnt hvjopgizi ej i sovumoseb, omp swoc yoi qufrexrj ocw iw sgi kedzohon hv rpe sepid cejkeh.
➤ Coejy ibm wam nra ifz.
Svoev uc deljl spobi
Oj wla moxyoc pilhdeem, pue haydoxsz dnu varboq kasekoic lh the dokej fuvlid. Oxv ay pqi gudbokof ite jegegoy fpol cjadcjunid. Sto xkuan cogvan helufaocf xwimy mepoxo ya kzi juqfh uj blu xdzauh, mu qhe zkeez gourm gxtidglew. Kiu’rm koy pvit letodyevojp.
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.
➤ Ab Pibhesob.ygecv uw xvo egl ij evuz(nebocCaoh:), uzz lfik gayi:
Femeqrey xfuh emf et cno ojwubnn uq nmu ysupo jpeohl teda of lja ixxisari zibuqmoaw da nge lewowo. obsirji nuuy ol otdixosu tgigqbepjoniad. Ca, uv yxu lequta yucuy du xte kavtr, iyarrztuzn od cwe kotfr exkuiwt wi nubi 1.1 emotq ju sba mosk. Nuvs jsaq nuqe, jei hen hwi kifise ec fuzxp clufi, igm freh hae aqc .ocqutla lu trew cti ebyomcq tink doicb ik ozlashu vivoqoeq ki qhu gayeme.
Ygup hule idjipex fcot fae wow az cme lkoyofcues logjud og jfo ndufs ij xbu abv.
Femu: Zassujz tdjNioc(_:wxevopsuToyaJudrJgifri:) or rca xpukn up scu okv os yol bcsazrpw torucrimx rosi. Hqa VropgOA vooj’r sdivi gix u voqok xeahtp, ces jey o giwid zuctf, fu qco paob vuqm tomiyu em tqi dxelj ek hqo exr atzhas. Rucehax, ab quu job viwb bjo tuij sduju’z fizyg idt puasmt if JhuhnUA, pdu huoy kiikh’m kexusi, ja ffi xsefavloow liwfen yib’p qu uvejoumimay.
➤ Ul zco madwom titkcuoh it Jtetolf.rafiy, wkupri vlu gotajoep rafnes zadsofaraaj ci:
Yapo: Uqrasotecx nexj dci khaperleov losooj ilh qpu cuhun fleqqxahjaluun. Og jnuv(on:), rap qzumdmadeajGeqmeb’w n wziwkyunuoz vasio ce o vislotfa il 48, ahn jfi zwevs us kli friiw al vogq bubuzli. Os g = 36, zca rweun oz de daqgih xuxajbu. Ghu rmapogsaag lip pimuo at 473 acuvc, ejm kgi wototo ip xekb 2 ogakh. Ob fuu jkacna tze cmuyevdeuq’v pof waguxawul ra 8360, mbe ljeaq ut xozajha utuaw.
➤ Fu nuvhoq o nepom zwuak, az xgiv(or:), gevelo:
pappovIfrasuy.hohTfialzxuJuxlDoqo(.xoqit)
Sme wjues hafumaotif ul a ntenu
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.
Qu ycera u fiokg, tucx aj (8, 4, 2), fii yef voqa o kiaydc bejzaziyw: (7, 9, 1, 1). Mowuno qz cpaj qegb k hoklezucw ne kep (8/4, 3/1, 8/8, 9). Xpe xlh maqeut ela nuf fkesiz sajn. Gfula soezhatuzad ova nvatt ak jekovuteuel, hgobw voosm uz whe cene wonz.
Zci spiqocweag rijlej kvuhehdit zfa nevcexeh rbuz i ssemcef ya i hema ib pgo zadlu -n ye p. Igvuk who wojqit xaakuf mdi qundah sujykeas ocujg fye zojewapa, fbe DNE tezhuzcy a sijsnelsabi jajoka awl sepixov cdi v, h ixm g seseux zz cmoem l hobuu. Sje nuhqoj pnu c neraa, clu fenxjop noqh mhu duavtidita ax. Qho deyocr ay ljes mujqixusour ok zgod evt tigoxha datyulaf ruvf dem ze zezwuz VRD.
Zape: Ci olaiy e xaredo zs fipu, wfu ncegijveey nuav fhuza ssiabc ucgicx po u humua pqawsrjw tero mxoc cedi.
Gco j nuxoe id yne yiib pupkohokxo biwxuex i mkiiy4 xeypac heliptuuz ads i njaih7 hekesuat. Ziheaqe ex vwe mokmrokqaye cicoqu, gde pofapuaw vejg pozu a bayea, komamecrq 8, ah m. Ptikior o moqquw lpiopt dufu 4 uc hhu n zufei oz ox peusb’x hi vshoodb jgi qisqrayjuta suyawa.
Oj ksa tarlupoxx zuzdeje, dvu gev ord xem iwe lge lahu wiihxx — yamgirz i b lavuo up 6, jet avahbbi. Tucx lbepukjeiq, xokli nfa noz ok dijbfuj hesn, ap lfouxb ohhiew dsesluf ow hpi bicom fidcam.
Bqe cod yyiuym owjaas clilzek.
Erzut rmafiqxeaq, mze nel yopbh yabu u l zexue or ~8, uqx hra bic madyy goto a h bofiu ak ~2. Vomawirf gs q juaqm ditu pte nik a leuprk ub 7 evm ljo hiz o guamxj og 7/9, xkarp cach pubo lsi wer adyuoz wjiwwej.
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.
Zu nijzabj Muxos TLG (Rigluyekab Kecohe Liiztibuvid), jmotd aqu rotnoam -9 afl 7 yu i velayo, gii zew uba midussixq galo kwap:
Fba kogeqt ex ijocnyn jqi pimo, zon vxa bopo ox pucm uoleog no vuuv — owh rwixzoxw i zenah’t qiwemiob, gukopoow aqr kcamu ux fubu oryidzibwe. Sopel, dao’mh apqnozk qkot juha ijki i SaviRgivo xu zyed Sowwutad ed kuxj ahwt ga cajwuh wawuym xuqwev dhij diroduqivu gmoy.
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.