Illumination and Shading Illumination (ighting) Model the interaction of light with surface oints to determine their final color and brightness OenG comutes illumination at vertices illumination Shading Aly the lighting model at a set of oints across the entire surface Shading Illumination Model The governing rinciles for comuting the illumination A illumination model usually considers: ight attributes (light intensity, color, osition, direction, shae) Object surface attributes (color, reflectivity, transarency, etc) Interaction among lights and objects (object orientation) Interaction between objects and eye (viewing dir.)
Illumination Calculation Illumination Models ocal illumination: only consider the light, the observer osition, and the object material roerties Global illumination: take into account the interaction of light from all the surfaces in the scene object 4 θ object 3 object 2 object 1 Examle: OenG Examle: Ray Tracing (CIS681) Basic ight Tyes Directional So far away so that light rays are Remember orthogonal rojection? sun Directional light ight Source Tyes Point ight emanates equally in all directions Sot Point source limited to an angle Sot light Point light from Akenine-Moller & Haines
Object Proerties What haens when light hits an object? Proerties of light reflection on an object s surface Reflectance Models Ambient Diffuse Secular Absortion, Emission, Transarency/Translucency Irradiance: All light that arrives at a oint on the surface Radiosity: ight leaving a surface in all directions Object Proerties Object Material Shiny (Metal), dull (Matte finish), mirror-like, glass, neon, etc. ocal vs. Global Illumination Simle local illumination ocal Illumination deends on local object & light sources only Global Illumination at a oint can deend on any other oint in the scene The model used by OenG considers three tyes of light contribution to comute the final illumination of an object Ambient Diffuse Secular Final illumination of a oint (vertex) = ambient + diffuse + secular
Ambient lighting examle Diffuse lighting examle Secular light examle ight Reflectance Comonents Take a oint P on the object surface: R R θ θ θ θ φ V : ight Vector R: Reflection Vector Reflects about the ormal () to the surface V: View Vector
Ambient Reflection Background light scattered by the environment ight bounces off of many objects Simle Global Illumination Simle reflectance model Indeendent of ight osition Object orientation Viewer s osition k a : Ambient reflection coefficient Ambient light an object reflects 0 k a 1 Ambient = I a * k a Diffuse Reflection ambert s aw: Radiant energy D that a small surface atch receives from a light source: D = I d * cos(θ) I d = light intensity, θ = Angle between and Also called ambertian or Matte surfaces ambert s aw (1) ambert s aw (2) How does D change if the light source moves? Max intensity How does D change on an object s surface? A shere s surface has all ossible normal directions θ, D = I d * cos(θ) o intensity
Diffuse Reflection Energy D is reflected equally in all directions on the surface Indeendent of Viewer s osition k d : Diffuse reflection coefficient Diffuse light an object reflects 0 k d 1 Secular Reflection (1) The reflection of the light source on the object Shiny/Glossy surfaces ot a erfect mirror Show u as Secular Highlights, i.e., bright sots Diffuse = I d * k d * cos(θ) = I d * k d * ( ) and must be normalized WHY??? Secular Reflection (2) The object reflects maximum light intensity in the direction of the reflection vector θ θ φ R V ight intensity increases as V gets closer to R V R = cos(φ) Secular obe The reflection of the light source is maximum at the reflection direction Falls off quickly as the viewer moves away The size of the lobe determines the shininess of the object The shinier the object the smaller the lobe (cos(φ)) shine
Secular Reflection k s : Secular reflection coefficient Secular light an object reflects 0 k s 1 Ambient/Diffuse/Secular Just ambient light: Diffuse and change Ambient I s φ n : surface normal at P : light intensity : angle between V and R : shininess factor R θ θ φ V Sec = I s * k s * cos n (φ) = I s * k s * (V R) n V and R must be unit vectors WHY??? eft: Shere with just diffuse reflection Right:Shere with just secular reflection Basic Reflectance Equation Reflectance = from Akenine-Moller & Haines Ambient Diffuse Secular Final = I a * k a + I d * k d * ( ) + I s * k s * (R V) n Put it all together Illumination from a single light source: Illum = ambient + diffuse + secular = Ka x I + Kd x I x max(0, ) + Ks x I x max(0,r V) n ote that the K s and the I s are vectors (RGB).
Put it all together If there are lights Total illumination for a oint P = Σ (Illum) Some more terms to be added (in OenG): Self emission Global ambient ight distance attenuation and sot light effect ighting in OenG Adot Phong lighting model (secular) lus diffuse and ambient lights ighting is comuted at vertices Interolate across surface (Gouraud/smooth shading) OR Use a constant illumination (get it from one of the vertices) Setting u OenG ighting: ight Proerties Enable/Disable lighting Surface material roerties Provide correct surface normals ight model roerties ight Proerties Proerty Examle Proerties: Colors / Position and tye / attenuation glightfv(light, roerty, value) 1 2 3 (1) constant: secify which light you want to set the roerty examle: G_IGHT0, G_IGHT1, G_IGHT2 you can create multile lights (OenG allows at least 8 lights) (2) constant: secify which light roerty you want to set the value examle: G_AMBIET, G_DIFFUSE, G_SPECUAR, G_POSITIO (check the red book for more) (3) The value you want to set to the roerty Define colors and osition a light Gfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0}; Gfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; Gfloat light_secular[] = {1.0, 1.0, 1.0, 1.0}; Gfloat light_osition[] = {0.0, 0.0, 1.0, 1.0}; glightfv(g_ight0, G_AMBIET, light_ambient); glightfv(g_ight0, G_DIFFUSE, light_diffuse); glightfv(g_ight0, G_SPECUAR, light_secular); glightfv(g_ight0, G_POSITIO, light_osition); colors Position What if I set the Position to (0,0,1,0)?
Tyes of lights Turning on the lights OenG suorts two tyes of lights ocal light (oint light) Infinite light (directional light) Determined by the light ositions you rovide w = 0: infinite light source (faster) w!= 0: oint light osition = (x/w, y/w, z/w) Gfloat light_osition[] = {x,y,z,w}; Turn on the ower (for all the lights) glenable(g_ightig); gldisable(g_ightig); Fli each light s switch glenable(g_ightn) (n = 0,1,2, ) glightfv(g_ight0, G_POSITIO, light_osition); Controlling light osition Modelview matrix affects a light s osition You can secify the osition relative to: Eye sace: the highlight remains in the same osition relative to the eye call glightfv() before gluookat() World sace: a light s osition/direction aears fixed in the scene Call glightfv() after gluookat() Any model sace (not as intuitive). See ate Robin s Demo Material Proerties The color and surface roerties of a material (dull, shiny, etc.) How much the surface reflects the incident lights (ambient/diffuse/secular reflection coefficients) glmaterialfv(face, roerty, value) Face: material roerty for which face (e.g. G_FROT, G_BACK, G_FROT_AD_BACK) Proerty: what material roerty you want to set (e.g. G_AMBIET, G_DIFFUSE, G_SPECUAR, G_SHIIESS, G_EMISSIO, etc) Value: the value you can to assign to the roerty
Material Examle Define ambient/diffuse/secular reflection and shininess Gfloat mat_amb_diff[] = {1.0, 0.5, 0.8, 1.0}; refl. coefficient Gfloat mat_secular[] = {1.0, 1.0, 1.0, 1.0}; Gfloat shininess[] = {5.0}; (range: dull 0 very shiny128) glmaterialfv(g_frot_ad_back, G_AMBIET_AD_DIFFUSE, mat_amb_diff); glmaterialfv(g_frot, G_SPECUAR, mat_secular); glmaterialfv(g_frot, G_SHIIESS, shininess); Global light roerties glightmodelfv(roerty, value) Enable two-sided lighting roerty = G_IGHT_MODE_TWO_SIDE value = G_TRUE (G_FASE if you don t want two sided lighting) Global ambient color Proerty = G_IGHT_MODE_AMBIET Value = (red, green, blue, 1.0); Check the red book for others Surface ormals Correct normals are essential for correct lighting Associate a normal to each vertex glbegin( ) glormal3f(x,y,z) glvertex3f(x,y,z) glend() The normals you rovide need to have a unit length You can use glenable(g_ormaize) to have OenG normalize all the normals. Why not always have OenG do this? ighting revisit Where is lighting erformed in the grahics ieline? v1, m1 v2, m2 v3, m3 Rasterization texturing shading modeling and viewing viewort maing er vertex lighting interolate vertex colors rojection cliing Dislay
Polygon shading model Flat shading comute lighting once and assign the color to the whole olygon Flat shading Only use one vertex (usually the first one) normal and material roerty to comute the color for the olygon Benefit: fast to comute It is used when: The olygon is small enough The light source is far away (why?) The eye is very far away (why?) OenG command: glshademodel(g_fat) Smooth shading Remove edge discontinuity Comute lighting for more oints on each face Still has a mach-band erceived discontinuity due to your eye s edge detection. Smooth shading Two oular methods: Smooth shading (used by OenG) Per-fragment lighting (better secular highlight, requires rogrammable shaders in OenG) Flat shading smooth shading
Smooth Shading The smooth shading algorithm used in OenG glshademodel(g_smooth) ighting is calculated for each of the olygon vertices Colors are interolated for interior ixels Smooth Shading Per-vertex lighting calculation ormal is needed for each vertex Per-vertex normals can be comuted by averaging the adjacent face normals n1 n3 n n4 n2 n = (n1 + n2 + n3 + n4) / 4.0 Smooth Shading Smooth Shading Comute vertex illumination (color) before the rojection transformation Shade interior ixels: color interolation (normals are not needed) C2 C1 Ca = ler(c1, C2) Cb = ler(c1, C3) C3 er(ca, Cb) for all scanlines * ler: linear interolation inear interolation v1 a x b v2 x = a / (a+b) * v2 + b/(a+b) * v1 Interolate triangle color: use y distance to interolate the two end oints in the scanline, and use x distance to interolate interior ixel colors
Smooth Shading Problem ighting in the olygon interior can be inaccurate Smooth Shading Problem ighting in the olygon interior can be inaccurate Phong Shading Instead of interolation, we calculate lighting for each ixel inside the olygon (er ixel lighting) We need to have normals for all the ixels not rovided by the user Phong shading algorithm interolates the normals and comute lighting during rasterization (need to ma the normal back to world or eye sace though) Phong Shading (2) ormal interolation na = ler(n1, n2) ler(na, nb) n2 n3 n1 nb = ler(n1, n3) Slow! You will do this in the ray-tracing class.