1 of 13

���Chapter X

Output Merger

All class materials including this PowerPoint file are available at

https://github.com/medialab-ku/openGLESbook

Introduction to Computer Graphics with OpenGL ES (J. Han)

2 of 13

Color and Depth Buffers

10-2

  • The fragment colors are not immediately displayed in the 2D viewport but are passed to the output merger, which manages three kinds of buffers.
    • Color buffer is a memory space storing the pixels to be displayed.
    • Depth buffer or z-buffer has the same resolution as the color buffer and records the z-values of the pixels currently stored in the color buffer.
    • Stencil buffer will not be discussed in this class.
  • The collection of these three is called the frame buffer.

h

w

h

w

Introduction to Computer Graphics with OpenGL ES (J. Han)

3 of 13

RGBZ Fragments

10-3

  • The rasterizer interpolates the z-coordinates as well as the normal, texture coordinates and view vectors. Unlike other per-fragment attributes, however, the interpolated z is delivered directly to the output merger (bypassing the fragment shader) so that the output merger has the color and z of each fragment.

v_normal

v_texCoord

v_view

z

{v_normal, v_texCoord, v_view}

z

fragColor

rasterization

Introduction to Computer Graphics with OpenGL ES (J. Han)

4 of 13

Z-buffering

10-4

  • In the example, two triangles compete for a pixel located at (x, y). The pixel will be colored in blue because the blue triangle is in front of the red triangle. Such a decision is made by comparing the z-values of the triangles at (x, y).

  • Suppose that the blue triangle is processed first.
    • The color buffer contains a blue pixel at (x, y) and the z-buffer contains its z-value, 0.5.
    • Then, the incoming red fragment competes with the blue pixel by comparing its z-value, 0.8, with the z-buffer value at (x,y), 0.5. The red fragment has a larger z-value. It loses! The red fragment is discarded.
  • This is called z-buffering.

  • Suppose that the red triangle is processed first.
    • The color buffer contains a red pixel at (x, y) and the z-buffer contains its z-value, 0.8.
    • Then, the incoming blue fragment competes with the red pixel by comparing its z-value, 0.5, with the z-buffer value at (x,y), 0.8. The blue fragment has a smaller z-value. It wins! Its color and z-value update the color buffer and z-buffer at (x,y), respectively.

z = 0.5

z = 0.8

viewport

Introduction to Computer Graphics with OpenGL ES (J. Han)

5 of 13

Z-buffering (cont’d)

10-5

  • Assume minZ is 0.0, maxZ is 1.0, the red triangle’s depth is 0.8, and the blue one’s is 0.5.

Two triangles are parallel to the xy-plane.

Introduction to Computer Graphics with OpenGL ES (J. Han)

6 of 13

Z-buffering (cont’d)

10-6

  • Rendering-order independence!!

Introduction to Computer Graphics with OpenGL ES (J. Han)

7 of 13

Z-buffering in GL

10-7

  • By calling glClear once per frame, the depth and color buffers are cleared.

  • All tests and operations on fragments are disabled by default and are enabled by calling glEnable.
  • Then, glDepthFunc(GLenum func) is invoked, where func specifies the conditions under which the fragment is drawn. The default is GL_LESS, which means that the fragment is drawn if its depth is less than that of the pixel.

Introduction to Computer Graphics with OpenGL ES (J. Han)

8 of 13

Alpha Blending

10-8

  • The input to the output merger is often called the RGBAZ fragment.
    • A: alpha for representing the fragment's opacity
    • Z: depth

  • The alpha channel in the range of [0,1]
    • 1 denotes “fully opaque.”
    • 0 denotes “fully transparent.”

  • Using alpha value, the fragment is merged with the pixel of the color buffer.

α=1

α=0.5

α=0.15

Introduction to Computer Graphics with OpenGL ES (J. Han)

9 of 13

Alpha Blending (cont’d)

10-9

  • A typical blending equation is described as follows:

  • For alpha blending, the primitives cannot be rendered in an arbitrary order. They must be rendered after all opaque primitives, and in back-to-front order. Therefore, the partially transparent objects should be sorted.
  • Think about rendering the five polygons competing for a pixel at (x,y).

α=0.5 α=0.5 α=1.0 α=0.5 α=1.0

(x,y)

Introduction to Computer Graphics with OpenGL ES (J. Han)

10 of 13

Alpha Blending in GL

10-10

  • glBlendFunc(GLenum sfactor, GLenum dfactor);
    • sfactor specifies the blending coefficient for the incoming (source) fragment
    • dfactor specifies the blending coefficient for the destination pixel
  • Many values such as GL_ZERO and GL_ONE can be assigned to sfactor and dfactor, but GL_SRC_ALPHA may best fit to sfactor and GL_ONE_MINUS_SRC_ALPHA to dfactor.
  • Once the fragment and pixel color are multiplied by sfactor and dfactor, respectively, they are combined using the operator specified by glBlendEquation.
    • Its argument can be GL_FUNC_ADD, GL_FUNC_SUBTRACT, GL_FUNC_REVERSE_SUBTRACT, GL_MIN or GL_MAX.
    • The default is GL_FUNC_ADD.

Introduction to Computer Graphics with OpenGL ES (J. Han)

11 of 13

Alpha Blending in Games�

10-11

Introduction to Computer Graphics with OpenGL ES (J. Han)

12 of 13

Alpha Blending for Fog

10-12

  • Fog is often used to enhance the realism of outdoor scenes.

  • The simplest implementation is a linear fog, which starts from the near plane and ends at the far plane of the view frustum. The objects located at the near plane are clearly visible whereas objects at the far plane are completely obscured by the fog.

equally

distributed

fog

fog accumulated

with the distance

from the eye

Silent Hills

Introduction to Computer Graphics with OpenGL ES (J. Han)

13 of 13

Alpha Blending for Fog (cont’d)

10-13

 

f = 0

f = 1

co

f = 0.2

f = 0.8

f

c = fcf + (1 − f)co

cf

N

F

d

d

Introduction to Computer Graphics with OpenGL ES (J. Han)