OpenGL Buffers - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

OpenGL Buffers

Description:

Motion blur: Render multiple frames representing samples in time, and add them together ... Motion blur. CS 536. Xiaoyu Zhang, CSUSM. Summary. OpenGL Buffers ... – PowerPoint PPT presentation

Number of Views:177
Avg rating:3.0/5.0
Slides: 23
Provided by: Xia575
Category:
Tags: opengl | blur | buffers

less

Transcript and Presenter's Notes

Title: OpenGL Buffers


1
OpenGL Buffers
2
OpenGL Buffers
  • Buffers storages for pixel information
  • Color buffers Store RGBA color information for
    each pixel
  • OpenGL actually may have four or more color
    buffers front/back (double buffering),
    left/right (stereo) and auxiliary color buffers
  • Depth buffer Stores depth information for each
    pixel
  • Stencil buffer Stores some number of bits for
    each pixel
  • Accumulation buffer Like a color buffer, but
    with higher resolution and different operations

3
Fragment Tests
  • A fragment is a pixel-sized piece of data, with
    color and depth information
  • After shading and/or texturing
  • The tests and operations performed with the
    fragment on its way to the color buffer are
    essential to understanding multi-pass techniques
  • Most important are, in order
  • Alpha test
  • Stencil test
  • Depth test
  • Blending
  • Tests must be explicitly enabled
  • glEnable(GL_DEPTH_TEST)
  • glEnable(GL_ALPHA_TEST)

4
Depth Test Review
  • We are familiar with depth test
  • Depth test compares the depth of the fragment and
    the depth in the buffer
  • Depth increases with greater distance from viewer
  • glEnable(GL_DEPTH_TEST)
  • Tests are Always, Never, lt, lt, , !, gt, gt
  • Default glDepthFunc(GL_LESS)
  • Depth operation is to write the fragments depth
    to the buffer, or to leave the buffer unchanged
  • glDepthMask(GL_FALSE)
  • Why do the test but leave the buffer unchanged?

5
Alpha Test
  • The alpha test either allows to pass or discard a
    fragment, depending on the outcome of a test on
    the alpha value
  • if ( ?fragment op ?reference ) pass
    fragment on
  • Here, ?fragment is the fragments alpha value,
    and ?reference is a reference alpha value that
    you specify
  • op is one of lt, lt, , !, gt, gt
  • There are also the special tests Always and
    Never
  • Always let the fragment through or never let it
    through
  • Example
  • glEnable(GL_ALPHA_TEST)
  • glAlphaFunc(GL_GREATER, 0)

6
Billboards
  • Billboards are texture-mapped polygons, typically
    used for fast rendering complex things like trees
  • Image-based rendering method where complex
    geometry (the tree) is replaced with an image
    placed in the scene (the textured polygon)
  • The texture has alpha values associated with it
    1 where the tree is, and 0 where it isnt
  • Use alpha test to discard pixels with alpha 0
  • So you can see through the polygon in places
    where the tree isnt

7
Billboard Problem
  • The polygon looks flat when the camera rotates to
    its side.
  • Solution?
  • Keep the polygon always face perpendicular to the
    viewing direction of the camera.
  • How?
  • Use the viewing matrix to find the up and right
    vectors of the projection plane and use those two
    vectors to determine the billboard plane

8
Billboarding
  • What vectors in the world coordinate system are
    correspondent to up and right vector in the eye
    space?
  • Get the current viewing matrix
  • glGetFloatv(GL_MODEL_VIEW_MATRIX, M)

9
Cacti Example
  • The four corners of a quad with a given center
    will be
  • Center up h right w
  • Center up h - right w
  • Center - up h right w
  • Center - up h - right w
  • Load the texture as RGBA format
  • Use alpha test
  • glEnable(GL_ALPHA_TEST)
  • glAlphaFunc(GL_GREATER,0)

10
Stencil Buffer and Test
  • The stencil buffer acts like a paint stencil - it
    lets some fragments through but not others
  • All tests/operation look at the value in the
    stencil that corresponds to the pixel location of
    the fragment
  • It stores multi-bit values you have some
    control of bits
  • You specify two things
  • The test that controls which fragments get
    through
  • The operations to perform on the buffer when the
    test passes or fails
  • Typical usage Multi-pass rendering
  • One rendering pass sets values in the stencil,
    which control how various parts of the screen are
    drawn in the second pass
  • e.g. reflection, shadowing

11
Set up stencil buffer
  • It is easy to set up a stencil buffer in glut
  • glutInitDisplayMode (GLUT_DOUBLE GLUT_RGBA
    GLUT_DEPTH GLUT_STENCIL)
  • Set up a stencil buffer in SDL
  • SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 16)

12
Stencil Test
  • You give a test operation, a reference value, and
    a mask
  • The mask is used to select particular bit-planes
    for the test
  • if ( (reference mask ) op ( buffer mask ))
  • pass fragment on
  • Operations
  • Always / Never let the fragment through
  • Logical operations between the reference value
    and the value in the buffer lt, lt, , !, gt, gt
  • glEnable(GL_STENSIL_TEST)
  • glStencilFunc(op, ref, mask)
  • Example
  • glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF)

13
Stencil Operations
  • The operations to perform on the stencil buffer
    when the test passes or fails
  • Specify operations for three different conditions
  • If the stencil test fails (fail)
  • If the stencil passes but the depth test fails
    (zfail)
  • If the stencil passes and the depth test passes
    (zpass)
  • glStencilOp(fail, zfail, zpass)
  • GL_KEEP Keep the current stencil value
  • GL_ZERO Zero the stencil
  • GL_REPLACE Replace the stencil with the
    reference value
  • GL_INCR Increment the stencil
  • GL_DECR Decrement the stencil
  • GL_INVERT Invert the stencil (bitwise)

14
Reflections
  • How to render the object casting reflection on a
    plane?
  • Draw the plane to the stencil buffer
  • Use stencil buffer to limit the reflection on the
    finite plane
  • reflect the object about the plane and render the
    reflected object.
  • Need to reflect light with the object.
  • Draw the plane
  • Blended with the reflected object
  • Need to draw the object behind the plane. Mask
    the depth buffer when the plane is drawn
  • Render the real object

15
Example Code
  • Remember to select stencil buffer in the pixel
    format
  • // render the floor to the stencil buffer
  • glColorMask(GL_FALSE, GL_FALSE, GL_FALSE,
    GL_FALSE)
  • glDepthMask(GL_FALSE)
  • glEnable(GL_STENCIL_TEST)
  • glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF)
  • glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE)
  • DrawFloor()
  • // enable color and depth buffers
  • glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE)
  • glDepthMask(GL_TRUE)
  • glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF)
  • glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP)

16
Example Code (cont.)
  • // draw reflected object
  • glPushMatrix()
  • glScalef(1.0, -1.0, 1.0)
  • positionLights()
  • DrawObject()
  • glPopMatrix()
  • // Draw the floor to the color buffer
  • glDisable(GL_STENCIL_TEST)
  • positionLights()
  • glEnable(GL_BLEND)
  • glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  • DrawFloor()
  • glDisable(GL_BLEND)
  • // Draw the real object
  • DrawObject()

17
Accumulation Buffer
  • The accumulation buffer is to accumulate pixels
    of several rendering passes
  • It is like a place to hold and compute on pixel
    data
  • Its used to create high-quality rendering
  • It trades speed for quality
  • It may be too slow for games
  • lots of copying data to and from
  • How to use accumulation buffer
  • Select accumulation buffer in the pixel format
  • glClear(GL_ACCUM_BUFFER_BIT) to clear the
    accumulation buffer
  • glAccum(op, value) to operates on the
    accumulation buffer
  • op accumulation operations
  • Value a floating point number

18
Accumulation function
19
Accumulation Buffer Applications
  • Anti-aliasing Render multiple frames with the
    image plane jittered slightly, and add them
    together
  • Hardware now does this for you, but this would be
    higher quality
  • Motion blur Render multiple frames representing
    samples in time, and add them together
  • Depth of field Render multiple frames moving
    both the viewpoint and the image plane in concert
  • Keep a point the focal point fixed in the
    image plane while things in front and behind
    appear to shift

20
Accumulation example
  • include ltGL/glut.hgt
  • void GL_display()
  • // clear the buffer
  • glClear(GL_ACCUM_BUFFER_BIT)
  • for(int i 0 i lt 360 i)
  • glClear(GL_COLOR_BUFFER_BIT)
  • glColor3f(1.0, 1 - (float)i/180, 1 - (float)i
    / 360)
  • glPushMatrix()
  • glRotatef(i, 0, 1, 0)
  • glutWireTeapot(5)
  • glPopMatrix()
  • glAccum(GL_ACCUM, 0.01)
  • glAccum(GL_RETURN, 1.0)
  • glutSwapBuffers()

21
Accumulation example
Motion blur
22
Summary
  • OpenGL Buffers
  • Color, depth, stencil, accumulation
  • OpenGL fragment tests (in order)
  • Alpha, stencil, depth
Write a Comment
User Comments (0)
About PowerShow.com