View-Perspective Projectiongraphics.ics.uci.edu/CS112/lectures/persp.pdf · View-Perspective...
Transcript of View-Perspective Projectiongraphics.ics.uci.edu/CS112/lectures/persp.pdf · View-Perspective...
Slide 1
View-Perspective Projection
Slide 2
Default OpenGL View
n Eye at Originn Image plane perpendicular to negative Zn View Up Vector coincident with Y
Z
Y
X
Eye (Normal to Image plane)
(View Up Vector)
Slide 3
View Transformationn Eye at E=(x0,y0,z0)
n Need not be (0,0,0)n LookAt (xn,yn,zn) vector defines
normal N to the image plane n Need not be the Z axis
n View Up vector V defines orientation of the view.n Need not be perpendicular to Nn Need not be the Y axis
n Transformation to default OpenGL View
T(-E).P
N
V
u’z = N/|N|u’x = u’z x (V/|V|) u’y = u’x x u’z
(0,0,0)
Slide 4
View Transformation
R(N,V).T(-E).P
nn Eye at E=(x0,y0,z0)
n Need not be (0,0,0)n LookAt (xn,yn,zn) vector defines
normal N to the image plane n Need not be the Z axis
n View Up vector V defines orientation of the view.n Need not be perpendicular to Nn Need not be the Y axis
n Transformation to default OpenGL View
Slide 5
gluLookAtn gluLookAt
n Eye coordinate (E)n Look At vector – where
normal meets the plane n Find N and n
n View Up Vector (V)n Generates this matrix
and premultiplies with modelview matrix
R(N,V).T(-E). P = PM
nn
Slide 6
Perspective Projection
Slide 7
Perspective Projection
n Eye (E) : (0, 0 ,0)n View Up Vector (V) : (0, 1, 0)n LookAt
n Normal to the Image Plane (N) : (0,0,1)n Distance to the Image Plane : n
n View Directionn Mimics eye movement after head is fixed
Slide 8
Perspective Projection
n
n
xp/x = yp/y = zp/z
xp = x yp = yz zn n
Slide 9
Perspective Projection
n
n
1 0 0 00 1 0 00 0 1 00 0 1 0
n
xyz1
~
xpyp
zp
1=
xyz
z/n
Slide 10
Perspective Projection
n
n
M(n).PM = Pp
Slide 11
View Direction
Z
Y
(0,0,zp)
(xv,yv,zp)
Slide 12
Projection Matrix
n Make the view direction coincident with negative z-axis
n Shear matrix
1 0 xv/n 00 1 yv/n 00 0 1 00 0 0 1
Sh(xv/n, yv/n) =
n
Slide 13
Projection Matrix
n xv and yv are given in terms of center of a window n Extends in x direction
from r to ln Extends in y direction
from t to b1 0 xv/n 00 1 yv/n 00 0 1 00 0 0 1
Sh(xv/n, yv/n) =
n
Slide 14
View Direction
Z
Y
(0,0,zp)
(xv,yv,zp)
r
l
t
b
Slide 15
Projection Matrix
n xv and yv are given in terms of center of a window n Extends in x direction
from r to ln Extends in y direction
from t to b1 0 r+l/2n 00 1 t+b/2n 0
0 0 1 00 0 0 1
Sh((r+l)/2n, (t+b)/2n) =
n
Slide 16
Projection Matrix
n xv and yv are given in terms of center of a window n Extends in x direction
from r to ln Extends in y direction
from t to bM(n).Sh(r+l , t+b).PM = Pp
2n 2n
n
Slide 17
Projection Matrix
n Cannot determine the size of the framebuffer since it is dependent on r, l, t, bn Normalize the window
to map [r, l] and [t,b] to [-1, +1]
n Scaling MatrixM(n). Sc( 2 , 2). Sh(r+l , t+b).PM = Pp
2n 2nr-l t-b
n
Slide 18
Projection Matrix
n With this transformationn x and y coordinates
map between -1 to +1n But z maps to n n Since we are
generating a 2D image with the image plane at depth n M(n). Sc(2 , 2 ). Sh(r+l , t+b).PM = Pp
2n 2nr-l t-b
n
Slide 19
Problem with non-unique z
n Mathematically correctn We would like to resolve occlusion using z
n Option 1: Object space – render from back to front
n Does not work for intersecting objectsn Option 2: Screen space – resolve occlusion while
rasterizationn Need to maintain proper z for triangle for screen
space z interpolationn Encode this information in the z after transformation
Slide 20
How to do this?xyz1
xpyp-n1
This is the correct perspective transform
xyz1
xpyp-z1
We would like to retain the value of z.We are only changing the value of z,
which is anyway not useful for 2D image generation using perspective projection.
Slide 21
Screen Space Interpolation
n Linear interpolation of z in screen space must give the linear interpolation of points in object space
X0+t(X1-X0)Z0+t(Z1-Z0)
= s0+t(s1-s0)
This does not hold !
Xt
Zt=
Slide 22
Screen Space Interpolation
n Linear interpolation of z in screen space must give the linear interpolation of points in object space
X0+t(X1-X0)Z0+t(Z1-Z0)
= s0+u(s1-s0)Xt
Zt=
u = Z0(1-t) +tZ1
Z1t
Slide 23
Screen Space Interpolation
n Correct interpolationn Reciprocal of Zn Interpolate in screen
spacen Take reciprocal again
1Z0
(1-u) +1Zt
=1Z1
u
Slide 24
Transforming z to 1/zxyz1
xpyp
-z1
Instead of this …
xyz1
xpyp
-1/z1
we would like to store 1/z for interpolation purposes
Slide 25
Normalizing 1/z
n Unbounded -1/zn Define far plane at distance f
n Bound -1/n and -1/f between -1 to +1n Three steps only on z coordinates
n Translate the center between -1/n and -1/f to originn T(tz) where tz = (1/n+1/f)/2
n Scale it to match -1 to +1n S(sz) where sz = 2/(1/n-1/f)
n Whole z transformn (1/z + tz)sz = 1/z(2nf/(f-n)) + (f+n)/(f-n)
Slide 26
Complete Transformation
M (n).Sc(2 , 2 )Sh(r+l , t+b).PM = Pp2n 2nr-l t-b
n M and the 1/z normalization can be combined to one matrix D(n,f)
Slide 27
Complete Transformation
n glFrustum(r, l, t, b, n, f)
D(n,f). Sc(2 , 2 ). Sh(r+l , t+b).PM = Pp2n 2nr-l t-b
n 0 0 00 n 0 00 0 f+n 2nf
0 0 1 0
D( n, f) =f-n f-n
Slide 28
gluPerspective
n Difference between gl and glu functionsn gluPerspective(vertical fov, aspect ratio,
near, far)n Calls glfrustumn Near and far pass directlyn t = n tan(v-fov/2), b = -tn r = t x aspect ratio, l =-r
Slide 29
Final DrawingTransform all vertices;Clear frame buffer;Clear depth buffer;for i=1:n triangles
for all pixels (xs,ys) in the triangle pixelz = 1/z interpolated from vertex;if (pixelz < depthbuffer[xs][ys])
framebuffer[xs][ys]= color interpolated from vertex attributes;
endif;endfor;
endfor;
Slide 30
Perspective Projection
Slide 31
Perpendicular Parallel Projection
When eye is at infinity