Illumination and Shading Illumination (Lighting)
Model the interaction of light with surface points to determine their final color and brightness
OpenGL computes illumination at vertices
illumination
Shading Illumination Model
The governing principles for computing the Apply the lighting model at a set of points illumination across the entire surface A illumination model usually considers:
Light attributes (light intensity, color, position, direction, shape) Shading Object surface attributes (color, reflectivity, transparency, etc)
Interaction among lights and objects (object orientation)
Interaction between objects and eye (viewing dir.)
1 Illumination Calculation Illumination Models
Local illumination: only consider the light, the Global illumination: take into account the observer position, and the object material properties interaction of light from all the surfaces in the scene
object 4
object 3 object 2 θ
object 1
Example: OpenGL Example: Ray Tracing (CIS681)
Basic Light Sources Simple local illumination
sun The model used by OpenGL – consider three types of light contribution to compute the final illumination of an object
Ambient Light intensity can be Point light Directional light independent or Diffuse dependent of the distance between object Specular and the light source Final illumination of a point (vertex) = ambient + diffuse + specular Spot light
2 Ambient light contribution Ambient lighting example
Ambient light (background light): the light that is scattered by the environment A very simple approximation of global illumination
object 4
object 3 object 2
object 1
Independent of the light position,object orientation, observer’s position or orientation – ambient light has no direction (Radiosity is the calculation of ambient light)
Ambient light calculation Diffuse light contribution
Each light source has an ambient light contribution Diffuse light: The illumination that a surface receives from a light source and reflects equally in all direction (Ia) Different objects can reflect different amounts of ambient (different ambient reflection coefficient Ka,
0 <= Ka <= 1) So the amount of ambient light that can be seen from an object is:
It does not matter where the eye is Ambient = Ia * Ka
3 Diffuse lighting example Diffuse light calculation
Need to decide how much light the object point receive from the light source – based on Lambert’s Law
Receive more light Receive less light
Diffuse light calculation (2) Diffuse light calculation (3)
Lambert’s law: the radiant energy D that a small Like the ambient light case, different objects can surface patch receives from a light source is: reflect different amount of diffuse light (different
D = I * cos (θ) diffuse reflection coefficient Kd, 0 <= Kd <= 1)) I: light intensity
θ: angle between the light vector and the surface normal So, the amount of diffuse light that can be seen is:
light vector (vector from object to light) θ Diffuse = K * I * cos (θ) N : surface normal d N L θ θ cos(θ) = N.L
4 Specular light contribution Specular light example
The bright spot on the object
The result of total reflection of the incident light in a concentrate region
See nothing!
Specular light calculation Specular light calculation (2)
Phong lighting model How much reflection you can see depends on n where you are specular = Ks * I * cos (φ) The only position the eye can see specular from P K : specular reflection coefficient if the object has an ideal reflection surface s N: surface normal at P I: light intensity N But for a non-perfect surface you will φ: angle between V and R L R still see specular highlight when you move a little bit away from the idea reflection cos( ): the larger is n, the smaller φ φ n φ θ θ V θ ? direction is the cos value cos(θ) = R.V p p When φ is small, you see more specular highlight
5 Specular light calculation (3) Put it all together
The effect of ‘n’ in the phong model Illumination from a light: Illum = ambient + diffuse + specular n n = Ka * I + Kd * I * (()N.L) + Ks * I * (()R.V) n = 10 n = 90 If there are N lights or
Total illumination for a point P = Σ (Illum) (N.H) Some more terms to be added (in OpenGL):
Self emission n = 30 n = 270 Global ambient
Light distance attenuation and spot light effect
Lighting in OpenGL Light Properties
Properties: Adopt Phong lighting model (specular) plus diffuse and ambient lights Colors / Position and type / attenuation Lighting is computed at vertices ggglLightfv( (g,pplight, property, value) ItInterpo ltlate across sur face (Gourau d/smoo thhdi)th shading) OR
Use a constant illumination (get it from one of the vertices) 1 2 3
Setting up OpenGL Lighting: (1) constant: specify which light you want to set the property Light Properties example: GL_LIGHT0, GL_LIGHT1, GL_LIGHT2 … you can Enable/Disable lighting create multiple lights (OpenGL allows at least 8 lights) Surface material properties (2) constant: specify which light property you want to set the value Provide correct surface normals example: GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_POSITION
Light model properties (check the red book for more) (3) The value you want to set to the property
6 Property Example Types of lights
Define colors and position a light OpenGL supports two types of lights
GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0}; Local light (point light) GLfloat light _diffuse[] = {1 .0 , 1 .0 , 1 .0 , 1 .0}; colors Infinite light (directional light) GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_position[] = {0.0, 0.0, 1.0, 1.0}; Position Determined by the light positions you provide
w = 0: infinite light source (faster) glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); What if I set the glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); Position to w != 0: point light – position = (x/w, y/w, z/w) glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); (0,0,1,0)? glLightfv(GL_LIGHT0, GL_POSITION, light_position); GLfloat light_position[] = {x,y,z,w}; glLightfv(GL_LIGHT0, GL_POSITION, light_position);
Turning on the lights Controlling light position
Modelview matrix affects a light’s position Turn on the power (for all the lights) You can specify the position relative to: glEnable(GL_LIGHTING); Eyype space: the hig gghlight remains in the same position relative to the eye glDisable(GL_LIGHTING); call glLightfv() before gluLookAt() World space: a light’s position/direction appears fixed in the scene Flip each light’s switch Call glLightfv() after gluLookAt() glEnable(GL_LIGHTn) (n = 0,1,2,…) See Nat Robin’s Demo http://www.xmission.com/~nate/tutors.html
7 Material Properties Material Example
Define ambient/diffuse/specular reflection The color and surface properties of a material (dull, shiny, etc) and shininess
How much the surface reflects the incident lights
(ambient/diffuse/specular reflecetion coefficients) GLfloat mat_amb_diff[] = {1.0, 0.5, 0.8, 1.0}; refl. coefficient glMaterialfv(face, property, value) GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat shininess[] = {5.0}; (range: dull 0 – very shiny128) Face: material property for which face (e.g. GL_FRONT, GL_BACK, GL_FRONT_AND_BACK) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Property: what material property you want to set (e.g. GL_AMBIENT, GL_DIFFUSE, mat_amb_diff); GL_SPECULAR, GL_SHININESS, GL_EMISSION, etc) glMaterialfv(GL_FRONT, GL_SPECULAR, mat_speacular); Value: the value you can to assign to the property glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
Global light properties Surface Normals
glLightModelfv(property, value) Correct normals are essential for correct lighting
Enable two sided lighting Associate a normal to each vertex
property = GL_LIGHT_MODEL_TWO_SIDE glBegin(…) value = GL_TRUE (GL_FALSE if you don’t want two sided glNormal3f(x,y,z) lighting) glVertex3f(x,y,z) Global ambient color …
Property = GL_LIGHT_MODEL_AMBIENT glEnd()
Value = (red, green, blue, 1.0); The normals you provide need to have a unit length
Check the red book for others You can use glEnable(GL_NORMALIZE) to have OpenGL normalize all the normals
8 Lighting revisit Polygon shading model
Where is lighting performed in the Flat shading – compute lighting once and graphics pipeline? assign the color to the whole polygon
v1, m1
modeling and per vertex projection viewing lighting v2, m2 v3, m3
Rasterization viewport interpolate clipping texturing mapping vertex colors shading
Display
Flat shading Mach Band Effect
Only use one vertex (usually the first one) Flat shading suffers from “mach band effect” normal and material property to compute the color for the ppygolygon Mach band effect – human eyes accentuate the discontinuity at the boundary Benefit: fast to compute perceived intensity It is used when:
The polygon is small enough
The light source is far away (why?)
The eye is very far away (why?) OpenGL command: glShadeModel(GL_FLAT) Side view of a polygonal surface
9 Smooth shading Smooth shading
Two popular methods:
Reduce the mach band effect – remove Gouraud shading (used by OpenGL) value discontinuity Phong shading (better specular highlight, Compute lighting for more points on each not supported by OpenGL) face
Flat shading smooth shading
Gouraud Shading (1) Gouraud Shading (2)
The smooth shading algorithm used in OpenGL Per-vertex lighting calculation glShadeModel(GL_SMOOTH) Normal is needed for each vertex Lighting is calculated for each of the polygon vertices Per-vertlbtdbtex normal can be computed by Colors are interpolated for interior pixels averaging the adjust face normals
n n1 n2 n4 n3 n = (n1 + n2 + n3 + n4) / 4.0
10 Gouraud Shading (3) Gouraud Shading (4)
Compute vertex illumination (color) before the Linear interpolation projection transformation
Shade interior pixels: color interpolation (normals x = a/(a+b)*v2+b/(a+b)*v1a / (a+b) * v2 + b/(a+b) * v1 are not needed) a b v1x v2 C1 Interpolate triangle color: use y distance to for all scanlines interpolate the two end points in the scanline, and Ca = lerp(C1, C2) Cb = lerp(C1, C3) use x distance to interpolate interior pixel colors C2 C3 * lerp: linear interpolation Lerp(Ca, Cb)
Gouraud Shading Problem Gouraud Shading Problem
Lighting in the polygon interior can be Lighting in the polygon interior can be inaccurate inaccurate
11 Phong Shading Phong Shading (2)
Normal interpolation Instead of color interpolation, we calculate n1 lighting for each pixel inside the polygon (per pixel lighting) na = lerp(n1 , n2) nb = lerp(n1 , n3)
We need to have normals for all the pixels – lerp(na, nb) not provided by the user n2
Phong shading algorithm interpolates the n3 normals and compute lighting during Slow – not supported by OpenGL and most of rasterization (need to map the normal back to the graphics hardware world or eye space though - WHY?)
12