computer graphics (cs 543) 5 (part projection (part...

32
Computer Graphics (CS 543) Lecture 5 (part 2): Projection (Part 2): Derivation Prof Emmanuel Agu Computer Science Dept. Worcester Polytechnic Institute (WPI)

Upload: ngokhue

Post on 13-Sep-2018

236 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Computer Graphics (CS 543) Lecture 5 (part 2): Projection (Part 2): 

Derivation

Prof Emmanuel Agu

Computer Science Dept.Worcester Polytechnic Institute (WPI)

Page 2: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Parallel Projection  normalization find 4x4 matrix to transform user‐specified 

view volume to canonical view volume (cube)

glOrtho(left, right, bottom, top,near, far)

Canonical View Volume

User‐specifiedView Volume

Page 3: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Parallel Projection: Ortho

Parallel projection: 2 parts1. Translation: centers view volume at origin

Page 4: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Parallel Projection: Ortho

2. Scaling: reduces user‐selected cuboid to canonical cube (dimension 2, centered at origin)

Page 5: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Parallel Projection: Ortho

Translation lines up midpoints:  E.g. midpoint of x = (right + left)/2

Thus translation factors:‐(right + left)/2,   ‐(top + bottom)/2,    ‐(far+near)/2 

Translation matrix:

10002/)(1002/)(010

2/)(001

nearfarbottomtop

leftright

Page 6: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Parallel Projection: Ortho

Scaling factor: ratio of ortho view volume to cube dimensions Scaling factors:  2/(right ‐ left),   2/(top ‐ bottom),   2/(far ‐ near) Scaling Matrix M2:

1000

0200

0020

0002

nearfar

bottomtop

leftright

Page 7: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Parallel Projection: Ortho

Concatenating Translation x Scaling, we get Ortho Projection matrix

X

1000

0200

0020

0002

nearfar

bottomtop

leftright

10002/)(1002/)(010

2/)(001

nearfarbottomtop

leftright

1000

200

020

002

nearfarnearfar

farnear

bottomtopbottomtop

bottomtop

leftrightleftright

leftright

P = ST =

Page 8: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Final Ortho Projection

Set z =0 Equivalent to the homogeneous coordinate transformation

Hence, general orthogonal projection in 4D is

1000000000100001

Morth =

P = MorthST

Page 9: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Perspective Projection

Projection – map the object  from 3D space to 2D screen 

x

y

z

Perspective()Frustrum( )

Page 10: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Perspective Projection: Classical 

(0,0,0)

-N

Projection plane

Eye (COP)

(x,y,z)

(x’,y’,z’)

-z

- z

yBased on similar triangles:

y’ N y -z

Ny’ = y x

-z

=

VRP

COP

Object in 3 space

Projectors

Projected image

Near Plane(VOP)

+ z

Page 11: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Perspective Projection: Classical

So (x*,y*) projection of point, (x,y,z) unto near plane N is given as:

Numerical example:Q. Where on the viewplane does P = (1, 0.5, ‐1.5) lie for a 

near plane at N = 1?

zNy

zNxyx ,**,

)333.0,666.0(5.1

15.0,5.1

11,**,

zNy

zNxyx

VRP

COP

Object in 3 space

Projectors

Projected image

Page 12: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Pseudodepth

Classical perspective projection projects (x,y) coordinates to (x*, y*), drops z coordinates

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 same (x*,y*)Compare their z values?

Page 13: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Perspective Transformation

Perspective transformationmaps actual z distance of perspective view volume to range [ –1 to 1] (Pseudodepth) for canonical view volume

NearFar

1 -1

Canonical view volume

Actual view volume

Pseudodepth

Actual depth We want perspectiveTransformation and NOT classical projection!!

Set scaling zPseudodepth = az + bNext solve for a and b

Page 14: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Perspective Transformation

We want to transform viewing frustum volume into canonical view volume

(-1, -1, 1)

(1, 1, -1)

Canonical View Volume

x

y

z

Page 15: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Perspective Transformation using Pseudodepth

Choose a, b so as z varies from Near to Far, pseudodepthvaries from –1 to 1 (canonical cube)

Boundary conditions z* = ‐1 when z = ‐N  z* = 1 when z = ‐F

zbaz

zNy

zN

xzyx ,,**,*,

Near Far

Canonical view volume

Actual view volume

Pseudodepth

Actual depth

-1 1

Z*

Z

Page 16: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Transformation of z: Solve for a and b

Solving:

Use boundary conditions z* = ‐1 when z = ‐N………(1)  z* = 1 when z = ‐F………..(2)

Set up simultaneous equations

zbazz

*

)1........(1 baNNN

baN

)2........(1 baFFF

baF

Page 17: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Transformation of z: Solve for a and b

Multiply both sides of (1) by ‐1 

Add eqns (2) and (3)

Now put (4) back into (3)

)1........(baNN

)2........(baFF

)3........(baNN

aFaNNF

)4.........()(NFNF

FNNFa

Page 18: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Transformation of z: Solve for a and b

Put solution for a back into eqn (3)

So 

bNF

NFNN

)(

)3........(baNN

NFNFNNb

)(

NFNF

NFNNFNNF

NFNFNNFNb

2)()( 22

NFNFa

)(

NFFNb

2

Page 19: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

What does this mean?

Original point z in original view volume, transformed into z* in canonical view volume

whereNear Far

Canonical view volume

Actual view volume

-1 1

Original vertex z value

Transformed vertex z* value

zbazz

*

NFNFa

)(

NFFNb

2

Page 20: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Homogenous Coordinates

Want to express projection transform as 4x4 matrix Previously, homogeneous coordinates of 

P = (Px,Py,Pz)  =>  (Px,Py,Pz,1) Introduce arbitrary scaling factor, w, so that 

P = (wPx, wPy, wPz, w)     (Note: w is non‐zero) 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, first divide all four terms by w and discard 4th term

Page 21: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Perspective Projection Matrix

Recall Perspective Transform

We have: 

In matrix form:

1

)(0100

00000000

zbazz

Nyz

Nx

wzbazw

wNywNx

wwzwywx

baN

N

zbaz

zNy

zN

xzyx ,,**,*,

zNxx

*z

Nyy

*zbazz

*

Perspective Transform Matrix

Original vertex

TransformedVertex

Transformed Vertex after dividing by 4th term

Page 22: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Perspective Projection Matrix

In perspective transform matrix, already solved for a and b:

So, we have transform matrix to transform z values

1

)(0100

00000000

zbazz

Nyz

Nx

wPbaPw

wNPwNP

wwPwPwP

baN

N

z

z

y

x

z

y

x

NFNFa

)(

NFFNb

2

Page 23: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Perspective Projection

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 -1

x

y

left rightbottom

top

Page 24: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Perspective Projection

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 -1

x

y

left rightbottom

top

10000100

2/)(0102/)(001

bottomtopleftright

Line up centersAlong x and y

Page 25: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Perspective Projection

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 -1

x

y

left rightbottom

top

Scale size downalong x and y

10000100

0020

0002

bottomtop

leftright

Page 26: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Perspective Projection Matrix

glFrustum(left, right, bottom, top, N, F) N = near plane, F = far plane

0100

2)(00

020

00minmax

2

NFFN

NFNF

bottomtopbottomtop

bottomtopN

leftrightleftright

xxN

010000

000000

10000100

2/)(0102/)(001

10000100

0020

0002

baN

Nbottomtop

leftright

bottomtop

leftright

Scale

Final PerspectiveTransform Matrix

Translate

Previous PerspectiveTransform Matrix

Page 27: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Perspective Transformation

After perspective transformation, viewing frustum volume is transformed into canonical view volume

(-1, -1, 1)

(1, 1, -1)

Canonical View Volume

x

y

z

Page 28: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Geometric Nature  of Perspective Transform

a) Lines through eye map into lines parallel to z axis after transformb) Lines perpendicular to z axis map to lines perp to z axis after transform

Page 29: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Normalization Transformation

original clippingvolume original object new clipping

volume

distorted objectprojects correctly

Page 30: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Implementation

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 shaderglUniformMatrix4fv(matrix_loc, 1, GL_TRUE, model_view);glUniformMatrix4fv(projection_loc, 1, GL_TRUE, projection);

.....}

Build 4x4 projection matrix

Page 31: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

Implementation

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;

}

Page 32: Computer Graphics (CS 543) 5 (part Projection (Part …emmanuel/courses/cs543/f13/slides/lecture... · Parallel Projection: Ortho Parallel projection: 2 parts 1. Translation:centers

References Interactive Computer Graphics (6th edition), Angel and 

Shreiner Computer Graphics using OpenGL (3rd edition), Hill and Kelley