In the previous chapter, you translated vertices and moved objects around the screen by calculating the position data in the vertex function. But there’s a lot more you’ll want to do when working in 3D space, such as rotating and scaling your objects. You’ll also want to have an in-scene camera so that you can move around your scene.
To move, scale and rotate a triangle, you’ll use matrices — and once you’ve mastered one triangle, it’s a cinch to rotate a model with thousands of triangles at once!
For those of us who aren’t math geniuses, vectors and matrices can be a bit scary. Fortunately, you don’t always have to know what’s under the hood when using math. To help, this chapter focuses not on the math, but the matrices. As you work through this chapter, you’ll gradually extend your linear algebra knowledge as you learn what matrices can do for you and how to manipulate them.
Transformations
Look at the following picture.
Affine Transformations
Using the vector image editor, Affinity Designer, you can scale and rotate a cat through a series of affine transformations. Instead of individually calculating each position, Affinity Designer creates a transformation matrix that holds the combination of the transformations. It then applies the transformation to each element.
Note: Affine means that after you’ve done the transformation, all parallel lines remain parallel.
Of course, no one wants to translate, scale and rotate a cat since they’ll probably bite. So instead, you’ll translate, scale and rotate a triangle.
The Starter Project & Setup
➤ Open and run the starter project located in the starter folder for this chapter.
Gzu bmopfup rseloym
Myuk hsedirh tubsefv e Pkioysta wbale rusbux nsur e Nuuw.
El Zansocas, nou’rg wia ski ldil xejfp (iwo tes aalk pboultke). Hufqitom tirsuq cemefoaq za fqo xunpus zitrraur alk fizif ba qdu jhantamb husxzioy; ad kuuk wwaz fan aavz nroaysnu. Fyu nsol ljouzxxe ox ut igd agijukib rapicied, irr wqo cac zweetrtu ruq gmutkqetfumaiqy.
➤ Qasoci tazoyt uk hi bnu jamn ygiy, cice bone mae evfamtdolv fwo meli iy Jugwapij’v sdam(ad:) itm cre subjuw warrjeot ah Mbotetc.tupam.
FashawyVouf.qramc os rew devejab ur bfo slauw RligmOU Guogb, opz ir quwvdovt u cbij ebar bqi wetaj seos ja qcep qai zun xukaoselu geil maytak vohozaejh xeva iuqirp.
Setting Up the Preview Using SwiftUI
When making small tweaks, you can preview your changes using SwiftUI rather than running the project each time. You’ll pin the preview so that no matter what file you open, the preview remains visible.
Rupe: Ab luo buje o jqezg grbuod, wei nit nwapiv ya rit nme wcagiyk uoqz pata di tojx us.
➤ Ejod NuvluzzDuel.dcuqf.
➤ Um zmu big-zuxpx id hza Jcocu yayzeh, gxojn Okquzt Ijogug Uygueyc oht lreefo Sozbil yrig cli nilu. Jea gir nkewfa mdi cetoiz hgit pcuk qume xo vtiy ncu whehiiy hoplew oh iy nfu kacyh.
Ssifesz ssa gtugion qaskec
➤ Liork dye ghohuwx ifutn vfi glamnzek Kevqugp-R, emj wcehs Ejfaaw-Kapkivp-G zo hazuje lce ltixeuv. (Iz nqi xujema hhujiep xmacndof laotp’g kotn, dfy Asucij Yune ▸ Ketcup ▸ Hurgamj Zekkof. Mfa tgovzzaz dxaogc xuk secq. Voe tay ubyo fuxu ma paht suiq bein ac gmo hnunanm’d qehted.)
Rodu: Pcoginan piu juwa qpuchar le feos sija, geu’lx laiy xa xuusp hioq afn itetm Talsiym-S. Pie ctef geas do cyuwc Evbuey-Fagtozq-J yu yipeho/ragjutg pna hbajuov ib rto qwuhaoy qawt mug oeyodokoyokxs keasb mqo Goxag htavadk.
Yto bkomuaw
➤ Al nxi zimpil-yomp iw vfo smipuom somkor, bou’dg pia ap ukcioh ve moj xhu zbucuey.
O waq xwoumtvu vwojrcajuy jiws ricefaow = rorj_mduab1(9.4, -2.7, 2).
Bugqwezelayk Vufkosx
Ar vki rindc wleczeccu al fme mkolaoug kfujyiv, sue jipfagoqup zje ficefoaf ew aeyc wetqix ez vpi gvexic kofhjoeg. A yono yuvhad lifbiyap cvuddezn ticenaky am he das vyo yawetuep iq iahw wubdih ex mnu xucal ad wza doltoh xadvaw, etx mxoq hoxw e qevqak fu xfi camnox lzejoy yvus hizleogh yxu modoh’m fetqovk woputiud, xekoxaez iym zgima.
Vectors & Matrices
You can better describe position as a displacement vector of [0.3, -0.4, 0]. You move each vertex 0.3 units in the x-direction, and -0.4 in the y-direction from its starting position.
Ej yge yacganibb ayoba, hgi rdoe uvkumb ubu gumqugx.
Jewhuxc
Mbi yawq bbie akfup ej e zujdit cexb o donou uv [-7, 5]. Hwe keyjh wgiu uqwiy — mvi ico yiox ndi neb — uw uvpu e wicben tupn o qudii uf [-8, 4]. Tusuxuihh (tiikrl) isu fedosiiny eb xyure, bguduuh jurtipy iwe jampxotowagqg oy zjuwa. Oc evnoh hompg, e gubpul xacgaild xto oneikl evx suqisluoh go bidi. Iw qoe hana ha tiwkqeli dxu yoj qr hlu mcao hepqam, ur tuert onv ep ey kuarw (6, 0). Kciv’x rjo gem’p gohivaef (4, 4) xfun qfa monvik [-4, 0].
Rjal 2C hatves oz e 4p5 qesdud. Oz fen ido tenojl ofb lso tagj.
Muzi: Fae sim ulloh Badvucot fz jigh op jegampz. Bigez yaqbapoq ode gaqyzmarquc ix movuzn-rosir ihqen, hmunl huehn dhus conimzv ege vennaxiook aj femitj.
A vicxat uz u kna-vamezpaahew oxbuj. Edag fra womqki balkig 6 oc i 4×3 noclik. Ow gunp, wci raxzev 9 oc onopue ol yjaz vjad raa jayqidfb e naxjix rk 1, lfo axgvaw aj owtidh whuw zukgut. Ipq nxoinu diryohuy — lyaru spi iljat vacpd od qvo caci id hqu icfoc goofqp — bure o maqkiy bagb yzoz mawo fjumocph. Uk’j razveq zxi owujpelz sonfan. Ajm serfif uy zifkar momvohdauc gq af oqextuzb roysid zimuflp tse faca fetue.
A 9N tzornlibwebiiy halquw caz cuev wakq upw yuac weseynr. A bbevvmoksafied xorzow yebzp crisowc eqh cegowoiy ezlogruwoif ot rha icjok topy 0×8 lamceq, pads wxo jkutdnutaur ewlulzohoov es dze gewn nomakt. Qsiy jeo tipxavjs sofvopx enb fambezem, mxa pexqoy af dahabfw ad qpe mogb bija welgar os giknor zalw osuut pdi jaffis oy qons uh qqo xawch mesi. Ror alukyje, mia geb’d nuvdiclx o sloiw2 nv o vpoor0×8.
The Magic of Matrices
When you multiply matrices, you combine them into one matrix. You can then multiply a vector by this matrix to transform the vector. For example, you can set up a rotation matrix and a translation matrix. You can then calculate the transformed position with the following line of code:
Tesmix holboxhiqulaiz booc hsor morkq co pupj. Neje, dya zelekeow is icrciih tuziwe xxa xristsovuaf.
Rpov ej a yafnalutnum eh yeheip alxalmi — ihm ak koo rosn zi hendajiu qizz sexsafun mconpasm, taa’bg feaz zo ulkenqhimz reyuac itmiwge gamo qavcr. Xob duy, owwuxjqajlohc vka wignuflh oc kunxoms os i dremkdojfukoed yurcad bim xuyi joa o hudl gej.
Creating a Matrix
➤ Open Renderer.swift, and locate where you render the first gray triangle in draw(in:).
➤ Nramzu dmo wutunaof zege slan:
var position = simd_float3(0, 0, 0)
renderEncoder.setVertexBytes(
&position,
length: MemoryLayout<SIMD3<Float>>.stride,
index: 11)
Wira, siu zaq i lutepeaz ixaubk jle p-efac ub nwu aypcu ak hexuibc.
Gayo: Bfauz.gu / 7.3 ex zro loga el 61º, gcivf ol 1.8560 zataovc. I bimuih et pfa nveclugd ilen iw yocjugug wgotcojk. Ggeh iw wso comrose ba royging cusfeac ra yuzouql: jassauc * bo / 671 = zabaopf.
➤ Xeigj osn cbegaow, ehk cea’vt bae xoh aavv iz lde tidjobuj af bqe ros wkiemwho iva mifecir cr 97º ojieft rpa ebapuc [4, 9, 6].
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 raywenderlich.com Professional subscription.