CENG – 477 Introduction to Computer...

51
CENG – 477 Introduction to Computer Graphics Rasterization

Transcript of CENG – 477 Introduction to Computer...

Page 1: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

CENG – 477

Introduction to Computer

GraphicsRasterization

Page 2: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Rasterization

• Rasterization is concerned with creating fragments from

vertices

• It works in screen coordinates – thus it is next step after the

viewport transform

• Goal: Given a set of vertices which fragments must be “turned

on” to create the primitive:

CENG 477 - Computer Graphics 2

Page 3: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Rasterization

• Subproblems:

– How to deal with different primitives

– How to make it fast

– How to interpolate color and other attributes

• We’ll start with line rasterization

CENG 477 - Computer Graphics 3

Page 4: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Line Rasterization

• Several methods exist

• Option 1: Treat the line as a thin rectangle and turn on pixels

that are inside this rectangle

CENG 477 - Computer Graphics 4

Too thin rectangle

• May cause gaps in

the line

Too thick rectangle

• Line thickness

varies

Page 5: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Line Rasterization

• What must be the minimum thickness?

CENG 477 - Computer Graphics 5

t<1

• May not draw

anything if it is

too thin

t=1

• Must be at least

one to draw a

horizontal line in

some cases

Page 6: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Line Rasterization

• But a thickness of 1 may result in too thick lines

CENG 477 - Computer Graphics 6

t=1

this segment is > 1

t=1

thus may contain more

than 1 pixel

Page 7: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Line Rasterization

• Therefore we need another solution

• We can use line equations to decide which pixels belong the

line

• Assume that we want to draw a line between two screen

coordinates: (x0, y0) to (x1, y1)

• Assume that the slope of the line, m, is in range (0, 1]

CENG 477 - Computer Graphics 7

� =�% − �'

�% − �'

(x0, y0)

(x1, y1)

Which pixels should we draw?

Page 8: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Line Equation

• Let’s first remember the implicit line equation:

• We can derive it from geometry:

CENG 477 - Computer Graphics 8

� �, � = �(�' − �%) + �(�% − �') + �'�% − �'�%

(x0, y0)

(x1, y1)n �. [�% − �', �% − �']

0= 0

� = [�' − �%, �% − �']

� � = � − �� . � = 0p0

p1

p

What is the meaning of f(p) = 0, f(p) > 0 and f(p) < 0

Page 9: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

The Basic Algorithm

• The basic algorithm is as follows:

• Because the slope is in (0, 1], we always go right and

sometimes go up (assuming that x0 < x1 and y0 < y1)

CENG 477 - Computer Graphics 9

y = y0

for x = x0 to x1 do:

draw(x, y)

if (some condition) then:

y = y + 1

Page 10: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

The Midpoint Algorithm

• Assume we have just drawn (x, y). Which pixel to draw next?

CENG 477 - Computer Graphics 10

(x, y)

• Compute the midpoint of the next pixel

• If the midpoint is on or above the line

choose right pixel (E: east)

• If the midpoint is below the line, choose the

top right pixel (NE: north-east)

y = y0

for x = x0 to x1 do:

draw(x, y)

if f(x+1, y+0.5) < 0 then:

y = y + 1

We call these pixels E and NE

E

NE

Page 11: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

The Midpoint Algorithm

• This algorithm works well but requires evaluating the line

equation at every iteration

• Can be optimized with an incremental algorithm:

CENG 477 - Computer Graphics 11

(x, y) E

NE

• If we selected E, in the next iteration we

need the value of f(M1)

• If we selected NE, in the next iteration

we need f(M2)

• Both can be computed from f(M0)

M1

M2

M0

� �% − � �' = �' − �%

� �5 − � �' = (�'−�%) + (�%−�')

Page 12: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

The Midpoint Algorithm

• This algorithm works well but requires evaluating the line

equation at every iteration

• Can be optimized with an incremental algorithm:

CENG 477 - Computer Graphics 12

(x0, y0) E

NE

• In other words, if we know f(M), we can

compute the next f(M) by simple integer

arithmetic

• What is the first f(M)?

• Note that f(x0, y0) = 0 as it is the starting

point of the line

M1

M2

M0� �' = � �' + 1, �' + 0.5

= (�'−�%) + 0.5(�%− �')

Page 13: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

The Midpoint Algorithm

• So the overall algorithm is:

CENG 477 - Computer Graphics 13

y = y0

d = (y0 – y1) + 0.5(x1 – x0)

for x = x0 to x1 do:

draw(x, y)

if d < 0 then: // choose NE

y = y + 1

d += (y0 – y1) + (x1 – x0)

else: // choose E

d += (y0 – y1)

Page 14: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

The Midpoint Algorithm

• For max efficiency, f(x, y) = 0 is written as 2f(x, y) = 0. This

entirely eliminates floating point operations:

CENG 477 - Computer Graphics 14

y = y0

d = 2(y0 – y1) + (x1 – x0)

for x = x0 to x1 do:

draw(x, y)

if d < 0 then: // choose NE

y = y + 1

d += 2[(y0 – y1) + (x1 – x0)]

else: // choose E

d += 2(y0 – y1)

Page 15: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

The Midpoint Algorithm

• The presented algorithm works when the slope of the line, m,

is in range (0, 1]

• For other slope ranges, minor modifications to the algorithm is

required

• For instance if m ∈ (1, ∞), we need to swap the roles of x and

y

• Other cases must be adapted similarly

CENG 477 - Computer Graphics 15

Page 16: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Float vs Integer

• The midpoint algorithm was originally developed by Pitteway

in 1967

• Floating point arithmetic was very expensive at the time

• Does it still matter?

• We implemented both algorithms and drew one million lines

each between 1000 and 1400 pixels long:

– Test run on Intel Core i7 CPU at 3.2 GHz

– Compiled with g++ and –O2 option

– Basic algorithm: 7.2 seconds

– Optimized algorithm: 3 seconds

– So it still makes a difference!

CENG 477 - Computer Graphics 16

Page 17: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Interpolating Attributes

• What if the two end points of the line have a different color?

• The color across the line must smoothly change:

CENG 477 - Computer Graphics 17

Page 18: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Interpolating Attributes

• What if the two end points of the line have a different color?

• The color across the line must smoothly change:

CENG 477 - Computer Graphics 18

Page 19: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Interpolating Attributes

CENG 477 - Computer Graphics 19

(x0, y0)

(x1, y1)

(x, y)

x – x0

x1 – x0

For � ∈ [0,1] it is better to

interpolate in the x direction as

as it will produce a smoother

variation

Page 20: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Interpolating Attributes

• Assume that the color of the endpoints are c0 and c1

• The color of an intermediate point should be:

• At any pixel (x, y), we can compute it based on the horizontal

or vertical distance of the pixel to the first endpoint:

CENG 477 - Computer Graphics 20

� = 1 − � �' + ��%

where � is the interpolation variable

� =� − �'

�% − �'

Page 21: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Interpolating Attributes

• Assume that the color of the endpoints are c0 and c1

• The color of an intermediate point should be:

• It can also be computed incrementally

CENG 477 - Computer Graphics 21

� = 1 − � �' + ��%

where � is the interpolation variable

� �' + 1 = �% =1

�% − �'

� �' + 2 = �5 = �% +1

�% − �'

Page 22: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Algorithm with Interpolation

• Algorithm with color interpolation:

CENG 477 - Computer Graphics 22

y = y0

d = (y0 – y1) + 0.5(x1 – x0)

c = c0

dc = (c1 – c0) / (x1 – x0) // skip α; directly compute color increment

for x = x0 to x1 do:

draw(x, y, round(c))

if d < 0 then: // choose NE

y = y + 1

d += (y0 – y1) + (x1 – x0)

else: // choose E

d += (y0 – y1)

c += dc

Page 23: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Triangle Rasterization

• Initially all we have is the screen coordinates of three vertices

CENG 477 - Computer Graphics 23

(x0, y0)

(x1, y1)

(x2, y2)

Page 24: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Triangle Rasterization

• From them we compute line equations for the edges:

CENG 477 - Computer Graphics 24

(x0, y0)

(x1, y1)

(x2, y2)

f12

f20

f01

�'% �,� = �(�' −�%) + �(�%− �') + �'�%−�'�%

�%5 �, � = �(�% − �5) + �(�5− �%) + �%�5− �%�5

�5' �,� = �(�5 −�') + �(�'−�5) + �5�' −�5�'

Page 25: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Triangle Rasterization

• We then walk across the viewport to determine inside pixels:

CENG 477 - Computer Graphics 25

(x0, y0)

(x1, y1)

(x2, y2)

Page 26: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Triangle Rasterization

• For efficiency we may only walk within the bounding box of

the triangle:

CENG 477 - Computer Graphics 26

• For each pixel we visit, we must make

an inside test with respect to all three

edges

• We can simply plug-in the (x, y) value

of the visited pixel to each line

equation

• If all are negative, the pixel is inside

the triangle

(x0, y0)

(x1, y1)

(x2, y2)

Page 27: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Triangle Rasterization

• However, using barycentric coordinates will help us with

interpolation of attributes

CENG 477 - Computer Graphics 27

What are the barycentric coordinates of (x,

y)?

(x0, y0)

(x1, y1)

(x2, y2)

(x, y)A2

A1

A0

A = A0 + A1 + A2

� =?@

?, � =

?B

?, � =

?D

?

(x0, y0)

(x1, y1)

(x2, y2)

Page 28: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Triangle Rasterization

• We don’t actually need to compute the areas as the bases are

shared and will cancel out

CENG 477 - Computer Graphics 28

What are the barycentric coordinates of (x,

y)?

A2

A1

A0

A = A0 + A1 + A2

� =?@

?, � =

?B

?, � =

?D

?

� =�%5(�, �)

�%5(�', �') f12

f20

f01

(x0, y0)

(x1, y1)

(x2, y2)

� =�5'(�, �)

�5'(�% ,�%)

� =�'%(�, �)

�'%(�5,�5)

Page 29: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Overall Algorithm

CENG 477 - Computer Graphics 29

for y = ymin to ymax do:

for x = xmin to xmax do:

α = �%5(�,�) �%5(�', �')⁄

� = �5'(�, �) �5'(�%, �%)⁄

� = �'%(�, �) �'%(�5, �5)⁄

if � ≥ 0 and � ≥ 0 and � ≥ 0 then:

c = �c0 + �c1 + �c2

draw(x, y, round(c))

• Note that the computation of the barycentric coordinates can

also be made incremental for greater efficiency

Page 30: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Summary

• The result of rasterization is a set of fragments (pixel-to-be)

for each primitive

• Each fragment has interpolated values of attributes:

– Color values

– Texture coordinates

– Depth value

– Normals

– Or any user-defined attribute for vertices

CENG 477 - Computer Graphics 30

Page 31: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

CENG – 477

Introduction to Computer

GraphicsFragment Processing

Page 32: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Fragment Processing

• The previous stages of the pipeline (up to rasterization) is

generally known as the vertex pipeline

• Rasterization creates a set of fragments that make up the

interior region of the primitive

• The rest of the pipeline which operates on these fragments is

called the fragment pipeline

• Fragment pipeline is comprised of several operations

• The end result of fragment processing is the update of

corresponding locations in the framebuffer

CENG 477 - Computer Graphics 2

Page 33: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Fragment Processing

CENG 477 - Computer Graphics 3

Vertex

Pipeline

Vertices

Fragment

Pipeline

Framebuffer

Rasterization

Color

Buffer

Depth

Buffer

Stencil

Buffer

Page 34: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Fragment Processing

• Fragment pipeline is comprised of many stages:

– Following is OpenGL’s handling of the fragment pipeline

– Different renderers may implement a different set of stages

CENG 477 - Computer Graphics 4

Page 35: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Depth Buffer Test

• Among these, the depth buffer test is very important to render

primitives in correct order

• Without depth buffer, the programmer must ensure to render

primitives in a back to front order

– Known as painter’s algorithm:

CENG 477 - Computer Graphics 5

From wikipedia.com

Page 36: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Depth Buffer Test

• Binary space partitioning (BSP) trees may be used for this

purpose

• However, they are costly to generate and may require splitting

primitives due to impossible ordering cases:

CENG 477 - Computer Graphics 6

From wikipedia.com

Page 37: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Depth Buffer Test

• When memory was a very valuable resource, such algorithms

were implemented

• Quake3 was one of the main games that used painter’s

algorithm using BSP trees

• Each game level was stored as a huge BSP tree

– Read more at: https://www.bluesnews.com/abrash/chap64.shtml

CENG 477 - Computer Graphics 7

Page 38: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Depth Buffer Test

• Main Idea:

– At each pixel, keep track of the distance to the closest fragment that has

been drawn in a separate buffer

– Discard fragments that are further away than that distance

– Otherwise, draw the fragment and update the z-buffer value with the z

value of that fragment

• Requires an extra memory region, called the depth buffer, to

implement this solution

• At the beginning of every frame, the depth buffer is reset to

infinity (1.0f in practice if the depth range is [0.0f,1.0f])

• Depth buffer is also known as z-buffer

CENG 477 - Computer Graphics 8

Page 39: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Example

CENG 477 - Computer Graphics 9

Initial state

of depth buffer

Resulting

depth buffer

z-values of the

second triangle

Resulting

depth buffer

wikipedia.com

z-values of the

first triangle

Page 40: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Depth Range

• The range of values written to the depth buffer can generally

be controlled by the programmer

• In OpenGL, the command glDepthRange(zMin, zMax)

is used

• The default depth range is [0, 1]

• The z-value in the canonical viewing volume (CVV), which is

in range [-1, 1] is scaled to this range during the viewport

transform

• glDepthRange is to the z-values what glViewport(x,

y, width, height) is to the x- and y-values

CENG 477 - Computer Graphics 10

Page 41: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Z-Fighting

• Remember that the z-values get compressed to [0, 1] range

from the [-n:-f] range after projection and viewport transforms

• Observe how it looks for n = 10 and f = 50

CENG 477 – Computer Graphics 11

Page 42: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Z-Fighting

• Remember that the z-values get compressed to [0, 1] range

from the [-n:-f] range after projection and viewport transforms

• Observe the same for n = 10 and f = 200

CENG 477 – Computer Graphics 12

Page 43: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Z-Fighting

• With a limited precision depth buffer, fragments that are close

in depth may get mapped to the same z-value

CENG 477 – Computer Graphics 13

Page 44: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Z-Fighting

• The compression is more severe for with larger depth range

• This may cause a problem known as z-fighting:

– Objects with originally different (but close) z-values get mapped to the

same final z-value (due to limited precision) making it impossible to

distinguish which one is in front and which one is behind

CENG 477 – Computer Graphics 14

Page 45: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Z-Fighting

• To avoid z-fighting, the depth range should be kept as small as

possible for keeping the compression less severe

• Alternatively, a floating point depth buffer can used

– Unavailable in older hardware

– Supported in all modern GPUs

• Finally, the command glPolygonOffset can be used to

push and pull polygons a little to avoid z-fighting

CENG 477 – Computer Graphics 15

Page 46: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Scissor Test

• Scissor test is a per-fragment operation that discards fragments

outside a certain rectangular region

CENG 477 - Computer Graphics 16

Without scissor Scissor rectangle

Result

In OpenGL, glScissor

command is used for this

purpose

Note that this operation

is different from clipping

Page 47: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Stencil Test

• While scissor test can be used to mask out rectangles, stencil

test can be used to mask arbitrary fragments

• Requires a different buffer known as the stencil buffer

CENG 477 - Computer Graphics 17

Original Stencil Buffer Result

From research.ncl.ac.uk

Page 48: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Stencil Test

• Typically depth and stencil buffers are combined to produce

single buffer made of 24-bit depth and 8-bit stencil

information for each pixel

CENG 477 - Computer Graphics 18

D S D S D S …

D S D S D S …

D S D S D S …

D S D S D S …

Pixel 0 Pixel 1

Page 49: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Stencil Test

• Stencil buffer and stencil test can also be used to implement

one type of shadowing algorithms (we’ll learn this later)

CENG 477 - Computer Graphics 19

Doom 3

Page 50: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Alpha Blending

• Alpha blending is another fragment operation in which new

objects can be blended with the existing contents of the color

buffer for a variety of effects

CENG 477 - Computer Graphics 20

Without blending With blending

Page 51: CENG – 477 Introduction to Computer Graphicssaksagan.ceng.metu.edu.tr/courses/ceng477/files/pdf/week_08.pdf · arithmetic • What is the first f(M)? ... • The rest of the pipeline

Summary

• At the end of the pipeline, input vertices with connectivity

information end of populating certain regions of the

framebuffer

• This pipeline can be implemented on the software (CPU),

hardware (GPU) or both (CPU + GPU)

CENG 477 - Computer Graphics 21

Vertex

Pipeline

Vertices

Fragment

Pipeline

Framebuffer

Rasterization

Color

Buffer

Depth

Buffer

Stencil

Buffer