Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 14 Shading models 1.Shading Constant Shading (to be implemented) Gouraud Shading Phong Shading 2.Light and shading with OpenGL 1.

Similar presentations


Presentation on theme: "Lecture 14 Shading models 1.Shading Constant Shading (to be implemented) Gouraud Shading Phong Shading 2.Light and shading with OpenGL 1."— Presentation transcript:

1 Lecture 14 Shading models 1.Shading Constant Shading (to be implemented) Gouraud Shading Phong Shading 2.Light and shading with OpenGL 1

2 2 Constant (Flat) Shading (polygon based) The shade of a surface is the amount of ambient, diffuse and specular light reflected from the surface In constant shading, the reflection calculation only carried out once for a point on a polygon, e.g., the first vertex. The resulting shade is assigned to all pixels covered by the polygon. The surface normal is used in calculation The entire polygon has the same shade Efficient but often too coarse n

3 3  A Mach band is a stripe found along the edge of two polygons of different shades. Mach bands often occur in constant shading. Our eyes are particularly sensitive to the discrete change of shade

4 4 Gouraud shading (vertex based) Shading calculation is carried out for each vertex of a polygon. Each vertex is assigned a normal that may be the normal of the polygon, or the average of the normals of adjacent polygons. The shade of the pixel at a vertex is calculated using the normal assigned to the vertex n4n4 n3n3 n2n2 n1n1 n0n0 nbnb ncnc nana ndnd n

5 5 The shades of other pixel covered by the a polygon is obtained though interpolation Example: The color of an in-between point, C, is determined by ABC

6 6 Determine the colors of a pixel covered by a polygon More general, if a polygon has k vertices B A C F

7 7 Constant Shading Gouraud Shading

8 8

9 9 Phong Shading (pixel based)  Phong shading interpolates the normal across a polygon. Then use the normal of a pixel to calculate the shade of the pixel  Using Gouraud shading, we often need to cut a surface into very small pieces in order to obtain satisfactory result. Phong shading reduces the excessive cutting but requires more calculation.  It may produce quality pictures but much slower B A C F

10 10 2. Lighting in OpenGL Enable/Disable lighting glEnable( GL_LIGHTING ); glDisable( GL_LIGHTING ); Specify a shading model Either flat shading (Constant) or smooth (Gouraud) shading glShadeModel( GL_FLAT ); glShadeModel( GL_SMOOTH ); To specify a normal for each vertex. glNormal3f( x, y, z); glVertex3f( x, y, z);

11 11 A normal remains in effect until another normal is specified. //Draw a cylinder glBegin( GL_QUAD_STRIP); t = 0.; dt = (360. / nslice) * 3.1416 / 180.; for (j = 0; j <= nslice; ++j) { glNormal3f( cos( t), 0., sin( t)); glVertex3f( cos( t), 0., sin( t)); glVertex3f( cos( t), 2., sin( t)); t = t + dt; } glEnd(); z y x

12 12 To avoid the troublesome of providing unit vectors for normals, you may ask the system to normalize the vectors for you by adding the following in init() glEnable( GL_NORMALIZE); Specify the color components (r, g, b, a) of a light source GLfloat ambient0[] = { 0.1, 0.1, 0.1, 1.};//Grey GLfloat diffuse0[] = { 1., 1., 1., 1.}; //White GLfloat specular0[] = { 1., 1., 1., 1.};//White glLightfv( GL_LIGHT0, GL_AMBIENT, ambient0); glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse0); glLightfv( GL_LIGHT0, GL_SPECULAR, specular0);

13 13 In real life, the diffuse and specular components of a light source are often similar. The ambient component depends on the environment. If the environment consists of many red objects, then the ambient component tends to be slightly red. GLfloat a[] = { 0.1, 0., 0., 1.};//Very pale red GLfloat d[] = { 1., 1., 0., 1.}; //Yellow GLfloat s[] = { 1., 1., 0., 1.};//Yellow glLightfv( GL_LIGHT0, GL_AMBIENT, a); glLightfv( GL_LIGHT0, GL_DIFFUSE, d); glLightfv( GL_LIGHT0, GL_SPECULAR, s);

14 14 Opengl has defined a dark grey (.2,.2,.2) global ambient source. To change these to (r, g, b), call float globalAmbient[] = { r, g, b, 1.}; glLightModelfv( GL_LIGHT_MODEL_AMBIENT, globalAmbient ); To specify the position of a point light source GLfloat light_position[] = { 2., 4., 6., 1.}; glLightfv( GL_LIGHT0, GL_POSITION, light_position); To specify the direction of a directional light source GLfloat light_position[] = { 1., 1., 1., 0.}; glLightfv( GL_LIGHT1, GL_POSITION, light_position);

15 15 There are up to 8 light sources GL_LIGHT0, GL_LIGHT1, …, GL_LIGHT7 To turn on/off a particular light source (right before/after the drawing) glEnable( GL_LIGHT0); glDisable( GL_LIGHT0); In the default mode, OpenGL assumes that the viewer is at infinite distance such that the viewing angle is a constant. This approximation may cause serious error when the viewer is actually close to an object. To ask OpenGL to use the true viewing angle in the calculation of specular reflection. glLightModeli( GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);

16 16 The effect of mesh sizes (Important) In the following, a shinny white wall cutting into various sizes is rendered with a directional white light. GLfloat ambient0[] = { 0., 0., 0., 1.}; GLfloat diffuse0[] = {.7,.7,.7, 1.}; GLfloat specular0[] = { 1., 1., 1., 1.}; position[] = { 0., 0., 1., 0.}; //Light direction //Viewing position (0, 0, 8) 20 × 20 10 × 104 × 42 × 21 × 1

17 17  A comparison of directional source and point source on shiny white objects. Viewing position is at (0, 0, 8) Point source at (0,0,8) Dir. source at (0,0,1)

18 18 For point light source, we may specify the constants in the formula of the attenuation factor glLightf( GL_LIGHT1, GL_CONSTANT_ATTENUATION,.0); glLightf( GL_LIGHT1, GL_LINEAR_ATTENUATION,.5); glLightf( GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.); The default is a = 1, b = 0, c = 0 (no attenuation) For higher attenuation, set larger values for b and c, e.g., 1, 1.5, 2 or 3.

19 19 To define a spot light (a point source) The color components, position and attenuation constants are specified in the same way as an ordinary point source. In addition, we can specify the spot light direction and the cut-off angle

20 20 504030 20 GLfloat light_direction[] = {0, 0, -1}; //From the light glLightfv( GL_LIGHT2, GL_SPOT_DIRECTION, light_direction); glLightf( GL_LIGHT2, GL_SPOT_CUTOFF, 20.);  The effects of cutoff angle on a white wall

21 21 e = 0e = 10 e = 100 You can also set the GL_SPOT_EXPONENT The default is 0 that implies uniform distribution of light intensity from the center to the rim A higher spot exponent results in a higher intensity around the center. glLightf( GL_LIGHT2, GL_SPOT_EXPONENT, 10.);

22 22 To specify the reflectances of a shiny red material GLfloat diffuse[] = {.41,.135,.067, 1.}; GLfloat specular[] = {1., 1., 1., 1.}; glMaterialfv( GL_FRONT, GL_AMBIENT, diffuse); glMaterialfv( GL_FRONT, GL_DIFFUSE, diffuse); glMaterialfv( GL_FRONT, GL_SPECULAR, specular); glMaterialf( GL_FRONT, GL_SHININESS, 125.); The direction of the white light source is (1,1,1)

23 23 Reflectance of a material The red, green and blue components of the specular reflectance are often the same so that the original color of a light source is reflected. (In the preceding picture of the teapot, the highlight is white, the same as the light source.) For highly shiny material, the specular reflectance is close to 1 and the shininess can be as high as 100 or more. The specular reflectance and shininess of a dull surface are close to 0. A material property, eg, reflectance, remains in effect until a new value is specified.

24 24 The color of a material mainly depends on the diffuse reflectance. The diffuse reflection alone provides most information about the curvature and the depth of an object The ambient reflectance of a material is often the same as the diffuse reflectance The highlights produced in specular reflections are the blurred images of the light sources.

25 Lecture 23-24 light and shading II 25 Overall Pure DiffusePure Ambient Pure Specular

26 26 The pale yellowish pink material GLfloat ambient[] = {.6,.59,.42, 1.}; GLfloat diffuse[] = {.5,.5,.35, 1.}; GLfloat specular[] = {.1,.1,.07, 1.}; glMaterialfv( GL_FRONT, GL_AMBIENT, ambient); glMaterialfv( GL_FRONT, GL_DIFFUSE, diffuse); glMaterialfv( GL_FRONT, GL_SPECULAR, specular); glMaterialf( GL_FRONT, GL_SHININESS, 1.); The light source is a directional white light coming from (1, 1, 1)

27 27 Emission To make a material appear to be glowing, eg, the moon in the picture next page. GLfloat emission[] = { r, g, b, a}; glMaterialfv( GL_FRONT, GL_EMISSION, emission); Turn off the emission: assign 0 to the r-, g-, b-components. Note that an emission object is NOT a light source. Emission is just a property of material. GLfloat emission[] = { 1., 1.,.75, 1.}; glMaterialfv( GL_FRONT, GL_EMISSION, emission); draw_moon(); GLfloat no_em[] = { 0., 0., 0., 1.}; glMaterialfv( GL_FRONT, GL_EMISSION, no_em);

28 28 A pictures rendered with 5 kinds of material, two textures, 16 spot lights. The moon is an emission object.

29 29 Transparent Objects 1. Define the blending function and turn on blending glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable( GL_BLEND); 2. Define the material with the opacity less than 1. 3. Draw the background objects before drawing the transparent objects. Draw both front and back faces as filled polygons. void transMaterial1() { GLfloat a[] = {.7, 1.,.85,.3}; GLfloat s[] = { 1., 1., 1., 1.}; glMaterialfv( GL_FRONT, GL_AMBIENT, a); glMaterialfv( GL_FRONT, GL_DIFFUSE, a); glMaterialfv( GL_FRONT, GL_SPECULAR, s); glMaterialf( GL_FRONT, GL_SHININESS, 100.); }


Download ppt "Lecture 14 Shading models 1.Shading Constant Shading (to be implemented) Gouraud Shading Phong Shading 2.Light and shading with OpenGL 1."

Similar presentations


Ads by Google