z Buffer

Embed Size (px)

Citation preview

  • 8/13/2019 z Buffer

    1/17

    Visible Surface Determination

    CS418 Computer Graphics

    John C. Hart

  • 8/13/2019 z Buffer

    2/17

    Painters Algorithm

    Display polygons inback-to-front order

    Sort polygons by z-value

    Which vertex?

    O(nlog n)

    Problems

    -z

  • 8/13/2019 z Buffer

    3/17

    Quadtree Algorithm

    Sort polygons

    Subdivide screen until each region

    contains one or zero edges

    Invented by John Warnock in 1969

  • 8/13/2019 z Buffer

    4/17

    Quadtree Algorithm

    Sort polygons

    Subdivide screen until each region

    contains one or zero edges

    Invented by John Warnock in 1969

  • 8/13/2019 z Buffer

    5/17

    Quadtree Algorithm

    Sort polygons

    Subdivide screen until each region

    contains one or zero edges

    Invented by John Warnock in 1969

  • 8/13/2019 z Buffer

    6/17

    Quadtree Algorithm

    Sort polygons

    Subdivide screen until each region

    contains one or zero edges

    Invented by John Warnock in 1969

  • 8/13/2019 z Buffer

    7/17

    Quadtree Algorithm

    Sort polygons

    Subdivide screen until each region

    contains one or zero edges

    Invented by John Warnock in 1969

  • 8/13/2019 z Buffer

    8/17

    Z-Buffer

    Key Observation: Each pixel displayscolor of only one triangle, ignores

    everything behind it

    Dont need to sort triangles, just find

    for each pixel the closest triangle Z-buffer: one fixed or floating point

    value per pixel

    Algorithm:

    For each rasterized fragment (x,y)

    Ifz> zbuffer(x,y) then

    framebuffer(x,y) = fragment color

    zbuffer(x,y) =z

    -far

    -far

    -far

    -far-far

    -far

    -far

    -far

    -far

    -far

    -far-far

    framebuffer

    zbuffer

  • 8/13/2019 z Buffer

    9/17

    Z-Buffer

    Key Observation: Each pixel displayscolor of only one triangle, ignores

    everything behind it

    Dont need to sort triangles, just find

    for each pixel the closest triangle Z-buffer: one fixed or floating point

    value per pixel

    Algorithm:

    For each rasterized fragment (x,y)

    Ifz> zbuffer(x,y) then

    framebuffer(x,y) = fragment color

    zbuffer(x,y) =z

    -far

    -.1

    -.2

    -.3-.4

    -.5

    -.6

    -.7

    -.8

    -far

    -far-far

    framebuffer

    zbuffer

  • 8/13/2019 z Buffer

    10/17

    Z-Buffer

    Key Observation: Each pixel displayscolor of only one triangle, ignores

    everything behind it

    Dont need to sort triangles, just find

    for each pixel the closest triangle Z-buffer: one fixed or floating point

    value per pixel

    Algorithm:

    For each rasterized fragment (x,y)

    Ifz> zbuffer(x,y) then

    framebuffer(x,y) = fragment color

    zbuffer(x,y) =z

    -far

    -.1

    -.2

    -.3-.4

    -.3

    -.1

    -.7

    -.8

    -far

    -far-far

    framebuffer

    zbuffer

  • 8/13/2019 z Buffer

    11/17

    Z-Buffer

    Get fragment z-values by interpolatingz-values at vertices during rasterization

    Perspective projection destroys

    z-values, setting them all tod

    Need a perspective distortion that

    preserves at least the ordering of

    z-values

    -far

    -.1

    -.2

    -.3-.4

    -.3

    -.1

    -.7

    -.8

    -far

    -far-far

    framebuffer

    zbuffer

  • 8/13/2019 z Buffer

    12/17

    Normalized View Volume

    x

    y

    z

    x

    y

    z

    W2V

    Model

    View

    Persp

    Model

    Coords

    World

    Coords

    Viewing

    Coords

    Clip

    Coords

    Screen

    Coords

    (0,0,-far)

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

    (-1,1,1)

    z

    1 -1

    x

    1

    -1

    y

    -1

    1

  • 8/13/2019 z Buffer

    13/17

    Perspective Projection

    screen

    -z

    y

    zview

    yview

    d

    yclip

    clip view

    view

    viewclip

    view/

    y y

    d z

    yy

    z d

    view

    viewviewview

    viewview view

    viewviewview

    view

    /1

    1

    /1

    /1/ 0 1

    1

    x

    z dxx

    yy y

    z dzz

    dz dd

  • 8/13/2019 z Buffer

    14/17

    Perspective Distortion

    screen

    -z

    y

    zview

    yview

    1

    yclip

    viewclip

    view

    yy

    z

    view

    viewviewview

    viewviewview

    viewviewview

    viewview

    1

    1

    1 0 1

    1

    x

    zxx

    yyyz

    zz

    zz

  • 8/13/2019 z Buffer

    15/17

    Distorted z-Values

    viewclip

    view

    yy

    z

    view

    viewviewview

    viewviewview

    viewviewview

    viewview

    1

    1

    1 0 1

    1

    x

    zxx

    yyyz

    zz

    zz

    -z

    -/z

    -

    1/-zcurve

    z1 z2

    -/z2

    -/z1

    ifz1>z2then

    -/z1> -/z2

  • 8/13/2019 z Buffer

    16/17

    Normalized Perspective Distortion

    2 near right left

    right left right left2 near top bottom

    top bottom top bottom

    far near 2 far near

    far near far near

    1 0

    x

    y

    z

    x

    y

    z(0,0,-far)

    (-1,1,1)

    z1 -1

    x

    1

    -1

    y

    -1

    1

  • 8/13/2019 z Buffer

    17/17

    Hierarchical Z-Buffer

    Invented by Ned Green in 1994

    Creates a MIP-map of the z-buffer

    z-value equal to farthest z-value of

    its four children

    Before rasterizing a triangle

    Check z-value of its nearest vertex

    against z-value of the smallest

    quadtree cell containing the triangle

    If z-test fails, then the entire

    triangle is hidden and need not be

    rasterized

    Works best when displaying

    front-to-back

    -.2

    -.3

    -.4

    -.5

    -.6

    -.7

    -.8

    -far

    framebuffe

    r

    hierarchical zbuffer

    -.3

    -.5

    -.7

    -far

    -.5

    -far

    -far