Shading Algorithms Ron Goldman Department of Computer Science Rice University
Illumination and Shading Standard Assumptions Curved surfaces are approximated by planar polygons. All light sources are point light sources (at infinity). Illumination Models Ambient Light Diffuse Reflection (Scattering) Specular Reflection Shading Models Uniform (Flat) Gouraud Phong
Shading Algorithms Uniform -- constant intensity for each polygon (Flat) Gouraud -- linear interpolation of intensity for each polygon along scan lines Phong -- linear interpolation of normal vectors (non-linear interpolation of intensity) for each polygon along scan lines Coordinate Dependent Methods Speed vs. Generality
Uniform Shading Assumptions 1. The surfaces really are polygonal. N = surface normal = constant 2. Light source at infinity L = vector to light source = constant L N = constant R = 2(L N)N L = constant 3. Eye at infinity V = vector to eye = constant N V = constant R V = constant
Polygon Normals Normal to a Polygon N = (P k O) (P k +1 O) = P k P k +1 P k O + P k +1 O Newell s Formula m N = P k P k +1 k =0 N = 2 Area(Polygon) (Green s Theorem) Avoids problem of collinear vertices P 2 P1 P 3 O
Uniform Shading (continued) Intensity I uniform = { I a k a + I pk d (L N) 1424 3 ambient diffuse + I p k s (R V) n 1 42 44 3 specular N, L, R,V constant I uniform constant along each polygon L N R V θ θ α Surface Mach Bands Discontinuities in intensity along polygon edges. Individual polygons highly visible. Heightened by physiological effects of the eye.
Purpose To Reduce Mach Bands Gouraud Shading Method Linear Interpolation of Intensities Incremental Computations -- Only One Addition per Pixel (Fast!) Strategy 1. Compute intensity at vertices, using an average unit normal vector. N vertex = vertex Polygon N polygon vertex Polygon N polygon (Normalized Average) 2. Interpolate intensity along edges. 3. Interpolate intensity along scan lines. Observation Polygon intensities agree along common edges. Integrates well with hidden surface scan line algorithm.
Polygon Vertex Edge Vertex Edge Vertex Scan Line Scan Line Edge Edge Vertex Vertex Vertex Edge Edge
Linear Interpolation Points Intensities L(t) = (1 t)p 1 + tp 2 I(t) = (1 t)i 1 + ti 2 L(t) = P 1 + t(p 2 P 1 ) I(t) = I 1 + t(i 2 I 1 ) L(t + Δt) = P 1 +(t + Δt)(P 2 P 1 ) I(t + Δt) = I 1 +(t + Δt)(I 2 I 1 ) ΔL = Δt(P 2 P 1 ) ΔI = Δt(I 2 I 1 ) Δx = Δt(x 2 x 1 ) I new = I old + ΔI Δy = Δt(y 2 y 1 ) Δz = Δt(z 2 z 1 ) Observation: If we know Δt, then we can compute ΔI.
Along a Scan Line Incremental Intensity Computation } I 1 Δx = 1 I 2 P 1 = (x 1, y 1,z 1 ) P 2 = (x 2, y 2, z 2 ) (x, y, z) (x + Δx, y + Δy, z + Δz) Δx = 1 Δt = 1/(x 2 x 1 ) ΔI = (I 2 I 1 )Δt = (I 2 I 1 )/(x 2 x 1 ) Next Scan Line I 2 I 1 P 1 = (x 1, y 1,z 1 ) (x, y, z) (x + Δx, y + Δy, z + Δz) P 2 = (x 2, y 2, z 2 ) Δy =1 Δt =1/(y 2 y 1 ) { ΔI = (I 2 I 1 )Δt = (I 2 I 1 )/(y 2 y 1 ) Δy =1
Orientation Dependence and Independence Problem Gouraud (Phong) Shading is Orientation Dependent Solution Subdivide the Polygons into Triangles Gouraud (Phong) Shading for Triangles is Orientation Independent Note Shading Algorithm Depends on the Particular Triangular Subdivision Shading Algorithm Gives Different Results for Different Subdivisions
Coordinate Dependence White White White White All White White Not All White Gray Black White Black
Barycentric Coordinates and Coordinate Independence (1 t)i 1 +ti 2 I 1 (1 s)i 1 + si 3 I I 2 I 3 β 3 I 1 β 2 I β 1 I 2 I 3 I = (1 u) ((1 t)i 1 +ti 2 ) +u ((1 s)i 1 + si 3 ) I = β 1 I 1 +β 2 I 2 + β 3 I 3 ( ) I = (1 u)(1 t)+u(1 s) I 144424 443 1 + (1 u)t 14 24 3 I 2 +(us) { I 3 β 1 ( ) β 2 β 3 Barycentric Coordinates β 1 +β 2 +β 3 =1 P = β 1 P 1 + β 2 P 2 +β 3 P 3 = P 1 + β 2 (P 2 P 1 )+β 3 (P 3 P 1 ) β 1,β 2,β 3 unique
Purpose To Reduce Mach Bands To Mimic Curved Surfaces Naive Phong Shading Method Linear Interpolation of Normal Vectors Incremental Computation of Normal Vectors Strategy 1. Compute unit normals at vertices by averaging the unit normals of the polygons to which the vertex belongs. N vertex = vertex Polygon N polygon vertex Polygon N polygon (Normalized Average) 2. Interpolate normals along edges. (Renormalize) 3. Interpolate normals along scan lines. (Renormalize) 4. Use normals to calculate intensities.
Linear Interpolation Points Normals L(t) = (1 t)p 1 + tp 2 N(t) = (1 t)n 1 + tn 2 L(t) = P 1 + t(p 2 P 1 ) N(t) = N 1 + t(n 2 N 1 ) L(t + Δt) = P 1 +(t + Δt)(P 2 P 1 ) N(t + Δt) = N 1 + (t + Δt)(N 2 N 1 ) ΔL = Δt(P 2 P 1 ) ΔN = Δt(N 2 N 1 ) Δx = Δt(x 2 x 1 ) N new = N old + ΔN N old + ΔN Δy = Δt(y 2 y 1 ) Δz = Δt(z 2 z 1 ) Observation: If we know Δt, then we can compute ΔN.
Along a Scan Line Incremental Normal Computation } N 1 Δx = 1 N 2 P 1 = (x 1, y 1,z 1 ) P 2 = (x 2, y 2, z 2 ) (x, y, z) (x + Δx, y + Δy, z + Δz) Δx = 1 Δt = 1/(x 2 x 1 ) ΔN = (N 2 N 1 )Δt = (N 2 N 1 )/(x 2 x 1 ) Next Scan Line N 2 N 1 P 1 = (x 1, y 1,z 1 ) (x, y, z) (x + Δx, y + Δy, z + Δz) P 2 = (x 2, y 2, z 2 ) Δy =1 Δt =1/(y 2 y 1 ) { Δy =1 ΔN = (N 2 N 1 )Δt = (N 2 N 1 )/(y 2 y 1 )
Clever Implementation -- Diffuse Reflection Diffuse Reflection I diffuse = I p k d (L N) = I p k d (1 t)(l N 1 )+t(l N 2 ) (1 t)n 1 +t N 2 J(t) = (1 t)(l 12 N 43 1 ) +t(l 14 2 N 43 2) J 1 J 2 -- Similar to Gouraud (Scalars) -- Avoids Dot Products Along Scan Lines N(t) = (1 t)n 1 +t N 2 -- N next = N current +ΔN -- Update Normal Vector Incrementally -- d next = N next = N next N next -- Normalization by Newton s Method
Normalization by Newton s Method Square Root by Newton s Method x 2 2 d next = 0 -- x 0 = d current -- x n+1 = x n x n 2 d next 2 2x n
Clever Implementation -- Specular Reflection Specular Reflection I specular = I p k s (RV) n -- R = 2(L N)N L I specular = I p k s ( 2(L N)(N V ) (L V)) n -- N = (1 t)n 1 +t N 2 2 {(1 t)(l N 1 )+t(l N 2 )} {(1 t)(v N 14444 244443 1 )+t(v N 2 )} 144442 44443 Similar to Gouraud Similar to Gouraud I specular = I p k s 2 (1 t)n 1 +t N 2 (LV 12 3 ) Constant n -- (1 t)n 1 +t N 2 2 = N + ΔN 2 = (N + ΔN)(N +ΔN) -- No Square Root
Spherical Linear Interpolation Slerp slerp(n 1,N 2,t) = sin ((1 t)φ) sin( tφ) N 1 + sin(φ) sin(φ) N 2 cos(φ) = N 1 N 2 -- sin(φ) = 1 (N 1 N 2 ) 2 slerp maps unit vectors to unit vectors more natural than linear interpolation -- avoids square roots Expand incrementally Expand Δsine and Δcosine Precompute sin(δtφ) and cos(δt φ)
Interpolating Unit Vectors Vector Interpolation Problem N 1 N t φ tφ N 0 N 0, N 1 = unit vectors Vector Interpolation Formula N t = sin ((1 t)φ) sin( tφ) N 0 + sin(φ) sin(φ) N 1
Incremental Spherical Linear Interpolation Slerp slerp(n 1,N 2,t) = sin ((1 t)φ) sin( tφ) N 1 + sin(φ) sin(φ) N 2 cos(φ) = N 1 N 2 sin(φ) = 1 (N 1 N 2 ) 2 Incremental Computation slerp(n 1,N 2,t +Δt) = sin ((1 t Δt)φ ) N 1 + sin(φ) sin ((t +Δt)φ) N 2 sin(φ) -- sin ((t +Δt)φ ) = sin(tφ)cos(δtφ)+ cos(tφ)sin(δtφ) -- sin ((1 t Δt)φ) = sin((1 t )φ)cos(δt φ) cos((1 t)φ)sin(δt φ) -- cos ((t + Δt)φ) = cos(tφ)cos(δtφ) sin(tφ)sin(δtφ) -- cos ((1 t Δt)φ ) = cos((1 t )φ)cos(δt φ)+sin((1 t)φ)sin(δt φ)
Fast Phong Shading with SLERP Diffuse Reflection I diffuse = I p k d (L N) = I p k d sin(φ) ( sin ((1 t)φ ) L N 1 +sin( tφ) L N 2 ) Specular Reflection I specular = I p k s (RV) n -- R = 2(L N)N L I specular = I p k s ( 2(L N)(N V ) (L V)) n 2 I specular = I p k s sin 2 (φ) ( ) sin (1 t)φ sin ( (1 t)φ )L N 1 + sin( tφ)l N 2 ( ( )V N 1 + sin( tφ)v N 2 ) (L 12 V) 3 Constant n
Comparison to Gouraud Shading Similar to Gouraud Shading Intensity calculations replaced by normal calculations Integrates well with hidden surface scan line algorithm Coordinate dependent method Slower than Gouraud Shading Intensities must be recalculated at each point from the normal vector I = { I a k a + I pk d (L N) 1424 3 ambient diffuse + I p k s (R V) n 1 42 4 3 N is different for each point R is different for each point specular More Accurate than Gouraud Shading Simulates curved surfaces (varying normal vectors) Further reduces Mach Bands Tighter highlights (and spotlights) than Gouraud shading Gouraud shading interpolates intensities -- smoothes out highlights
Problems with Gouraud and Phong Shading Orientation Dependence Problem: Linear Interpolation for Polygons is Orientation Dependent Visual Effect: Shading Changes Abruptly During Animation Solution: Subdivide into Triangles