framebuffer in opengl

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

Upload: ordell

Post on 08-Feb-2016

76 views

Category:

Documents


0 download

DESCRIPTION

Framebuffer in OpenGL. MAE152 Computer Graphics for Scientists and Engineers Fall 03. Rasterization. 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Framebuffer in OpenGL

Framebuffer in OpenGL

MAE152Computer Graphics for Scientists and Engineers

Fall 03

Page 2: Framebuffer in OpenGL

Rasterization

Page 3: Framebuffer in OpenGL

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

Page 4: Framebuffer in OpenGL

Framebuffer

Page 5: Framebuffer in OpenGL

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

Page 6: Framebuffer in OpenGL

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

Page 7: Framebuffer in OpenGL

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

Page 8: Framebuffer in OpenGL

Buffer

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

pixel

Page 9: Framebuffer in OpenGL

The OpenGL Pipeline(The Macroscopic View)

Appl

icatio

n

Tran

sfor

mat

ion

Pipe

line

Rast

eriza

tion

Fram

ebuff

er

Page 10: Framebuffer in OpenGL

A Closer Look at OpenGL’s Rasterization Pipeline

TextureMappingEngine

ToFragment Tests

PointRasterization

LineRasterization

TriangleRasterization

Pixel Rectangle

Rasterization

BitmapRasterization

FogEngine

Color Sum(Sep.

Specular Color)

Page 11: Framebuffer in OpenGL

A Closer Look at OpenGL’s Rasterization Pipeline (cont.)

PixelOwnership

TestScissorTest

AlphaTest

StencilTest

DepthBufferTest

Blending Dithering LogicalOperations

Fram

ebuff

er

Fragments(from previous

stages)

Page 12: Framebuffer in OpenGL

OpenGL Frame Buffer

Page 13: Framebuffer in OpenGL

OpenGL Buffers

• Color buffers can be displayedFrontBackAuxiliaryOverlay

• Depth• Accumulation

High resolution buffer• Stencil

Holds masks

Page 14: Framebuffer in OpenGL

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

sourcememory

Page 15: Framebuffer in OpenGL

Writing Model

Read destination pixel before writing source

Page 16: Framebuffer in OpenGL

Writing Modes

• Source and destination bits are combined bitwise• 16 possible functions (one per column in table)

replace ORXOR

Page 17: Framebuffer in OpenGL

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 menuthe sequence S S M M S M S S Mswaps the S and M

Page 18: Framebuffer in OpenGL

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

Page 19: Framebuffer in OpenGL

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

Page 20: Framebuffer in OpenGL

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 buffervoid 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 commandvoid 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

Page 21: Framebuffer in OpenGL

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

Page 22: Framebuffer in OpenGL

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

Page 23: Framebuffer in OpenGL

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

Page 24: Framebuffer in OpenGL

Scissor Test

Page 25: Framebuffer in OpenGL

Scissor Test

• 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 enablingglEnable(GL_SCISSOR_TEST);

Page 26: Framebuffer in OpenGL

Using Alpha

Page 27: Framebuffer in OpenGL

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 enablingglEnable(GL_ALPHA_TEST);

Page 28: Framebuffer in OpenGL

Using Alpha

• The 4th 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:

BlendingAlpha 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 TestAlpha can be tested, in ways similar to the stencil buffer

Page 29: Framebuffer in OpenGL

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 withglEnable(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.

Page 30: Framebuffer in OpenGL

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.

Page 31: Framebuffer in OpenGL

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.

Page 32: Framebuffer in OpenGL

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.

Page 33: Framebuffer in OpenGL

Stencil Test

Page 34: Framebuffer in OpenGL

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

Raster Frag FB

Pixel

Texture

Page 35: Framebuffer in OpenGL

Stencil buffer (OpenGL)

• Controls whether an image fragment continues toward frame buffer

• Two operations involved1) Image fragment is passed or not2) Stencil buffer is updated by result of stencil test

and depth test

Page 36: Framebuffer in OpenGL

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

Page 37: Framebuffer in OpenGL

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.

Page 38: Framebuffer in OpenGL

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

Page 39: Framebuffer in OpenGL

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

Page 40: Framebuffer in OpenGL

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_INVERTfail = failed stencil test; zfail = failed z test; zpass = passed z test

Page 41: Framebuffer in OpenGL

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

Page 42: Framebuffer in OpenGL

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

Page 43: Framebuffer in OpenGL

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.

Page 44: Framebuffer in OpenGL

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.

Page 45: Framebuffer in OpenGL

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

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

Page 46: Framebuffer in OpenGL

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

Page 47: Framebuffer in OpenGL

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.

Page 48: Framebuffer in OpenGL

Depth Test

Page 49: Framebuffer in OpenGL

Depth Test

• 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 enablingglEnable(GL_DEPTH_TEST);

Page 50: Framebuffer in OpenGL

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

Page 51: Framebuffer in OpenGL

How to Specify?

• In void glDepthFunc(Glenum func);Defualt value of func is used: GL_LESS usedglEnable(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

Page 52: Framebuffer in OpenGL

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 <, ≤, >, ≥, =, ≠

Page 53: Framebuffer in OpenGL

Accumulation Buffer

Page 54: Framebuffer in OpenGL

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.

Page 55: Framebuffer in OpenGL

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.

Page 56: Framebuffer in OpenGL

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

Page 57: Framebuffer in OpenGL

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

Page 58: Framebuffer in OpenGL

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

Page 59: Framebuffer in OpenGL

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

Page 60: Framebuffer in OpenGL

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.

Page 61: Framebuffer in OpenGL

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.

Page 62: Framebuffer in OpenGL

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 colorsNeeds 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, etcNeeds enabling with GL_COLOR_LOGIC

Page 63: Framebuffer in OpenGL

Hidden-Surface Removal

Page 64: Framebuffer in OpenGL

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

Page 65: Framebuffer in OpenGL
Page 66: Framebuffer in OpenGL
Page 67: Framebuffer in OpenGL
Page 68: Framebuffer in OpenGL

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

Raster Frag FB

Pixel

Texture

Page 69: Framebuffer in OpenGL

Dithering

• glEnable( GL_DITHER )• Dither colors for better looking results

Used to simulate more available colors

Page 70: Framebuffer in OpenGL
Page 71: Framebuffer in OpenGL

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:

Buffer Corresponding Test

-- Scissor Test

Color Buffers Alpha Test

Depth Buffer Depth Test

Stencil Buffer Stencil Test

Accumulation Buffer --

Page 72: Framebuffer in OpenGL

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

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.

Page 73: Framebuffer in OpenGL

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.

Page 74: Framebuffer in OpenGL
Page 75: Framebuffer in OpenGL

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:

Buffer Corresponding Test

-- Scissor Test

Color Buffers Alpha Test

Depth Buffer Depth Test

Stencil Buffer Stencil Test

Accumulation Buffer --