Download presentation
Presentation is loading. Please wait.
Published byJeffrey Young Modified over 9 years ago
1
1 Graphics CSCI 343, Fall 2013 Lecture 20 Lighting and Shading III
2
2 The Phong Reflection Model The phong reflection model leads to efficient computation of surface lighting and gives reasonably realistic renderings. It uses 4 vectors to calculate the color and intensity of a point on a surface. n = normal to the surface at p l = direction to light source v = direction to viewer (COP) r = direction of perfectly reflected ray (determined by n and l) p l n v r Last time we saw how to calculate n and r.
3
3 Computation of r l r ii rr r must lie in the same plane as l and n. Therefore, n From the above three equations we can derive r in terms of l and n (we will do this in class):
4
4 The halfway vector Computing r takes a lot of computation. We can approximate the angle between r and v by computing the halfway vector. l n r v ii h The angle between n and h, , is 1/2 the angle between r and v, . What is in terms of ? We can use instead of in our calculation of specular reflection: We adjust ' to account for the fact that < 2 =
5
5 Example of computing h Compute h from the following l and r:
6
6 The Phong reflection model and OpenGL The Phong reflection model: In openGL we must specify: 1) Lighting properties: Ld, Ls and La 2) Light position (or direction): l 3) Material reflectance properties: kd, ks, ka, 4) Surface normals
7
7 Light Sources in OpenGL OpenGL allows up to 8 light sources. Enable lighting with: glEnable(GL_FLAT);//Or GL_SMOOTH glEnable(GL_LIGHTING); glEnable(GL_LIGHT0);//first light source glEnable(GL_LIGHT1);//etc. up to GL_LIGHT7 glShadeModel(GL_FLAT);//Or GL_SMOOTH These are usually called once before the glutMainLoop( ). myInit( ) is a good place to call them.
8
8 Specify Light properties To specify values of the ambient, diffuse and specular light parameters, use the following: glLightf(SourceNumber, Parameter, Value) ORglLightfv(SourceNumber, Parameter, PointerToArray) Example: GLfloatlight0_pos[ ] = {1.0, 2.0, 3.0, 1.0};//x, y, z, w GLfloatdiffuse0[ ] = {1.0, 0.0, 0.0, 1.0};//r, g, b, a glLightfv(GL_LIGHT0, GL_POSITION, light0_pos); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0); Can also use GL_AMBIENT and GL_SPECULAR for Parameter.
9
9 Lighting Code GLfloat light_ambient[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; GLfloat light0_pos[] = {100.0, 200.0, 0.0, 1.0}; glLightfv(GL_LIGHT0, GL_POSITION, light0_pos); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
10
10 Distant vs. Nearby light sources For distant light sources, we only need to specify the direction, because the light rays hitting the surface are all approximately parallel: postion0[ ] = {1.0, 1.0, 1.0, 0.0};//Note w is zero. For nearby light sources, we must specify position explicitly. postion0[ ] = {100.0, 100.0, 100.0, 1.0};//Note w is one. Distant sourceNearby source
11
11 Specifying Material Properties To specify material properties, use: glMaterialfv(face, type, pointer_to_array); Values for face: GL_FRONT or GL_FRONT_AND_BACK Values for type: GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_SHININESS Example: GLfloat ambient[ ] = {0.2, 0.2, 0.2, 1.0};//r, g, b, a GLMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
12
12 Code for Material Properties GLfloat mat_specular[] = {1.0, 0.0, 1.0, 1.0}; GLfloat mat_diffuse[] = {0.3, 0.0, 0.3, 1.0}; GLfloat mat_ambient[] = {0.1, 0.0, 0.1, 1.0}; GLfloat mat_shininess[] = {200.0}; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
13
13 Specifying the Normal vectors (-100, -100, 100) (100, 100, -100) The normals for a box with faces parallel to the x-y, y-z and z-x planes are the unit vectors along the x, y or z axes, in the positive or negative directions. n 1 = (1.0, 0.0, 0.0) n 4 = (0.0, 1.0, 0.0) 01 23 5 67 4 n 0 = (0.0, 0.0, 1.0)
14
14 Set up vertices and normals point3 vertices[8] = { {-100, -100, 100}, {100, -100, 100}, {100, 100, 100}, {-100, 100, 100}, {-100, -100, -100}, {100, -100, -100}, {100, 100, -100}, {-100, 100, -100}}; point3 normals[6] = { {0.0, 0.0, 1.0}, {1.0, 0.0, 0.0}, {0.0, 0.0, -1.0}, {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {0.0, -1.0, 0.0}};
15
15 Flat Shading In flat shading, the light source is modeled as a distant source. Each point on each plane has the same light hitting it and so each plane is a uniform color. In this case, we specify each surface normal once, immediately before listing the vertices of the corresponding polygon: glNormal3fv(normals[0]);// (0, 0, 1) for (i=0; i<4; i++){/*First face--front of cube*/ glVertex3fv(vertices[i]); } glNormal3fv(normals[1]);// (1, 0, 0) glVertex3fv(vertices[1]); /*Second face--right side of cube*/ glVertex3fv(vertices[5]); glVertex3fv(vertices[6]); glVertex3fv(vertices[2]); etc.
16
16 Calculating Surface Normals Not all surface normals are as easy to compute as for the cube. We can calculate surface normal for any flat surface from 3 of its vertices. p0p0 p1p1 p3p3 Calculating the cross product: (p 1 - p 0 ) x (p 3 - p 0 ) = ? vertices from cube
17
17 Code for computing the cross product /* Compute (b - a) x (c - a). Store the result in d. After computing the cross product, normalize the length of d to 1. */ void cross(point3 a, point3 b, point3 c, point3 d){ //we will complete this code in class. normalize(d);//Set length to one. Not technically // part of cross product computation } a b c
18
18 Code for computing the cross product /* Compute (b - a) x (c - a). Store the result in d. After computing the cross product, normalize the length of d to 1. */ void cross(point3 a, point3 b, point3 c, point3 d){ d[0] = (b[1] - a[1])*(c[2] - a[2]) - (b[2] - a[2]) * (c[1] - a[1]); d[1] = (b[2] - a[2])*(c[0] - a[0]) - (b[0] - a[0]) * (c[2] - a[2]); d[2] = (b[0] - a[0])*(c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]); normalize(d);//Set length to one. Not technically // part of cross product computation } a b c
19
19 Problem with Flat Shading Flat shading models a distant viewer and a distant light source. If the polygon is flat, then n, l, and v are constant over the entire polygon. OpenGL uses the surface normal associated with the first vertex to specify the normals for the entire polygon. Problem: The changes in lighting look too abrupt. Sometimes see light and dark lines at edges (Mach bands), because of the way the visual system processes the intensity changes. l l l v v v Sharp Edge I x I x IntensityAppearance
20
20 Smooth Shading Smooth shading varies the color across the polygon, so that the intensity changes are not so abrupt. Models for Smooth shading: Interpolative Shading Gouraud Shading Phong Shading We will examine each of these in the next lecture!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.