Projection Matrices - University of New Mexicoangel/CS433/LECTURES/CS433_17.pdf · •Derive the...

Preview:

Citation preview

1Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Projection Matrices

Ed AngelProfessor of Computer Science,

Electrical and ComputerEngineering, and Media Arts

University of New Mexico

2Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Objectives

•Derive the projection matrices used forstandard OpenGL projections

• Introduce oblique projections• Introduce projection normalization

3Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Normalization

•Rather than derive a different projectionmatrix for each type of projection, we canconvert all projections to orthogonalprojections with the default view volume

•This strategy allows us to use standardtransformations in the pipeline and makesfor efficient clipping

4Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Pipeline View

modelviewtransformation

projectiontransformation

perspective division

clipping projection

nonsingular4D → 3D

against default cube3D → 2D

Hidden surface removal

5Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Notes

•We stay in four-dimensional homogeneouscoordinates through both the modelview andprojection transformations

- Both these transformations are nonsingular- Default to identity matrices (orthogonal view)

•Normalization lets us clip against simplecube regardless of type of projection

•Delay final projection until end- Important for hidden-surface removal to retain

depth information as long as possible

6Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Orthogonal Normalization

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

normalization ⇒ find transformation to convertspecified clipping volume to default

7Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Orthogonal Matrix

• Two steps- Move center to origin

T(-(left+right)/2, -(bottom+top)/2,(near+far)/2))- Scale to have sides of length 2

S(2/(left-right),2/(top-bottom),2/(near-far))

!!!!!!!!

"

#

$$$$$$$$

%

&

'

+

'

'

+'

'

'

''

'

1000

200

02

0

002

nearfar

nearfar

farnear

bottomtop

bottomtop

bottomtop

leftright

leftright

leftright

P = ST =

8Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Final Projection

• Set z =0• Equivalent to the homogeneous coordinatetransformation

• Hence, general orthogonal projection in 4D is

!!!!

"

#

$$$$

%

&

1000

0000

0010

0001

Morth =

P = MorthST

9Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Oblique Projections

•The OpenGL projection functions cannotproduce general parallel projections such as

•However if we look at the example of thecube it appears that the cube has beensheared

•Oblique Projection = Shear + OrthogonalProjection

10Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

General Shear

top viewside view

11Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Shear Matrix

xy shear (z values unchanged)

Projection matrix

General case:!

1 0 "cot# 0

0 1 "cot$ 0

0 0 1 0

0 0 0 1

%

&

' ' ' '

(

)

* * * *

H(θ,φ) =

P = Morth H(θ,φ)

P = Morth STH(θ,φ)

12Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Equivalency

13Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Effect on Clipping

•The projection matrix P = STH transformsthe original clipping volume to the defaultclipping volume

top view

DOP DOP

near plane

far plane

object

clippingvolume

z = -1

z = 1

x = -1x = 1

distorted object(projects correctly)

14Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Simple Perspective

Consider a simple perspective with the COP at theorigin, the near clipping plane at z = -1, and a 90degree field of view determined by the planes

x = ±z, y = ±z

15Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Perspective Matrices

Simple projection matrix in homogeneouscoordinates

Note that this matrix is independent of thefar clipping plane!

1 0 0 0

0 1 0 0

0 0 1 0

0 0 "1 0

#

$

% % % %

&

'

( ( ( (

M =

16Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Generalization

!

1 0 0 0

0 1 0 0

0 0 " #

0 0 $1 0

%

&

' ' ' '

(

)

* * * *

N =

after perspective division, the point (x, y, z, 1) goes to

x’’ = x/zy’’ = y/zZ’’ = -(α+β/z)

which projects orthogonally to the desired point regardless of α and β

17Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Picking α and β

If we pick

α =

β =

nearfar

farnear

!

+

farnear

farnear2

!

"

the near plane is mapped to z = -1the far plane is mapped to z =1and the sides are mapped to x = ± 1, y = ± 1

Hence the new clipping volume is the default clipping volume

18Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

NormalizationTransformation

original clipping volume original object new clipping

volume

distorted objectprojects correctly

19Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Normalization andHidden-Surface Removal

• Although our selection of the form of theperspective matrices may appear somewhatarbitrary, it was chosen so that if z1 > z2 in theoriginal clipping volume then the for thetransformed points z1’ > z2’

• Thus hidden surface removal works if we firstapply the normalization transformation

• However, the formula z’’ = -(α+β/z) implies that thedistances are distorted by the normalizationwhich can cause numerical problems especially ifthe near distance is small

20Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

OpenGL Perspective

•glFrustum allows for an unsymmetricviewing frustum (although gluPerspectivedoes not)

21Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

OpenGL Perspective Matrix

•The normalization in glFrustum requiresan initial shear to form a right viewingpyramid, followed by a scaling to get thenormalized perspective volume. Finally,the perspective matrix results in needingonly a final orthogonal transformation

P = NSH

our previously defined perspective matrix

shear and scale

22Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Why do we do it this way?

•Normalization allows for a single pipelinefor both perspective and orthogonalviewing

•We stay in four dimensionalhomogeneous coordinates as long aspossible to retain three-dimensionalinformation needed for hidden-surfaceremoval and shading

•We simplify clipping

Recommended