Computer Graphics (CS 543) Lecture 5 (part 2): Projection (Part 2): Derivation Prof Emmanuel Agu
Computer Science Dept. Worcester Polytechnic Institute (WPI)
Computer Graphics (CS 543) Lecture 5 (part 2): Projection (Part 2): - - PowerPoint PPT Presentation
Computer Graphics (CS 543) Lecture 5 (part 2): Projection (Part 2): Derivation Prof Emmanuel Agu Computer Science Dept. Worcester Polytechnic Institute (WPI) Parallel Projection normalization find 4x4 matrix to transform user specified
Computer Science Dept. Worcester Polytechnic Institute (WPI)
normalization find 4x4 matrix to transform user‐specified
glOrtho(left, right, bottom, top,near, far) Canonical View Volume User‐specified View Volume
Parallel projection: 2 parts
1.
2.
Translation matrix:
1 2 / ) ( 1 2 / ) ( 1 2 / ) ( 1 near far bottom top left right
1 2 2 2 near far bottom top left right
Concatenating Translation x Scaling, we get Ortho Projection matrix
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
Set z =0 Equivalent to the homogeneous coordinate
Hence, general orthogonal projection in 4D is
x y z Perspective( ) Frustrum ( )
(0,0,0)
Projection plane Eye (COP) (x,y,z) (x’,y’,z’)
y Based on similar triangles: y’ N y -z N y’ = y x
=
VRP COP Object in 3 space Projectors Projected image
Near Plane (VOP) + z
So (x*,y*) projection of point, (x,y,z) unto near plane N is
Numerical example:
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
Classical perspective projection projects (x,y) coordinates to
But we need z to find closest object (depth testing)!!!
VRP COP Object in 3 space Projectors Projected image
(0,0,0) z
Map to sam e ( x* ,y* ) Com pare their z values?
Perspective transformation maps actual z distance of
Near Far 1
Canonical view volum e Actual view volum e
Pseudodepth Actual depth
W e w ant perspective Transform ation and NOT classical projection!! Set scaling z Pseudodepth = az + b Next solve for a and b
(-1, -1, 1) (1, 1, -1) Canonical View Volume x y z
Choose a, b so as z varies from Near to Far, pseudodepth
Boundary conditions
z* = ‐1 when z = ‐N
z* = 1 when z = ‐F
z b az z N y z N x z y x , , * *, *,
Near Far Canonical view volum e Actual view volum e
Pseudodepth Actual depth
1 Z* Z
Solving: Use boundary conditions
z* = ‐1 when z = ‐N………(1) z* = 1 when z = ‐F………..(2)
Set up simultaneous equations
z b az z * ) 1 ........( 1 b aN N N b aN ) 2 ........( 1 b aF F F b aF
Multiply both sides of (1) by ‐1 Add eqns (2) and (3) Now put (4) back into (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
Put solution for a back into eqn (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
Original point z in original view volume, transformed
where
Near Far Canonical view volum e Actual view volum e
1
Original vertex z value Transform ed vertex z* value
z b az z *
N F N F a ) (
N F FN b 2
Want to express projection transform as 4x4 matrix Previously, homogeneous coordinates of
Introduce arbitrary scaling factor, w, so that
For example, the point P = (2,4,6) can be expressed as (2,4,6,1) or (4,8,12,2) where w=2 or (6,12,18,3) where w = 3, or…. To convert from homogeneous back to ordinary coordinates,
Recall Perspective Transform We have: In matrix form:
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 Transform ed Vertex Transform ed Vertex after dividing by 4 th term
In perspective transform matrix, already solved for a
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
Not done yet!! Can now transform z!
Also need to transform the x = (left, right) and y = (bottom, top) ranges of viewing frustum to [‐1, 1]
Similar to glOrtho, we need to translate and scale previous matrix along x and y to get final projection transform matrix
we translate by
–(right + left)/2 in x
‐(top + bottom)/2 in y
Scale by:
2/(right – left) in x
2/(top – bottom) in y
1
x y left right bottom top
Translate along x and y to line up center with origin of CVV
–(right + left)/2 in x
‐(top + bottom)/2 in y
Multiply by translation matrix:
1
x y left right bottom top
1 1 2 / ) ( 1 2 / ) ( 1 bottom top left right
Line up centers Along x and y
To bring view volume size down to size of of CVV, scale by
2/(right – left) in x
2/(top – bottom) in y
Multiply by scale matrix:
1
x y left right bottom top
Scale size dow n along x and y
1 1 2 2 bottom top left right
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
(-1, -1, 1) (1, 1, -1) Canonical View Volume x y z
a)
Lines through eye map into lines parallel to z axis after transform
b) Lines perpendicular to z axis map to lines perp to z axis after transform
Set modelview and projection matrices in application program Pass matrices to shader
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
And the corresponding shader
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; }
Interactive Computer Graphics (6th edition), Angel and
Computer Graphics using OpenGL (3rd edition), Hill and Kelley