Presentation is loading. Please wait.

Presentation is loading. Please wait.

Framebuffer in OpenGL MAE152 Computer Graphics for Scientists and Engineers Fall 03.

Similar presentations

Presentation on theme: "Framebuffer in OpenGL MAE152 Computer Graphics for Scientists and Engineers Fall 03."— Presentation transcript:

1 Framebuffer in OpenGL MAE152 Computer Graphics for Scientists and Engineers Fall 03

2 Rasterization

3 What is Rasterization? Is a process by which a primitive is converted to a 2D imge  First, it determines which squares of an integer grid in window coordinates are occupied by the primitive  Assign a color and a depth value to each square A grid square along with its assigned color and depth is called a fragment The results of the process are passed to the next stage of per-fragment operations

4 Point Rasterization Point is rasterized as a single fragment truncating its (x w,y w ) coordinates to integers For wide points, fragment centers are at The data associated with each rasterized fragment is the same as that of the vertex (Odd) (Even)

5 Line Segment Rasterization Diamond-exit rule to determine the fragments produced by rasterization Specify the data associate with each rasterized fragment Where papa pbpb p

6 Polygon Rasterization Point sampling  Rasterized fragment centers lie inside of the polygon  If two or more polygons share the same fragment, it is rasterized by one of them Specify the data associated with each rastered fragment p papa pcpc pbpb p = ap a + bp b + cp c defines any point in a triangle with barycentric coordinates

7 Framebuffer

8 Video Memory Before an image can be sent to a display device, it must be first represented as a bitmap in an area of video memory called the frame buffer. The amount of video memory, therefore, dictates the maximum resolution and color depth available In a conventional video adapter, the bitmap to be displayed is first generated by the main processor and then sent to the frame buffer. Most modern video adapters use graphics accelerators that have their own processors capable of manipulating bitmaps and graphics objects. A separate memory area is reserved for such operations

9 Video Memory Graphics systems place heavy demands on video memory, therefore, video memory needs to be much faster than main memory Most video memory is dual-ported or bi-directional, and capable of transferring data between the video memory and the video processor simultaneously while another set of data is being transferred to the display device There are many different types of video memory, including :  VRAM: Video RAM - dual-ported memory  WRAM: Windows RAM - dual-ported windowed memory  RDRAM: A type of memory developed by Rambus Inc.  SGRAM: Synchronous Graphic Random Access Memory, single-ported, capable of opening two memory pages at once

10 The Frame Buffer The portion of memory reserved for holding the bit- mapped image that is sent to the display device is called the frame buffer Typically the frame buffer is stored on the video adapter’s memory chips. In some cases, the video chipset is integrated into the motherboard, and the frame buffer is stored in main memory The frame buffer is a bit-map that contains among other things the color depth or bit depth which defines the number of distinct colors in the graphics subsystem A 24-bit video adapter, has a color depth of 2 ^ 24 (~16.7 million) colors. It follows that its color depth is 24 bits

11 What is Framebuffer Each fragment has coordinate data which corresponds to a pixel, as well as color and depth values Storages to hold the various kinds of information of pixels are called buffers OpenGL implementation supports the following buffers  Color buffer  Depth buffer  Stencil buffer  Accumulation buffer The buffers are used to perform special tasks before pixels are finally written to the viewable color buffer A collection of these buffers is called framebuffer

12 Buffer Define a buffer by its spatial resolution (n x m) and its depth k, the number of bits/pixel pixel

13 The OpenGL Pipeline (The Macroscopic View) Application Transformation Pipeline Rasterization Framebuffer

14 A Closer Look at OpenGL’s Rasterization Pipeline Texture Mapping Engine To Fragment Tests Point Rasterization Line Rasterization Triangle Rasterization Pixel Rectangle Rasterization Bitmap Rasterization Fog Engine Color Sum (Sep. Specular Color)

15 A Closer Look at OpenGL’s Rasterization Pipeline (cont.) Pixel Ownership Test Scissor Test Alpha Test Stencil Test Depth Buffer Test BlendingDithering Logical Operations Framebuffer Fragments (from previous stages)

16 OpenGL Frame Buffer

17 OpenGL Buffers Color buffers can be displayed  Front  Back  Auxiliary  Overlay Depth Accumulation  High resolution buffer Stencil  Holds masks

18 Writing in Buffers Conceptually, we can consider all of memory as a large two- dimensional array of pixels We read and write rectangular block of pixels  Bit block transfer (bitblt) operations The frame buffer is part of this memory frame buffer (destination) writing into frame buffer source memory

19 Writing Model Read destination pixel before writing source

20 Writing Modes Source and destination bits are combined bitwise 16 possible functions (one per column in table) replace OR XOR

21 XOR mode We can use XOR by enabling logic operations and selecting the XOR write mode XOR is especially useful for swapping blocks of memory such as menus that are stored off screen If S represents screen and M represents a menu the sequence S  S  M M  S  M S  S  M swaps the S and M

22 The Pixel Pipeline OpenGL has a separate pipeline for pixels  Writing pixels involves Moving pixels from processor memory to the frame buffer Format conversions Mapping, Lookups, Tests  Reading pixels Format conversion

23 Color Buffers Color buffers are the ones to which you draw  They contain RGBA data Stereoscopic viewing needs left and right color buffers for the left and right stereo images Double-buffered systems have front and back color buffers Non-displayable auxiliary color buffers can be used Minimum requirement is a front-left color buffer

24 Other Buffers Depth buffer (z-buffer):  Stores a depth value for each pixel  Depth is usually measured in terms of distance to the eye  Used for a hidden-surface removal Stencil buffer:  Stores the information to restrict drawing to certain portions of the screen Accumulation buffer:  Holds RGBA color data for accumulating a series of images into a final, composite image  When accumulation is finished, the result is copied back into the color buffer for viewing  Used for Scene antialiasing, motion blur, simulating depth of field, and calculating soft shadows

25 Clearing Buffers Clearing the screen (or any of the buffers) is expensive  Hardware can clear more than one buffer at once First, specify the current clearing values for each buffer void glClearColor(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha); void glClearDepth(GLclampf depth); void glClearStencil(GLuint s); void glClearAccum(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha); Then issue a single clear command void glClear(GLbitfield mask); mask is the bitwise logical OR of some combination of GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_STECIL_BUFFER_BIT, and GL_ACCUM_BUFFER_BIT

26 Color Buffers for Writing and Reading void glDrawBuffer(GLenum mode);  Selects the color buffers enabled for writing or clearing  mode can be GL_FRONT, GL_BACK, GL_LEFT, GL_RIGHT, GL_FRONT_LEFT, etc  Defualt mode is GL_FRONT for single-buffered contexts and GL_BACK for double-buffered contexts void glReadBuffer(GLenum mode);  Selects the color buffer enabled as the source for reading pixels

27 Masking Buffers Sets the masks used to control writing into the indicated buffers void glColorMask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha);  The red, green, blue and alpha values control whether corresponding component is written void glDepthMask(Glboolean flag);  flag is GL_TRUE for writing void glStencilMask(Gluint mask);  mask = 1 for writing the bit

28 Testing and Operating on Fragments After fragments are generated, several processing stages occur determining how and whether a given fragment is drawn as pixel into the framebuffer Set of tests:  Scissor test  Alpha test  Depth test  Stencil test  Blending  Dithering  Logical Operation

29 Scissor Test

30 void glScissor(Glint x, Glint y, GLsizei width, GLsizei height);  Sets the location and size of the scissor rectangle or box  By default, the rectangle matches the window  Drawing occurs only inside the rectangle: pixels lying inside the rectangle pass the scissor test  Needs enabling glEnable(GL_SCISSOR_TEST);

31 Scissor Box Additional Clipping Test glScissor( x, y, w, h )  any fragments outside of box are clipped  useful for updating a small section of a viewport affects glClear() operations

32 Using Alpha

33 Alpha Test void glAlphaFunc(GLenum func, GLclampf ref);  Sets the reference value and comparison function for the alpha test  In RGBA mode, a fragment is accepted or rejected by the alpha test on its alpha value  By default, ref is zero, and func is GL_ALWAYS  func can be GL_ALWAYS, GL_NEVER, GL_LESS, GL_EQUAL, GL_LEQUAL, GL_GEQUAL, GL_GREATER or GL_NOTEQUAL  Needs enabling glEnable(GL_ALPHA_TEST);

34 Using Alpha The 4 th component of RGBA color is “alpha”. Alpha is specified with the glColor … and glClearColor commands, as well as various lighting and material-definition commands. Alpha is stored in the color buffer, along with R, G, and B. Two major applications of alpha:  Blending Alpha can determine how a color to be drawn is blended with the color already present at that pixel. The most common application of blending is transparent objects.  The Alpha Test Alpha can be tested, in ways similar to the stencil buffer

35 Using Alpha: Blending Blending is covered in chapter 6 of the Red Book.  You may also wish to read about anti-aliasing and depth-cueing (“fog”) in that chapter. To do blending, enable it, and specify a blend function.  Blending is enabled with glEnable(GL_BLEND);  It is not necessary to allocate any additional buffers; alpha is stored in the color buffer.  The blending function is specified with glBlendFunc.

36 Using Alpha: glBlendFunc() Blending involve mixing colors based on their respective alpha values. A blending function blends two colors:  The source color: the color of the fragment be drawn.  The destination color: the color already present in the color buffer. Blending functions are specified using glBlendFunc, which takes two parameters:  GLenum : blending factor for the source color.  GLenum : blending factor for the destination color.

37 Using Alpha: glBlendFunc() Some possible blending factors are:  GL_ZERO : Multiply this color by zero (0,0,0,0).  GL_ONE : Multiply this color by one (1,1,1,1).  GL_SRC_ALPHA : Multiply this color by the source alpha.  GL_ONE_MINUS_SRC_ALPHA : Multiply this color by one minus the source alpha.  GL_DST_ALPHA : Multiply this color by the destination alpha.  GL_SRC_COLOR (for dest. blend factor only): Multiply this color by the source color, component by component. For a complete list of possible blending factors, see p. 223 of the Red Book.

38 Using Alpha: Applications of Blending What are some effects one can do with a blend function?  Painter’s Algorithm Src blend factor: 1, dest blend factor: 0 (same as no blending).  Transparency Src bf: source alpha, dest bf: 1-source alpha. Src alpha = 1: opaque, 0: invisible, between: translucent.  Weird Lighting Method Src bf: 0, dest bf: source color. Buffer holds unlit scene, source color is color of light at that point in the scene. (Alpha is ignored.) What difficulties are involved in using a blend function?  Drawing order matters. For example, when doing transparency via blending, polygons should be drawn back-to-front (use an object space HSR method).  Sadly, blending often gives rather poor-looking results.

39 Using Alpha: The Alpha Test glAlphaFunc takes two parameters:  GLenum : What test to perform.  GLclampf : Reference value Type is like GLfloat, but required to be in [0,1], that is, “clamped”. The possible tests all compare the alpha value of the pixel to be drawn with the reference value.  GL_LESS : Passes if alpha to be drawn is less than the ref value.  GL_EQUAL : Passes if the two are equal.  GL_ALWAYS : Always passes.  Etc… Note: The alpha test is backwards from the stencil test.  Stencil test: REF comparison VALUE_IN_BUFFER.  Alpha test: VALUE_FOR_NEW_PIXEL comparison REF.

40 Stencil Test

41 Stencil Buffer Used to control drawing based on values in the stencil buffer  Fragments that fail the stencil test are not drawn  Example: create a mask in stencil buffer and draw only objects not in mask area CPU DL Poly. Per Vertex Per Vertex Raster Frag FB Pixel Texture

42 Stencil buffer (OpenGL) Controls whether an image fragment continues toward frame buffer Two operations involved 1)Image fragment is passed or not 2)Stencil buffer is updated by result of stencil test and depth test

43 Stencil buffer (continued) Image fragment passes only if stencil test and depth test succeed 1)Stencil test fails (color and depth of pixel remain unchanged) 2)Stencil test passes, depth test fails (color and depth of pixel remain unchanged) 3)Both pass (color and depth for pixel are given the new values) In all cases stencil buffer is updated according to stencil operation pre-set for the condition

44 Stencil Buffer: Functions The two major functions used in stenciling are glStencilFunc and glStencilOp.  glStencilFunc determines what the stencil test does.  glStencilOp determines what happens to the stencil buffer if the stencil test passes or fails. If the stencil test passes, then you can also have different outcomes based on the depth test.

45 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: for example glBegin(GL_QUADS); glVertex2f (-1.0, 0.0); glVertex2f (0.0, 1.0); glVertex2f (1.0, 0.0); glVertex2f (0.0, -1.0); glEnd();

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

47 Stencil Test The stencil test takes place only if there is a stencil buffer  It compares a reference value with the value stored at a pixel in the buffer  Depending on the test result, the value in the stencil buffer is modified void glStencilFunc(GLenum func, GLint ref, GLuint mask);  Sets the comparison func, reference ref and mask for the test Comparison applies to those bits for which bits of the mask are 1  func can be GL_ALWAYS, GL_LESS, etc.  Needs enabling: glEnable(GL_STENCIL); 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  fail, zfail, zpass can be GL_KEEP, GL_ZERO, GL_REPLACE, GL_INCR, GL_DECR, GL_INVERT fail = failed stencil test; zfail = failed z test; zpass = passed z test

48 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

49 Stencil Buffer: Introduction The stencil buffer and its associated test, the stencil test, can be used for a variety of yes/no, pass/fail-type effects.  The stencil buffer holds an integer for each viewport pixel. The value in the buffer can be affected by several things, including the depth test.  You can place values in the stencil buffer and then test them to determine whether to draw pixels.  Allocate the stencil buffer using GLUT_STENCIL in your glutInitDisplayMode call.  Clear the stencil buffer using glClear(GL_STENCIL_BUFFER_BIT); after setting the clearing value with glClearStencil.  Enable the stencil test using glEnable(GL_STENCIL_TEST);

50 Stencil Buffer: glStencilFunc glStencilFunc takes three parameters:  A GLenum : what comparison the stencil test will do.  A GLint used as a “reference value” in the stencil test.  A GLuint used as a mask (an “and” mask). Think: REF COMPARE (buffer pixel & mask) Examples  Stencil test passes if bit in SB is on: glStencilFunc(GL_EQUAL, 0x1, 0x1);  Stencil test passes if bit in SB is off: glStencilFunc(GL_NOTEQUAL, 0x1, 0x1);  Test passes if 20 < low 8 bits in SB: glStencilFunc(GL_LESS, 20, 0xff);

51 Stencil Buffer: glStencilOp glStencilOp takes three parameters, all GLenum ’s:  Operation to perform if stencil test fails.  Op. to perform if stencil test passes and depth test fails.  Op. to perform if stencil test passes and depth test passes. Examples  Replace the SB value with the reference value (from glStencilFunc ): glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);  Do not modify the SB: glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);  Replace SB value with zero, the reference value, or the bitwise inversion of the current SB value, respectively: glStencilFunc(GL_ZERO, GL_REPLACE, GL_INVERT);  Increment or decrement the SB value, as appropriate: glStencilFunc(GL_DECR, GL_INCR, GL_INCR);

52 Review: Stencil Buffer [1/2] Stenciling involves the stencil buffer and the stencil test.  Remember: allocate the buffer, enable the test.  Clear the buffer the same way you clear any buffer. The two major functions used in stenciling are glStencilFunc and glStencilOp.  glStencilFunc determines what the stencil test does.  glStencilOp determines what happens to the stencil buffer if the stencil test passes or fails. If the stencil test passes, then you can also have different outcomes based on the depth test.

53 Review: Stencil Buffer [2/2] glStencilFunc takes three parameters:  GLenum : Which comparison the stencil test will do.  GLint : “Reference value” in the stencil test. Also used by operations specified with glStencilOp.  GLuint : Used as a mask (an “and” mask). glStencilOp takes three parameters:  GLenum : Operation to do if stencil test fails.  GLenum : Operation if stencil passes and depth fails.  GLenum : Operation if stencil passes and depth passes.

54 Stenciling Examples: Ordinary Stenciling To draw a shape in the stencil buffer:  Redo when viewport changes size! Code goes in the reshape function. glClearStencil(0); glClear(GL_STENCIL_BUFFER_BIT); glStencilFunc(GL_NEVER, 1, 1); glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); // only 1st param matters Draw a shape here. glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); To use the above shape: glStencilFunc(GL_EQUAL, 1, 1); Draw something; it will appear only inside the above shape. glStencilFunc(GL_NOTEQUAL, 1, 1); Draw something; it will appear only outside the above shape.

55 Stenciling Examples: Odd Things to Do Draw each pixel at most 5 times: glClearStencil(0); glClear(GL_STENCIL_BUFFER_BIT); glStencilFunc(GL_GREATER, 5, 0xff); glStencilOp(GL_KEEP, GL_INCR, GL_INCR); Draw each pixel successfully only on every other attempt: glClearStencil(0); glClear(GL_STENCIL_BUFFER_BIT); glStencilFunc(GL_EQUAL, 0, 1); glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT);

56 Stenciling Examples: Capping Here is an implementation of “capping” (see Red p. 446).  You are drawing a number of closed objects. You wish to make sure that the inside of these is never visible, even if the near clipping plane slices one of them. glClearStencil(0); glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glStencilFunc(GL_ALWAYS, 1, 1); glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT); Draw scene. glStencilFunc(GL_EQUAL, 1, 1); Draw rectangle covering entire viewport, in “capping” color.

57 Depth Test

58 glDepthFunc(GLenum func);  Sets the comparison function for the depth test  An incoming fragment passes the depth test if its z value has specified relation to the value already stored in the depth buffer  By default, func is GL_LESS Pixels with larger depth-buffer values are overwritten by pixels with smaller values  func can be GL_ALWAYS, GL_EQUAL, GL_GREATER, etc.  Needs enabling glEnable(GL_DEPTH_TEST);

59 Use of Depth Buffer Use of depth buffer (z-buffer) to achieve hidden surface removal Graphical calculations convert each surface (before drawing) to a set of corresponding pixels on the window and also compute depth value for each pixel A comparison is done with the depth value already stored at that pixel to accept the pixel only if it has a smaller depth Color and depth information of the incoming pixel with greater depth is discarded

60 How to Specify? In void glDepthFunc(Glenum func); Defualt value of func is used: GL_LESS used glEnable(GL_DEPTH_TEST); glutInitDisplayMode (GLUT_RGB | GLUT_DEPTH); Before drawing, each time you need to clear the depth buffer and draw objects in any order glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear() clears both color and depth buffers Planet hides behind the sun in solar system example

61 Z-buffer (OpenGL) Requires increased functionality beyond ‘if- statement’ implementation Six relational operators Ability to separate depth testing and Z-value updating Z (incoming) op Z (stored) where op is, ≥, =, ≠

62 Accumulation Buffer

63 Accumulation Buffer: Overview Next we look at OpenGL’s accumulation buffer.  The accumulation buffer allows you to blend together different 2- D scenes. These can be renderings of 3-D scenes.  The accumulation buffer holds RGBA color data, just like the color buffers.  There are special commands that allow you to blend a color buffer with the accumulation buffer (possibly several times) and then transfer the contents of the accumulation buffer to a color buffer.  Allocate the accumulation buffer using GLUT_ACCUM in your glutInitDisplayMode call. There is nothing to enable.

64 Accumulation Buffer: Operations Five operations can be performed on the accumulation buffer (AB). They are all performed on the entire buffer at once:  AB can be cleared.  Contents of a color buffer can be multiplied by a value and then copied to AB.  Contents of a color buffer can be multiplied by a value and then added to AB.  An arithmetic operation (  or +) can be performed on every pixel in AB.  The contents of AB can be multiplied by a value and copied to a color buffer. The first operation above, clearing, is done with glClear : glClearAccum( R, G, B, A ); // like glClearColor (optional) glClear(GL_ACCUM_BUFFER_BIT); // Clear AB The other four operations involve the glAccum command.

65 Accumulation Buffer: glAccum [1/2] glAccum takes two parameters:  A GLenum telling which operation to perform.  A GLfloat giving a relevant constant value. To multiply the contents of a color buffer by a value and copy the result to the AB: glAccum(GL_LOAD, value );  This uses the color buffer selected for reading. Use glReadBuffer to change this. (Generally, you do not need to worry about it.) To multiply the contents of a color buffer by a value and add the result to the AB: glAccum(GL_ACCUM, value );

66 Accumulation Buffer: glAccum [2/2] To multiply the contents of the AB by a value: glAccum(GL_MULT, value );  There is also GL_ADD, to add instead of multiplying, but I have never seen a use for it. To multiply the contents of the AB by a value and copy the result to a color buffer: glAccum(GL_RETURN, value );  This uses the color buffer selected for drawing. Use glDrawBuffer to change this. (Generally, you do not need to worry about it.)

67 Accumulation Buffer: Typical Code void display() // The display function { glClear(GL_ACCUM_BUFFER_BIT); for (int i = 0; i < numscenes; ++i) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Draw scene number i here glAccum(GL_ACCUM, scenefraction[i]); } glAccum(GL_RETURN, 1.0); glutSwapBuffers(); } The values scenefraction[i] should be in [0,1] and should probably add up to 1.  Replacing “ scenefraction[i] ” with “ 1.0/numscenes ” would give equal weight to all scenes being blended. Note how the clearing works: AB outside the loop, color & depth inside. Also note that the above code is not as efficient as it could be. (Why?)

68 Accumulation Buffer: Applications The AB can be used for:  Fading between scenes.  Motion blur.  Anti-aliasing.  Depth-of-field effects.  Soft shadows (if you know how to do shadows). The last three applications above are usually done with “jittering”.  Jittering means making repeated small perturbations to a scene.  Then we blend the jittered scenes together to form the finished product.  To do anti-aliasing and depth-of-field effects, we jitter the projection matrix; to do soft shadows, we do shadows (somehow …) and jitter the light source. What are some problems with using the AB?  AB operations are generally slow; they may be unsuitable for real-time graphics.  OpenGL implementations are not required to support accumulation buffers, so it might reduce the portability of code. (In practice, this does not seem to be a problem.)

69 Review: Accumulation Buffer [1/2] The AB holds RGBA color data; it is used to blend of 2-D scenes. Five operations can be performed on the AB:  AB = 0. (Clear AB; the “0” can be anything you want) With glClear. Other operations are done with glAccum.  GL_LOAD : AB = k*CB. (CB = Color Buffer)  GL_ACCUM : AB += k*CB.  GL_MULT : AB *= k. (Or “+=” using GL_ADD )  GL_RETURN : CB = k*AB. Typically:  Clear AB (unless first operation below is a LOAD).  Repeat: Clear color buf. And draw a scene in it. Color buf.  value → add to AB.  Copy AB to color buf. “ Values ” above should be in [0,1], and add up to 1. Using a larger value gives that scene greater weight in the final image.

70 Review: Accumulation Buffer [2/2] Here is an implementation of “fade between scenes”: void display() // The display function { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Draw scene 1 here glAccum(GL_LOAD, 1.0-fadefraction); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Draw scene 2 here glAccum(GL_ACCUM, fadefraction); glAccum(GL_RETURN, 1.0); glutSwapBuffers(); } The variable fadefraction should be in [0,1]. It should slowly increase from 0 to 1, changing values each time the display function is called.

71 Other Operations Blending  Combines the incoming fragment’s R, G, B and A values with those of the pixel already stored at the location Dithering  Dither the values of red, green and blue on neighboring pixels for the perception of a wide range of colors Needs enabling with GL_DITHER Logical Operations  Are applied between the incoming fragment’s color and the color stored at the corresponding location in the framebuffer  The result replaces the value in the framebuffer for that fragment void glLogicOp(GLenum opcode); opcode can be GL_CLEAR, GL_COPY, GL_AND, etc Needs enabling with GL_COLOR_LOGIC

72 Hidden-Surface Removal

73 Hidden Surface? In a scene composed of 3D objects, some of them might obscure all or parts of others The obscuring relationship changes with viewpoint and needs to be properly maintained Hidden-surface removal is elimination of parts of solid objects that are obscured by others Otherwise, the objects are drawn in the order the drawing commands appear in the code Hidden-surface removal increases performance




77 Alpha Test Reject pixels based on their alpha value glAlphaFunc( func, value ) glEnable( GL_ALPHA_TEST )  use alpha as a mask in textures CPU DL Poly. Per Vertex Per Vertex Raster Frag FB Pixel Texture

78 Dithering glEnable( GL_DITHER ) Dither colors for better looking results  Used to simulate more available colors


80 Review: Buffers & Tests [1/3] OpenGL has 4 kinds of buffers.  Each buffer holds a piece of data about every pixel in the viewport.  The kind of data depends on which buffer and how it is used. OpenGL has 4 tests.  A test gives a Boolean result for each fragment.  True → test passes → fragment is drawn. Buffers and tests are associated: BufferCorresponding Test --Scissor Test Color BuffersAlpha Test Depth BufferDepth Test Stencil BufferStencil Test Accumulation Buffer--

81 Review: Buffers & Tests [2/3] Remember:  Allocate a buffer. In your glutInitDisplayMode call.  Buffers generally need to be cleared. Use the proper “ GL_ … _BUFFER_BIT ” constant in glClear. Set the value to clear to using glClear buffername.  Enable a test. Enable with glEnable. Disable with glDisable. Most buffers have masks associated with them.  For example, the color-buffer mask is controlled by the glColorMask command.  The mask affects all commands that would change the buffer, even glClear.

82 Review: Buffers & Tests [3/3] The scissor test allows you to restrict drawing to a rectangular portion of the viewport.  To use: enable the scissor test, and specify a rectangle with glScissor.  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. We discuss stenciling later.


84 Review: Buffers & Tests OpenGL has 4 kinds of buffers.  Each buffer holds a piece of data about every pixel in the viewport.  The kind of data depends on which buffer and how it is used. OpenGL has 4 tests.  A test gives a Boolean result for each fragment.  True → test passes → fragment is drawn. Buffers and tests are associated: BufferCorresponding Test --Scissor Test Color BuffersAlpha Test Depth BufferDepth Test Stencil BufferStencil Test Accumulation Buffer--

Download ppt "Framebuffer in OpenGL MAE152 Computer Graphics for Scientists and Engineers Fall 03."

Similar presentations

Ads by Google