projection (part 2): derivation created by dr. slim...

Post on 13-May-2020

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Projection (Part 2): DerivationCreated by Dr. Slim BECHIKHfor SPSU course - CS4363 Computer Graphics and MultimediaFall 2014

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!specified

View Volume

Parallel Projection: Ortho

Parallel projection: 2 parts

1. Translation: centers view volume at origin

Parallel Projection: Ortho

2. Scaling: reduces user!selected cuboid to canonical

cube (dimension 2, centered at origin)

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:

!!!!!

"

#

$$$$$

%

&

'(

'(

'(

1000

2/)(100

2/)(010

2/)(001

nearfar

bottomtop

leftright

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

02

00

002

0

0002

nearfar

bottomtop

leftright

Parallel Projection: Ortho

Concatenating Translation x Scaling, we get Ortho Projection matrix

X

!!!!!!!!

"

#

$$$$$$$$

%

&

(

(

(

1000

02

00

002

0

0002

nearfar

bottomtop

leftright

!!!!!

"

#

$$$$$

%

&

'(

'(

'(

1000

2/)(100

2/)(010

2/)(001

nearfar

bottomtop

leftright

))))))))

*

+

,,,,,,,,

-

.

('

(

('

((

((

((

1000

200

02

0

002

nearfar

nearfar

farnear

bottomtop

bottomtop

bottomtop

leftright

leftright

leftright

P = ST =

Final Ortho Projection

Set z =0

Equivalent to the homogeneous coordinate

transformation

Hence, general orthogonal projection in 4D is

))))

*

+

,,,,

-

.

1000

0000

0010

0001

Morth =

P = MorthST

Perspective Projection

Projection – map the object from 3D space to

2D screen

x

y

z

Perspective()Frustrum( )

Perspective Projection: 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

Ny’ = y x

-z

=

VRP

COP

Object in 3 space

Projectors

Projected image

Near Plane(VOP)

+ z

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?

/ 0 !!"

#$$%

&

((1

z

Ny

z

Nxyx ,**,

/ 0 )333.0,666.0(5.1

15.0,

5.1

11,**, 1!

"

#$%

& 221!!"

#$$%

&

((1

z

Ny

z

Nxyx

VRP

COP

Object in 3 space

Projectors

Projected image

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?

Perspective Transformation

Perspective transformationmaps actual z distance of

perspective view volume to range [ –1 to 1] (Pseudodepth)

for canonical view volume

-Near

-Far

-1 1

Canonical view volume

Actual view volume

Pseudodepth

Actual depthWe want perspectiveTransformation and NOT classical projection!!

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

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

Perspective Transformation using

Pseudodepth

Choose a, b so as z varies from Near to Far, pseudodepth

varies from –1 to 1 (canonical cube)

Boundary conditions

z* = !1 when z = !N

z* = 1 when z = !F

/ 0 !!"

#$$%

&

(

'

((1

z

baz

z

Ny

z

Nxzyx ,,**,*,

-Near -Far

Canonical view volume

Actual view volume

Pseudodepth

Actual depth

1 -1

Z*

Z

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

z

bazz

('

1*

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

baN'(1(

'(1(

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

baF'(1

'(1

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

)2........(baFF '(1

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

aFaNNF (1'

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

NF

NF

FN

NFa

('(

1('

1

Transformation of z: Solve for a and b

Put solution for a back into eqn (3)

So

bNF

NFNN

!

"#)(

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

NF

NFNNb

!

"#)(

NF

NF

NF

NNFNNF

NF

NFNNFNb

"

! "

!

"#2)()( 22

NF

NFa

!

")(

NF

FNb

"2

What does this mean?

Original point z in original view volume, transformed

into z* in canonical view volume

where

-Near -Far

Canonical view volume

Actual view volume

1 -1

Originalvertex z value

Transformedvertex z* value

z

bazz

!

"*

NF

NFa

!

")(

NF

FNb

"2

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

Perspective Projection Matrix

Recall Perspective Transform

We have:

In matrix form:

$$$$$$$

%

&

'''''''

(

)

!

#

$$$$$

%

&

'''''

(

)

!"

$$$$$

%

&

'''''

(

)

$$$$$

%

&

'''''

(

)

1

)(

0100

00

000

000

z

bazz

Ny

z

Nx

wz

bazw

wNy

wNx

w

wz

wy

wx

ba

N

N

* + $$%

&''(

)

!

"

z

baz

z

Ny

z

Nxzyx ,,**,*,

z

Nxx

"*z

Nyy

"*z

bazz

!

"*

PerspectiveTransform Matrix

Originalvertex

TransformedVertex

Transformed Vertex after dividing by 4th term

Perspective Projection Matrix

In perspective transform matrix, already solved for a

and b:

So, we have transform matrix to transform z values

$$$$$$$

%

&

'''''''

(

)

!

#

$$$$$

%

&

'''''

(

)

!"

$$$$$

%

&

'''''

(

)

$$$$$

%

&

'''''

(

)

1

)(

0100

00

000

000

z

bazz

Ny

z

Nx

wP

baPw

wNP

wNP

w

wP

wP

wP

ba

N

N

z

z

y

x

z

y

x

NF

NFa

!

")(

NF

FNb

"2

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

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 right

bottom

top

$$$$$

%

&

'''''

(

)

!

!

1000

0100

2/)(010

2/)(001

bottomtop

leftright

Line up centersAlong x and y

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 right

bottom

top

Scale size downalong x and y

$$$$$$$

%

&

'''''''

(

)

1000

0100

002

0

0002

bottomtop

leftright

Perspective Projection Matrix

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

$$$$$$$$

%

&

''''''''

(

)

!

!

!

0100

2)(00

02

0

00minmax

2

NF

FN

NF

NF

bottomtop

bottomtop

bottomtop

N

leftright

leftright

xx

N

$$$$$

%

&

'''''

(

)

,

$$$$$

%

&

'''''

(

)

!

!

,

$$$$$$$

%

&

'''''''

(

)

0100

00

000

000

1000

0100

2/)(010

2/)(001

1000

0100

002

0

0002

ba

N

N

bottomtop

leftright

bottomtop

leftright

Scale

Final PerspectiveTransform Matrix

Translate

PreviousPerspectiveTransformMatrix

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

Geometric Nature of Perspective

Transform

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

Normalization Transformation

original clippingvolume original object new clipping

volume

distorted object

projects correctly

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 shader

glUniformMatrix4fv(matrix_loc, 1, GL_TRUE, model_view);

glUniformMatrix4fv(projection_loc, 1, GL_TRUE, projection);

.....

}

Build 4x4 projection matrix

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;

}

References

Interactive Computer Graphics (6th edition), Angel and

Shreiner

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

top related