Cs543 15 Projection 1up

Embed Size (px)

Citation preview

  • 8/6/2019 Cs543 15 Projection 1up

    1/32

    CS 543 - Computer Graphics:

    Projection

    byRobert W. Lindeman

    [email protected](with help from Emmanuel Agu ;-)

  • 8/6/2019 Cs543 15 Projection 1up

    2/32

    R.W. Lindeman - WPI Dept. of Computer Science 2

    3D Viewing and View VolumeRecall: 3D viewing set up

  • 8/6/2019 Cs543 15 Projection 1up

    3/32

    R.W. Lindeman - WPI Dept. of Computer Science 3

    Projection TransformationView volume can have different shapes Parallel, perspective, isometric

    Different types of projection Parallel (orthographic), perspective, etc.

    Important to control Projection type: perspective or orthographic,

    etc.

    Field of view and image aspect ratio

    Near and far clipping planes

  • 8/6/2019 Cs543 15 Projection 1up

    4/32

    R.W. Lindeman - WPI Dept. of Computer Science 4

    Perspective ProjectionSimilar to real world

    Characterized by object foreshorteningObjects appear larger if they are closer to

    cameraNeed to defineCenter of projection (COP) Projection (view) plane

    ProjectionConnecting the object to the center of

    projection

    projection plane

    camera

  • 8/6/2019 Cs543 15 Projection 1up

    5/32

    R.W. Lindeman - WPI Dept. of Computer Science 5

    Why is it Called Projection?

    View plane

  • 8/6/2019 Cs543 15 Projection 1up

    6/32

    R.W. Lindeman - WPI Dept. of Computer Science 6

    Orthographic (Parallel) ProjectionNo foreshortening effectDistance from camera does not matter

    The center of projection is at infinity

    Projection calculation Just choose equal z coordinates

  • 8/6/2019 Cs543 15 Projection 1up

    7/32

    R.W. Lindeman - WPI Dept. of Computer Science 7

    Field of ViewDetermine how much of the world is

    taken into the picture

    Larger field of view = smaller object-projection size

    x

    y

    z

    y

    z

    field of view(view angle)

    center of projection

  • 8/6/2019 Cs543 15 Projection 1up

    8/32

    R.W. Lindeman - WPI Dept. of Computer Science 8

    Near and Far Clipping PlanesOnly objects between near and far planes

    are drawn

    Near plane + far plane + field of view =View Frustum

    x

    y

    z

    Near plane Far plane

  • 8/6/2019 Cs543 15 Projection 1up

    9/32

    R.W. Lindeman - WPI Dept. of Computer Science 9

    View Frustum3D counterpart of 2D-world clip window

    Objects outside the frustum are clipped

    x

    y

    z

    Near plane Far plane

    View Frustum

  • 8/6/2019 Cs543 15 Projection 1up

    10/32

    R.W. Lindeman - WPI Dept. of Computer Science 10

    Projection TransformationIn OpenGL Set the matrix mode to GL_PROJECTION

    For perspective projection, use

    gluPerspective( fovy, aspect, near, far );or

    glFrustum(left, right, bottom, top,

    near, far );

    For orthographic projection, useglOrtho( left, right, bottom, top,

    near, far );

  • 8/6/2019 Cs543 15 Projection 1up

    11/32

    R.W. Lindeman - WPI Dept. of Computer Science 11

    gluPerspective( fovy, aspect, near, far )

    Aspect ratio is used to calculatethe window width

    x

    y

    z

    y

    z

    fovy

    eye

    near farAspect = w / h

    w

    h

  • 8/6/2019 Cs543 15 Projection 1up

    12/32

    R.W. Lindeman - WPI Dept. of Computer Science 12

    glFrustum( left, right, bottom, top,

    near, far )

    Can use this function in place ofgluPerspective( )

    x

    y

    z

    left

    rightbottom

    top

    near far

  • 8/6/2019 Cs543 15 Projection 1up

    13/32

    R.W. Lindeman - WPI Dept. of Computer Science 13

    glOrtho( left, right, bottom, top,

    near, far )

    For orthographic projection

    x

    y

    z

    left

    rightbottom

    top

    nearfar

  • 8/6/2019 Cs543 15 Projection 1up

    14/32

    R.W. Lindeman - WPI Dept. of Computer Science 14

    Example: Projection

    Transformation void display( ) {

    glClear( GL_COLOR_BUFFER_BIT );glMatrixMode( GL_PROJECTION );glLoadIdentity( );gluPerspective( FovY, Aspect, Near, Far );glMatrixMode( GL_MODELVIEW );glLoadIdentity( );gluLookAt( 0, 0, 1, 0, 0, 0, 0, 1, 0 );myDisplay( ); // your display routine

    }

  • 8/6/2019 Cs543 15 Projection 1up

    15/32

    R.W. Lindeman - WPI Dept. of Computer Science 15

    Projection TransformationProjectionMap the object from 3D space to 2D screen

    x

    y

    z

    x

    y

    z

    Perspective: gluPerspective( ) Parallel: glOrtho( )

  • 8/6/2019 Cs543 15 Projection 1up

    16/32

    R.W. Lindeman - WPI Dept. of Computer Science 16

    Parallel Projection (The Math) After transforming the object to eye space,After transforming the object to eye space,

    parallel projection is relatively easy: we couldparallel projection is relatively easy: we could

    just set all Z to the same valuejust set all Z to the same value X

    p= x

    Yp = y

    Zp = -d

    We actually want to remember Z

    why?

    x

    y

    z(x,y,z)

    (Xp, Yp)

  • 8/6/2019 Cs543 15 Projection 1up

    17/32

    R.W. Lindeman - WPI Dept. of Computer Science 17

    Parallel ProjectionOpenGL maps (projects) everything in

    the visible volume into a canonical viewvolume (CVV)

    (-1, -1, 1)

    (1, 1, -1)

    Canonical View VolumeglOrtho( xmin, xmax, ymin,ymax, near, far )

    (xmin, ymin, near)

    (xmax, ymax, far)

    Projection: Need to build 4x4 matrix to domapping from actual view volume to CVV

  • 8/6/2019 Cs543 15 Projection 1up

    18/32

    R.W. Lindeman - WPI Dept. of Computer Science 18

    Parallel Projection: glOrthoParallel projection can be broken down

    into two parts Translation, which centers view volume at

    origin Scaling, which reduces cuboid of arbitrary

    dimensions to canonical cubeDimension 2, centered at origin

  • 8/6/2019 Cs543 15 Projection 1up

    19/32

    R.W. Lindeman - WPI Dept. of Computer Science 19

    Parallel Projection: glOrtho

    (cont.) Translation sequence moves midpoint of view

    volume to coincide with origin e.g., midpoint of x = (xmax + xmin)/2

    Thus, translation factors are-(xmax+xmin)/2, -(ymax+ymin)/2, -(far+near)/2

    So, translation matrix M1:

    1 0 0 "(xmax+ xmin)/2

    0 1 0 "(y max+ y min)/20 0 1 "(zmax+ zmin)/2

    0 0 0 1

    #

    $

    %

    %%%

    &

    '

    (

    (((

  • 8/6/2019 Cs543 15 Projection 1up

    20/32

    R.W. Lindeman - WPI Dept. of Computer Science 20

    Parallel Projection: glOrtho

    (cont.)Scaling factor is ratio of cube dimension

    to Ortho view volume dimension

    Scaling factors

    2/(xmax-xmin), 2/(ymax-ymin), 2/(zmax-zmin)

    So, scaling matrix M2:

    2

    x max" x min0 0 0

    0

    2

    y max" y min 0 0

    0 02

    zmax" zmin0

    0 0 0 1

    #

    $

    %%%%%%%

    &

    '

    (((((((

  • 8/6/2019 Cs543 15 Projection 1up

    21/32

    R.W. Lindeman - WPI Dept. of Computer Science 21

    Parallel Projection: glOrtho()

    (cont.)Concatenating M1xM2, we get transform

    matrix used by glOrtho

    M2 " M1=

    2 /(xmax# xmin) 0 0 #(xmax+ xmin)/(xmax# xmin)

    0 2 /(ymax# ymin) 0 #(ymax+ ymin)/(ymax# ymin)0 0 2 /(zmax# zmin) #(zmax+ zmin)/(zmax# zmin)

    0 0 0 1

    $

    %

    &&&&

    '

    (

    ))))

    Refer to: Hill, 7.6.2

    2

    x max" x min0 0 0

    0 2y max" y min

    0 0

    0 02

    zmax" zmin0

    0 0 0 1

    #

    $

    %%%%%%%

    &

    '

    (((((((

    X

    1 0 0 "(xmax+ xmin)/2

    0 1 0 "(y max+ y min)/20 0 1 "(zmax+ zmin)/2

    0 0 0 1

    #

    $

    %

    %%%

    &

    '

    (

    (((

  • 8/6/2019 Cs543 15 Projection 1up

    22/32

    R.W. Lindeman - WPI Dept. of Computer Science 22

    Perspective Projection: ClassicalSide view

    x

    y

    z

    (0,0,0)

    d

    Projection plane

    Eye (center of projection )

    (x,y,z)

    (x,y,z)

    -z

    z

    y

    Based on similar triangles:

    y -zy d

    dy = y *

    -z

    =

  • 8/6/2019 Cs543 15 Projection 1up

    23/32

    R.W. Lindeman - WPI Dept. of Computer Science 23

    Perspective Projection:

    Classical (cont.) So (x*, y*), the projection of point, (x, y, z)

    onto the near plane N, is given as

    Similar triangles

    Numerical example

    Q: Where on the viewplane does P = (1, 0.5, -1.5) lie for a near plane at N = 1?

    (x*, y*) = (1 x 1/1.5, 1 x 0.5/1.5) = (0.666, 0.333)

    x*,y *

    ( )= N

    Px

    "Pz,N

    Py

    "Pz

    #

    $%

    &

    '(

  • 8/6/2019 Cs543 15 Projection 1up

    24/32

    R.W. Lindeman - WPI Dept. of Computer Science 24

    Pseudo Depth Checking Classical perspective projection drops z coordinates

    But we need z to find closest object (depth testing)

    Keeping actual distance of P from eye is

    cumbersome and slow

    Introduce pseudodepth: all we need is a measureof which objects are further if two points project tothe same (x, y)

    Choose a, b so that pseudodepth varies from 1 to1 (canonical cube)

    distance = Px2+ Py

    2+ Pz

    2

    ( )

    x*,y*,z*( ) = N Px"Pz

    ,N Py"Pz

    , aPz + b"Pz

    #

    $% &

    '(

  • 8/6/2019 Cs543 15 Projection 1up

    25/32

    R.W. Lindeman - WPI Dept. of Computer Science 25

    Pseudo Depth Checking (cont.)Solving:

    For two conditions, z* = -1 when Pz = -Nand z* = 1 when Pz = -F, we can set uptwo simultaneous equations

    Solving for a and b, we get

    z*=aP

    z+ b

    "Pz

    a ="(F+ N)

    F" N

    b ="2FN

    F" N

  • 8/6/2019 Cs543 15 Projection 1up

    26/32

    R.W. Lindeman - WPI Dept. of Computer Science 26

    Homogenous Coordinates Would like to express projection as 4x4 transformmatrix

    Previously, homogeneous coordinates for the point P =(Px, Py, Pz) was (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 expressedas (2, 4, 6, 1) or (4, 8, 12, 2) where w=2 or (6, 12, 18, 3) where w = 3

    So, to convert from homogeneous back to ordinarycoordinates, divide all four terms by last component anddiscard 4th term

  • 8/6/2019 Cs543 15 Projection 1up

    27/32

    R.W. Lindeman - WPI Dept. of Computer Science 27

    Perspective ProjectionSame for x, so we have

    x = x * d / -zy = y * d / -z

    z = -d

    Put in a matrix form

    OpenGL assumes d = 1, i.e., the image plane is at z = -1

    1 0 0 0

    0 1 0 0

    0 0 1 0

    0 0 1"d( ) 1

    #

    $

    %%

    %%

    &

    '

    ((

    ((

    x

    y

    z

    1

    #

    $

    %%%%

    &

    '

    ((((

    =

    x'

    y'

    z'

    w

    #

    $

    %%%%

    &

    '

    (((()

    "d xz( )

    "d y

    z

    #

    $% &

    '(

    "d

    1

    #

    $

    %%

    %%%

    &

    '

    ((

    (((

  • 8/6/2019 Cs543 15 Projection 1up

    28/32

    R.W. Lindeman - WPI Dept. of Computer Science 28

    Perspective Projection (cont.)We are not done yet!

    Need to modify the projection matrix to

    include a and b

    x 1 0 0 0 x

    y = 0 1 0 0 y

    z 0 0 a b z

    w 0 0 (1/-d) 0 1

    We have already solved a and b

    x

    y

    z

    Z = 1 z = -1

  • 8/6/2019 Cs543 15 Projection 1up

    29/32

  • 8/6/2019 Cs543 15 Projection 1up

    30/32

    R.W. Lindeman - WPI Dept. of Computer Science 30

    Perspective Projection (cont.)Final projection matrix

    glFrustum( xmin, xmax, ymin, ymax, N, F )

    N = near plane, F = far plane

    2N

    xmax" xmin0

    xmax+ xmin

    xmax" xmin0

    02N

    ymax" ymin

    ymax+ ymin

    ymax" ymin0

    0 0"(F+ N)

    F"N

    "2FN

    F"N0 0 "1 0

    #

    $

    %%%%%%%

    &

    '

    (((((((

  • 8/6/2019 Cs543 15 Projection 1up

    31/32

    R.W. Lindeman - WPI Dept. of Computer Science 31

    Perspective Projection (cont.)After perspective projection, viewing

    frustum is also projected into a canonicalview volume (like in parallel projection)

    (-1, -1, 1)

    (1, 1, -1)

    Canonical View Volume

    x

    y

    z

  • 8/6/2019 Cs543 15 Projection 1up

    32/32

    R.W. Lindeman - WPI Dept. of Computer Science 32

    ReferencesHill, Chapter 7