Illumination and Shading Illumination Lighting n n Model

  • Slides: 53
Download presentation
Illumination and Shading

Illumination and Shading

Illumination (Lighting) n n Model the interaction of light with surface points to determine

Illumination (Lighting) n n Model the interaction of light with surface points to determine their final color and brightness Open. GL computes illumination at vertices illumination

Shading n Apply the lighting model at a set of points across the entire

Shading n Apply the lighting model at a set of points across the entire surface Shading

Illumination Model n n The governing principles for computing the illumination A illumination model

Illumination Model n n The governing principles for computing the illumination A illumination model usually considers: n n Light attributes (light intensity, color, position, direction, shape) Object surface attributes (color, reflectivity, transparency, etc) Interaction among lights and objects (object orientation) Interaction between objects and eye (viewing dir. )

Illumination Calculation n Local illumination: only consider the light, the observer position, and the

Illumination Calculation n Local illumination: only consider the light, the observer position, and the object material properties q n Example: Open. GL

Illumination Models n Global illumination: take into account the interaction of light from all

Illumination Models n Global illumination: take into account the interaction of light from all the surfaces in the scene object 4 object 3 object 2 object 1 n Example: Ray Tracing (CIS 681)

Basic Light Types n Directional Ø Ø n So far away so that light

Basic Light Types n Directional Ø Ø n So far away so that light rays are ║ Remember orthogonal projection? Directional light Point Ø n sun Light emanates equally in all directions Point light Spot Ø Point source limited to an angle Spot light

Light Source Types from Akenine-Moller & Haines

Light Source Types from Akenine-Moller & Haines

Object Properties n What happens when light hits an object? n Properties of light

Object Properties n What happens when light hits an object? n Properties of light reflection on an object’s surface n Reflectance Models n n n Ambient Diffuse Specular Absorption, Emission, Transparency/Translucency Irradiance: All light that arrives at a point on the surface Radiosity: Light leaving a surface in all directions

Object Properties n Object Material Ø Shiny (Metal), dull (Matte finish), mirror-like, glass, neon,

Object Properties n Object Material Ø Shiny (Metal), dull (Matte finish), mirror-like, glass, neon, etc.

Local vs. Global Illumination Local Global Illumination depends on local object & light sources

Local vs. Global Illumination Local Global Illumination depends on local object & light sources only Illumination at a point can depend on any other point in the scene

Simple local illumination n The model used by Open. GL – considers three types

Simple local illumination n The model used by Open. GL – considers three types of light contribution to compute the final illumination of an object n n Ambient Diffuse Specular Final illumination of a point (vertex) = ambient + diffuse + specular

Ambient lighting example

Ambient lighting example

Diffuse lighting example

Diffuse lighting example

Specular light example

Specular light example

Light Reflectance Components Take a point P on the object surface: L L N

Light Reflectance Components Take a point P on the object surface: L L N q p L: Light Vector R q p R: Reflection Vector Reflects about the Normal (N) to the surface L N q R q φ p V: View Vector V

Ambient Reflection n n Background light scattered by the environment Ø Light bounces off

Ambient Reflection n n Background light scattered by the environment Ø Light bounces off of many objects Ø Simple Global Illumination Simple reflectance model n Independent of … Ø Ø Ø n Light position Object orientation Viewer’s position ka: Ambient reflection coefficient Ø Ambient light an object reflects Ø 0 ≤ ka ≤ 1 Ambient = Ia * ka

Diffuse Reflection n Lambert’s Law: Ø Radiant energy D that a small surface patch

Diffuse Reflection n Lambert’s Law: Ø Radiant energy D that a small surface patch receives from a light source: D = Id * cos( ) Ø n Id = light intensity, = Angle between L and N Also called Lambertian or Matte surfaces

Lambert’s Law (1) n L How does D change if the light source moves?

Lambert’s Law (1) n L How does D change if the light source moves? Max intensity N q L L, N p L D = Id * cos( ) No intensity p

Lambert’s Law (2) n How does D change on an object’s surface? Ø A

Lambert’s Law (2) n How does D change on an object’s surface? Ø A sphere’s surface has all possible normal directions N N

Diffuse Reflection n Energy D is reflected equally in all directions on the surface

Diffuse Reflection n Energy D is reflected equally in all directions on the surface n Independent of … Ø n Viewer’s position kd: Diffuse reflection coefficient Ø Ø Diffuse light an object reflects 0 ≤ kd ≤ 1 Diffuse = Id * kd * cos( ) = Id * kd * (N L) N and L must be normalized … WHY? ? ?

Specular Reflection (1) n n The reflection of the light source on the object

Specular Reflection (1) n n The reflection of the light source on the object Shiny/Glossy surfaces Ø Not a perfect mirror Show up as Specular Highlights, i. e. , bright spots

Specular Reflection (2) n The object reflects maximum light intensity in the direction of

Specular Reflection (2) n The object reflects maximum light intensity in the direction of the Light intensity reflection vector increases as V gets closer to R N L R q q V φ V R = cos(φ) p

Specular Lobe n n The reflection of the light source is maximum at the

Specular Lobe n n 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

Specular Reflection ks: Specular reflection coefficient n Ø Specular light an object reflects 0

Specular Reflection ks: Specular reflection coefficient n Ø Specular light an object reflects 0 ≤ ks ≤ 1 N Is φ n : : Ø L surface normal at P light intensity angle between V and R shininess factor N q R q p φ V Spec = Is * ks * cosn(φ) = Is * ks * (V R)n V and R must be unit vectors … WHY? ? ?

Ambient/Diffuse/Specular n Just ambient light: n Diffuse and change Ambient n n Left: Sphere

Ambient/Diffuse/Specular n Just ambient light: n Diffuse and change Ambient n n Left: Sphere with just diffuse reflection Right: Sphere with just specular reflection

Basic Reflectance Equation n Reflectance = from Akenine-Moller & Haines Ambient Diffuse Specular Final

Basic Reflectance Equation n Reflectance = from Akenine-Moller & Haines Ambient Diffuse Specular Final = Ia * ka + Id * kd * (N L) + Is * ks * (R V)n

Put it all together n Illumination from a single light source: n Illum =

Put it all together n Illumination from a single light source: n Illum = ambient + diffuse + specular = Ka x I + Kd x I x max(0, N L) n + Ks x I x max(0, R V) n Note that the K’s and the I’s are vectors (RGB).

Put it all together n If there are N lights n n Total illumination

Put it all together n If there are N lights n n Total illumination for a point P = S (Illum) Some more terms to be added (in Open. GL): n n n Self emission Global ambient Light distance attenuation and spot light effect

Lighting in Open. GL n Adopt Phong lighting model (specular) plus diffuse and ambient

Lighting in Open. GL n Adopt Phong lighting model (specular) plus diffuse and ambient lights n Lighting is computed at vertices n n n Interpolate across surface (Gouraud/smooth shading) OR Use a constant illumination (get it from one of the vertices) Setting up Open. GL Lighting: n n n Light Properties Enable/Disable lighting Surface material properties Provide correct surface normals Light model properties

Light Properties n Properties: n Colors / Position and type / attenuation gl. Lightfv(light,

Light Properties n Properties: n Colors / Position and type / attenuation gl. Lightfv(light, property, value) 1 2 3 (1) constant: specify which light you want to set the property example: GL_LIGHT 0, GL_LIGHT 1, GL_LIGHT 2 … you can create multiple lights (Open. GL allows at least 8 lights) (2) constant: specify which light property you want to set the value example: GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_POSITION (check the red book for more) (3) The value you want to set to the property

Property Example n Define colors and position a light GLfloat light_ambient[] = {0. 0,

Property Example n Define colors and position a light GLfloat light_ambient[] = {0. 0, 1. 0}; light_diffuse[] = {1. 0, 1. 0}; light_specular[] = {1. 0, 1. 0}; light_position[] = {0. 0, 1. 0}; gl. Lightfv(GL_LIGHT 0, GL_AMBIENT, light_ambient); GL_DIFFUSE, light_diffuse); GL_SPECULAR, light_specular); GL_POSITION, light_position); colors Position What if I set the Position to (0, 0, 1, 0)?

Types of lights n Open. GL supports two types of lights n n n

Types of lights n Open. GL supports two types of lights n n n Local light (point light) Infinite light (directional light) Determined by the light positions you provide n n w = 0: infinite light source (faster) w != 0: point light – position = (x/w, y/w, z/w) GLfloat light_position[] = {x, y, z, w}; gl. Lightfv(GL_LIGHT 0, GL_POSITION, light_position);

Turning on the lights n n Turn on the power (for all the lights)

Turning on the lights n n Turn on the power (for all the lights) n gl. Enable(GL_LIGHTING); n gl. Disable(GL_LIGHTING); Flip each light’s switch n gl. Enable(GL_LIGHTn) (n = 0, 1, 2, …)

Controlling light position n n Modelview matrix affects a light’s position You can specify

Controlling light position n n Modelview matrix affects a light’s position You can specify the position relative to: n Eye space: the highlight remains in the same position relative to the eye n n World space: a light’s position/direction appears fixed in the scene n Call gl. Lightfv() after glu. Look. At() Any model space (not as intuitive). See Nate Robin’s Demo n n call gl. Lightfv() before glu. Look. At()

Material Properties n n The color and surface properties of a material (dull, shiny,

Material Properties n n The color and surface properties of a material (dull, shiny, etc. ) How much the surface reflects the incident lights (ambient/diffuse/specular reflection coefficients) gl. Materialfv(face, property, value) Face: material property for which face (e. g. GL_FRONT, GL_BACK, GL_FRONT_AND_BACK) Property: what material property you want to set (e. g. GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_SHININESS, GL_EMISSION, etc) Value: the value you can to assign to the property

Material Example n Define ambient/diffuse/specular reflection and shininess GLfloat mat_amb_diff[] = {1. 0, 0.

Material Example n Define ambient/diffuse/specular reflection and shininess GLfloat mat_amb_diff[] = {1. 0, 0. 5, 0. 8, 1. 0}; refl. coefficient GLfloat mat_specular[] = {1. 0, 1. 0}; GLfloat shininess[] = {5. 0}; (range: dull 0 – very shiny 128) gl. Materialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_amb_diff); gl. Materialfv(GL_FRONT, GL_SPECULAR, mat_specular); gl. Materialfv(GL_FRONT, GL_SHININESS, shininess);

Global light properties n gl. Light. Modelfv(property, value) Enable two-sided lighting n n n

Global light properties n gl. Light. Modelfv(property, value) Enable two-sided lighting n n n Global ambient color n n n property = GL_LIGHT_MODEL_TWO_SIDE value = GL_TRUE (GL_FALSE if you don’t want two sided lighting) Property = GL_LIGHT_MODEL_AMBIENT Value = (red, green, blue, 1. 0); Check the red book for others

Surface Normals n n Correct normals are essential for correct lighting Associate a normal

Surface Normals n n Correct normals are essential for correct lighting Associate a normal to each vertex gl. Begin(…) gl. Normal 3 f(x, y, z) gl. Vertex 3 f(x, y, z) … gl. End() n The normals you provide need to have a unit length n n You can use gl. Enable(GL_NORMALIZE) to have Open. GL normalize all the normals. Why not always have Open. GL do this?

Lighting revisit Where is lighting performed in the graphics pipeline? n v 1, m

Lighting revisit Where is lighting performed in the graphics pipeline? n v 1, m 1 modeling and viewing v 2, m 2 per vertex lighting projection interpolate vertex colors clipping v 3, m 3 Rasterization texturing shading viewport mapping Display

Polygon shading model n Flat shading – compute lighting once and assign the color

Polygon shading model n Flat shading – compute lighting once and assign the color to the whole polygon

Flat shading n n n Only use one vertex (usually the first one) normal

Flat shading n n n Only use one vertex (usually the first one) normal and material property to compute the color for the polygon Benefit: fast to compute It is used when: n n The polygon is small enough The light source is far away (why? ) The eye is very far away (why? ) Open. GL command: gl. Shade. Model(GL_FLAT)

Smooth shading n n n Remove edge discontinuity Compute lighting for more points on

Smooth shading n n n Remove edge discontinuity Compute lighting for more points on each face Still has a mach-band – perceived discontinuity due to your eye’s edge detection. Flat shading smooth shading

Smooth shading n Two popular methods: n n Smooth shading (used by Open. GL)

Smooth shading n Two popular methods: n n Smooth shading (used by Open. GL) Per-fragment lighting (better specular highlight, requires programmable shaders in Open. GL)

Smooth Shading n n n The smooth shading algorithm used in Open. GL gl.

Smooth Shading n n n The smooth shading algorithm used in Open. GL gl. Shade. Model(GL_SMOOTH) Lighting is calculated for each of the polygon vertices Colors are interpolated for interior pixels

Smooth Shading n n n Per-vertex lighting calculation Normal is needed for each vertex

Smooth Shading n n n Per-vertex lighting calculation Normal is needed for each vertex Per-vertex normals can be computed by averaging the adjacent face normals n n 1 n 3 n 2 n 4 n = (n 1 + n 2 + n 3 + n 4) / 4. 0

Smooth Shading n n Compute vertex illumination (color) before the projection transformation Shade interior

Smooth Shading n n Compute vertex illumination (color) before the projection transformation Shade interior pixels: color interpolation (normals are not needed) C 1 for all scanlines Ca = lerp(C 1, C 2) C 2 Cb = lerp(C 1, C 3) C 3 Lerp(Ca, Cb) * lerp: linear interpolation

Smooth Shading n Linear interpolation b a v 1 n x = a /

Smooth Shading n Linear interpolation b a v 1 n x = a / (a+b) * v 2 + b/(a+b) * v 1 x v 2 Interpolate triangle color: use y distance to interpolate the two end points in the scanline, and use x distance to interpolate interior pixel colors

Smooth Shading Problem n Lighting in the polygon interior can be inaccurate

Smooth Shading Problem n Lighting in the polygon interior can be inaccurate

Smooth Shading Problem n Lighting in the polygon interior can be inaccurate

Smooth Shading Problem n Lighting in the polygon interior can be inaccurate

Phong Shading n n n Instead of interpolation, we calculate lighting for each pixel

Phong Shading n n n Instead of interpolation, we calculate lighting for each pixel inside the polygon (per pixel lighting) We need to have normals for all the pixels – not provided by the user Phong shading algorithm interpolates the normals and compute lighting during rasterization (need to map the normal back to world or eye space though)

Phong Shading (2) n Normal interpolation n 1 nb = lerp(n 1, n 3)

Phong Shading (2) n Normal interpolation n 1 nb = lerp(n 1, n 3) na = lerp(n 1, n 2) lerp(na, nb) n 2 n 3 n n Slow! You will do this in the ray-tracing class.

What do Games do? n n n Games typically use precomputed diffuse illumination. Call

What do Games do? n n n Games typically use precomputed diffuse illumination. Call light-maps. We will look at these when we look at texture maps. http: //abstractmindmaps. blogspot. com/2007_01_01_archive. html