CSC 830 Computer Graphics Lecture 5 Shading Course Note Credit: Some of slides are extracted from the course notes of prof. Mathieu Desburn (USC) and prof. Han-Wei Shen (Ohio State University). 1 Shading Determining the light traveling from a point in the scene to the viewer’s eye Images courtesy of Watt, Watt & Watt, and Foley & van Dam 2 Shading Light comes from many sources: light emitted reflected transmitted scattered absorbed reflection scattering absorption transmission emission 3 Local versus Global Illumination Local Illumination Only considers direct illumination No reflection No refraction Shadows possible Global Illumination Considers indirect illumination Reflection Refraction Shadows 4 Local versus Global Illumination Direct Illumination P2 P1 Indirect Illumination We will do local only for this lecture… 5 Local illumination • Only consider the light, the observer position, and the object material properties 6 Local versus Global Illumination Images courtesy of Francois Sillion To understand shading properly, we need to review some basic notions of physics… 7 Basic Illumination Model • Simple and fast method for calculating surface intensity at a given point • Lighting calculation are based on: – The background lighting conditions – The light source specification: color, position – Optical properties of surfaces: • Glossy OR matte • Opaque OR transparent (control refection and absorption) 8 Radiance Radiance: Power per unit projected area perpendicular to the ray, per unit solid angle in the direction of the ray N I ( x, ) d dA Think of it as a flux of photons leaving the surface… 9 Irradiance Irradiance: Radiant energy per unit area E dE I ( x, ) cos d I ( x, ) Think of it as a flux of photons bombarding the surface… 10 BRDF Bidirectional Reflection Distribution Function I ( x, r ) f ( x, i , r ) dE ( x, i ) Determines the fraction of light from an incoming direction reflected to an outgoing direction Relates reflected radiance to incoming irradiance… 11 Image courtesy of Watt, 3D Computer Graphics Properties of the BRDF Reciprocity f ( x, i , r ) f ( x, r , i ) In photography and holography, reciprocity refers to the relationship between the intensity of the light and duration of the exposure that result in identical exposure. Within the normal range for intensity and time for the film, the reciprocity law states that exposure = intensity × time. Outside the normal range the reciprocity law breaks down, which is known as reciprocity failure. 12 Properties of the BRDF Anisotropy f ( x, i , i , r , r ) f ( x, i , i , r , r ) Anisotropy (the opposite of isotropy) is the property of being directionally dependent. In the field of computer graphics, an anisotropic surface will change in appearance as it is rotated about its geometric normal, as is the case with velvet. Anisotropic scaling occurs when something is scaled by different amounts in different directions. 13 Reflectance Equation BRDF allows us to calculate outgoing light, given incoming light I from i ( x, r ) f ( x, i , r ) dE ( x, i ) Image courtesy of Watt, 3D Computer Graphics 14 Splitting the BRDF In practice, BRDF can be really funky… To simplify, we divide the BRDF into 3 components: f f ideal diffuse f directional diffuse f ideal specular M M L N L N L N 15 Reflectance • 3 forms 16 Slide Courtesy of Dutre et. al on SIGGRAPH 2001 Ideal Specular Reflection Reflection is only in the mirror direction M L N 17 Ideal Diffuse Reflection Reflection is equal in all directions f ( x, i , r ) diffuse L N 18 Directional Diffuse Reflection Reflection is concentrated around the mirror direction M L N 19 Ambient Light If a surface is visible from the eye, but not a light, it will be rendered black (if indirect light is not considered). Ambient light is an approximation to indirect light Difficult to compute Approximate this as a constant Or a light source at the eye 20 Phong Reflection Assume point lights and direct illumination only I I ambient I diffuse I specular 21 Diffuse Light • The illumination that a surface receives from a light source and reflects equally in all directions • This type of reflection is called Lambertian Reflection (thus, Lambertian surfaces) • The brightness of the surface is indepenent of the observer position (since the light is reflected in all direction equally) 22 Lambert’s Law • How much light the surface receives from a light source depends on the angle between its angle and the vector from the surface point to the light (light vector) • Lambert’s law: the radiant energy ’Id’ from a small surface da for a given light source is: Id = IL * cos() IL : the intensity of the light source is the angle between the surface normal (N) and light vector (L) 23 Phong Diffuse Component Diffuse component depends only on incident angle. I diffuse I l k d cos I l k d ( N L) Note: L and N are unit… Image courtesy of Watt, 3D Computer Graphics 24 Examples Sphere diffusely lighted from various angles ! 25 Specular Light These are the bright spots on objects (such as polished metal, apple ...) Light reflected from the surface unequally to all directions. The result of near total reflection of the incident light in a concentrated region around the specular reflection angle 26 Specular Highlights • Shiny surfaces change appearance when viewpoint is changed • Specularities are caused by microscopically smooth surfaces. • A mirror is a perfect specular reflector 27 Phong Specular Component Phong combines directional diffuse & ideal specular n specular l s I I k cos I l k s ( R V ) Image courtesy of Watt, 3D Computer Graphics n 28 Half Vector • An alternative way of computing phong lighting is: Is = ks * Is * (N*H)n • H (halfway vector): halfway between V and L: (V+L)/2 • Fuzzier highlight • Check http://www.lighthouse3d.com/opengl/glsl/in N H dex.php?ogldir2 L V 29 Phong Illumination Moving Light Change n 30 Phong Ambient Component Treat it as a constant: I ambient I a ka Where Ia is the ambient light in the scene. 31 Adding Color I l ,r k d ,r I l ,r k s ,r I r I a ,r k a ,r I I k ( N L) I k ( N H ) n I k l,g d ,g l ,g s,g g a,g a,g I l ,b k d ,b I l ,b k s ,b I b I a ,b k a ,b 32 Adding Lights I l ,r k d ,r I l ,r k s ,r I r I a ,r k a ,r I I k n ( N L) I l , g k d , g ( N H ) I l , g k s , g g a , g a , g l I l ,b k d ,b I l ,b k s ,b I b I al ,b k a ,b lights I ka I a (k l lights n Il ( N L ) k ( N H ) d s 33 Phong Reflection 34 Image courtesy of Watt, 3D Computer Graphics Aluminium 35 Bronze 36 Chrome 37 Stainless Steel 38 OpenGL Materials GLfloat white8[] = {.8, .8, .8, 1.}, white2 = {.2,.2,.2,1.},black={0.,0.,0.}; GLfloat mat_shininess[] = {50.}; /* Phong exponent */ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, black); glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, white8); glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, white2); glMaterialfv( GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess); 39 OpenGL Lighting GLfloat white[] = {1., 1., 1., 1.}; GLfloat light0_position[] = {1., 1., 5., 0.}; /* directional light (w=0) */ glLightfv(GL_LIGHT0, GL_POSITION, light0_position); glLightfv(GL_LIGHT0, GL_DIFFUSE, white); glLightfv(GL_LIGHT0, GL_SPECULAR, white); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); /* normalize normal vectors */ glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);/* two-sided lighting*/ glEnable(GL_LIGHTING); 40 Vertex Normals vs. Face Normals What are the normals to the surface? Each polygonal face has a normal. c N = (b - a) x (c - b) a b We call these face normals. 41 Flat Shading Assume a constant color across the polygon c a b Uses face normals Equivalent to single point sampling… Polygon mesh is only an approximation. Can we do better? 42 Vertex Normals vs. Face Normals Should use the actual surface’s normals Usually stored at the vertices of the object Can calculate as averages of face normals 43 Mach Band ? 44 Mach Band ? 45 Shading Models for Polygons Constant Shading (flat shading) Compute illumination at any one point on the surface. Use face or one normal from a pair of edges. Good for far away light and viewer or if facets approximate surface well. Per-Pixel Shading Compute illumination at every point on the surface. Interpolated Shading Compute illumination at vertices and interpolate color 46 Interpolation Given vertex normals, how to color the interior: Gouraud Interpolation Phong Interpolation 47 Un-lit 48 Flat Shading 49 Gouraud Interpolation – Interpolated Shading 50 Phong Interpolation – Per pixel Shading 51 Gouraud Interpolation Calculate the color at each vertex C(c) C(a) C(b) Interpolate the colors C(c) C = t C(b) + (1 – t) C(c) C(a) C(b) 52 Gouraud Interpolation Problems Misses some highlights light shading will be constant! Shading is not linear 53 Phong Interpolation Interpolate the normals, then compute the colors light Interpolation is usually done component-wise 54 Phong Shading Model Gouraud shading does not properly handle specular highlights, specially when the n parameter is large (small highlight). Reason: colors are interpolated. Solution: (Phong Shading Model) 1. Compute averaged normal at vertices. 2. Interpolate normals along edges and scan-lines. (component by component) 3. Compute per-pixel illumination. 55 56 Interpolation Image courtesy of Watt & Watt, Advanced Animation and Rendering Techniques 57 Interpolated Shading - Problems Problems at shared vertices – shared by right polygons and not by one on left and hence discontinuity Incorrect Vertex normals – no variation in shade 58 Shade Trees Phong Shade Tree: + * ka * * Ia kd Il ks Il dot dot N pow L N n H 59 Shading Language A language for implementing shading models State is passed to/from the shader by global variables Ci – Outgoing ray color Oi – Outgoing ray opacity N I Cs – Surface color Os – Surface opacity P – Surface point N - Surface normal I - Direction of viewing (eye ray) L - Direction to the light source L 60 Shading Language Phong Surface Shader: surface plastic(float Ks = .5, float Kd = .5, float Ka = 1, float roughness = .1, color specularColor = 1) { point Nf = faceforward(N,I); Oi = Os; Ci = Os*(Cs*(Ka*ambient()+Kd*diffuse(Nf) + specularColor*Ks*specular(Nf,-I,roughness); } 61 Light Sources • Point light source • Directional light source: e.g. sun light • Spot light 62 Spot Light • To restrict a light’s effects to a limited area of the scene • Flap: confine the effects of the light to a designed range in x, y, and z world coordinate • Cone: restrict the effects of the light using a cone with a generating angle d 63 Light Source Attenuation • Takes into account the distance of the light from the surface I’L = I L * fatt (d) I’L: the received light after attenuation I L: the original light strength fatt: the attenuation factor d: the distance between the light source and the surface point • fatt = max ( 1/(c1 + c2*d + c3*d2) , 1) • C1, C2, C3 are user defined constants associated with each light source 64 Hints for HW4 • Shading can be done in easily in view space or in world space where all light, view, & surface normal vectors are described in same coordinate space. – how can you achieve this effect? • For normal transformation, use only 3x3 rotation matrix out of the 4x4 transformation & inverse transpose of it – remember normal transformation from the lecture 2? • For Phong shading, interpolate normal & compute shading for each pixel. For Gouraud shading, compute shading at only vertex and interpolate the color. 65