Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lighting Glenn G. Chappell

Similar presentations


Presentation on theme: "Lighting Glenn G. Chappell"— Presentation transcript:

1 Lighting Glenn G. Chappell CHAPPELLG@member.ams.org
U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, November 12 and Friday, November 14, 2003

2 Review: Simplifying Lighting
Various lighting models do computations based on the physical properties of light. E.g., ray tracing, discussed briefly last time. These are all very slow and typically do not integrate well with our polygon-based pipeline rendering model. So we simplify, to get a very unrealistic, but quite workable, lighting model. 12-14 Nov 2003 CS 381

3 Review: Simplified View of Surfaces
We identify broad categories of surfaces. Specular Surfaces These surfaces act like mirrors. Some light can be absorbed. Light that does reflect bounces off at a single angle. Angle of incidence = angle of reflection. Diffuse Surfaces These surfaces scatter light in all directions. We generally assume a perfectly diffuse, or Lambertian surface, which scatters light equally in all directions. Insert disagreement from Dr. H here?? Translucent Surfaces These allow some light to penetrate and emerge somewhere else. In practice, we generally model a surface as some combination of these three. Angle of incidence Angle of reflection 12-14 Nov 2003 CS 381

4 Review: Simplified View of Light Sources
Direct light comes directly from the light source to the object. Point sources. A point source has a position (point: x, y, z) in 3-D space. Homogenous form: (x, y, z, 1). A spotlight only emits light in or close to a certain direction. Sources “at infinity”. Such a distant light source has a direction (vector: x, y, z). Homogeneous form: (x, y, z, 0). Some light is not direct. We approximate it with ambient light. Approximate light bouncing around in the scene using dim light coming equally from all directions. 12-14 Nov 2003 CS 381

5 The Phong Model: Introduction
Now we look at the model behind OpenGL’s built-in lighting: The Phong Reflection Model. The Phong Model is based on the work of Bui-Tuong Phong in the early/mid-1970’s. It is a simple, easy-to-compute model, but it is highly inaccurate (physically). However, as we have seen, it does produce decent looking pictures. As long as you don’t mind everything looking plastic-y. Note: “The Phong Model” is not the same as “Phong shading”. Same Phong, different idea. 12-14 Nov 2003 CS 381

6 The Phong Model: Types of Reflected Light
The Phong Model divides reflected light into three types: Ambient. Ambient lighting reflected in a diffuse manner. Diffuse. Direct lighting reflected in a diffuse manner. Specular. Direct lighting reflected in a specular manner. As we can see in the photograph, these categories do correspond somewhat to real-world effects. Next we look at: How to compute the color of each of these. How to use the results to find the vertex color. 12-14 Nov 2003 CS 381

7 The Phong Model: Computing Ambient
Last time, we discussed a way to compute the effect of ambient light. This method is what the Phong Model uses. We need: The light color of the ambient light (AR, AG, AB). The paint color of the material of the surface the vertex lies on (MR, MG, MB). Paint color = color of the “paint” on the surface, not the color the surface appears to be. The resulting color is found via componentwise multiplication: (ARMR, AGMG, ABMB). So, for example, if there is no ambient light, then the ambient color is always black (0, 0, 0). If the ambient lighting is cut in half, then the ambient reflection color is also cut in half. In short, this computation makes sense. 12-14 Nov 2003 CS 381

8 The Phong Model: Computing Diffuse [1/3]
The method for computing the diffuse component of the Phong Model is essentially the method we discussed last time. This portion of the model is based on the idea of Lambertian (perfectly diffuse) reflection. The apparent color of the surface does not depend on where the viewer is. But it does depend on the relationship between light direction and the surface normal. Consider a Lambertian surface lit by direct lighting. If the light direction is parallel to the surface normal, then the surface is brightly lit. As the surface turns away from the light source, it gets dimmer. The illustrations show why. As the surface turns away from the light, the points at which rays hit are spaced farther apart. The same amount of light illuminates more area, and so the reflected light is not as bright. 12-14 Nov 2003 CS 381

9 The Phong Model: Computing Diffuse [2/3]
How does intensity of the the diffuse reflection change when the surface turns? Let  be the angle between the surface normal vector and the light direction vector. The two angles labeled “ ” in the illustration have the same measure. (Right? Why?) The light in an area of size a hits an area of size b on the surface. So: multiply each component of the reflected light color by a/b = cos . cos  is called the Lambert cosine. If the two vectors have length 1, then the Lambert cosine is just their dot product. a Surface normal vector b 12-14 Nov 2003 CS 381

10 The Phong Model: Computing Diffuse [3/3]
One last piece: attenuation. In the real world, the effect of light is less the farther away it is. Divide the brightness of the light by the square of its distance to get the right effect. We do not do attenuation with lights at infinity. In practice we often do not do attenuation at all, so that a light source has the same effect at any distance. Putting it all together, we need: The light color of the direct light (DR, DG, DB). The paint color of the surface the vertex lies on (MR, MG, MB). The Lambert cosine k. The distance d to the light source, if we are doing attenuation. Diffuse reflection color = (kDRMR, kDGMG, kDBMB). If we are doing attenuation, multiply all the above by 1/d2. 12-14 Nov 2003 CS 381

11 The Phong Model: Computing Specular [1/4]
Next, we look at the specular component of the Phong model. This is Phong’s main contribution. Now it matters where the viewer is. We want the viewer to see a specular highlight when the viewing direction is the same as the reflected light direction. Problem Our light source is a point. But a normal-looking specular highlight is not a single point. Solution Spread out the specular highlight a little, by allowing the viewer to see the highlight if the viewing direction is close to the proper reflected light direction. Sees specular highlight at point A. Sees specular highlight at point B. A B 12-14 Nov 2003 CS 381

12 The Phong Model: Computing Specular [2/4]
As with the diffuse component, we want to multiply the specular reflection color by something. Let α be the angle between the reflected light direction and the viewing direction. It seems reasonable to multiply the intensity of the reflected light by cos α. (Right? Think about it …) However, this results in overly large, fuzzy specular highlights. To shrink the highlight, we raise cos α to a power before multiplying. cos α is a number between 0 and 1. Raising it to a high power reduces its value, which makes the specular reflection less apparent, far away from the correct reflected light direction. Result: smaller highlight. The power is often called the shininess. The higher the shininess, the smaller and sharper the highlight. Sees specular highlight at point A. Sees faint specular highlight at point A. α A 12-14 Nov 2003 CS 381

13 The Phong Model: Computing Specular [3/4]
One last piece: How do we find the reflected light direction? Let N be the (unit) surface normal vector. Let L be (unit) light direction vector. The reflected light direction R is the component of L in the direction of N, times 2, minus L: R = 2(L·N)N – L. R is a unit vector. So: Do the lighting computation as for ambient. But use the specular reflection color, not the paint color. Multiply by (R·V)s, where V is the viewing direction, and s is the shininess. If desired, do attenuation as for diffuse. 12-14 Nov 2003 CS 381

14 The Phong Model: Computing Specular [4/4]
Not everyone is happy with the method used to compute specular reflection in the Phong Model. “Bui-Tuong Phong is a great guy and he did wonderful work and so forth. But he introduced this concept of the cosine power. … The thing is, this has no physical basis whatsoever, but all graphics systems today, now, do highlights this way … . This was a crude approximation at the time. There are better ways of calculating things like this. I’d like to see cosine power retired and better approximations being done.” — Jim Blinn, SIGGRAPH 1998 Keynote Address 12-14 Nov 2003 CS 381

15 The Phong Model: Putting It All Together
We have seen how the Phong Model computes the effects of ambient, diffuse, and specular reflection. Now, what is the color of the vertex? How about just adding these up? Final red = ambient red + diffuse red + specular red. Similarly for green & blue. But this may result in RGB components greater than 1. Solution: If any of R, G, B ends up being greater than 1, set it equal to 1. This is not entirely satisfactory, but at least it gives legal results. 12-14 Nov 2003 CS 381

16 Basic OpenGL Lighting: Introduction
OpenGL implements the Phong Model. We still generally need to compute normals ourselves. We set up lights and materials; OpenGL does the rest. How it works: Set up and enable one or more lights (light sources). Use glLight*, glEnable. Various other lighting properties can be set. With glLightModel*, glShadeModel, etc. Enable/disable lighting as appropriate. Enable with “glEnable(GL_LIGHTING);”. When drawing, set material properties. Use glMaterial*. Forget about glColor*, for now. Before each glVertex* command, specify a normal vector. Use glNormal*. Now we look at lights and materials. Our sample code is sample3d.cpp, from the web page (week 7). 12-14 Nov 2003 CS 381

17 Basic OpenGL Lighting: Setting Up Lights [1/2]
We set the properties of a light with glLight* (generally glLightfv). Three parameters: Which light. Possibilities: GL_LIGHT0 … GL_LIGHT7. Which property. Possibilities: GL_POSITION, GL_DIFFUSE, etc. Value of the property. For the major properties, this is given as a pointer to an array of four GLfloat’s. Position/direction is given in homogeneous form. 4th coord. = 1 for position. 4th coord. = 0 for direction. Color is given as RGBA. Set the 4th coord. to 1 unless you have a good reason not to. Enable a light by passing the “which light” parameter to glEnable. A light has no effect unless it is enabled. 12-14 Nov 2003 CS 381

18 Basic OpenGL Lighting: Setting Up Lights [2/2]
Here is the code from sample3d.cpp that sets up light #0. GLfloat light0_position[] = { -1.0, 1.5, 2.0, 0.0 }; GLfloat light0_maincolor[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light0_ambientcolor[] = { 0.0, 0.0, 0.0, 1.0 }; glLightfv(GL_LIGHT0, GL_POSITION, light0_position); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_maincolor); glLightfv(GL_LIGHT0, GL_SPECULAR, light0_maincolor); glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambientcolor); glEnable(GL_LIGHT0); An OpenGL light has: Position/direction. Set with GL_POSITION. Ambient, diffuse, and specular colors. Set as above. Be sure to enable, if you want the light to do anything. 12-14 Nov 2003 CS 381

19 Basic OpenGL Lighting: Setting Up Materials [1/2]
We set the properties of a surface’s material with Material* (often glMaterialfv). Three parameters: Which face. Possibilities: GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK. For now, use GL_FRONT_AND_BACK. Which property. Possibilities: GL_DIFFUSE, GL_SHININESS, etc. Value of the property. Again, a pointer to an array of GLfloat’s. Color is given as RGBA. Set the 4th coord. to 1 unless you have a good reason not to. Shininess is a single value. Think of glMaterial* calls as replacing glColor*. 12-14 Nov 2003 CS 381

20 Basic OpenGL Lighting: Setting Up Materials [2/2]
Here is the code from sample3d.cpp that sets material properties. void setmaterial_main() { GLfloat paintcolor[] = { 0.9, 0.2, 0.5, 1.0 }; GLfloat specularcolor[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat shininess[] = { 50. }; glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, paintcolor); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specularcolor); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess); } Looks pretty similar to setting up a light. Note “GL_AMBIENT_AND_DIFFUSE”. These are almost always the same, so this is offered as a convenience. 12-14 Nov 2003 CS 381

21 Basic OpenGL Lighting: A Few Tips
Lights Diffuse and specular colors are generally the same. Ambient color is generally either a dim (10-30%) version of the main color, or else black. Materials Ambient and diffuse colors are generally the same (the paint color). Specular color is generally white (1, 1, 1, 1). Shininess can range from 0 to 128. There is no “material #1”, “material #2”; there are just the current material properties. Write a function for each material you use, as in sample3d.cpp. Other Don’t forget to enable your lights, as well as lighting in general. Understand the difference! If you do both lighting and scaling, be sure to do “glEnable(GL_NORMALIZE);”. If you use glutSolidTorus, glutSolidSphere, etc., then the normals are done for you. 12-14 Nov 2003 CS 381

22 Normal Vectors: Introduction
When we do lighting, each polygon vertex needs an associated normal vector. This should be a unit vector pointing straight out from the surface at the vertex. In OpenGL We specify a normal with glNormal*, inside glBegin-glEnd. A glNormal* call comes before the associated glVertex*. Example: glNormal3d(0., 0., 1.); glVertex3d(1., 2., 3.); The tricky part is computing the normals. We will discuss three methods. Facet normals using cross products. Normals based on the mathematical formula for the surface. Other methods. 12-14 Nov 2003 CS 381

23 Normal Vectors: Facet Normals [1/2]
A single polygon that is part of a mesh making up a surface is called a facet. A facet normal is the normal of a facet. Using facet normals gives a “gem-like” look to a surface. We do not use facet normals when we want a surface that appears to be smooth. The facet normal of a polygon is the normalized cross product of two (non-colinear) vectors parallel to the plane of the polygon. We can find these vectors by subtracting points in this plane (e.g., vertices of the polygon). Fancier forms of OpenGL lighting require the normal to point toward the front side of a polygon. If you compute facet normals, and your polygons all appear dark, try reversing them. When using facet normals, all vertices in a polygon get the same normal, so only one glNormal* call is required per polygon. On the next page is some sample code. It uses the pos and vec classes from vecpos.h, on the web page. 12-14 Nov 2003 CS 381

24 Normal Vectors: Facet Normals [2/2]
Here is a function that computes and uses a facet normal. It requires vecpos.h. You probably also want “using namespace tf;”. This code fragment is on the web page as drawtriangle.cpp. // drawtriangle // Draw a triangle with given vertices & computed facet normal. void drawtriangle(const pos & a, const pos & b, const pos & c) { const vec n = cross(b-a, c-a).normalized(); // normal vector glBegin(GL_TRIANGLES); glNormal3d(n[0], n[1], n[2]); glVertex3d(a[0], a[1], a[2]); // The normal is a GL state. We need not repeat glNormal*. glVertex3d(b[0], b[1], b[2]); glVertex3d(c[0], c[1], c[2]); glEnd(); } 12-14 Nov 2003 CS 381

25 Normal Vectors: From Surface Formula [1/3]
Every surface we draw in OpenGL is a mesh of polygons. However, many of these are actually approximations of smooth surfaces described by mathematical formulae. For example, a sphere, a torus, etc. Therefore, the normal for the surface described by the formula is a good choice for a vertex normal. 12-14 Nov 2003 CS 381

26 Normal Vectors: From Surface Formula [2/3]
Example 1: A Sphere. Suppose we want to draw a sphere with radius 1, centered at (0, 0, 0). For any point (x, y, z) on this sphere, the arrow from (0, 0, 0) to (x, y, z) points straight out from the surface of the sphere. Further, sqrt[x2 + y2 + z2] = 1, and so (x, y, z), as a vector, is a unit vector. Thus, for each point, we can do something like glNormal3d(x, y, z); // Same as vertex coords glVertex3d(x, y, z); 12-14 Nov 2003 CS 381

27 Normal Vectors: From Surface Formula [3/3]
Example 2: A Plane. Recall (from the discussion of shadows): The equation of a plane in 3-D can be written as ax + by + cz + d = 0. Then (a, b, c) is a normal for the plane. It might not be a unit vector, though, so normalize! It also might not point toward the “front” of the plane. If not, reverse it: (–a, –b, –c). Proof that (a, b, c) is a normal for the plane: We can restate the equation as follows: for any point P in the plane, P · (a, b, c) = –d. Now, if V is a vector parallel to the plane, then P + V is a point in the plane, and V · (a, b, c) = (P + V) · (a, b, c) – P · (a, b, c) = (–d) – (–d) = 0. Thus, (a, b, c) is perpendicular to every vector parallel to the plane; (a, b, c) is a normal vector for the plane. 12-14 Nov 2003 CS 381

28 Normal Vectors: Other Methods
Sometimes we are simply given a mesh of polygons, that we want to draw as a smooth-looking surface. We can compute facet normals, but these do not give a smooth appearance. We need a normal at each vertex that expresses roughly what direction the surface is facing at that spot. One solution: Gouraud Shading. “Gouraud” is pronounced GOO-ROW. Gargle a bit while saying the “r”, and you’ve got it.  To find vertex normals: Compute a facet normal for each facet. For each vertex find the average of the facet normals for the facets surrounding it. E.g., if there are 6 such facets, add up the normals, and divide by 6. This might not be a unit vector, so normalize. This gives us the normal vector. Gouraud shading also includes the idea of lirping colors over a facet, but OpenGL already does that. 12-14 Nov 2003 CS 381

29 Moving Lights: Introduction
We have seen how OpenGL implements the Phong Model using lights and material properties. Now we look (briefly) at how to move lights using the model/view transformation. More on this next time. 12-14 Nov 2003 CS 381

30 Moving Lights: Lights and Model/View
A light is something to be positioned within the world, just like a polygon. Therefore, it is natural that light positions pass through the model/view transformation. The matrix used is the current one at the time when glLightfv(…, GL_POSITION, …) is called. Not the current one when a lit object is drawn! Therefore, you probably want to position your lights in the display function … If you want a light to move. If you want to make a light source visible by drawing an object at its location. If you have any other reason to be interested in exactly where your lights are. You can still set a light’s colors in the initialization, then set its position in the display function. 12-14 Nov 2003 CS 381


Download ppt "Lighting Glenn G. Chappell"

Similar presentations


Ads by Google