Projection (Part 2) : Derivation Created by Dr. Slim BECHIKH for - - PowerPoint PPT Presentation
Projection (Part 2) : Derivation Created by Dr. Slim BECHIKH for - - PowerPoint PPT Presentation
Projection (Part 2) : Derivation Created by Dr. Slim BECHIKH for SPSU course - CS4363 Computer Graphics and Multimedia Fall 2014 Parallel Projection normalization find 4x4 matrix to transform user
ParallelProjection
normalization find4x4matrixtotransformuserspecified
viewvolume tocanonicalviewvolume(cube)
glOrtho(left, right, bottom, top,near, far) Canonical ViewVolume Userspecified ViewVolume
ParallelProjection:Ortho
Parallelprojection:2parts
1.
Translation: centersviewvolumeatorigin
ParallelProjection:Ortho
2.
Scaling: reducesuserselectedcuboidtocanonical cube(dimension2,centeredatorigin)
ParallelProjection:Ortho
- Translationlinesupmidpoints:E.g. midpointofx=(right+left)/2
- Thustranslationfactors:
(right+left)/2,(top+bottom)/2,(far+near)/2
Translationmatrix:
- 1
2 / ) ( 1 2 / ) ( 1 2 / ) ( 1 near far bottom top left right
ParallelProjection:Ortho
- Scalingfactor:ratiooforthoviewvolumetocubedimensions
- Scalingfactors:2/(right left),2/(top bottom),2/(far near)
- ScalingMatrixM2:
- 1
2 2 2 near far bottom top left right
ParallelProjection:Ortho
Concatenating Translation x Scaling, we get Ortho Projection matrix
X
- 1
2 2 2 near far bottom top left right
- 1
2 / ) ( 1 2 / ) ( 1 2 / ) ( 1 near far bottom top left right
- 1
2 2 2 near far near far far near bottom top bottom top bottom top left right left right left right
P = ST =
FinalOrthoProjection
Setz =0 Equivalenttothehomogeneouscoordinate
transformation
Hence,generalorthogonalprojectionin4Dis
- 1
1 1
Morth = P = MorthST
PerspectiveProjection
Projection– maptheobjectfrom3Dspaceto
2Dscreen
x y z Perspective() Frustrum( )
PerspectiveProjection:Classical
(0,0,0)
- N
Projection plane Eye (COP) (x,y,z) (x’,y’,z’)
- z
- z
y Based on similar triangles: y’ N y -z N y’ = y x
- z
=
VRP COP Object in 3 space Projectors Projected image
Near Plane (VOP) + z
PerspectiveProjection:Classical
So(x*,y*)projectionofpoint,(x,y,z)untonearplaneNis
givenas:
Numericalexample:
Q.Whereontheviewplane doesP=(1,0.5,1.5)liefora nearplaneatN=1?
- z
N y z N x y x , * *,
- )
333 . , 666 . ( 5 . 1 1 5 . , 5 . 1 1 1 , * *,
- z
N y z N x y x
VRP COP Object in 3 space Projectors Projected image
Pseudodepth
Classicalperspectiveprojectionprojects(x,y)coordinatesto
(x*,y*),dropszcoordinates
Butweneedztofindclosestobject(depthtesting)!!!
VRP COP Object in 3 space Projectors Projected image
(0,0,0) z
Map to same (x*,y*) Compare their z values?
PerspectiveTransformation
Perspectivetransformation mapsactualzdistanceof
perspectiveviewvolumetorange[–1to1](Pseudodepth) forcanonicalviewvolume
- Near
- Far
- 1
1 Canonical view volume Actual view volume
Pseudodepth Actual depth
We want perspective Transformation and NOT classical projection!! Set scaling z Pseudodepth = az + b Next solve for a and b
PerspectiveTransformation
Wewanttotransformviewingfrustum
volumeintocanonicalviewvolume
(-1, -1, 1) (1, 1, -1) Canonical View Volume x y z
PerspectiveTransformationusing Pseudodepth
Choosea,bsoaszvariesfromNeartoFar,pseudodepth
variesfrom–1to 1(canonicalcube)
- Boundaryconditions
- z*=1whenz=N
- z*=1whenz=F
- z
b az z N y z N x z y x , , * *, *,
- Near
- Far
Canonical view volume Actual view volume
Pseudodepth Actual depth
1
- 1
Z* Z
Transformationofz:Solveforaandb
Solving: Useboundaryconditions
z*=1whenz=N………(1) z*=1whenz=F………..(2)
Setupsimultaneousequations
z b az z
- *
) 1 ........( 1 b aN N N b aN
- )
2 ........( 1 b aF F F b aF
Transformationofz:Solveforaandb
Multiplybothsidesof(1)by1 Addeqns (2)and(3) Nowput(4)backinto(3)
) 1 ........( b aN N
- )
2 ........( b aF F
- )
3 ........( b aN N
- aF
aN N F
- )
4 .........( ) ( N F N F F N N F a
Transformationofz:Solveforaandb
Putsolutionforabackintoeqn(3) So
b N F N F N N
- )
( ) 3 ........( b aN N
- N
F N F N N b
- )
( N F NF N F N NF N NF N F N F N N F N b
- 2
) ( ) (
2 2
N F N F a
- )
( N F FN b
- 2
Whatdoesthismean?
Originalpointzinoriginalviewvolume,transformed
intoz*incanonicalviewvolume
where
- Near
- Far
Canonical view volume Actual view volume 1
- 1
Original vertex z value Transformed vertex z* value
z b az z
- *
N F N F a
- )
(
N F FN b
- 2
HomogenousCoordinates
Wanttoexpressprojectiontransformas4x4matrix Previously,homogeneouscoordinatesof
P=(Px,Py,Pz)=>(Px,Py,Pz,1)
Introducearbitraryscalingfactor,w,sothat
P=(wPx,wPy,wPz,w)(Note:wisnonzero)
Forexample,thepointP=(2,4,6)canbeexpressedas (2,4,6,1)
- r(4,8,12,2)wherew=2
- r(6,12,18,3)wherew=3,or….
Toconvertfromhomogeneousbacktoordinarycoordinates,
firstdivideallfourtermsbyw anddiscard4th term
PerspectiveProjectionMatrix
RecallPerspectiveTransform Wehave: Inmatrixform:
- 1
) ( 1 z b az z N y z N x wz b az w wNy wNx w wz wy wx b a N N
- z
b az z N y z N x z y x , , * *, *,
z N x x
- *
z N y y
- *
z b az z
- *
Perspective Transform Matrix Original vertex Transformed Vertex Transformed Vertex after dividing by 4th term
PerspectiveProjectionMatrix
Inperspectivetransformmatrix,alreadysolvedfora
andb:
So,wehavetransformmatrixtotransformz
values
- 1
) ( 1 z b az z N y z N x wP b aP w wNP wNP w wP wP wP b a N N
z z y x z y x
N F N F a
- )
(
N F FN b
- 2
PerspectiveProjection
- Notdoneyet!!Cannowtransformz!
- Alsoneedtotransformthex=(left,right)andy=(bottom,top)
rangesofviewingfrustumto[1,1]
- SimilartoglOrtho,weneedtotranslateandscalepreviousmatrix
alongxandytogetfinalprojectiontransformmatrix
- wetranslateby
- –(right+left)/2inx
- (top+bottom)/2iny
- Scaleby:
- 2/(right– left)inx
- 2/(top– bottom)iny
1
- 1
x y left right bottom top
PerspectiveProjection
TranslatealongxandytolineupcenterwithoriginofCVV
- –(right+left)/2inx
- (top+bottom)/2iny
Multiplybytranslationmatrix:
1
- 1
x y left right bottom top
- 1
1 2 / ) ( 1 2 / ) ( 1 bottom top left right
Line up centers Along x and y
PerspectiveProjection
- TobringviewvolumesizedowntosizeofofCVV,scaleby
- 2/(right– left)inx
- 2/(top– bottom)iny
Multiplybyscalematrix:
1
- 1
x y left right bottom top
Scale size down along x and y
- 1
1 2 2 bottom top left right
PerspectiveProjectionMatrix
glFrustum(left, right, bottom, top, N, F) N = near plane, F = far plane
- 1
2 ) ( 2 min max 2 N F FN N F N F bottom top bottom top bottom top N left right left right x x N
- 1
1 1 2 / ) ( 1 2 / ) ( 1 1 1 2 2 b a N N bottom top left right bottom top left right
Scale
Final Perspective Transform Matrix
Translate
Previous Perspective Transform Matrix
PerspectiveTransformation
Afterperspectivetransformation,viewing
frustumvolumeistransformedintocanonical viewvolume
(-1, -1, 1) (1, 1, -1) Canonical View Volume x y z
GeometricNatureofPerspective Transform
a)
Linesthrougheyemapintolinesparalleltozaxisaftertransform
b) Linesperpendiculartozaxismaptolinesperptozaxisaftertransform
NormalizationTransformation
- riginal clipping
volume
- riginal object
new clipping volume distorted object projects correctly
Implementation
Setmodelview andprojectionmatricesinapplicationprogram Passmatricestoshader
void display( ){ ..... model_view = LookAt(eye, at, up); projection = Ortho(left, right, bottom,top, near, far); // pass model_view and projection matrices to shader glUniformMatrix4fv(matrix_loc, 1, GL_TRUE, model_view); glUniformMatrix4fv(projection_loc, 1, GL_TRUE, projection);
..... }
Build 4x4 projection matrix
Implementation
Andthecorrespondingshader
in vec4 vPosition; in vec4 vColor; Out vec4 color; uniform mat4 model_view; Uniform mat4 projection; void main( ) { gl_Position = projection*model_view*vPosition; color = vColor; }
References
InteractiveComputerGraphics(6th edition),Angeland
Shreiner
ComputerGraphicsusingOpenGL(3rd edition),HillandKelley