Lighting and Shading Wen-Chieh (Steve) Lin Department of Computer Science I-Chen Lin’ CG Slides, Doug James’ CG Slides, Angel, Interactive Computer Graphics, Chap 6 Shirley, Fundamentals of Computer Graphics, Chap 9
Illumination and Shading Is it a ball or a plate? What color should I set for each pixel? ILE5014 Computer Graphics 10F
Why Do We Need Shading? Suppose we color a sphere model. We get something like But we want ILE5014 Computer Graphics 10F
Shading Why does the image of a real sphere look like Light-material interactions cause each point to have a different color or shade Need to consider Light sources Material properties Location of the viewer Surface orientation ILE5014 Computer Graphics 10F
Factors that affect the “color” of a pixel Light sources Emittance spectrum (color) Geometry (position and direction) Directional attenuation Objects’ surface properties Reflectance spectrum (color) Geometry (position, orientation, and micro-structure) Absorption ILE5014 Computer Graphics 10F
Light and Matter Light strikes A Some of reflected light strikes B Some reflected Some absorbed Some of reflected light strikes B Some of reflected light reflected back to A and B, and so on Rendering equation describes this recursive process ILE5014 Computer Graphics 10F
Rendering Equation The infinite reflection and absorption of light can be described by the rendering equation [outgoing] = [emitted] + [reflected] (+[transmitted]) Cannot be solved in general Ray tracing is a special case for perfectly reflecting surfaces Rendering equation is global and includes Shadows Multiple reflection from object to object ILE5014 Computer Graphics 10F
Global Effects shadow transmission multiple reflection translucent surface ILE5014 Computer Graphics 10F
Local vs Global Illumination Correct shading requires a global calculation Incompatible with a pipeline model which shades each polygon independently (local rendering) However, in computer graphics, especially real time graphics, we are happy if things “look right” many techniques for approximating global effects exist ILE5014 Computer Graphics 10F
Local Illumination Adequate for real-time graphics. No inter-reflection, no refraction, no realistic shadow …. ILE5014 Computer Graphics 10F
Light Sources General light sources are difficult to simulated because we must integrate light coming from all points on the source ILE5014 Computer Graphics 10F
Simple Light Sources Point light Directional light Spotlight Model with position and color Directional light Distant source = infinite distance away (parallel) Spotlight a point light source with a small light cone radius Ambient light Same amount of light everywhere in scene Can model contribution of many sources and reflecting surfaces ILE5014 Computer Graphics 10F
Surface Types The smoother a surface, the more reflected light is concentrated in the direction A very rough surface scatters light in all directions smooth surface rough surface ILE5014 Computer Graphics 10F
Phong Reflection (Lighting) Model A simple model that can be computed rapidly Has three components Ambient Diffuse Specular Uses four vectors To source l To viewer v Normal n Perfect reflector r r = 2 (l · n ) n - l ILE5014 Computer Graphics 10F
Ambient Light The result of multiple interactions between (large) light sources and the objects in the environment. Ia = kaLa ambient light intensity ILE5014 Computer Graphics 10F
Diffuse Reflection Light scatters equally in all directions Reflected intensities vary with the direction of the light. Lambertian Surface Perfect diffuse reflector Id = kd (l·n) Ld ILE5014 Computer Graphics 10F
Specular Surfaces Most surfaces are neither perfectly diffuse nor perfectly specular (ideal reflectors) Incoming light reflects in concentrated directions close to the direction of a perfect reflection specular highlight ILE5014 Computer Graphics 10F
Modeling Specular Relections Phong model Is = ks Ls (r·v)α = ks Ls cosαΦ f shininess coefficient reflected intensity incoming intensity absorption coefficient ILE5014 Computer Graphics 10F
The Shininess Coefficient α Values of α between 100 and 200 correspond to metals Values between 5 and 10 give surfaces that look like plastic ILE5014 Computer Graphics 10F
Distance Terms Account for attenuation Original: Inversely proportional to the square of the distance d between light source and surface Modified: add a factor of the form 1/(a + bd +cd2) to the diffuse and specular terms The constant and linear terms soften the effect of the point source ILE5014 Computer Graphics 10F
Adding up the Components A primitive virtual world with lighting can be shaded by combining the three light components: I = Iambient + Idiffuse + Ispecular = kaLa + kd (l · n) Ld + ks Ls (v · r )α diffuse diffuse + specular diffuse + ambient ILE5014 Computer Graphics 10F
Coefficients 3 color channels and 3 surface types 9 coefficients for each point light source Lra, Lrd, Lrs, Lga, Lgd, Lgs, Lba, Lbd, Lbs Material properties 9 absorption coefficients kra, krd, krs, kga, kgd, kgs, kba, kbd, kbs Shininess coefficient α ILE5014 Computer Graphics 10F
Modified Phong Model r = 2 (l · n ) n - l Problem: In the specular component of Phong model, it requires the calculation of a new reflection vector and view vector for each vertex Blinn suggested an approximation using the halfway vector that is more efficient r = 2 (l · n ) n - l ILE5014 Computer Graphics 10F
The Halfway Vector h is the normalized vector halfway between l and v h = ( l + v )/ | l + v | ILE5014 Computer Graphics 10F
Using the Halfway Angle Replace (v · r )α by (n · h )b b is chosen to match shininess Note that halfway angle is half of angle between r and v if vectors are coplanar n β Θ-β h l r θ θ Φ v ILE5014 Computer Graphics 10F
Blinn lighting model Resulting model is known as the modified Phong or Blinn lighting model Specified in OpenGL standard and most real-time applications ILE5014 Computer Graphics 10F
Example Teapots with different parameters. ILE5014 Computer Graphics 10F
What’s next? So far, we know how to compute the color at a point given lighting model, illumination model, and surface normals The required computation can be huge if we compute for every point on a surface Exploit efficiency by decomposing curved surfaces into many small flat polygons ILE5014 Computer Graphics 10F
Polygonal Shading Curved surfaces are approximated by polygons How do we shade? Flat shading Interpolative shading Gouraud shading Phong shading (not Phong reflection model) Two questions: How do we determine normals at vertices? How do we calculate shading at interior points? ILE5014 Computer Graphics 10F
Flat Shading: Normal: given explicitly before vertex Shading constant across polygon Single polygon: first vertex Triangle strip: vertex n+2 for triangle n glShadeModel(GL_FLAT); glNormal3f(nx, ny, nz); glVertex3f(x, y, z); ILE5014 Computer Graphics 10F
Flat Shading Assessment Inexpensive to compute Appropriate for objects with flat faces Less pleasant for smooth surfaces ILE5014 Computer Graphics 10F
Flat Shading and Perception Lateral inhibition: exaggerates perceived intensity Mach bands: perceived “stripes” along edges ILE5014 Computer Graphics 10F
ILE5014 Computer Graphics 10F
Interpolative Shading Enable with glShadeModel(GL_SMOOTH); Calculate color at each vertex Interpolate color in interior Compute during scan conversion (rasterization) More expensive to calculate ILE5014 Computer Graphics 10F
Gouraud Shading Special case of interpolative shading How do we calculate vertex normals? Gouraud: average all adjacent face normals Requires knowledge about which faces share a vertex—adjacency info ILE5014 Computer Graphics 10F
Gouraud Shading Algorithm Find average normal at each vertex Apply Phong reflection model at each vertex Linearly interpolate vertex shades (colors) across each polygon I1(x1, y1) Ia(xa, ys) Ib(xb, ys) Is(xs, ys) I2(x2, y2) I4(x4, y4) I3(x3, y3) ILE5014 Computer Graphics 10F
Problem with Gouraud Shading See the artifact of specular highlight on a rotating sphere Highlight lies inside a polygon will not be apparent Highlight occurs at vertex will be spread unnaturally across all neighboring polygons if a highlight lies in the middle of a polygon, but does not spread to the polygon's vertex, it will not be apparent in a Gouraud rendering; conversely, if a highlight occurs at the vertex of a polygon, it will be rendered correctly at this vertex (as this is where the lighting model is applied), but will be spread unnaturally across all neighboring polygons via the interpolation method. The problem is easily spotted in a rendering which ought to have a specular highlight moving smoothly across the surface of a model as it rotates. Gouraud shading will instead produce a highlight continuously fading in and out across neighboring portions of the model, peaking in intensity when the intended specular highlight passes over a vertex of the model ILE5014 Computer Graphics 10F
Phong Shading Interpolate normals rather than colors Significantly more expensive Not supported in OpenGL Can be implemented using pixel shader In Phong shading we linearly interpolate a normal vector across the surface of the triangle from the three given normals. The surface normal is interpolated and normalized at each pixel and then used in the Phong reflection model to obtain the final pixel color. Phong shading is more computationally expensive than Gouraud shading since the reflection model must be computed at each pixel instead of at each vertex. In some modern hardware, variants of this algorithm are implemented using pixel or fragment shaders. ILE5014 Computer Graphics 10F
Phong Shading Algorithm Find vertex normals Interpolate vertex normals across edges Interpolate edge normals across each line Compute shades at each pixels of a polygon The most serious problem with Gouraud shading occurs when specular highlights are found in the middle of a large triangle. Since these specular highlights are absent from the triangle's vertices and Gouraud shading interpolates based on the vertex colors, the specular highlight will be missing from the triangle's interior. This problem is fixed by Phong shading. ILE5014 Computer Graphics 10F
Polygonal Shading Summary Gouraud shading Set vertex normals Calculate colors at vertices Interpolate colors across polygon Must calculate vertex normals! Must normalize vertex normals to unit length! ILE5014 Computer Graphics 10F
Problems with Interpolated Shading Polygonal silhouette Perspective distortion Orientation dependence Problems at shared vertices Unrepresentative vertex normals ILE5014 Computer Graphics 10F