lecture 09: graphics pipeline (rasterization and shading)
TRANSCRIPT
IntroductionRasterization
Z-bufferingShading
Graphics 2010/2011, 4th quarter
Lecture 9
Graphics pipeline (rasterization and shading)
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Graphics pipeline
3D graphics pipeline:
a specialhardware/softwaresubsystem that efficientlydraws 3D primitives inperspective
a sequence of operationstransforming 3D modelsinto pixels on the screen
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Graphics pipeline - part 1 (recap)
Perspective projection by matrixmultiplication:
xpixel
ypixel
zcanonical
1
= MvpMperMcam
xyz1
This is an important part of the graphicspipeline
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Graphics pipeline - part 2 (recap)
Triangles that lie (partly)outside the view frustumneed not be projected,and are clipped.
The remaining trianglesare projected if they arefront facing.
Projected triangles haveto be shaded and/ortextured.
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Rasterizing triangles
We know how to project thevertices of a triangle in ourmodel onto pixel centers.
To draw the complete triangle,we have to decide which pixelsto turn on.
For now, let’s assume that allpixels of the triangle get thesame color.
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Limiting the number pixels to consider
Instead of testing for all pixels in the image if they belong to thetriangle, our book suggests to test only pixels in the bounding boxof the triangle.
The bounding box of apolygone can be specified bytwo points ~b0 and ~b1 with
~b0 = (min{xi},min{yj})~b1 = (max{xi},max{yj})
for all i, j = 1, · · · , n(n = number of vertices)
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Limiting the number pixels to consider
However, for long and skinnytriangles that are diagnonallyoriented, considering all pixelsin their bounding box may stillbe quite inefficient.
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Rasterizing general polygons
Let’s look at a more efficientway to draw triangles.
The method actually works forgeneral polygons; for triangles,it becomes even more efficient.
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
From pixel centers to pixel coordinates
First, let’s move our attentionfrom pixels to the underlyingcoordinate system where thepixel centers have integercoordinates.
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
From pixel centers to pixel coordinates
First, let’s move our attentionfrom pixels to the underlyingcoordinate system where thepixel centers have integercoordinates.
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
From pixel centers to pixel coordinates
First, let’s move our attentionfrom pixels to the underlyingcoordinate system where thepixel centers have integercoordinates.
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Scanline conversion
Idea of the scanline algorithm:
Instead of looking at eachpixel individually, we draw ourpolygon one scanline at atime, from bottom to top,thus taking advantage ofscanline coherence.
012345
6789
0 1 2 3 4 5 6 7 8 9 10
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Scanline conversion
Scanline 3 intersects twoedges; these are called activeedges.
If we know the intersections ofthe active edges with thecurrent scanline, then we knowwhich pixels to set.
012345
6789
0 1 2 3 4 5 6 7 8 9 10
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Computing intersections
The left active edge runs from(1, 1) to (3, 8).
The implicit equation for theline through these points is3.5x− y − 2.5 = 0.
The x-coordinate of theintersection of the edge withscanline 3 is 14
7 , so we knowthat we have to set pixelsstarting from x = 2.
012345
6789
0 1 2 3 4 5 6 7 8 9 10
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Computing intersections incrementally
Computing intersections ofscanlines and edges requires adivision. These are expensive,and we’d like to avoid them.
We use the fact that theintersection of an edge withscanline i is related to theintersection with scanlinei− 1.
This is called verticalcoherence.
012345
6789
0 1 2 3 4 5 6 7 8 9 10
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Computing intersections incrementally
For the left active edge, theincrease in x-coordinate fromone scanline to the next is
∆x = 3−18−1 = 2
7
012345
6789
0 1 2 3 4 5 6 7 8 9 10
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Computing intersections incrementally
Example:
The x-coordinate at scanline 1is obviously 1.
Hence, we have:
scanline x-coordinate1 12 12
73 14
74 16
7 012345
6789
0 1 2 3 4 5 6 7 8 9 10
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Data structures: edge table (ET)
In the Edge Table (ET), we maintainfor every scanline a list of edges thatstart at the scanline.
Each edge record contains:
The x-coordinateof the lowest vertex.
The y-coordinateof the highest vertex
The value of ∆x
for the edge.
012345
6789
0 1 2 3 4 5 6 7 8 9 10
ET:1: (1, 8, 2/7), (1, 2, 8)2:(9, 6,−1/2)4:(4, 8,−1/4), (4, 6, 1.5)
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Data structures: active edge table (AET)
When we proceed from one scanlineto the next, we also maintain anActive Edge Table (AET).
This table contains the edge recordfrom the ET that are intersected bythe current scanline.
Whenever we move to the nextscanline, we add the ∆x-values ofthe active edges to the x-value.
012345
6789
0 1 2 3 4 5 6 7 8 9 10
ET:1: (1, 8, 2/7), (1, 2, 8)2:(9, 6,−1/2)4:(4, 8,−1/4), (4, 6, 1.5)
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Data structures: active edge table (AET)
AET:
scanline: edge records:
012345
6789
0 1 2 3 4 5 6 7 8 9 10
ET:1: (1, 8, 2/7), (1, 2, 8)2:(9, 6,−1/2)4:(4, 8,−1/4), (4, 6, 1.5)
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Active edge table
An edge record is removed from theAET when the current scanlinereaches the top of the edge.
Recall that the 2nd entry of the edgerecord stores the y-coordinate of thetop vertex.
012345
6789
0 1 2 3 4 5 6 7 8 9 10
ET:1: (1, 8, 2/7), (1, 2, 8)2:(9, 6,−1/2)4:(4, 8,−1/4), (4, 6, 1.5)
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Scanline conversion
Summary of the whole algorithm:
AET = ∅for i = 0 to n− 1 do for each scanline
update(AET,i) delete edge records for which y == iadd ∆x to x-values
append(AET,ET[i]) add edges starting heresort(AET) sort by x-valueJoinLines(AET,i) set pixels between pairs of edges
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Scanline conversionIncremental updatesEdge table and active edge table
Active edge table
AET:
1: (1, 8, 27), (1, 2, 8)
2: (1 27, 8, 2
7), (9, 2, 8), (9, 6, −1
2)
3: (1 47, 8, 2
7), (8 1
2, 6, −1
2)
4: (1 67, 8, 2
7), (4, 8, −1
4), (4, 6, 1 1
2), (8, 6, −1
2)
5: (2 17, 8, 2
7), (3 3
4, 8, −1
4), (5 1
2, 6, 1 1
2), (7 1
2, 6, −1
2)
6: (2 37, 8, 2
7), (3 1
2, 8, −1
4), (7, 6, 1 1
2), (7, 6, −1
2)
7: (2 57, 8, 2
7), (3 1
4, 8, −1
4)
8: (3, 8, 27), (3, 8, −1
4)
012345
6789
0 1 2 3 4 5 6 7 8 9 10
ET:1: (1, 8, 2/7), (1, 2, 8)2:(9, 6,−1/2)4:(4, 8,−1/4), (4, 6, 1.5)
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Interpolating z-values
Z-buffering with scanline conversion
Recall that hidden surface elimination can be done efficiently withz-buffer algorithm. But how do we get the z-values for each pixel?
Again, we can compute these efficiently with bilinear interpolation,maintaining a ∆z value for every edge, and computing ∆ valuesfor the z-increment along every scanline.
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Bilinear interpolation to color triangles
With this we can linearlyinterpolate color
1 between two vertices
2 between two edges
Q: How to do this efficiently?What about phong shading?→ We will learn this in a laterlecture
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Bilinear interpolation to color triangles
With this we can linearlyinterpolate color
1 between two vertices
2 between two edges
Q: How to do this efficiently?What about phong shading?→ We will learn this in a laterlecture
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Gouraud shading / interpolation
For every scanline, color-values at the edges of the triangle arecomputed by linearly interpolating the color values at the vertices.
This can be done incrementaly, by precomputing ∆r, ∆g, and ∆b
values, and adding these to the r, g and b values of the previousscanline.
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Gouraud shading / interpolation
Colors along each scanline are also determined by linearinterpolation. Again, this is done incrementally, by precomputing∆ values.
So for each triangle, we do four devisions for each of its edges, tocompute the ∆x, ∆r, ∆g, and ∆b values, and three divisions forevery scanline it spans, to compute the color increments inhorizontal direction.
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Gouraud shading / interpolation
Gouraud shading producesdiffuse lighting that we usuallysee on matte surfaces.
We had diffuse reflectionbefore, remember?
We called it Lambertianshading.
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Lambertian shading
The direction of the incident lightinfluences the intensity of thereflected light.
Lambert’s cosine law:
c ∼ cos θl or c ∼ ~n ·~l
with c being the color at thisparticular point
ne
θe
lθl
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Lambertian shading
Notice that we have to do thiscalculations in world coordinates(because of the normals).
Remember that the light source isonly described by the vector ~l, i.e. byits direction and not its location.
We call such light sources directionallight (in contrast to, e.g., a spotlightthat is defined by direction andlocation).
ne
θe
lθl
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Lambertian shading
Lambertian shading:
c = crcl max(0, ~n ·~l)
with
c = the perceived color value
cr = diffuse reflectance (fractionof light reflected by surface)
cl = RGB intensity term(intensity of light source)
(Note: this is the notation from chapter 10
in the book, otherwise it’s the same as in
lecture 3 / chapter 4)
ne
θe
lθl
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Gouraud shading / interpolation
But how do we computediffuse lighting at the verticesof the triangles/surfaces?
It depends.
We may specify that normalsare indeed perpendicular to thesurfaces.
We may also make surfaceshave their vertices shared, andsomehow interpolate vertexnormals.
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Considering ambient light
Problem: points with normals facingaway from the light will be black.We need some global illumination.
Some heuristics to do this:
Put dim light source at eyeposition or
Add a constant color term tothe color of each object(simulating a “global light”)
We already know that the latter iscalled ambient shading:
c = cr(ca + cl max(0, ~n ·~l))
ne l
h
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Glossy reflection
Glossy reflection / Phong shading
(source: http://en.wikipedia.org/wiki/Phong shading)
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Glossy reflection
For glossy reflection, the viewingdirection matters.
Reflection is maximized when theangle of the reflected light equals theangle of the incident light.
ne l
h
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Glossy reflection
In the Phong model, we use thevector h that lies “halfway” between~e and ~l:
c = cl(~n · ~h)p
with
h = ~e+~l / ‖~e+~l‖(note: ~e, ~l must be unit vectors)
cl = RGB intensity term(intensity of light source)
p = Phong exponent
(Note: again we are using the notation of
chapter 10 here)
ne l
h
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Glossy reflection
In the Phong model, we use thevector h that lies “halfway” between~e and ~l:
c = cl(~n · ~h)p
with
h = ~e+~l / ‖~e+~l‖(note: ~e, ~l must be unit vectors)
cl = RGB intensity term(intensity of light source)
p = Phong exponent
(Note: again we are using the notation of
chapter 10 here)
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Glossy reflection
Influence of the Phong exponent p:
(cf. textbook, fig. 10.6, page 237 (9.6, p. 195 in 2nd ed.))Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Phong normal interpolation
Interpolating color values doesn’t combine well with the Phongmodel: highlights in the interior of triangles are completely lost.
However, we can interpolate normals instead, and do thecomputations of the Phong model using the interpolated normals.
Similarly to color interpolation, we can use barycentric coordinatesfor that:
~n = α ~n0 + β ~n1 + γ ~n2
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Phong normal interpolation
Barycentric coordinates for Phong normal interpolation:
~n = α ~n0 + β ~n1 + γ ~n2
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Bringing it all together . . .
Combining ambient, Lambertian, and Phong shading we get
c = cr(ca + cl max(0, n · l)) + cl(~h · ~n)p
Variations exist, e.g. we can add a control term cp that allows usto dim the highlight of the Phong shading:
c = cr(ca + cl max(0, n · l)) + clcp(~h · ~n)p
Or we can add multiple light sources (cf. lecture 3).Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Conclusion
Notice that we already covered almost all of the ”basic stuff” i.e.chapters 1-11 of the book:
We will not cover chapter 9, signal processing(2nd edition: chapter 4)
We will not look into chapter 3: raster images(2nd edition: chapter 3, raster algorithms)
We will however look into
Chapter 4, ray tracing and 13, more ray tracing(2nd edition: chapter 10, ray tracing)
And next time, we’ll cover two topics not in the textbook, i.e.
Radiosity (global illuminations)
Shadows
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)
IntroductionRasterization
Z-bufferingShading
Gouraud shading / interpolationAmbient shadingPhong shading / glossy reflection
Conclusion
Remember: tutorials start again today (and it is highlyrecommended to go there!)
TA sessions for the practicals are now
Tuesdays, 11:00-17:00
Wednesdays, 13:15-17:00
Thursdays, 15:15-17:00
Notice that the deadline is very soon. Don’t wait till the last week!
Graphics 2010/2011, 4th quarter Lecture 09: graphics pipeline (rasterization and shading)