3D vector computer graphcs Paolo Varagnolo: freelance engneer Padova Aprl 2016 Prvate Practce ----------------------------------- 1. Introducton Vector 3D model representaton n computer graphcs requres some smple maths transformatons to adapt model dmensons to the graphcs object where the model wll be drawn. In the followng a vb.net envronment s consdered, and therefore a graphcs object s an object that can contan graphc enttes such as a form or a panel control 1. In fgure 1-a) the typcal coordnate system for a computer devce s shown, whle the coordnate system for a generc model s shown n fgure 1-b). The dmensons of the graphcal wndow are usually measured n pxels and are ndcated as Graphc.Wdth and Graphc.Heght. The dmensons of the model s any, and t naturally can be n any locaton wth respect of the model coordnate system. The thrd dmenson wll be treated further on, after dealng wth the man mathematcal transformatons. It s useful to hghlght that f a prnter object s consdered nstead of a graphcs object, nothng changes. Fgure 1-a) Fgure 1-b) 1 To obtan the best graphcal management ths object needs to recognze the mouse wheel event. 1
2. Man mathematcal transformatons 2.1 Zoom extents Ths s the man task of the model representaton. The model s drawn wth the maxmum possble sze n the graphc wndow, as shown n fgure 2. The axes are named Xd, Yd meanng x, y coordnates of the drawng. Fgure 2 Ths requres a scale transformaton to fll the graphc area, a shft transformaton to move the model center (Xmc, Ymc) to the graphc center (Xgc, Ygc), and fnally a mrror transformaton to accomplsh wth the opposte orentaton of the y axs. All the three above operatons can be wrtten as: where: Xmc - x Xdraw = Xgc - ; GrRato - x, y are the coordnates of a pont to be drawn Ymc - y Ydraw Ygc + GrRato - Xgc, Ygc are the coordnates of the graphc center, as shown n fgures 1-a) - Xmc, Ymc are the coordnates of the model center, as shown n fgures 1-b) = (1) - Xdraw, Ydraw are the drawng coordnates of the model, transformed from ther orgnal reference system to the drawng wndow reference system X max X mn GraphcWdth. Y max Y mn Graphc. Heght - GrRato = max(, ) s the rato of the model sze to the graphc wndow 2
2.2 Zoom wndow The second man task of the model representaton s to draw a zoom of the model contaned n a selecton rectangle, as shown n fgure 3-a). The result of ths s represented n fgure 3-b). Fgure 3-a) Fgure 3-b) To obtan ths, two smple operatons are needed: a check for the lnes contaned n the zoom rectangle, and a zoom extents operaton as descrbed n the prevous paragraph. The check of the lnes contaned n the zoom rectangle wll calculate new Xmn, Xmax,Ymn and Ymax coordnates to ntroduce n equatons (1). 3
2.3 Pannng In addton to the man operatons descrbed above, t could be useful to menton the pan transformaton that means a repostonng of the vew n the drawng area. It can be accomplshed smply by changng the Xgc, Ygc coordnates of the graphc wndow. For example the pan represented n fgure 4, from the lower-left poston to the hgher-rght poston, s obtaned wth the followng transformaton: Xgc =Xgc + x; Ygc =Ygc - y where x and y are postve values n ths case. The drawng coordnates are always obtaned usng equatons (1). Fgure 4 4
2.4 Zoom wth the mouse wheel The use of the mouse wheel n computer graphcs s usually assocated to the operatons of zoom and pan at the same tme. In the followng fgure 5 a postve zoom (magnfcaton) s represented, together wth a shft of the model from the orgnal poston to another. For an outward rotaton of the mouse wheel, wth reference to fgure 5, the equatons that descrbe the transformaton are: Xcg = Xcg wp x; Ycg = Ycg wp y turn away the model GrRato = GrRato / wp magnfes the model where: - wp s the wheel power, a postve constant that medates the effect of the wheel rotaton. The value could be 0.02 or 0.03 for a power of 20% or 30%. - x, xy are the dstances from the mouse poston and the graphc center (both negatve n the case of fgure 5) Fgure 5 For a nward rotaton of the mouse wheel, that could reverse the transformaton shown n fgure 5, the equatons are: Xcg = Xcg + wp x; Ycg = Ycg + wp y brng near the model GrRato = GrRato wp reduces the model 5
3. 3D representaton There are many artcles avalable on the subject, nonetheless I beleve that a smple descrpton, along wth some practcal examples, could be useful. The only representaton consdered n ths artcle s the axonometrc orthographc projecton of a 3D model on a 2D plane. In ths knd of transformaton the real model s projected on a plane by means of parallel rays sent out from a pont of vew. Perspectve effects are neglected and dmensons are not altered; parallelsm of lnes are preserved but angles are not preserved. It s the way engneerng calculaton programs act. Fgure 6 Fgure 6 shows a parallelepped projected from on three pont of vew: - wth the pont of vew parallel to x axs the result s a projecton on a plane parallel to (y, z) plane; - wth the pont of vew parallel to y axs the result s a projecton on a plane parallel to (z, x) plane; - wth the pont of vew parallel to z axs the result s a projecton on a plane parallel to (x, y) plane. Fgure 7 shows a projecton obtaned from a generc pont of vew. The pont of vew s defned by means of two angles: the frst angle, named α, defnes the poston of the projecton of the pont of vew on the (x, y) plane; the second angle, named β, defnes the elevaton of the pont of vew wth respect to the (x, y) plane. Wth these assumptons, the coordnates (x, y, z ) of a generc pont n a 3D space are projected on a plane n the pont (xp, yp, zp ) wth the followng equatons: xp = x senα + y cosα yp = ( x cosα y senα) senβ + z cosβ Fgures 8, 9 and 10 show some examples of the projecton of a cube from three dfferent ponts of vew. It s nterestng to notce how the global coordnate system appears from the varous ponts of vew. Fgure 7 6
Fgure 8 Fgure 9 Fgure 10 7