MAE 152 Computer Graphics for Scientists and Engineers

  • Slides: 75
Download presentation
MAE 152 Computer Graphics for Scientists and Engineers Fall 03 Framebuffer in Open. GL

MAE 152 Computer Graphics for Scientists and Engineers Fall 03 Framebuffer in Open. GL

Rasterization

Rasterization

What is Rasterization? • Is a process by which a primitive is converted to

What is Rasterization? • Is a process by which a primitive is converted to a 2 D 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

Framebuffer

Framebuffer

Video Memory • Before an image can be sent to a display device, it

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

Video Memory • Graphics systems place heavy demands on video memory, therefore, video memory

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

The Frame Buffer • The portion of memory reserved for holding the bitmapped image

The Frame Buffer • The portion of memory reserved for holding the bitmapped 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

Buffer Define a buffer by its spatial resolution (n x m) and its depth

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

Framebuffer Rasterization Transformation Pipeline Application The Open. GL Pipeline (The Macroscopic View)

Framebuffer Rasterization Transformation Pipeline Application The Open. GL Pipeline (The Macroscopic View)

A Closer Look at Open. GL’s Rasterization Pipeline Point Rasterization Line Rasterization Triangle Rasterization

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

A Closer Look at Open. GL’s Rasterization Pipeline (cont. ) (from previous stages) Pixel

A Closer Look at Open. GL’s Rasterization Pipeline (cont. ) (from previous stages) Pixel Ownership Test Depth Buffer Test Scissor Test Blending Alpha Test Stencil Test Dithering Logical Operations Framebuffer Fragments

Open. GL Frame Buffer

Open. GL Frame Buffer

Open. GL Buffers • Color buffers can be displayed ØFront ØBack ØAuxiliary ØOverlay •

Open. GL Buffers • Color buffers can be displayed ØFront ØBack ØAuxiliary ØOverlay • Depth • Accumulation ØHigh resolution buffer • Stencil ØHolds masks

Writing in Buffers • • Conceptually, we can consider all of memory as a

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

Writing Model Read destination pixel before writing source

Writing Model Read destination pixel before writing source

Writing Modes • • Source and destination bits are combined bitwise 16 possible functions

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

XOR mode • • We can use XOR by enabling logic operations and selecting

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 S M swaps the S and M

Color Buffers • Color buffers are the ones to which you draw Ø They

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

Other Buffers • Depth buffer (z-buffer): Ø Stores a depth value for each pixel

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

Clearing Buffers • Clearing the screen (or any of the buffers) is expensive Ø

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 gl. Clear. Color(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha); void gl. Clear. Depth(GLclampf depth); void gl. Clear. Stencil(GLuint s); void gl. Clear. Accum(GLclampf red, GLclampf green, GLclampf blue, GLclampf alpha); • Then issue a single clear command void gl. Clear(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

Color Buffers for Writing and Reading • void gl. Draw. Buffer(GLenum mode); Ø Selects

Color Buffers for Writing and Reading • void gl. Draw. Buffer(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 gl. Read. Buffer(GLenum mode); Ø Selects the color buffer enabled as the source for reading pixels

Masking Buffers • Sets the masks used to control writing into the indicated buffers

Masking Buffers • Sets the masks used to control writing into the indicated buffers • void gl. Color. Mask(GLboolean red, GLboolean green, GLboolean blue, GLboolean alpha); Ø The red, green, blue and alpha values control whether corresponding component is written • void gl. Depth. Mask(Glboolean flag); Ø flag is GL_TRUE for writing • void gl. Stencil. Mask(Gluint mask); Ø mask = 1 for writing the bit

Testing and Operating on Fragments • After fragments are generated, several processing stages occur

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

Scissor Test

Scissor Test

Scissor Test • void gl. Scissor(Glint x, Glint y, GLsizei width, GLsizei height); Ø

Scissor Test • void gl. Scissor(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 gl. Enable(GL_SCISSOR_TEST);

Using Alpha

Using Alpha

Alpha Test • void gl. Alpha. Func(GLenum func, GLclampf ref); Ø Sets the reference

Alpha Test • void gl. Alpha. Func(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_GREATER or GL_NOTEQUAL Ø Needs enabling gl. Enable(GL_ALPHA_TEST);

Using Alpha • The 4 th component of RGBA color is “alpha”. • Alpha

Using Alpha • The 4 th component of RGBA color is “alpha”. • Alpha is specified with the gl. Color… and gl. Clear. Color 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

Using Alpha: Blending • Blending is covered in chapter 6 of the Red Book.

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 gl. Enable(GL_BLEND); Ø It is not necessary to allocate any additional buffers; alpha is stored in the color buffer. Ø The blending function is specified with gl. Blend. Func.

Using Alpha: gl. Blend. Func() • Blending involve mixing colors based on their respective

Using Alpha: gl. Blend. Func() • 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 gl. Blend. Func, which takes two parameters: Ø GLenum: blending factor for the source color. Ø GLenum: blending factor for the destination color.

Using Alpha: gl. Blend. Func() • Some possible blending factors are: Ø Ø GL_ZERO:

Using Alpha: gl. Blend. Func() • 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.

Using Alpha: The Alpha Test • gl. Alpha. Func takes two parameters: Ø GLenum:

Using Alpha: The Alpha Test • gl. Alpha. Func 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.

Stencil Test

Stencil Test

Stencil Buffer • Used to control drawing based on values in the stencil buffer

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 Poly. CPU Per Vertex Raster DL Texture Pixel Frag FB

Stencil buffer (Open. GL) • Controls whether an image fragment continues toward frame buffer

Stencil buffer (Open. GL) • 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

Stencil buffer (continued) • 1) 2) 3) Image fragment passes only if stencil test

Stencil buffer (continued) • 1) 2) 3) Image fragment passes only if stencil test and depth test succeed Stencil test fails (color and depth of pixel remain unchanged) Stencil test passes, depth test fails (color and depth of pixel remain unchanged) 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

Stencil Buffer: Functions • The two major functions used in stenciling are gl. Stencil.

Stencil Buffer: Functions • The two major functions used in stenciling are gl. Stencil. Func and gl. Stencil. Op. Ø gl. Stencil. Func determines what the stencil test does. Ø gl. Stencil. Op 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.

Creating a Mask • • • gl. Init. Display. Mode( …|GLUT_STENCIL|… ); gl. Enable(

Creating a Mask • • • gl. Init. Display. Mode( …|GLUT_STENCIL|… ); gl. Enable( GL_STENCIL_TEST ); gl. Clear. Stencil( 0 x 0 ); • • gl. Stencil. Func( GL_ALWAYS, 0 x 1 ); gl. Stencil. Op( GL_REPLACE, GL_REPLACE ); draw mask: for example • gl. Begin(GL_QUADS); gl. Vertex 2 f (-1. 0, 0. 0); gl. Vertex 2 f (0. 0, 1. 0); gl. Vertex 2 f (1. 0, 0. 0); gl. Vertex 2 f (0. 0, -1. 0); gl. End();

Using Stencil Mask • Draw objects where stencil = 1 • • gl. Stencil.

Using Stencil Mask • Draw objects where stencil = 1 • • gl. Stencil. Func( GL_EQUAL, 0 x 1 ) Draw objects where stencil != 1 • • gl. Stencil. Func( GL_NOTEQUAL, 0 x 1 ); gl. Stencil. Op( GL_KEEP, GL_KEEP );

Stencil Test • The stencil test takes place only if there is a stencil

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 gl. Stencil. Func(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: gl. Enable(GL_STENCIL); • gl. Stencil. Op(GLenum fail, GLenum zpass); Ø Specifies how the data in the stencil buffer is modified when a fragment passes or fails the stencil test Ø fail, 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

Stencil Buffer: gl. Stencil. Func • gl. Stencil. Func takes three parameters: Ø A

Stencil Buffer: gl. Stencil. Func • gl. Stencil. Func 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: gl. Stencil. Func(GL_EQUAL, 0 x 1); Ø Stencil test passes if bit in SB is off: gl. Stencil. Func(GL_NOTEQUAL, 0 x 1); Ø Test passes if 20 < low 8 bits in SB: gl. Stencil. Func(GL_LESS, 20, 0 xff);

Stencil Buffer: gl. Stencil. Op • gl. Stencil. Op takes three parameters, all GLenum’s:

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

Review: Stencil Buffer [1/2] • Stenciling involves the stencil buffer and the stencil test.

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 gl. Stencil. Func and gl. Stencil. Op. Ø gl. Stencil. Func determines what the stencil test does. Ø gl. Stencil. Op 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.

Review: Stencil Buffer [2/2] • gl. Stencil. Func takes three parameters: Ø GLenum: Which

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

Stenciling Examples: Ordinary Stenciling • To draw a shape in the stencil buffer: Ø

Stenciling Examples: Ordinary Stenciling • To draw a shape in the stencil buffer: Ø Redo when viewport changes size! Code goes in the reshape function. gl. Clear. Stencil(0); gl. Clear(GL_STENCIL_BUFFER_BIT); gl. Stencil. Func(GL_NEVER, 1, 1); gl. Stencil. Op(GL_REPLACE, GL_REPLACE); // only 1 st param matters Draw a shape here. gl. Stencil. Op(GL_KEEP, GL_KEEP); • To use the above shape: gl. Stencil. Func(GL_EQUAL, 1, 1); Draw something; it will appear only inside the above shape. gl. Stencil. Func(GL_NOTEQUAL, 1, 1); Draw something; it will appear only outside the above shape.

Stenciling Examples: Odd Things to Do • Draw each pixel at most 5 times:

Stenciling Examples: Odd Things to Do • Draw each pixel at most 5 times: gl. Clear. Stencil(0); gl. Clear(GL_STENCIL_BUFFER_BIT); gl. Stencil. Func(GL_GREATER, 5, 0 xff); gl. Stencil. Op(GL_KEEP, GL_INCR); • Draw each pixel successfully on every other attempt: gl. Clear. Stencil(0); gl. Clear(GL_STENCIL_BUFFER_BIT); gl. Stencil. Func(GL_EQUAL, 0, 1); gl. Stencil. Op(GL_INVERT, GL_INVERT);

Stenciling Examples: Capping • Here is an implementation of “capping” (see Red p. 446).

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. gl. Clear. Stencil(0); gl. Clear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Stencil. Func(GL_ALWAYS, 1, 1); gl. Stencil. Op(GL_INVERT, GL_INVERT); Draw scene. gl. Stencil. Func(GL_EQUAL, 1, 1); Draw rectangle covering entire viewport, in “capping” color.

Depth Test

Depth Test

Depth Test • gl. Depth. Func(GLenum func); Ø Sets the comparison function for the

Depth Test • gl. Depth. Func(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 gl. Enable(GL_DEPTH_TEST);

Use of Depth Buffer • Use of depth buffer (z-buffer) to achieve hidden surface

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

How to Specify? • In void gl. Depth. Func(Glenum func); Defualt value of func

How to Specify? • In void gl. Depth. Func(Glenum func); Defualt value of func is used: GL_LESS used gl. Enable(GL_DEPTH_TEST); • glut. Init. Display. Mode (GLUT_RGB | GLUT_DEPTH); • Before drawing, each time you need to clear the depth buffer and draw objects in any order gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl. Clear() clears both color and depth buffers • Planet hides behind the sun in solar system example

Z-buffer (Open. GL) • • Requires increased functionality beyond ‘ifstatement’ implementation Six relational operators

Z-buffer (Open. GL) • • Requires increased functionality beyond ‘ifstatement’ implementation Six relational operators Ability to separate depth testing and Z-value updating Z (incoming) op Z (stored) where op is <, ≤, >, ≥, =, ≠

Accumulation Buffer

Accumulation Buffer

Accumulation Buffer: Overview • Next we look at Open. GL’s accumulation buffer. Ø The

Accumulation Buffer: Overview • Next we look at Open. GL’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 glut. Init. Display. Mode call. There is nothing to enable.

Accumulation Buffer: Operations • Five operations can be performed on the accumulation buffer (AB).

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 gl. Clear: gl. Clear. Accum(R, G, B, A); // like gl. Clear. Color (optional) gl. Clear(GL_ACCUM_BUFFER_BIT); // Clear AB • The other four operations involve the gl. Accum command.

Accumulation Buffer: gl. Accum [1/2] • gl. Accum takes two parameters: Ø A GLenum

Accumulation Buffer: gl. Accum [1/2] • gl. Accum 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: gl. Accum(GL_LOAD, value); Ø This uses the color buffer selected for reading. Use gl. Read. Buffer 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: gl. Accum(GL_ACCUM, value);

Accumulation Buffer: gl. Accum [2/2] • To multiply the contents of the AB by

Accumulation Buffer: gl. Accum [2/2] • To multiply the contents of the AB by a value: gl. Accum(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: gl. Accum(GL_RETURN, value); Ø This uses the color buffer selected for drawing. Use gl. Draw. Buffer to change this. (Generally, you do not need to worry about it. )

Accumulation Buffer: Typical Code void display() // The display function { gl. Clear(GL_ACCUM_BUFFER_BIT); for

Accumulation Buffer: Typical Code void display() // The display function { gl. Clear(GL_ACCUM_BUFFER_BIT); for (int i = 0; i < numscenes; ++i) { gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Draw scene number i here gl. Accum(GL_ACCUM, scenefraction[i]); } gl. Accum(GL_RETURN, 1. 0); glut. Swap. Buffers(); } • 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? )

Accumulation Buffer: Applications • The AB can be used for: Ø Ø Ø •

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. Ø Open. GL 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. )

Review: Accumulation Buffer [1/2] • • The AB holds RGBA color data; it is

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 gl. Clear. Other operations are done with gl. Accum. Ø 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.

Review: Accumulation Buffer [2/2] • Here is an implementation of “fade between scenes”: void

Review: Accumulation Buffer [2/2] • Here is an implementation of “fade between scenes”: void display() // The display function { gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Draw scene 1 here gl. Accum(GL_LOAD, 1. 0 -fadefraction); gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Draw scene 2 here gl. Accum(GL_ACCUM, fadefraction); gl. Accum(GL_RETURN, 1. 0); glut. Swap. Buffers(); } • 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.

Other Operations • Blending Ø Combines the incoming fragment’s R, G, B and A

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 gl. Logic. Op(GLenum opcode); opcode can be GL_CLEAR, GL_COPY, GL_AND, etc Needs enabling with GL_COLOR_LOGIC

Hidden-Surface Removal

Hidden-Surface Removal

Hidden Surface? • In a scene composed of 3 D objects, some of them

Hidden Surface? • In a scene composed of 3 D 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

Alpha Test Poly. CPU Per Vertex Raster DL Frag FB Texture • Pixel Reject

Alpha Test Poly. CPU Per Vertex Raster DL Frag FB Texture • Pixel Reject pixels based on their alpha value • gl. Alpha. Func( func, value ) • gl. Enable( GL_ALPHA_TEST ) Ø use alpha as a mask in textures

Dithering • • gl. Enable( GL_DITHER ) Dither colors for better looking results Ø

Dithering • • gl. Enable( GL_DITHER ) Dither colors for better looking results Ø Used to simulate more available colors

Review: Buffers & Tests [1/3] • Open. GL has 4 kinds of buffers. Ø

Review: Buffers & Tests [1/3] • Open. GL has 4 kinds of buffers. Ø Ø • Open. GL has 4 tests. Ø Ø • 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. A test gives a Boolean result for each fragment. True → test passes → fragment is drawn. Buffers and tests are associated: Buffer Corresponding Test -- Scissor Test Color Buffers Alpha Test Depth Buffer Depth Test Stencil Buffer Stencil Test Accumulation Buffer --

Review: Buffers & Tests [2/3] • Remember: Ø Allocate a buffer. In your glut.

Review: Buffers & Tests [2/3] • Remember: Ø Allocate a buffer. In your glut. Init. Display. Mode call. Ø Buffers generally need to be cleared. Use the proper “GL_…_BUFFER_BIT” constant in gl. Clear. Set the value to clear to using gl. Clearbuffername. Ø Enable a test. Enable with gl. Enable. Disable with gl. Disable. • Most buffers have masks associated with them. Ø For example, the color-buffer mask is controlled by the gl. Color. Mask command. Ø The mask affects all commands that would change the buffer, even gl. Clear.

Review: Buffers & Tests [3/3] • The scissor test allows you to restrict drawing

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 gl. Scissor. Ø 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.

Review: Buffers & Tests • Open. GL has 4 kinds of buffers. Ø Ø

Review: Buffers & Tests • Open. GL has 4 kinds of buffers. Ø Ø • Open. GL has 4 tests. Ø Ø • 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. A test gives a Boolean result for each fragment. True → test passes → fragment is drawn. Buffers and tests are associated: Buffer Corresponding Test -- Scissor Test Color Buffers Alpha Test Depth Buffer Depth Test Stencil Buffer Stencil Test Accumulation Buffer --