1 of 69

Shading with Scan Conversion

Lecture 15

6.837 Fall 2001

Tong-Yee Lee

2 of 69

Phong Illumination Model

3 of 69

4 of 69

Shading

5 of 69

Cartoon shading

6 of 69

7 of 69

Lecture 15

Slide 7

6.837 Fall 2001

8 of 69

Flat Shading Or Facet Shading

The simplest shading method applies only one illumination calculation for each primitive. This technique is called constant or flat shading. It is often used on polygonal primitives.

Drawbacks:

    • the direction to the light source varies over the facet
    • the direction to the eye varies over the facet

Nonetheless, often illumination is computed for only a single point on the facet. Which one? Usually the centroid.

OpenGL picks any vertex of a polygon such as the first vertex

9 of 69

Solution: increase the resolutions of polygons

10 of 69

OpenGL: Flat shading

Compute shading at a representative point and apply to whole polygon

OpenGL uses one of the vertices

Advantages:

Fast - one shading value per polygon

Disadvantages:

Inaccurate

Discontinuities at polygon boundaries

11 of 69

Constant Shading with Ambient�Illumination Only

Lecture 15

Slide 11

6.837 Fall 2001

12 of 69

Facet Shading With Diffuse Reflection

Lecture 15

Slide 12

6.837 Fall 2001

13 of 69

Facet Shading

Even when the illumination equation is applied at each point of the faceted nature of the polygonal nature is still apparent.

To overcome this limitation normals are introduced at each vertex.

    • different than the polygon normal
    • for shading only (not backface culling or other computations)
    • better approximates smooth surfaces

14 of 69

15 of 69

Gouraud Shading

The Gouraud shading method applies the illumination model on a subset of surface points and interpolates the intensity of the remaining points on the surface. In the case of a polygonal mesh the illumination model is usually applied at each vertex and the colors in the triangles interior are linearly interpolated from these vertex values.

The linear interpolation can be accomplished using the plane equation method discussed in the lecture on rasterizing polygons.

16 of 69

Lecture 15

Slide 16

6.837 Fall 2001

17 of 69

Lecture 15

Slide 17

6.837 Fall 2001

Barycentric coordinate

 

/

It is slow……

18 of 69

Lecture 15

Slide 18

6.837 Fall 2001

19 of 69

Lecture 15

Slide 19

6.837 Fall 2001

Gourand Shading

Shade each vertex with it’s own location and normal

Linearly interpolate across the face

Advantages:

Fast - incremental calculations when rasterizing

Much smoother - use one normal per shared vertex to get continuity between faces

Disadvantages:

Specularities get lost

No highlights

Highlight is loss

if Gourand shading

is applied

20 of 69

21 of 69

Gouraud Shading with Diffuse Reflection

Lecture 15

Slide 21

6.837 Fall 2001

Do not average normals

everywhere. Only for the

approximately curve surface

22 of 69

Shading and OpenGL

OpenGL defines two particular shading models

Controls how colors are assigned to pixels

glShadeModel(GL_SMOOTH) interpolates between the colors at the vertices (the default)

glShadeModel(GL_FLAT) uses a constant color across the polygon

GL_SMOOTH

GL_FLAT

23 of 69

Phong Shading

In Phong shading (not to be confused with the Phong illumination model), the surface normal is linearly interpolated across polygonal facets, and the Illumination model is applied at every point.

A Phong shader assumes the same input as a Gouraud shader, which means that it expects a normal for every vertex. The illumination model is applied at every point on the surface being rendered, where the normal at each point is the result of linearly interpolating the vertex normals defined at each vertex of the triangle.

Phong shading will usually result in a very smooth appearance, however, evidence of the polygonal model can usually be seen along silhouettes.

24 of 69

Phong Shading

Lecture 15

Slide 24

6.837 Fall 2001

25 of 69

26 of 69

27 of 69

Lecture 15

Slide 27

6.837 Fall 2001

28 of 69

29 of 69

Example

Only differences in

these teapots are

the parameters

in the Phong model

30 of 69

Phong Shading with Correct Highlight

Lecture 15

Slide 30

6.837 Fall 2001

Edge Silhouettes

31 of 69

32 of 69

33 of 69

Gouraud shading

34 of 69

Lecture 15

Slide 34

6.837 Fall 2001

Note that:

vi can be either r,g,b, normal

vector, 3D vertex, z (depth)

35 of 69

v1

v2

a

b

Note that:

vi can be either xi,r,g,b, normal

vector, 3D vertex, z (depth)

36 of 69

37 of 69

38 of 69

39 of 69

40 of 69

Similarly, we can compute (x,y,z) for phong shading and Z (depth) , but do not need to normalize them

41 of 69

42 of 69

43 of 69

44 of 69

45 of 69

OpenGL

Zbuffer

0<=Z<=1

1 is maxmum

46 of 69

47 of 69

48 of 69

48

49 of 69

Scanline Conversion.

50 of 69

51 of 69

Ex:

floor(0.22) = 0�floor(1.22) = 1

ceil(0.22) = 1�ceil(1.22) = 2

ceil(1.0) =1

floor(1.0)=0

Note that: for Y: from ceil (Yi) to floor (Yj), too.

52 of 69

52

53 of 69

53

54 of 69

54

insert at x

ceil(x)

ceil(1.2)=2

ceil(1.0)=1

55 of 69

55

output at x

floor(x)

floor(1.2)=1

floor(1.0)= 0

56 of 69

floor(p1)

is not equal

to ceil(p1)

Ex:

floor(3.0) =2.0

ceil(3.0) = 3.0

Fill ceil(p0) to floor(p1)

Fill ceil(p1) to floor(p2)

so, p1 will be drawn only once from e2

Note that: for Y: from ceil (Yi) to floor (Yj)

For X: from ceil (Xi) to floor (Xj)

P1: e1’s end

And e2’ start

57 of 69

58 of 69

floor(p1) for e2, so it will not include

p1 from e2 but will be included from e1

59 of 69

60 of 69

61 of 69

Note that: for Y: from ceil (Yi) to floor (Yj)

For X: from ceil (Xi) to floor (Xj)

62 of 69

x = x +1/m

63 of 69

y_i = mx_i +a;

y_(i+1) = mx_(i+1)+a;

1 = mx_(i+1)-mx_i

1/m=x_(i+1)-x_i

x_(i+1) = x_i + 1/m

(x_i,y_i)

(x_i+1,y_i+1)

y=mx+a

64 of 69

Note: xmin is the x at the minimum y for the edge

ymax

xmin

65 of 69

Scan Conversion (Rasterization)

The Algorithm (page 98 in Computer Graphics FvDFH second ed.)

Create an Edge Table for the polygon being rendered, sorted on y.

Don’t include horizontal edges, they are handled by the edges they connect to (see page 95 in text).

65

(FvDFH, pages 92, 98)

Note: xmin is the x at the minimum y for the edge, not necessarily the minimum x of the edge. Hence xmin = 7 for edge AB.

Horizontal and

Vertical edges.

66 of 69

(Ymax,Xcurrent, 1/m)

67 of 69

Scan Conversion (cont.)

Once you have your Edge Table (ET) for the polygon, you’re ready to step through y coordinates and render scan lines:

1. Set y to the first non-empty bucket in the ET. This is bucket 1 in the example.

2. Initialize the Active Edge Table (AET) to be empty. The AET keeps track of which edges cross the current y scan line.

3. Repeat the following until the AET and ET are empty:

3.1 Add to the AET the ET entries for the current y. (edges AB, BC in example)

3.2 Remove from the AET entries where y = ymax. (none at first in example)

Then sort the AET on x. (order: {AB, BC})

3.3 Fill in pixel values on the y scan line using the x coordinates from the AET. Be wary of parity– use the even/odd test to determine whether to fill (see next slide).

3.4 Increment y by 1 (to the next scan line).

3.5 For every non-vertical edge in the AET update x for the new y (calculate the next intersection of the edge with the scan line).

Note: the algorithm in the book (presented here and in course lecture notes) attempts to fix the problems that occur when polygons share an edge, by not rasterizing the top-most row of pixels along an edge.

67

68 of 69

Active Edge Table Example

Example of an AET containing edges {FA, EF, DE, CD} on scan line 8:

  • 3.1: (y = 8) Get edges from ET bucket y (none in this case, y = 8 has no entry)
  • 3.2: Remove from the AET any entries where ymax = y (none here)
  • 3.3: Draw scan line. To handle multiple edges, group in pairs: {FA,EF}, {DE,CD}
  • 3.4: y = y+1 (y = 8+1 = 9)
  • 3.5: Update x for non-vertical edges, as in simple line drawing.

68

(FvDFH pages 92, 99)

Current X

Slope

Y val

69 of 69

Active Edge Table Example (cont.)

  • 3.1: (y = 9) Get edges from ET bucket y (none in this case, y = 9 has no entry in ET)
    • “Scan line 9” shown in fig 3.28 below
  • 3.2: Remove from the AET any entries with ymax = y (remove FA, EF)
  • 3.3: Draw scan line between {DE, CD}
  • 3.4: y = y+1 = 10
  • 3.5: Update x in {DE, CD}
  • 3.1: (y = 10) (Scan line 10 shown in fig 3.28 below)
  • And so on…

69

(FvDFH pages 92, 99)

(FvDFH pages 92, 99)

Current X

Slope

Y val