COMP 175 | COMPUTER GRAPHICS Lecture 10: Illumination COMP 175: Computer Graphics March 29, 2016 Remco Chang 10 – Lighting and Illumination 1/70 COMP 175 | COMPUTER GRAPHICS Admin ο½ Confusion with Camera Angles Remco Chang 10 – Lighting and Illumination 2/70 COMP 175 | COMPUTER GRAPHICS Scaling the Perspective View Volume ο½ π€ 2 To find , we need to use a bit of trig: ο½ ο½ = tan ππ€ 2 ∗ πππ Now, scale to send the X-Coordinates of the far plane to [-1, 1]: ο½ ο½ π€ 2 πππ 1 π tan 2π€ ∗πππ Repeat for the Y-Coordinates, replace ππ€ with πβ , and scale in the Y-dimension, and we have: ο½ ππ€ /2 π€ 2 1 πβ /2 β 2 πππ π tan 2β ∗πππ Remco Chang 10 – Lighting and Illumination 3/70 COMP 175 | COMPUTER GRAPHICS Camera Angles ο½ We don’t have both ππ€ and πβ , we are just given a single “camera angle” and the aspect ratio of w and h. So we need to infer one from the other. For example, let’s say that you let camera angle = ππ€ . Then: π€ ππ€ = tan ∗ πππ 2 2 ο½ Divide w on both sides and multiply h on both sides: 1 ππ€ 1 = tan ∗ πππ ∗ 2 2 π€ β ππ€ β = tan ∗ πππ ∗ 2 2 π€ ο½ Note that you can map camera angle to either ππ€ or πβ . In my implementation of Camera, my camera angle is mapped to πβ Remco Chang 10 – Lighting and Illumination 4/70 COMP 175 | COMPUTER GRAPHICS Summary: ο½ Pseudocode for a non-recursive ray tracer: P = eyePoint for each pixel d = Compute Ray in World Coordinate for each object intersect P+td with object, record t and object Select object with smallest non-negative t value Find intersection point in Object Coordinate Compute normal of the point in Object Coordinate Transform normal to World Coordinate Solve lighting equation in World Coordinate Set Pixel Color Remco Chang 10 – Lighting and Illumination 5/70 COMP 175 | COMPUTER GRAPHICS Note about Ray ο½ An important point about transforming Ray from World space to Object space: ο½ ο½ Normalize the Ray after it’s created in World space. However, do NOT normalize Ray after the transform into Object Space!! ο½ Reason: ο½ ο½ ο½ If you normalize, then the value t you find will be in the object space. When there are many objects, it’s hard to figure out how to compare all the different t values across different object spaces If you do not normalize, then the value t will apply in the world space. That means that you can compare the t values derived from the different objects in the same coordinate. Think about it a little bit… This is the only time in this class where you should not normalize a vector. Remco Chang 10 – Lighting and Illumination 6/70 COMP 175 | COMPUTER GRAPHICS Overview of Lighting ο½ ο½ Different types of lighting (diffuse, specular, ambient, etc.) In almost all cases, we need to know about how light bounces off of a surface ο½ This means knowing about the normal at the intersection point Remco Chang 10 – Lighting and Illumination 7/70 COMP 175 | COMPUTER GRAPHICS Normal Vectors at the Intersection Points ο½ We discussed all three steps of Ray Casting ο½ The key to Ray Casting is to do geometric computation in object coordinate space, but covert the results to world coordinate space. ο½ One mathematical issue involves transforming the normal of a surface point from object coordinate space to world coordinate space… Remco Chang 10 – Lighting and Illumination 8/70 COMP 175 | COMPUTER GRAPHICS Computing Surface Normal (in Object Coordinate) ο½ As an example, recall the equation for a sphere is: ο½ ο½ ο½ π(π₯, π¦, π§) = π₯ 2 + π¦ 2 + π§ 2 – π 2 If R = 1, then π(π₯, π¦, π§) = π₯ 2 + π¦ 2 + π§ 2 – 1 Finding the normal of a point can be thought of as finding the gradient ο½ which can be done by taking the partial derivative of he equation: ο½ ο½ ππ ππ₯ π₯, π¦, π§ = 2π₯, ππ ππ¦ π₯, π¦, π§ = 2π¦, ππ ππ§ π₯, π¦, π§ = 2π§ So the gradient is: ο½ ο½ π = π»π π₯, π¦, π§ = (2π₯, 2π¦, 2π§) After normalization, π = (π₯, π¦, π§) Remco Chang 10 – Lighting and Illumination 9/70 COMP 175 | COMPUTER GRAPHICS Normal Vector Transformed ο½ We have a normal vector in object coordinate ο½ We need a world-coordinate normal vector to compute the illumination model ο½ How do we transform a point or a vector from the object coordinate to the world coordinate? ο½ ο½ We apply the object’s transformation matrix M Can we treat the normal the same way? ο½ Answer: no… Remco Chang 10 – Lighting and Illumination 10/70 COMP 175 | COMPUTER GRAPHICS Normal Vector Transformed ο½ Can we treat the normal the same way? ο½ Answer: no… ππππππ ≠ π΄π πππππππ ο½ Here’s an example of why that is: ο½ Say that M scales in x by ½, and y by 2 Wrong! Mnobject Normal must be perpendicular nobject Remco Chang 10 – Lighting and Illumination 11/70 COMP 175 | COMPUTER GRAPHICS What Happened? ο½ ο½ Why doesn’t this work? Well, actually it does work for translation, rotation, and uniform scaling nobject ο½ ο½ Let’s test it out… Non-uniform scale causes a problem: ο½ ο½ The normal is distorted in exactly the opposite of what we want. Example below: Scale by 2 in x, 1 in y <0.5, 0.5> <0.25, 0.5> <1.0, 0.5> Remco Chang 10 – Lighting and Illumination 12/70 COMP 175 | COMPUTER GRAPHICS So How Do I Do This? ο½ If we can’t use π, but some kind of inverse of it, then what does it look like, (π−1 )−1 ? ο½ Nope, the answer is π−1 π , because: 1. 2. 3. we need to invert the non-uniform scale, but not disturb the rotation component (same as in as M) we don’t care about translation because vectors can’t be translated ο½ Recall that π −1 = π π , so we can invert the matrix π to π−1 , but then “un-apply” the rotation component by adding in the transpose. ο½ The addition of the transpose does not affect the scale matrix because the scale matrix is a diagonal matrix whose transpose remains the same as the original ο½ π π₯, π¦, π§ ο½ π π₯, π¦, π§ Remco Chang π = π(π₯, π¦, π§) −1 π = π π₯, π¦, π§ −1 = π(1/π₯, 1/π¦, 1/π§) 10 – Lighting and Illumination 13/70 COMP 175 | COMPUTER GRAPHICS A More Rigorous Proof ο½ Let’s compute the relationship between object-space normal ππππ to polygon H and world-space normal ππ€ππππ to transformed version of H, called MH ο½ For a vector π in the world space that lies in the polygon (e.g., one of its edge vectors), the normal is perpendicular to π: ππ€ππππ β ππ€ππππ = 0 ο½ But ππ€ππππ is just a transformed version of some vector in object space, ππππ , so we could write: ππ€ππππ β πππππ = 0 ο½ Recall that since vectors have no position, they are unaffected by translations (w = 0) ο½ ο½ So we need to only consider: π3 =upper left 3x3 of π (the rotation / scaling components) ππ€ππππ β π3 ππππ = 0 Remco Chang 10 – Lighting and Illumination 14/70 COMP 175 | COMPUTER GRAPHICS A More Rigorous Proof ο½ The next step is to write this: ππ€ππππ β (π3 ππππ ) = 0 ο½ As this: (π3π‘ ππ€ππππ ) β ππππ = 0 ο½ Recall that if we think of vector as ππ₯1 matrix, then switching notation, π β π = ππ π ο½ Rewriting our original formula, we thus have: ππ‘π€ππππ π3 ππππ = 0 ο½ Writing π = ππ‘ π‘ , we get: ππ‘π€ππππ π3π‘π‘ ππππ = 0 ο½ Recall that π΅π‘ π΄π‘ = π΄π΅ π‘ , we can re-write this as: π3π‘ ππ€ππππ π‘ ππππ = 0 ο½ Switching back to dot product notation, our result: (π3π‘ ππ€ππππ ) β ππππ = 0 Remco Chang 10 – Lighting and Illumination 15/70 COMP 175 | COMPUTER GRAPHICS A More Rigorous Proof ο½ So, we have: π3π‘ ππ€ππππ β ππππ = 0 ο½ We also already have: ππππ β ππππ = 0 ο½ Therefore: π3π‘ ππ€ππππ = ππππ ο½ Left-multiplying by π3π‘ −1 : π3π‘ −1 π3π‘ ππ€ππππ = π3π‘ −1 π πππ ππ€ππππ = π3π‘ −1 ππππ ππ€ππππ = π3−1 t ππππ Remco Chang 10 – Lighting and Illumination 16/70 COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 10 – Lighting and Illumination 17/70 COMP 175 | COMPUTER GRAPHICS What is Light? ο½ Can be thought of as small packets of photons ο½ ο½ When an electron drops from a higher level orbit to a lower orbit, energy is emitted as photons Different energy levels of electrons allow for different wavelengths of light to be reflected ο½ ο½ Metals have “loose” electrons, which can move relatively freely and occupy many different levels, which is why they are more reflective Insulators have more constrained electrons which cannot change energy levels as easily, so they convert more absorbed light into heat instead of reflecting it Remco Chang 10 – Lighting and Illumination 18/70 COMP 175 | COMPUTER GRAPHICS What is Light? ο½ Another way to think of light is to treat it as a continuous wave (as opposed to discrete photons) ο½ ο½ ο½ ο½ The wavelength (π) of a wave is measured as the distance from one peak of wave to the next Different colors correspond to different wavelengths Visible light has a wavelength between 400nm – 700nm Different ways of thinking about light (photons vs. waves) can result in different rendering styles Remco Chang 10 – Lighting and Illumination 19/70 COMP 175 | COMPUTER GRAPHICS Illumination ο½ Problem: ο½ ο½ ο½ In ray casting, we have generated a ray (from the eye through a pixel), found the point in which the ray intersects with an object Now we want to determine the color and brightness of that point so that we can color in the pixel Solution: we model the interactions between light sources and the surface Remco Chang 10 – Lighting and Illumination 20/70 COMP 175 | COMPUTER GRAPHICS Illumination Models ο½ An “illumination model” describes inputs, assumptions, and outputs used to calculate illumination (color / brightness) of surface elements ο½ Usually includes ο½ ο½ ο½ Light attributes (intensity, color, position, direction, shape) Object surface properties (color, reflectivity, transparency, etc.) Interaction between lights and objects Remco Chang 10 – Lighting and Illumination 21/70 COMP 175 | COMPUTER GRAPHICS Illumination Models ο½ Physically-based Illumination Models ο½ ο½ Some models of illumination are based on real physics Require accurate input data and make few assumptions ο½ ο½ ο½ Rarely have enough information to specify all inputs Takes a long time to compute Non-Physically-Based Illumination Models ο½ ο½ Just need a model that looks good enough for a specific application Emphasis on speed and efficiency (use of resources like memory, CPU, etc.) Remco Chang 10 – Lighting and Illumination 22/70 COMP 175 | COMPUTER GRAPHICS Light Attenuation: Inverse Square Law ο½ The amount that a light illuminates an object decreases with the square of the distance between them. This is known as Light Attenuation. ο½ Inverse square law: for a given 3D angle (solid angle), the area it covers grows with the square of the distance ο½ ο½ Intensity of light per unit area falls off with the inverse square Conversely, for a fixed area, the angle it covers decreases with the square of the distance Remco Chang 10 – Lighting and Illumination 23/70 COMP 175 | COMPUTER GRAPHICS Basic Light Sources ο½ As part of illumination models, one can specify whether the light intensity varies with the distance between the object and the light source Remco Chang 10 – Lighting and Illumination 24/70 COMP 175 | COMPUTER GRAPHICS Illumination Models ο½ Local Illumination ο½ ο½ ο½ ο½ Takes only direct lighting information Is an approximation of global illumination Usually involves the use of an “ambient” term to simulate indirect lighting Global Illumination ο½ ο½ ο½ Most light striking a surface element comes directly from a light emitting source (direct illumination) Sometimes light from a source is blocked by another object, resulting in shadows However, objects in the shadow can still receive light from light bouncing off other objects (indirect illumination) Remco Chang 10 – Lighting and Illumination 25/70 COMP 175 | COMPUTER GRAPHICS Example 1: Local Illumination ο½ ο½ Only considers the light, the observer position, and the object’s material properties OpenGL does this. Remco Chang 10 – Lighting and Illumination 26/70 COMP 175 | COMPUTER GRAPHICS Example 2: Global Illumination ο½ ο½ Takes into account of the interaction of light from all the surfaces in the scene Recursive ray tracing is an example. It models light rays bouncing between objects Remco Chang 10 – Lighting and Illumination 27/70 COMP 175 | COMPUTER GRAPHICS Example 3: Global Illumination, Radiosity ο½ ο½ Models energy moving from emitters (light sources) into the scene as patches Is view independent Remco Chang 10 – Lighting and Illumination 28/70 COMP 175 | COMPUTER GRAPHICS Examples of Global Illumination Direct illumination + specular reflection Ray trace Remco Chang + soft shadows and caustics Ray trace + caustic photon map 10 – Lighting and Illumination + diffuse reflection (color bleeding) Ray trace + caustic and diffuse photon maps 29/70 COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 10 – Lighting and Illumination 30/70 COMP 175 | COMPUTER GRAPHICS Simple Local Illumination (Phong) ο½ ο½ The model used by OpenGL Reduce the light model to 3 simple components: ο½ ο½ ο½ ο½ Illumination (color intensity) of a point is equal to: ο½ ο½ Ambient Diffuse Specular Intensity = ambient + diffuse + specular Materials reflect each component differently ο½ Specified as material reflection coefficients: πΎπ , πΎπ , πΎπ Remco Chang 10 – Lighting and Illumination 31/70 COMP 175 | COMPUTER GRAPHICS Ambient Light Contribution ο½ Ambient light = background light ο½ ο½ As noted before, it is used to simulate indirect lighting Ambient component ο½ Independent of ο½ ο½ ο½ ο½ Object’s position Viewer’s position Light source’s position Dependent of ο½ A constant factor (in each of the R, G, B channels) Remco Chang 10 – Lighting and Illumination 32/70 COMP 175 | COMPUTER GRAPHICS Diffuse Light Contribution ο½ Diffuse light = illumination that a surface receives from a light source that reflects equally in all directions ο½ Independent of: ο½ ο½ Viewer’s position Dependent of: ο½ ο½ Light’s position Surface property (normal, reflectance property) Remco Chang 10 – Lighting and Illumination 33/70 COMP 175 | COMPUTER GRAPHICS Diffuse Light Calculation ο½ ο½ Need to know how much light a point on the object receives from the light source Solution based on Lambert’s Law Point receives more light Remco Chang 10 – Lighting and Illumination Point receives less light 34/70 COMP 175 | COMPUTER GRAPHICS Diffuse Light Calculation ο½ Lambert’s Law: The radiant energy D that a small surface patch (or a point) receives from a light source is: ο½ Diffuse = πΎπ π₯ πΌ π₯ cos(π) ο½ ο½ ο½ ο½ πΎπ = diffuse reflection constant I = light intensity π = angle between the light vector and the surface normal How do we compute cos(π)? ο½ π β πΏ (dot product between N and L) As the angle between the light and the normal increases, the light’s energy spreads across a larger area Remco Chang 10 – Lighting and Illumination The hemisphere represents equal magnitude of the reflected intensity for any outgoing vector. 35/70 COMP 175 | COMPUTER GRAPHICS Diffuse Light Examples ο½ Diffuse = πΎπ π₯ πΌ π₯ cos(π) ο½ For I = 1.0 Remco Chang 10 – Lighting and Illumination 36/70 COMP 175 | COMPUTER GRAPHICS Specular Light Contribution ο½ ο½ Specular light = light reflection from shiny surfaces Color depends on material and how it scatters light ο½ ο½ Shiny surfaces (metal, mirror, etc.) reflect more light Specular light depends on both light source position and view position Remco Chang 10 – Lighting and Illumination 37/70 COMP 175 | COMPUTER GRAPHICS Specular Light Calculation ο½ ο½ π is the angle between the view direction and the reflective vector When π is small, the viewer sees more specular light Remco Chang 10 – Lighting and Illumination 38/70 COMP 175 | COMPUTER GRAPHICS Specular Light Calculation ο½ The Phong lighting model (not the Phong shading model) ο½ Specular = πΎπ π₯ πΌ π₯ cos π (π) ο½ ο½ ο½ ο½ πΎπ = specular reflection constant πΌ = light intensity π = angle between reflective ray and view vector π = surface property for specular highlight Remco Chang 10 – Lighting and Illumination 39/70 COMP 175 | COMPUTER GRAPHICS Specular Light Calculation ο½ Specular = πΎπ π₯ πΌ π₯ cosπ (π) Diffuse hemisphere with specular Gaussian surface Remco Chang 10 – Lighting and Illumination Shape of the Gaussian 40/70 COMP 175 | COMPUTER GRAPHICS Specular Light Examples ο½ Specular = πΎπ π₯ πΌ π₯ cosπ (π) ο½ For I = 1.0 Remco Chang 10 – Lighting and Illumination 41/70 COMP 175 | COMPUTER GRAPHICS Variation in Diffuse and Specular ο½ Putting Diffuse and Specular together in our local illumination model Remco Chang 10 – Lighting and Illumination 42/70 COMP 175 | COMPUTER GRAPHICS Lighting Equation ο½ Direct Illumination = ambient + diffuse + specular color =πΎπ π₯ πΌ + πΎπ π₯ πΌ π₯ (π β πΏ) + πΎπ π₯ πΌ π₯ π β π π ο½ If there are m lights, we sum over each light ο½ ο½ ο½ color =πΎπ π₯ πΌ color =πΎπ π₯ πΌ Remco Chang + π(πΎπ π₯ πΌπ π₯ (π β πΏπ ) + πΎπ π₯ πΌπ π₯ π π β π π ) + π πΌπ π₯ (πΎπ π₯ (π β πΏπ ) + πΎπ π₯ π π β π π ) 10 – Lighting and Illumination 43/70 COMP 175 | COMPUTER GRAPHICS Finding the Reflective Ray ο½ π = π − 2(π β π)π ο½ Rationale: think about it, r is very much similar to d: ο½ ο½ ο½ ο½ ο½ Remco Chang r_horizontal = d_horizontal r_vertical = -d_vertical So we just have to find d_horizontal and d_vertical: ο½ ππ£πππ‘ = π β π π ο½ πβππππ§ = π − π β π π r = r_vertical + r_horizontal r = -d_vertical + d_horizontal 10 – Lighting and Illumination 44/70 COMP 175 | COMPUTER GRAPHICS Adding in Object Color ο½ Since the color we perceive is based on the color of the light and the color of the object, we need to model both terms: ο½ ο½ π For multiple lights: ο½ ο½ color =πΎπ π₯ πΌ π₯ πΆπ + πΎπ π₯ πΌ π₯ πΆπ π₯ (π β πΏ) + πΎπ π₯ πΌ π₯ πΆπ π₯ π» β π color =πΎπ π₯ πΌ π₯ ππ + π πΌπ π₯ (πΎπ π₯ ππ π₯ (π β πΏπ ) + πΎπ π₯ ππ π₯ π»π β π π ) Adding in light attenuation (how quickly light falls off as distance increases): ο½ color =πΎπ π₯ πΌ π₯ ππ + π πππππ π₯ πΌπ π₯ (πΎπ π₯ ππ π₯ (π β πΏπ ) + πΎπ π₯ ππ π₯ π»π β π π ) Remco Chang 10 – Lighting and Illumination 45/70 COMP 175 | COMPUTER GRAPHICS Debug Lighting Issues ο½ Make sure that you normalize your normals!! ο½ Since all calculations are based on surface normals (e.g. dot product with normals), if your normals are bad, you’re stuck. Remco Chang 10 – Lighting and Illumination 46/70 COMP 175 | COMPUTER GRAPHICS Summary: ο½ Pseudocode for a non-recursive ray tracer: P = eyePoint for each pixel d = Compute Ray in World Coordinate for each object intersect P+td with object, record t and object Select object with smallest non-negative t value Find intersection point in Object Coordinate Compute normal of the point in Object Coordinate Transform normal to World Coordinate for each light Solve lighting equation in World Coordinate Set Pixel Color Remco Chang 10 – Lighting and Illumination 47/70 COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 10 – Lighting and Illumination 48/70 COMP 175 | COMPUTER GRAPHICS Shading vs. Lighting ο½ How is Shading different from Lighting? ο½ Lighting evaluates the lighting equation at each point on the surface of an object ο½ Shading is kind of a “hack” that computes only lighting equations on the vertices, and interpolates the pixels in between Remco Chang 10 – Lighting and Illumination 49/70 COMP 175 | COMPUTER GRAPHICS Shading Models – Flat/Constant ο½ We define a normal at each polygon (not at each vertex) ο½ Lighting: Evaluate the lighting equation at the center of each polygon using the associated normal ο½ Shading: Each sample point on the polygon is given the interpolated lighting value at the vertices (i.e. a total hack) Remco Chang 10 – Lighting and Illumination 50/70 COMP 175 | COMPUTER GRAPHICS Shading Models – Gouraud ο½ We define a normal vector at each vertex ο½ Lighting: Evaluate the lighting equation at each vertex using the associated normal vector ο½ Shading: Each sample point’s color on the polygon is interpolated from the color values at the polygon’s vertices which were found in the lighting step Remco Chang 10 – Lighting and Illumination 51/70 COMP 175 | COMPUTER GRAPHICS Shading Models – Phong ο½ Each vertex has an associated normal vector ο½ Lighting: Evaluate the lighting equation at each vertex using the associated normal vector ο½ Shading: For every sample point on the polygon we interpolate the normals at vertices of the polygon and compute the color using the lighting equation with the interpolated normal at each interior pixel Remco Chang 10 – Lighting and Illumination 52/70 COMP 175 | COMPUTER GRAPHICS Shading Models Compared Constant shading: no interpolation, pick a single representative intensity and propagate it over entire object. Loses almost all depth cues. Pixar “Shutterbug” images from: www.siggraph.org/education/materials/HyperGraph /scanline/shade_models/shading.htm Remco Chang 10 – Lighting and Illumination 53/70 COMP 175 | COMPUTER GRAPHICS Shading Models Compared Flat or Faceted Shading: constant intensity over each face Constant Shading Remco Chang 10 – Lighting and Illumination 54/70 COMP 175 | COMPUTER GRAPHICS Shading Models Compared Gouraud Shading: Linear Interpolation of intensity across triangles to eliminate edge discontinuity Flat Shading Remco Chang 10 – Lighting and Illumination 55/70 COMP 175 | COMPUTER GRAPHICS Shading Models Compared Phong Shading: Interpolation of vertex surface normals Note: specular highlights but no shadows. Still a pure local illumination model Gouraud Shading Remco Chang 10 – Lighting and Illumination 56/70 COMP 175 | COMPUTER GRAPHICS Shading Models Compared Global Illumination: Objects enhanced using shadow, texture, bump, and reflection mapping (see S20) Phong Shading Remco Chang 10 – Lighting and Illumination 57/70 COMP 175 | COMPUTER GRAPHICS Shading Models Explained (1/6: Faceted) ο½ Faceted Shading: ο½ Single illumination value per polygon (GL_FLAT) With many polygons approximating a curved surface, this creates an undesirable faceted look. ο½ Facets exaggerated by “Mach banding” effect ο½ Remco Chang 10 – Lighting and Illumination 58/70 COMP 175 | COMPUTER GRAPHICS Shading Models Explained (2/6: Gouraud) ο½ Illumination intensity interpolation (GL_SMOOTH) ο½ ο½ ο½ ο½ Illumination values are computed at vertices, linearly interpolated across the pixels of the polygon Eliminates intensity discontinuities at polygon edges; still have gradient discontinuities, Mach banding is largely ameliorated, not eliminated Must differentiate desired creases from tesselation artifacts (edges of cube vs. edges on tesselated sphere) Step 1: Compute vertex normals by averaging surrounding polygon normals π2 ππ£ ππ£ = π π=1 ππ π π=1 ππ π3 π1 π4 Remco Chang 10 – Lighting and Illumination 59/70 COMP 175 | COMPUTER GRAPHICS Shading Models Explained (3/6: Gouraud cont.) ο½ ο½ ο½ Step 2: Evaluate illumination at each vertex using lighting model (πΌ1 , πΌ2 , πΌ3 ) Step 3: Interpolate illumination along polygon edges (πΌπ , πΌπ ) Step 4: Interpolate illumination along scan lines (πΌπ ) I1 y1 Ia ys y2 y3 y s ο y2 y ο ys ο« I2 1 y1 ο y2 y1 ο y2 y ο y3 y ο ys I b ο½ I1 s ο« I3 1 y1 ο y3 y1 ο y3 xb ο x p x p ο xa I p ο½ Ia ο« Ib xb ο xa xb ο xa I a ο½ I1 Ib scan line Ip I2 Remco Chang I3 June 10 –21, Lighting 2016 and Illumination 60 60/70 COMP 175 | COMPUTER GRAPHICS Shading Models Explained (4/6: Gouraud cont.) ο½ Takes advantage of scan line algorithm for efficiency ο½ ο½ βπΌ βπ¦ is constant along polygon edge, βπΌ βπ₯ is constant along scan line Gouraud vs. Faceted shading: Remco Chang 10 – Lighting and Illumination 61/70 COMP 175 | COMPUTER GRAPHICS Shading Models Explained (5/6: Gouraud cont.) ο½ ο½ ο½ Gouraud shading can miss specular highlights because it interpolates vertex colors instead of calculating intensity directly at each point, or interpolating vertex normals Na and Nb would cause no appreciable specular component, whereas Nc would. Interpolating between Ia and Ib misses the highlight that evaluating I at c would catch Phong shading: ο½ Interpolated normal comes close to the actual normal of the true curved surface at a given point ο½ Reduces temporal “jumping” affect of highlight, e.g., when rotating sphere during animation (example on next slide) Remco Chang 10 – Lighting and Illumination 62/70 COMP 175 | COMPUTER GRAPHICS Shading Models Explained (6/6: Phong Shading) ο½ Phong Model: normal vector interpolation ο½ Interpolate N rather than I ο½ Always captures specular highlights, but computationally expensive ο½ At each pixel, N is recomputed and normalized (requires sq. root operation) ο½ Then I is computed at each pixel (lighting model is more expensive than interpolation algorithms) ο½ This is now implemented in hardware, very fast ο½ Looks much better than Gouraud, but still no global effects Gouraud Remco Chang Phong Gouraud http://en.wikipedia.org/wiki/Gourad_shading 10 – Lighting and Illumination Phong 63/70 COMP 175 | COMPUTER GRAPHICS Gouraud vs. Phong Shading Remco Chang 10 – Lighting and Illumination 64/70 COMP 175 | COMPUTER GRAPHICS Additional Material Remco Chang 10 – Lighting and Illumination 65/70 COMP 175 | COMPUTER GRAPHICS Using Halfway Vector for Speed (Blinn-Phong) n n ο½ Calculating reflection vector is expensive ο½ As an approximation (don’t do this for your assignments), graphics people will use the halfway vector: h = s + v ο½ Don’t forget to normalize!! ο½ Instead of using π, we would use π½ as an approximation ο½ The results will be incorrect (note when h = n), but it is possible to achieve “good enough” results by playing with the parameter f. Remco Chang 10 – Lighting and Illumination 66/70 COMP 175 | COMPUTER GRAPHICS Lighting Equation Using Halfway Vector ο½ Direct Illumination = ambient + diffuse + specular color =πΎπ π₯ πΌ + πΎπ π₯ πΌ π₯ (π β πΏ) + πΎπ π₯ πΌ π₯ π» β π π ο½ If there are m lights, we sum over each light ο½ ο½ color =πΎπ π₯ πΌ Remco Chang + π πΌπ π₯ (πΎπ π₯ (π β πΏπ ) + πΎπ π₯ π»π β π π ) 10 – Lighting and Illumination 67/70 COMP 175 | COMPUTER GRAPHICS Choosing the Constants ο½ Here are some examples for the constants used in the previous equation. Note that since objects might have different properties for different color channels (R, G, B) Remco Chang 10 – Lighting and Illumination 68/70 COMP 175 | COMPUTER GRAPHICS Lights in OpenGL ο½ This would have been useful for your SceneView assignment… ο½ Defining lights: Remco Chang 10 – Lighting and Illumination 69/70 COMP 175 | COMPUTER GRAPHICS Lights in OpenGL ο½ Setting the light position as (0, 0, 1, 1) means that the light is a point light. Setting the light position as (0, 0, 1, 0) means that the light is a directional light (infinite light) ο½ Enable lighting, you need to do both: ο½ ο½ glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); // for light 0. Light1 for light 1, etc. Remco Chang 10 – Lighting and Illumination 70/70 COMP 175 | COMPUTER GRAPHICS Materials in OpenGL ο½ Material Example: Remco Chang 10 – Lighting and Illumination 71/70 COMP 175 | COMPUTER GRAPHICS Birectional Reflectance Distribution Function (BRDF) ο½ We have looked at a simple model of how light scatters as a function: ο½ ο½ ο½ Direction of scattering is determined by the material Intensity at a given outgoing direction is dependent on incoming direction and material properties This type of function that measures reflectance is called the “Birectional Reflectance Distribution Function (BRDF)”, denoted π (rho) specular reflection (big) diffuse reflections surface Remco Chang 10 – Lighting and Illumination reflected scatter distribution BRDF Image credit: © Ben Herila, 2010 72/70 COMP 175 | COMPUTER GRAPHICS BSSRDF ο½ The BRDF can be generalized to model transmission through an object (i.e., Refraction) and sub-surface scattering by defining other terms, such as the Bidirectional scattering surface reflectance distribution function (BSSRDF) No scattering with BSSRDF scattering Remco Chang 10 – Lighting and Illumination 73/70 COMP 175 | COMPUTER GRAPHICS Modeling Reflectance ο½ There are many more complex and more accurate BRDFs ο½ Researchers collect tables of data for B*DFs of specific materials using devices like the one pictured Image credit: Chuck Moidel Remco Chang 10 – Lighting and Illumination 74/70 COMP 175 | COMPUTER GRAPHICS Rendering Equation (Kajiya, 1986) πΏπ π₯, ππ , π, π‘ = πΏπ π₯, ππ , π, π‘ + fr is the BRDF function L is light toward position x Subscript i is for (inward), o for (outward), e for (emission) ππ π₯, ππ , ππ , π, π‘ πΏπ π₯, ππ , π, π‘ ππ β π πππ Ω Remco Chang 10 – Lighting and Illumination 75/70 COMP 175 | COMPUTER GRAPHICS Rendering Equation (Kajiya, 1986) πΏπ π₯, ππ , π, π‘ = πΏπ π₯, ππ , π, π‘ + ππ π₯, ππ , ππ , π, π‘ πΏπ π₯, ππ , π, π‘ ππ β π πππ Ω ο½ Sometimes you see this written as (without lambda (wavelength)and time) πΏπ π₯, ππ = πΏπ π₯, ππ + ππ π₯, ππ , ππ πΏπ π₯, ππ ππ β π πππ Ω πΏπ π₯, ππ = πΏπ π₯, ππ + ππ π₯, ππ , ππ πΏπ π₯, ππ ππ β π πππ ππ ππ‘ π‘ Remco Chang π Ω 10 – Lighting and Illumination 76/70 COMP 175 | COMPUTER GRAPHICS Questions? Remco Chang 10 – Lighting and Illumination 77/70