Shading with Scan Conversion
Lecture 15
6.837 Fall 2001
Tong-Yee Lee
Phong Illumination Model
Shading
Cartoon shading
Lecture 15
Slide 7
6.837 Fall 2001
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:
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
Solution: increase the resolutions of polygons
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
Constant Shading with Ambient�Illumination Only
Lecture 15
Slide 11
6.837 Fall 2001
Facet Shading With Diffuse Reflection
Lecture 15
Slide 12
6.837 Fall 2001
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.
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.
Lecture 15
Slide 16
6.837 Fall 2001
Lecture 15
Slide 17
6.837 Fall 2001
Barycentric coordinate
/
It is slow……
Lecture 15
Slide 18
6.837 Fall 2001
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
Gouraud Shading with Diffuse Reflection
Lecture 15
Slide 21
6.837 Fall 2001
Do not average normals
everywhere. Only for the
approximately curve surface
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
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.
Phong Shading
Lecture 15
Slide 24
6.837 Fall 2001
Lecture 15
Slide 27
6.837 Fall 2001
Example
Only differences in
these teapots are
the parameters
in the Phong model
Phong Shading with Correct Highlight
Lecture 15
Slide 30
6.837 Fall 2001
Edge Silhouettes
Gouraud shading
Lecture 15
Slide 34
6.837 Fall 2001
Note that:
vi can be either r,g,b, normal
vector, 3D vertex, z (depth)
v1
v2
a
b
Note that:
vi can be either xi,r,g,b, normal
vector, 3D vertex, z (depth)
Similarly, we can compute (x,y,z) for phong shading and Z (depth) , but do not need to normalize them
OpenGL
Zbuffer
0<=Z<=1
1 is maxmum
48
Scanline Conversion.
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
53
54
insert at x
ceil(x)
ceil(1.2)=2
ceil(1.0)=1
55
output at x
floor(x)
floor(1.2)=1
floor(1.0)= 0
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
floor(p1) for e2, so it will not include
p1 from e2 but will be included from e1
Note that: for Y: from ceil (Yi) to floor (Yj)
For X: from ceil (Xi) to floor (Xj)
x = x +1/m
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
Note: xmin is the x at the minimum y for the edge
ymax
xmin
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.
(Ymax,Xcurrent, 1/m)
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
Active Edge Table Example
Example of an AET containing edges {FA, EF, DE, CD} on scan line 8:
68
(FvDFH pages 92, 99)
Current X
Slope
Y val
Active Edge Table Example (cont.)
69
(FvDFH pages 92, 99)
(FvDFH pages 92, 99)
Current X
Slope
Y val