The Buffers and Operations

Slides:



Advertisements
Similar presentations
Lecture 8 Transparency, Mirroring
Advertisements

Compositing and Blending Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics.
Technische Universität München Computer Graphics SS 2014 Graphics Effects Rüdiger Westermann Lehrstuhl für Computer Graphik und Visualisierung.
CS 352: Computer Graphics Chapter 7: The Rendering Pipeline.
OpenGL Buffers and Tests Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Friday, February 8, 2002.
Introduction to OpenGL (Part 4)
Framebuffer in OpenGL MAE152 Computer Graphics for Scientists and Engineers Fall 03.
OpenGL Fragment Operations
More HSR, OpenGL Buffers & Tests Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Monday, February 2, 2004.
Understanding the graphics pipeline Lecture 2 Original Slides by: Suresh Venkatasubramanian Updates by Joseph Kider.
Graphics Pipeline.
1 Buffers and Processing Fragments 2011 Autumn Animação e Visualização Tridimensional 2011/2012.
Computer Graphics(Fall 2003) COMS 4160, Lecture 7: OpenGL 3 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
OPENGL Return of the Survival Guide. Buffers (0,0) OpenGL holds the buffers in a coordinate system such that the origin is the lower left corner.
CS 4363/6353 BASIC RENDERING. THE GRAPHICS PIPELINE OVERVIEW Vertex Processing Coordinate transformations Compute color for each vertex Clipping and Primitive.
University of Sulaimani - School of Science - Computer Dept.
Computer Graphic Creator: Mohsen Asghari Session 2 Fall 2014.
CSL 859: Advanced Computer Graphics Dept of Computer Sc. & Engg. IIT Delhi.
Week 7 - Monday.  What did we talk about last time?  Specular shading  Aliasing and antialiasing.
Week 10 - Monday.  What did we talk about last time?  Global illumination  Shadows  Projection shadows  Soft shadows.
Compositing and Blending - Chapter 8 modified by Ray Wisman Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Chapter 6. More on Color and Material Presented by Garrett Yeh.
Raster Displays Images are composed of arrays of pixels displayed on a raster device. Two main ways to create images: –Scan and digitize an existing image.
Computer Graphics Shadows
09/18/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Bump Mapping Multi-pass algorithms.
9/20/2001CS 638, Fall 2001 Today Finishing Up Reflections More Multi-Pass Algorithms Shadows.
Jittering, Introduction to Stenciling Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Friday, February 6, 2004.
Shadows Computer Graphics. Shadows Shadows Extended light sources produce penumbras In real-time, we only use point light sources –Extended light sources.
Erdem Alpay Ala Nawaiseh. Why Shadows? Real world has shadows More control of the game’s feel  dramatic effects  spooky effects Without shadows the.
Computer Graphics Mirror and Shadows
Picking. What is picking? Selecting an object on the screen What does this require? –Get Mouse Location –Compute what objects are rendered at the position.
JOGL in MS Windows GDI – Graphics Device Interface (Windows-specific) OpenGL Lib JOGL Commands GDI Processor Graphics Hardware JOGL Lib.
CS 638, Fall 2001 Today Light Mapping (Continued) Bump Mapping with Multi-Texturing Multi-Pass Rendering.
 Bitmap: A bitmap is a rectangular array of 0s and 1s that serves as a drawing mask for a corresponding rectangular portion of the window.  Applications:
Tools for Raster Displays CVGLab Goals of the Chapter To describe pixmaps and useful operations on them. To develop tools for copying, scaling, and rotating.
Korea University Korea University Computer Graphics Laboratory Computer Graphics Laboratory Jung Lee, Chapter 13.
Homogeneous Form, Introduction to 3-D Graphics Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 20,
CS 638, Fall 2001 Multi-Pass Rendering The pipeline takes one triangle at a time, so only local information, and pre-computed maps, are available Multi-Pass.
CS 4363/6353 OPENGL BACKGROUND. WHY IS THIS CLASS SO HARD TO TEACH? (I’LL STOP WHINING SOON) Hardware (GPUs) double in processing power ever 6 months!
1 10/24/ :01 UML Graphics II Shadows Session 4.
Multi-pass Rendering. © 2002 James K. Hahn, N.H. Baek2 Multi-pass Rendering Repeat: image  Rendering pass  result imageRepeat: image  Rendering pass.
OpenGL Architecture Display List Polynomial Evaluator Per Vertex Operations & Primitive Assembly Rasterization Per Fragment Operations Frame Buffer Texture.
09/16/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Environment mapping Light mapping Project Goals for Stage 1.
OpenGL The Viewing Pipeline: Definition: a series of operations that are applied to the OpenGL matrices, in order to create a 2D representation from 3D.
UniS CS297 Graphics with Java and OpenGL Blending.
OpenGL Special Effects Jian-Liang Lin 2002 Blending: Alpha Channel Alpha value can be specify by glColor* When blending is enabled, the alpha value is.
Computing & Information Sciences Kansas State University Lecture 12 of 42CIS 636/736: (Introduction to) Computer Graphics CIS 636/736 Computer Graphics.
11/24/ :45 Graphics II Shadow Maps Reflections Session 5.
Accumulation-Based Effects Glenn G. Chappell U. of Alaska Fairbanks CS 481/681 Lecture Notes Wednesday, February 4, 2004.
Real-Time Dynamic Shadow Algorithms Evan Closson CSE 528.
Chapter 4 -- Color1 Color Open GL Chapter 4. Chapter 4 -- Color2 n The goal of almost all OpenGL applications is to draw color pictures in a window on.
Shadows David Luebke University of Virginia. Shadows An important visual cue, traditionally hard to do in real-time rendering Outline: –Notation –Planar.
09/23/03CS679 - Fall Copyright Univ. of Wisconsin Last Time Reflections Shadows Part 1 Stage 1 is in.
The Framebuffer Hyun-Chul Cho. HyunChul Cho - KUCG -2 Buffer Goal of a graphics program Draw pictures on the screen Screen Rectangular array.
Tan-Chi Ho, CGGM Lab., CSIE of NCTU Pixel Operations and Buffers.
Buffers Ed Angel Professor Emeritus of Computer Science
Stenciling Effects Glenn G. Chappell
Discrete Techniques.
Real-Time Rendering Shadow Volumes
Jim X. Chen George Mason University
OpenGL Buffers and Tests
CSc4820/6820 Computer Graphics Algorithms Ying Zhu Georgia State University Fog and Transparency.
Geb Thomas Adapted from the OpenGL Programming Guide
Computer Graphics Buffers
Buffers Ed Angel Professor Emeritus of Computer Science
Chapter X Output Merger.
Ref: OpenGL Programming Guide (The Red Book)
The Framebuffer 1 Lecture 37 Fri, Nov 30, 2007.
Stencil Buffer & Decals
OpenGL Background CS 4722.
Presentation transcript:

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.

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.

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.

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.

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

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);

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

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 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu

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 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu

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 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu

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 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu

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

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 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu

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

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 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu

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.

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).

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.

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 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu

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 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu

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 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu

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 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu

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 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu

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 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu

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 Copyright @ 2001 by Jim X. Chen: jchen@cs.gmu.edu

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.

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