CS488 Shading and Illumination Luc R ENAMBOT
Introduction • Through the first half of the class, polygons were represented by the line segments connecting their vertices, or by filling them with a solid color. This is not very realistic looking • Now we are going to talk about how different lighting models are used to make computer graphics look more realistic 2
General Principles • Trying to recreate reality is difficult • Lighting calculations can take a VERY long time. • The techniques described here are heuristics which produce appropriate results, but they do not work in the same way reality works - because that would take too long to compute, at least for interactive graphics • Instead of just specifying a single color for a polygon we will instead specify the properties of the material that the polygon is supposed to be made out of, and the properties of the light or lights shining onto that material. 3
Illumination Models • No Lighting • There are no lights in the scene • Each polygon is self-luminous (it lights itself, but does not give off light) • Each polygon has its own color which is constant over its surface • That color is not affected by anything else in the world • That color is not affected by the position or orientation of the polygon in the world • This is very fast, but not very realistic • position of viewer is not important 4
No Lighting • I = Ki • I: intensity • Ki: object's intrinsic intensity, 0.0 - 1.0 for each of R, G, and B 5
Ambient Model • Non-directional light source • Simulates light that has been reflected so many times from so many surfaces it appears to come equally from all directions • Intensity is constant over polygon's surface • Intensity is not affected by anything else in the world • Intensity is not affected by the position or orientation of the polygon in the world • Position of viewer is not important • I = Ia Ka • I: intensity • Ia: intensity of Ambient light • Ka: object's ambient reflection coefficient, 0.0 - 1.0 for each of R, G, and B 6
Types of Light Sources • Point light - a light that gives off equal amounts of light in all directions. Polygons, and parts of polygons which are closer to the light appear brighter than those that are further away • Directional light - if a point light is moved to infinity, all of the light rays emanating from the light strike the polygons in the scene from a single direction • Spotlight - light that radiates light in a cone with more light in the center of the cone, gradually tapering off towards the sides of the cone 7
Examples • Model of an owl: • Bounding boxes of the components of the owl 8
Self-luminous owl 9
Directional light from the front of the owl 10
Point light slightly in front of the owl 11
Spotlight slightly in front of the owl aimed at the owl 12
Diffuse Reflection • Lambertian Reflection • Using a point light: • Comes from a specific direction • Reflects off of dull surfaces • Light reflected with equal intensity in all directions • Brightness depends on theta - angle between surface normal (N) and the direction to the light source (L) • Position of viewer is not important 13
Illumination • Usually three components: • I = ambient + diffuse + specular 14
Diffuse Reflection • I = Ip Kd cos(theta) or I = Ip Kd(N' * L') • I: intensity • Ip: intensity of point light • Kd: object's diffuse reflection reflection coefficient, 0.0 - 1.0 for each of R, G, and B • N': normalized surface normal • L': normalized direction to light source 15
Diffuse Reflection • Using a directional light • I = Ip Kd cos(theta) or I = Ip Kd(N' * L') • theta is constant • L' is constant • Directional lights are faster than point lights because L' does not need to be recomputed for each polygon 16
Adding Ambient Light • It is rare that we have an object in the real world illuminated only by a single light • There is always some ambient light. To make sure all sides of an object get at least a little light we add some ambient light: • I = Ia Ka + Ip Kd(N' * L') 17
Attenuation • Currently there is no distinction made between an object close to a point light and an object far away from that light • It helps to introduce a term based on distance from the light • Source attenuation factor: Fatt. • I = Ia Ka + Fatt Ip Kd(N' * L') • Coming up with an appropriate value for Fatt is rather tricky 18
Specular Reflection • Reflection off of shiny surfaces - you see a highlight • Shiny metal or plastic has high specular component • Chalk or carpet has very low specular component • Position of the viewer IS important in specular reflection 19
Specular Reflection • Depends on perfect reflection direction, viewer direction, and surface normal • I = Ip cos^n(a) W(theta) • I: intensity • Ip: intensity of point light • n: specular-reflection exponent (higher is sharper falloff) • W: gives specular component of non-specular materials 20
Reflection 21
Falloff 22
Specular Reflection 23
Putting It All Together • I = Ia Ka + Ip Kd(N' * L') + Ip cos^n(a) W(theta) • Multiple Lights • With multiple lights the affect of all the lights are additive 24
OpenGL • A polygon can have the following material properties: • ambientColor (R, G, B) • diffuseColor (R, G, B) • specularColor (R, G, B) • emissiveColor (R, G, B) • transparency 0.0 - 1.0 • shininess 0.0 - 1.0 25
OpenGL • glLightfv(GL_LIGHT0, GL_AMBIENT, amb_light_rgba ); • glLightfv(GL_LIGHT0, GL_DIFFUSE, dif_light_rgba ); • glLightfv(GL_LIGHT0, GL_SPECULAR, spec_light_rgba ); • glLightfv(GL_LIGHT0, GL_POSITION, position); • glEnable(GL_LIGHT0); • glMaterialfv( GL_FRONT, GL_AMBIENT, ambient_rgba ); • glMaterialfv( GL_FRONT, GL_DIFFUSE, diffuse_rgba ); • glMaterialfv( GL_FRONT, GL_SPECULAR, specular_rgba ); • glMaterialfv( GL_FRONT, GL_SHININESS, n ); 26
OpenGL • These properties describe how light is reflected off the surface of the polygon • A polygon with diffuse color (1, 0, 0) reflects all of the red light it is hit with, and absorbs all of the blue and green • If this red polygon is hit with a white light it will appear red. • If it with a blue light, or a green light, or an aqua light it will appear black (as those lights have no red component.) • If it is hit with a yellow light or a purple light it will appear red (as the polygon will reflect the red component of the light) 27
Examples Ball Light Image Ball Light Image white red purple blue red white yellow aqua red green 28
Fog • Atmospheric affects give us a sense of depth • Fog useful to simulate this effect • Characterized by • a starting distance, • an ending distance, • a color • The fog begins at the starting distance and all the colors slowly transition to the fog color towards the ending distance. • At the ending distance all colors are the fog color 29
Examples 30
OpenGL Fog • Specify: • Color of the fog as R, G, and B values • Function for how to map the intermediate distances (linear, exponential, exponential squared • Where the fog begins and where the fog ends if using linear mapping • Density of the fog if using one of the two exponentials mappings 31
OpenGL Fog Without fog With fog 32
OpenGL Calls • glEnable (GL_FOG); • glFogi (GL_FOG_MODE, GL_LINEAR); • glFogfv (GL_FOG_COLOR, fogColor); • glFogf (GL_FOG_DENSITY, density); • glFogf (GL_FOG_START, 1); • glFogf (GL_FOG_END, 10); • glHint (GL_FOG_HINT, GL_NICEST); 33
Shading Models • We often use polygons to simulate curved surfaces. If these cases we want the colors of the polygons to flow smoothly into each other • Flat shading • Goraud shading (interpolation shading) • Phong shading 34
Flat Shading • Each entire polygon is drawn with the same color • Need to know one normal for the entire polygon • Fast • Lighting equation used once per polygon 35
Flat Shading 36
Gouraud Shading • Colors are interpolated across the polygon • Need to know a normal for each vertex of the polygon • Slower than flat shading • Lighting equation used at each vertex 37
Gouraud Shading 38
Phong Shading • Normals are interpolated across the polygon • Need to know a normal for each vertex of the polygon • Better at dealing with highlights than Gouraud shading • Slower than Gouraud shading • Lighting equation used at each pixel 39
Phong Shading 40
Phong Shading Ia = (Ys - Y2) / (Y1 - Y2) * I1 + (Y1 - Ys) / (Y1 - Y2) * I2 Ib = (Ys - Y3) / (Y1 - Y3) * I1 + (Y1 - Ys) / (Y1 - Y3) * I3 Ip = (Xb - Xp) / (Xb - Xa) * Ia + (Xp - Xa) / (Xb - Xa) * Ib 41
Anti-Aliasing • Lines and the edges of polygons still look jagged at this point. • This is especially noticeable when moving through a static scene looking at sharp edges • This is known as aliasing, and is caused by the conversion from the mathematical edge to a discrete set of pixels. We saw near the beginning of the course how to scan convert a line into the frame buffer, but at that point we only deal with placing the pixel or not placing the pixel. 42
Recommend
More recommend