3D projection
A 3D projection ismathematical processprojectseries3D shapes to2D surface, usuallycomputer monitor.This particular algorithmoften referredusingword render, even if usually this word refers tomore general tasktaking some data fromcomputer memorydrawing it,whatever way, oncomputer screen.
The algorithm wasstandard on early computer simulationsvideogames,itstilluseheavy modificationseach particular case. This article describessimple, general case.
| Tablecontents |
|
2 Mathematical tools 3 First step: world transform 4 Second step: camera transform 5 Third step: perspective transform 6 Other algorithms required 7 See also |
Data necessaryprojection
Data aboutobjectsrenderusually stored ascollectionpoints, linked togethertriangles. Each point isseriesthree numbers, representing its X,Y,Z coordinates from an origin relative toobjectbelong to. Each triangle isseriesthree points or three indexespoints. In addition,object has three coordinates X,Y,Zsome kindrotation,example, three angles alpha, thetagamma, describing its positionorientation relative to"world" reference frame.
Last comesobserver (the term camera isone commonly used). The camera hassecond setthree X,Y,Z coordinatesthree alpha, thetagamma angles, describingobserver's position anddirection along which itlooking.
All this datausually storedfloating point, even if many programs convert itintegers at various points inalgorithm,speed upcalculations.
Mathematical tools
The 3D transformation makes heavy usesquare matrices,4x4 dimensions,trigonometric functions. Each step ofalgorithm ismatrix multiplication, whereelements ofmatricesderived fromcoordinatesangles listed above,various combinationssinescosines. Matrices have 4 rows4 columns,use homogenous coordinates, where vectorsthree elementstypically extendedfour adding"1" element at their end.
Givenpoint ofform {x, y, z, 1}, one will applytransformation resulting inpoint ofform {x', y', z', ω'}. The projected point onscreenthen at2D coordinates {x'/ω', y'/ω'}. The 1D coordinate z'/ω'neededsee ifprojected pointin front ofcamera or behind it. The number ω' (in addition toscreen coordinates)needed when drawing textured triangles, but not when drawing monochromatic triangles.
Thanks toassociativity propertymatrix multiplication,program can pre-calculate many matrices,example ifknows that some coordinate will never change.
Sometimes,final "transformation matrix" validall points can be calculated,then applied. This saves considerable time, since applyingmatrix topoint uses only upsixteen multiplications, instead ofdozens necessarymultiply matrices together.
Atvery least,transformation matrix can be calculated forsingle objectthen appliedall pointsthat object.
Note: The matrixesmultiplied inorder Last matrix×...×Second matrix×First matrix×Point
First step: world transform
The first step istransformpoints coordinates taking into accountpositionorientation ofobjectbelong to. Thisdone usingsetfour matrices:
Object translation
Rotation aboutX axis
Rotation aboutY axis
Rotation aboutZ axis
The four matricesmultiplied together, andresult isworld transform matrix:matrix that, ifpoint's coordinates where multiplied by it, would result inpoint's coordinates being expressed in"world" reference frame.
Note that, unlike multiplication between numbers,order usedmultiplymatricessignificant: changingorder will changeresults too. When dealing withthree rotation matrices,fixed order good fornecessity ofmoment must be chosen. The object should be rotated before ittranslated, since otherwiseposition ofobject inworld would get rotated aroundcentre ofworld, wherever that happensbe.
Second step: camera transform
The second stepvirtually identical tofirst one, except forfact thatusessix coordinates ofobserver instead ofobject, andinverses ofmatrixes should be used,they should be multiplied inopposite order. (Note that (A×B)-1=B-1×A-1.) The resulting matrix can transform coordinates fromworld reference frame toobserver's one.
The camera looksits z direction,x directiontypically left, andy directiontypically up.
Inverse object translation (The inverse oftranslation istranslation inopposite direction.)
Inverse rotation aboutX axis (The inverse ofrotation isrotation inopposite direction. Note that sin -x = -sin x,cos -x = cos x.)
Inverse rotation aboutY axis
Inverse rotation aboutZ axis
The two matrices obtained fromfirst two steps can be multiplied togethergetmatrix capabletransformingpoint's coordinates fromobject's reference frame toobserver's reference frame.
Third step: perspective transform
The resulting coordinates would be already goodan isometric projection or something similar, but realistic rendering requires an additional stepcorrectly simulateperspective distortion. Indeed, this simulated perspective ismain aid forviewerjudge distances insimulated view.
A perspective distortion can be generated usingfollowing 4x4 matrix:
where μ isangle betweenline pointing out ofcameraz direction andplane throughcamera andright-hand edge ofscreen,ν isangle betweensame line andplane throughcamera andtop edge ofscreen. This projection should look correct, if youlookingone eye; your actual physical eyelocated online throughcentre ofscreen normal toscreen,μνphysically measured assuming your eye iscamera. On typical computer screens as2003, tan μprobably about 11/3 times tan ν,tan μ might be about 15, depending on how far fromscreen you are.
F ispositive number representingdistance ofobserver fromfront clipping plane, which isclosest any object can be tocamera. B ispositive number representingdistance toback clipping plane,farthest away any object can be. If objects can be at an unlimited distance fromcamera, B can be infinite,which case (B+F)/(B-F) = 1-2BF/(B-F) = -2F.
If younot usingZ-bufferall objectsin front ofcamera, you can just use 0 instead(B+F)/(B-F)-2BF/(B-F). (Or anything you want.)
Allcalculated matrices can be multiplied togethergetfinal transformation matrix. One can multiply each ofpoints (represented asvectorthree coordinates) by this matrix,directly obtainscreen coordinate at whichpoint must be drawn. The vector must be extendedfour dimensions using homogenous coordinates:
Note thatcomputer graphics libraries, such as OpenGL, you should givematrixes inopposite order asshould be applied, that is, firstperspective transform, thencamera transform, thenobject transform, asgraphics library appliestransformations inopposite order than you givetransformations in! Thisuseful, sinceworld transform typically changes more often thancamera transform, andcamera transform changes more often thanperspective transform. One can,example, popworld transform offstacktransformsmultiplynew world transform on, without havingdo anything withcamera transformperspective transform.
Remember that {x'/ω', y'/ω'} isfinal coordinates, where {-1, -1}typicallybottom left corner ofscreen, {1, 1} istop right corner ofscreen, {1, -1} isbottom right corner ofscreen{-1, 1} istop left corner ofscreen.
Ifresulting image may turn out upside down, swaptopbottom.
If usingZ-buffer,z'/ω' value-1 corresponds tofront ofZ-buffer, andvalue1 corresponds toback ofZ-buffer. Iffront clipping planetoo close,finite precision Z-buffer will be more inaccurate. The same applies toback clipping plane, but tosignificantly lesser degree;Z-buffer works correctly withback clipping plane at an infinite distance, but not withfront clipping plane at 0 distance.
Objects should only be drawn where -1 ≤ z'/ω' ≤ 1. If itless than -1,objectin front offront clipping plane. If itmore than 1,objectbehindback clipping plane. To drawsimple single-colour triangle, {x'/ω', y'/ω'} forthree corners contains sufficient information. To drawtextured triangle, where one ofcorners oftrianglebehindcamera, allcoordinates {x', y', z', ω'}all three pointsneeded, otherwisetexture would not havecorrect perspective, andpoint behindcamera would not appear incorrect location. In fact,projection oftriangle wherepointbehindcameranot technicallytriangle, sinceareainfinitetwo ofangles summore than 180°,third angle being effectively negative. (Typical modern graphics libraries use all four coordinates,can correctly draw "triangles"some points behindcamera. (At least, I'd be very surprised ifcouldn't...)) Also, ifpointonplane throughcamera normal tocamera direction, ω'0,{x'/ω', y'/ω'}meaningless.
Other algorithms required
The resulting arraypointsOK frompointviewsimply drawing them onscreen, but more processingneededgetgood image. Seeexample:
- hidden face removal
- Z buffer
See also
