Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Buffers and Operations

Similar presentations


Presentation on theme: "The Buffers and Operations"— Presentation transcript:

1 The Buffers and Operations
An Interactive Introduction to OpenGL Programming The Buffers and Operations Blending Depth Test Dithering Logical Operations Scissor Stencil Alpha Fragment Framebuffer In order for a fragment to make it to the frame buffer, it has a number of testing stages and pixel combination modes to go through. The tests that a fragment must pass are: scissor test - an additional clipping test alpha test - a filtering test based on the alpha color component stencil test - a pixel mask test depth test - fragment occlusion test Each of these tests is controlled by a glEnable() capability. If a fragment passes all enabled tests, it is then blended, dithered and/or logically combined with pixels in the framebuffer. Each of these operations can be enabled and disabled.

2 Usage Part of the OpenGL philosophy is to provide tools without dictating their use. These tools can be used in many ways. Often the name (“depth buffer”) suggests the most common use, but there is nothing “wrong” with using a tool quite differently.

3 OpenGL Buffers OpenGL has 4 types of buffers:
Color buffers Depth buffer Stencil buffer Accumulation buffer Each buffer has an intended function; however, you may use the buffers in any way you wish. In order to be used, a buffer must be allocated. Do this in your glutInitDisplayMode call.

4 OpenGL Tests Related to the buffers are the OpenGL tests:
Scissor test Alpha test Depth test Stencil test A test is an expression with a boolean value that OpenGL evaluates for every fragment. If the result is true, then the test passes, and the fragment continues down the pipeline. Otherwise, the test fails, and fragment is discarded. All tests are done in the fragment-processing part of the pipeline. In order to have any effect, a test must be enabled. Do this with glEnable.

5 Buffers & Tests Associated
Remember: Allocate buffers; enable tests! Buffer Corresponding Test -- Scissor Test Color Buffers Alpha Test Depth Buffer Depth Test Stencil Buffer Stencil Test Accumulation Buffer

6 Masking Buffers Most buffers have masks associated with them. If flag is GL_FALSE, buffer writing is disabled. The mask determines whether a buffer (or part of a buffer) is ever written. For example, glColorMask(false, true, true, true); means that the R portion of the color buffer will not be changed. Note: The mask affects all commands that would change the buffer, even glClear. void glIndexMask(GLuint mask); void glColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha); void glDepthMask(GLboolean flag); void glStencilMask(GLuint mask);

7 Buffers Clearing The buffers to clear are specified by bitwise-or’ing together. For example, glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_ACCUM_BUFFER_BIT); The value to store in each pixel of a buffer is set by a separate command for each buffer: glClearColor glClearDepth glClearStencil glClearAccum

8 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu
Parameter Meaning GL_RED_BITS, Number of bits per R, G, B, or A GL_GREEN_BITS, component in the color buffers GL_BLUE_BITS, GL_ALPHA_BITS GL_DEPTH_BITS No of bits per pixel in the depth buffer GL_STENCIL_BITS No of bits per pixel in the stencil buffer GL_ACCUM_RED_BITS, No of bits per R, G, B, or A GL_ACCUM_GREEN_BITS, component in the GL_ACCUM_BLUE_BITS, accumulation buffer GL_ACCUM_ALPHA_BITS glGetIntegerv() to query your OpenGL system about per-pixel buffer storage for a particular buffer 8 by Jim X. Chen:

9 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu
Color Buffers (Framebuffers) Stereoscopic viewing has left and right color buffers for the left and right stereo images. Double-buffered systems have front & back buffers. Up to four optional, non-displayable auxiliary color buffers can also be supported. Stencil Buffer One use for the stencil buffer is to restrict drawing to certain portions of the screen, just as a cardboard stencil can be used with a can of spray paint to make fairly precise painted images. You can store an image of the windshield’s shape in the stencil buffer, and then draw the entire scene. 9 by Jim X. Chen:

10 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu
Accumulation Buffer The accumulation buffer holds RGBA color data just like the color buffers do in RGBA mode. It’s typically used for accumulating a series of images into a final, composite image. You can perform operations like scene antialiasing by supersampling an image and then averaging the samples to produce the values that are finally painted into the pixels of the color buffers. You don’t draw directly into the accumulation buffer; accumulation operations are always performed in rectangular blocks, usually transfers of data to or from a color buffer. 1010 by Jim X. Chen:

11 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu
Testing and Operating on Fragments Scissor Test If a fragment lies inside the rectangle, it passes the scissor test. void glScissor(GLint x, GLint y, GLsizei width, GLsizei height); Sets the location and size of the scissor rectangle. The parameters define the lower left corner (x, y), and the width and height of the rectangle. Pixels that lie inside the rectangle pass the scissor test. Scissoring is enabled and disabled by passing GL_SCISSOR to glEnable() and glDisable(). The scissor test is just a version of a stencil test using a rectangular region of the screen. It’s fairly easy to create a blindingly fast hardware implementation of scissoring, while a given system might be much slower at stenciling -- perhaps because the stenciling is performed in software. 1111 by Jim X. Chen:

12 The Scissor Test The scissor test is by far the simplest of the tests.
It allows you to restrict drawing to a rectangular portion of the viewport. To enable: glEnable(GL_SCISSOR_TEST); Then: glScissor(x, y, width, height); Parameters are as in the glViewport command. (x,y) is the lower-left corner of the rectangle. The scissor test passes if the pixel is within the rectangle; otherwise, it fails. The scissor test is really just a quick, simple version of stenciling. 2 Feb 2004 CS 481/681

13 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu
Alpha Test In RGBA mode, the alpha test allows you to accept or reject a fragment based on its alpha value. void glAlphaFunc(GLenum func, GLclampf ref); Sets the reference value and comparison function for the alpha test. The reference value ref is clamped to be between 0 and 1. Parameter Meaning GL_NEVER Never accept the fragment GL_ALWAYS Always accept the fragment GL_LESS Accept fragment if fragment alpha < GL_LEQUAL Accept fragment if fragment alpha <= reference alpha GL_EQUALAccept fragment if fragment alpha = GL_GEQUAL Accept fragment if fragment alpha >= GL_GREATER Accept fragment if fragment alpha > GL_NOTEQUAL Accept fragment if fragment alpha != 1313 by Jim X. Chen:

14 An Interactive Introduction to OpenGL Programming
Alpha Test Reject pixels based on their alpha value glAlphaFunc( func, value ) glEnable( GL_ALPHA_TEST ) use alpha as a mask in textures Alpha values can also be used for fragment testing. glAlphaFunc() sets a value which, if glEnable(GL_ALPHA_TEST) has been called, will test every fragment’s alpha against the value set, and if the test fails, the fragment is discarded. The functions which glAlphaFunc() can use are: GL_NEVER GL_LESS GL_EQUAL GL_LEQUAL GL_GREATER GL_NOTEQUAL GL_GEUQAL GL_ALWAYS The default is GL_ALWAYS, which always passes fragments. Alpha testing is particularly useful when combined with texture mapping with textures which have an alpha component. This allows your texture map to act as a localized pixel mask. This technique is commonly used for objects like trees or fences, where modeling the objects (and all of its holes) becomes prohibitive. CPU DL Poly. Per Vertex Raster Frag FB Pixel Texture

15 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu
Stencil Test void glStencilFunc(GLenum func, GLint ref, GLuint mask); The ref value is compared to the value in the stencil buffer using the comparison func, but the comparison applies only to those bits where the corresponding bits of the mask are 1. The function can be GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, or GL_NOTEQUAL. The stencil test is enabled and disabled by passing GL_STENCIL_TEST to glEnable() and glDisable(). void glStencilOp(GLenum fail, GLenum zfail, GLenum zpass); Specifies how the data in the stencil buffer is modified when a fragment passes or fails the stencil test. The fail, zfail, and zpass can be GL_KEEP, GL_ZERO, GL_REPLACE, GL_INCR, GL_DECR, or GL_INVERT. They correspond to keeping the current value, replacing it with zero, replacing it with the reference value, incrementing it, decrementing it, or bitwise-inverting it. 1515 by Jim X. Chen:

16 Controlling Stencil Buffer
An Interactive Introduction to OpenGL Programming Controlling Stencil Buffer glStencilFunc(func, ref, mask) compare value in buffer with ref using func only applied for bits in mask which are 1 func is one of standard comparison functions glStencilOp(fail, zfail, zpass) Allows changes in stencil buffer based on passing or failing stencil and depth tests: GL_KEEP, GL_INCR The two principal functions for using the stencil buffer are glStencilFunc()which controls how the bits in the stencil buffer are used to determine if a particular pixel in the framebuffer is writable. glStencilOp()controls how the stencil buffer values are updated, based on three tests: 1) did the pixel pass the stencil test specified with glStencilFunc() 2) did the pixel fail the depth test for that pixel. 3) did the pixel pass the depth test for that pixel. This would mean that the pixel in question would have appeared in the image.

17 An Interactive Introduction to OpenGL Programming
Creating a Mask glInitDisplayMode( …|GLUT_STENCIL|… ); glEnable( GL_STENCIL_TEST ); glClearStencil( 0x0 ); glStencilFunc( GL_ALWAYS, 0x1, 0x1 ); glStencilOp( GL_REPLACE, GL_REPLACE, GL_REPLACE ); draw mask In this example, we specify a simple stencil mask. We do this by specifying that regardless of which tests the pixel passes or fails, we replace its value in the stencil buffer with the value 0x1. This permits us to render the shape of the pixel mask we want directly into the stencil buffer (in a manner of speaking).

18 An Interactive Introduction to OpenGL Programming
Using Stencil Mask Draw objects where stencil = 1 glStencilFunc( GL_EQUAL, 0x1, 0x1 ) Draw objects where stencil != 1 glStencilFunc(GL_NOTEQUAL, 0x1, 0x1); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); After the stencil mask is specified, we can use the mask to selectively update pixels. With the first set of commands, we only update the pixels where the stencil buffer is set to 0x01 in the stencil buffer. In the second example, we set the stencil state up to render only to pixels where the stencil value is not 0x01.

19 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu
Depth Test void glDepthFunc(GLenum func); Sets the comparison function for the depth test. The value for func must be GL_NEVER, GL_ALWAYS, GL_LESS, GL_LEQUAL, GL_EQUAL, GL_GEQUAL, GL_GREATER, or GL_NOTEQUAL. An incoming fragment passes the depth test if its z value has the specified relation to the value already stored in the depth buffer. The default is GL_LESS. In this case, the z value represents the distance from the object to the viewpoint. 1919 by Jim X. Chen:

20 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu
The Accumulation Buffer Images is generated in one of the standard color buffers, and these are accumulated, one at a time, into the accumulation buffer. When the accumulation is finished, the result is copied back into a color buffer for viewing. To reduce rounding errors, the accumulation buffer may have higher precision (more bits per color) than the standard color buffers. A photographer typically creates a multiple exposure by taking several pictures of the same scene without advancing the film. If anything in the scene moves, that object appears blurred. 2020 by Jim X. Chen:

21 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu
void glAccum(GLenum op, GLfloat value); Controls the accumulation buffer. The op parameter selects the operation, and value is a number to be used in that operation. The possible operations are GL_ACCUM, GL_LOAD, GL_RETURN, GL_ADD, and GL_MULT: ·GL_ACCUM reads each pixel from the buffer currently selected for reading, multiplies the R, G, B, and alpha values by value, and adds the result to the accumulation buffer. ·GL_LOAD does the same thing, except that the values replace those in the accumulation buffer rather than being added to them. ·GL_RETURN takes values from the accumulation buffer, multiplies them by value, and places the result in the color buffer(s) enabled for writing. ·GL_ADD and GL_MULT simply add or multiply the value of each pixel in the accumulation buffer by value, and then return it to the accumulation buffer. For GL_MULT, value is clamped to be in the range [- 1.0,1.0]. For GL_ADD, no clamping occurs. 2121 by Jim X. Chen:

22 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu
Scene Antialiasing First clear the accumulation buffer and enable the front buffer for reading and writing. Then loop several times (say, n) through code that draws the image in a slightly different position, accumulating the data with glAccum(GL_ACCUM, 1.0/n); and finally calling glAccum(GL_RETURN, 1.0); To decide what n should be, you need to trade off speed (the more times you draw the scene, the longer it takes to obtain the final image) and quality (the more times you draw the scene, the smoother it gets, until you make maximum use of the accumulation buffer’s resolution). Motion Blur 2222 by Jim X. Chen:

23 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu
Suppose you want to make a motion-blurred image extending over a small interval of time. Instead of spatially jittering the images, jitter them temporally. The entire scene can be made successively dimmer by calling glAccum (GL_MULT, decayFactor); where decayFactor is a number between 0.0 & 1.0. Smaller numbers for decayFactor cause the object to appear to be moving faster. You can transfer the completed scene with the object’s current position and “vapor trail” of previous positions from the accumulation buffer to the standard color buffer with glAccum (GL_RETURN, 1.0); The image looks correct even if the items move at different speeds, or if some of them are accelerated. As before, the more jitter points you use, the better the final image. You can combine motion blur with antialiasing by jittering in both the spatial and temporal domains, but you pay for higher quality with longer rendering times. 2323 by Jim X. Chen:

24 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu
Depth of Field A photograph made with a camera is in perfect focus only for items lying on a single plane a certain distance from the film. The farther an item is from this plane, the more out of focus it is. The depth of field for a camera is a region about the plane of perfect focus where items are out of focus by a small enough amount. The accumulation buffer can be used to approximate what you would see in a photograph where items are more and more blurred as their distance from a plane of perfect focus increases. It isn’t an exact simulation of the effects produced in a camera, but the result looks similar. To achieve this result, draw the scene repeatedly using calls with different arg values to glFrustum(). Choose the arguments so that the position of the viewpoint varies slightly around its true position and so that each frustum shares a common rectangle that lies in the plane of perfect focus. 2424 by Jim X. Chen:

25 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu
Soft Shadows To accumulate soft shadows due to multiple light sources, render the shadows with one light turned on at a time, and accumulate them together. 2525 by Jim X. Chen:

26 An Interactive Introduction to OpenGL Programming
Dithering glEnable( GL_DITHER ) Dither colors for better looking results Used to simulate more available colors Dithering is a technique to trick the eye into seeing a smoother color when only a few colors are available. Newspaper’s use this trick to make images look better. OpenGL will modify a fragment’s color value with a dithering table before it is written into the framebuffer.

27 Logical Operations on Pixels
An Interactive Introduction to OpenGL Programming Logical Operations on Pixels Combine pixels using bitwise logical operations glLogicOp( mode ) Common modes GL_XOR GL_AND Logical operations allows pixels to be combined with bitwise logical operations, like logical ands, ors and nots. The fragment’s color bits are combined with the pixel’s color bits using the logical operation, and then written into the framebuffer. GL_XOR is a useful logical operation for creating “rubber banding” type techniques, where you only momentarily want to modify a pixel, and then return back to its original value. There are several OpenGL logical operation modes: GL_CLEAR GL_SET GL_COPY, GL_COPY_INVERTED GL_NOOP GL_INVERT GL_AND GL_NAND GL_OR GL_NOR GL_XOR GL_AND_INVERTED GL_AND_REVERSE GL_EQUIV GL_OR_REVERSE GL_OR_INVERTED


Download ppt "The Buffers and Operations"

Similar presentations


Ads by Google