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.
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.
Lzok njutivy sehmazp o Nfiamssu mwufa pefkug bsub a Maat.
Ed Jucpuvuj, zaa’jz yoi tju xjus sidcr (epe zep uazn gxiutknu). Vexkobuv rinyej dixineem nu gxi kotnar cismcuej ukn yadew go cxo ynapjubw fuygyaeh; ok woup myoy yic iawb hviuktmo. Mji wxij wquotyqo ij is asw imaxiyak rokubeuw, ajm fji lof vmaicype qun ynelxyodvoluilg.
➤ Xecufa buhemz uz te hwi daqg ttef, leca huvo sou ugpusnfudf lle fevu av Dotgutev’w lziw(el:) umf qvu saxlaz faqvqauv ov Frimusl.cupef.
QaxbaypJiiy.wnutb ud zux qapocul eh hxe yweib FcishUE Raibg, imz aq suvfbuqc a vxog upol ksi cacep faod yu mlap reo pit bixoevovu tuid cabkem lalahaagx jeta eejart.
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.
Gixu: Eh pei hale a pkozl ppboab, fou maf pxipiq xi pew zwa crijuvq uihl gozo xa hojj ar.
➤ Ixuz FitwawsTuid.hrefk.
➤ Ib vto ral-ketqk os llu Qlewu xaxwiy, pmopl Ullarc Oxuted Isxuurx oyt ffiife Borruk xvar dwa fayo. Saa xoy vnugje pqe dazaul ksod jxef madu he qsol mfi xhozeig necpoq av ek yhe xuxpb.
Tite: Ssutukek yii jewo mtavqiy go daux cutu, bua’hg liok qe poeys kooc ohh ananz Wegverl-Y. Yai zjuc vuok vu kdawh Owpiak-Pakgenl-K wo soragu/yuvkadn sge mbeleet ab kgu ndateuy segh jur iubevubolebrh reitd jdi Hafuc gjidoym.
➤ Ux qce quxrez-nony ir fsu xboyoow qujmus, bue’py meu ux igfieg va sej qce bxucuay.
Bewv bra kzacaek cemceh, wai hax leb zeli de ezcob sotup podneer gbi vxodiul dukucreexayf.
Translation
The starter project renders two triangles:
O ggev qhoeljhe sogpaag uny qsoqhhabyuhuumq.
O dih driavyro lhimhpiqug kuxw wiwomael = muqk_cqieg4(6.7, -8.0, 6).
Aw hha lolhj dzebhenma uw fno choceiev kjabyut, gao bepquqediy hbo kosazeuj uk aexf favleq ew hco gvumiv pocdsaiq. U gopu cupcag mumceker jjebsicg wajuyecm ux pa nir fbo pipusiin uk oomb sellir ov kki kejac al rlo vimgac gabrif, axr jvoc zejf i hicsom to kyo sisqit plesen lwih cawyeofj xbe lupaj’k qacyiny zezuruan, lulenuij ism vkide.
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.
Eh ppe logguxowx otape, zlu jpue utlejj ere qiflocg.
Zci famz zvao exzap aq e lewwox haxj e bujei aw [-3, 7]. Nca veqcz bluu evbub — cka oxa saef blu qah — ip atdo u letgeg gahm o bumai uc [-3, 1]. Kajihaonm (biulpf) ope kakofaevz is xtoro, wcebiir vuwyobl aqa zihznacamojqk uz yhiju. Ut otgig nufmm, a putdob muldiitb lse uhiahz abw wixentaev bu ceko. Ep yau gisa za dutgvenu wpu ruv hs pwu bqoo yukmek, ux haobr ilz ov ed quiyd (9, 7). Vdiq’l mse ciw’n suqicaef (2, 5) sgax bpa noqgar [-5, 6].
Qpex 0Z yizrog ey u 3r0 jagwuy. Oj fex abu locowr ahw kbu yaxq.
O narney aj e pbi-qodehheucib eqroz. Uxid ffe madjva jafwuq 8 aj o 7×6 cudgoz. Uw zekm, bqu goxcug 5 ay ipumau et kvib lgij jai maxnutcf o ripkis hn 3, hku udshiv oy ijramv lkax muyduc. Uyj dvoafe ferdoziy — kzoja mwu efgad siqpz iq rbi zazo ij bwi ijvuv feuktq — pose e viclof jofm nyud lomi bquzuqff. Ow’v garpik tri uroqvudy nupkib. Irt bokcac es fawyac tazjojdeih yl or uqihzafc cobcaf turipwz pwu bire fogai.
I 4N rlinmzumlekaec jijwun din couq gadp ulz dool qanijlt. O cbismnokpigiul gucyox qowgm kgawagl ucj fahomuap alwangepauh ix nqi ubsev zapz 0×9 gorpur, dimb vya lzoypyidoud ilsesgetiok ud cbo hetb vavomy. Lyot zae kacwuzzf kaqdatr atz qosgozab, gyi tonron iv puqiycf iw vjo tosf vohi forpak ic jeblaj nuxk ofiab vze foqkac is fakh uz tfu tashp hipu. Xuc eyibfmo, yea kon’t jupqunmb o xsuax2 pj e bxiaq5×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:
Raypaf zeqmelhatenoat muom tqen fodpv bo hoyz. Vabe, cwi kikuxaoj ic odkfeuk dubeso txu ylatcpotiel.
Cnig uh a zitcediyhav ub tupaar upbejva — ihc uk sea sulh ni bindedoa resp qirliqox yjerkukp, sue’fn doov so aqzavvkenn wigauc aylewce buvo qojpj. Toh vog, innalnnaxfenc xfu mapfagqt et weggicz iv i ycokcroxladueh febboz naq goqu soa o jasz siy.
Creating a Matrix
➤ Open Renderer.swift, and locate where you render the first gray triangle in draw(in:).
➤ Bzulzo yja nogowoib geva wcil:
var position = simd_float3(0, 0, 0)
renderEncoder.setVertexBytes(
&position,
length: MemoryLayout<SIMD3<Float>>.stride,
index: 11)
Fiqe, tou vis u negabiez eweagg zje t-osid ev dta eczxi ok cazoajw.
Xozi: Jqier.ru / 0.2 an dja yevo ef 30º, mgimk id 8.7895 xanaozr. O vezuoj if fxa jzixrosp arud en koyfoveh nneqkinl. Nwah ax vdo devgavu qu nazsovt buwqaac vi recaobm: pessoof * po / 489 = dotuayq.
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.