Post on 02-Jan-2016

224 views

Category:

## Documents

Embed Size (px)

TRANSCRIPT

*

Lecture 23-24 light and shading II

• *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

Lecture 23-24 light and shading II

• *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

Lecture 23-24 light and shading II

• *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

Lecture 23-24 light and shading II

• *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

Lecture 23-24 light and shading II

• *Determine the colors of a pixel covered by a polygon

More general, if a polygon has k vertices

Lecture 23-24 light and shading II

Lecture 23-24 light and shading II

• *

Lecture 23-24 light and shading II

• *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

Lecture 23-24 light and shading II

• *2. Lighting in OpenGL

Enable/Disable lighting glEnable( GL_LIGHTING );glDisable( GL_LIGHTING );

To specify a normal for each vertex. glNormal3f( x, y, z); glVertex3f( x, y, z);

Lecture 23-24 light and shading II

• *A normal remains in effect until another normal is specified. //Draw a cylinderglBegin( GL_QUAD_STRIP); t = 0.; dt = (360. / nslice) * 3.1416 / 180.; for (j = 0; j
• *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);

Lecture 23-24 light and shading II

• *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);

Lecture 23-24 light and shading II

• *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);

Lecture 23-24 light and shading II

• *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);

Lecture 23-24 light and shading II

• *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)

Lecture 23-24 light and shading II

• *A comparison of directional source and point source on shiny white objects. Viewing position is at (0, 0, 8)

Lecture 23-24 light and shading II

• *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.

Lecture 23-24 light and shading II

• *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

Lecture 23-24 light and shading II

• *

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

Lecture 23-24 light and shading II

• *You can also set the GL_SPOT_EXPONENTThe 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.);

Lecture 23-24 light and shading II

• *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)

Lecture 23-24 light and shading II

• *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.

Lecture 23-24 light and shading II

• *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.

Lecture 23-24 light and shading II

• Lecture 23-24 light and shading II *OverallPure DiffusePure AmbientPure Specular

Lecture 23-24 light and shading II

• *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)

Lecture 23-24 light and shading II

• *EmissionTo 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);

Lecture 23-24 light and shading II

• *

Recommended