chapter 6 shading - ritgs/spring06/570-761/notes/ch6.pdf · 2006. 5. 15. · 3. lighting models...

25
Chapter 6 Shading 1. Overview 2. Light interactions with matter Light sources Surface types 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection 4. Shading: polygonal shading Flat Phong Gouraud 5. Shading in OpenGL Normals Material properties Light sources 6. Example of sphere (taken from author’s website) _______________________________________________________________________

Upload: others

Post on 17-Oct-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Chapter 6 Shading 1. Overview 2. Light interactions with matter Light sources Surface types 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection 4. Shading: polygonal shading Flat Phong Gouraud 5. Shading in OpenGL Normals Material properties Light sources 6. Example of sphere (taken from author’s website) _______________________________________________________________________

Page 2: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

1. Overview

Why we need shading

• Suppose we build a model of a sphere using many polygons and color it with glColor. We get something like

• But we want

Summary

Color and shading provide depth cues in addition to being object attributes . Light can come from different types of sources and interact with objects having

different optical attributes Lighting models are based on reflection and shading effects that can be modeled

using vector operations When objects are modeled with polygonal faces vector methods make it easy to

compute shading and reflection effects based on the use of the normal to the plane containing the face.

Phong shading is an artificial approach to shading that is simple to implement but does not correspond to physical reality, whereas Gouraud shading is based on optics.

OpenGL supports different types of light sources + materials + Gouraud shading.

Page 3: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

2. Light interactions with matter

Why does the image of a real sphere look like ?

• Light-material interactions cause each point to have a different color or shade • Need to consider

Light sources Material properties Location of viewer Surface orientation

Basic principles

Light is a mixture of frequencies in the EM spectrum: R through V visible

The eye is differentially sensitive to wavelengths in the visible spectrum because red, green and blue cones behind the retina have spectral sensitivities respectively centered in the red-yellow region, yellow-green region, and green-blue region.

Page 4: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Objects in scene reflect incident light, photons strike eye. Cells respond to light quanta: quite sensitive, 1 photon is sufficient, 5 to 8 to see flash

The scene observed is the result of a range of interactions of light from different sources with all the objects in the field of view (and possibly some outside).

Shadow Translucent Light source surface M Multiple reflection

Global versus local

• Correct shading requires a global calculation involving all objects and light sources

Incompatible with pipeline model which shades each polygon independently (local rendering)

• However, in computer graphics, especially real time graphics, we are happy if things “look right”

Many techniques for approximating global effects

Page 5: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

For example:

• Light strikes A Some scattered Some absorbed

• Some of scattered light strikes B Some scattered Some absorbed

• Some of this scattered light strikes A and so on Rendering Equation

• The infinite scattering and absorption of light can be described by the rendering equation

Cannot be solved in general Ray tracing is a special case for perfectly reflecting surfaces

• Rendering equation is global and includes

Shadows Multiple scattering from object to object

Page 6: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Light-Material Interaction

• The amount reflected determines the color and brightness of the object A surface appears red under white light because the red component of the light is reflected and the rest is absorbed (see figure above)

• The reflected light is scattered in a manner that depends on the smoothness and orientation of the surface

Approximation to physical models of light striking retina

Light sources Ambient Point Diffuse Reflection Perfect Scattered Specular Shading Phong shading Gouraud shading Shadows

Page 7: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Simple Light sources

• Point source Model with position and color Distant source = infinite distance away (parallel)

• Spotlight

Restrict light from ideal point source • Ambient light

Same amount of light everywhere in scene Can model contribution of many sources and reflecting surfaces

Surface types

• The smoother a surface, the more reflected light is concentrated in the direction a perfect mirror would reflected the light

• A very rough surface scatters light in all directions Ideal Reflector

• Normal is determined by local orientation • Angle of incidence = angle of relection • The three vectors must be coplanar

Page 8: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

3. Lighting models Effects modeled:

Light sources (point, ambient, diffuse) Material properties Optical effects: Reflection (including Specular Reflection) / Scattering

Ideal reflector

Here the reflection can only be seen if the viewer is situated along the reflected ray.

• Normal is determined by local orientation • Angle of incidence = angle of relection • The three vectors must be coplanar

Relationship is given by

r = 2 (l · n ) n - l Lambertian Surface

• Perfectly diffuse reflector • Light scattered equally in all directions • Amount of light reflected is proportional to the vertical component of incoming

light reflected light ~ cos θi cos θi = l · n if vectors normalized There are also three coefficients, kr, kb, kg that show how much of each

color component is reflected Specular Surfaces

• Most surfaces are neither ideal diffusers nor perfectly specular (ideal reflectors) • Smooth surfaces show specular highlights due to incoming light being reflected in

directions concentrated close to the direction of a perfect reflection specular highlight

Page 9: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Phong model

• This is a simple lighting model that can be computed rapidly • Has three components

Diffuse Specular Ambient

• Uses four vectors To source To viewer Normal Perfect reflector

Specular reflection in Phong model

Uses term that drops off as angle between viewer and ideal reflection increases

Ir ~ ks I cosαφ

shininess coef

absorption coef incoming intensity

reflected intensity

Shininess coefficient = cosα φ

• Values of a between 100 and 200 correspond to metals • Values between 5 and 10 give surface that look like plastic

-90 φ 90

Page 10: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Ambient Light

• Ambient light is the result of multiple interactions between (large) light sources and the objects in the environment

• Amount and color depend on both the color of the light(s) and the material properties of the object

• Add ka Ia to diffuse and specular terms ka = reflection coef Ia = intensity of ambient light Distance Terms

• The light from a point source that reaches a surface is inversely proportional to the square of the distance between them

• Add a factor of the form 1/(ad + bd +cd2) to the diffuse and specular terms • The constant and linear terms soften the effect of the point source

Page 11: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Cumulative Phong model

Light sources

• In the Phong Model, we add the results from each light source • Each light source has separate diffuse, specular, and ambient terms to allow for

maximum flexibility even though this form does not have a physical justification • Separate red, green and blue components • Hence, 9 coefficients for each point source

Idr, Idg, Idb, Isr, Isg, Isb, Iar, Iag, Iab

Material Properties

• Material properties match light source properties

o Nine absorbtion coefficients

kdr, kdg, kdb, ksr, ksg, ksb, kar, kag, kab

o Shininess coefficient α

Adding up the Components For each light source and each color component, the Phong model can be written (without the distance terms) as

I =kd Id l · n + ks Is (v · r )α + ka Ia For each color component we add contributions from all sources

Page 12: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Example of use of Phong model

Differences arise from the parameters used.

Page 13: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

4. Shading: polygonal shading To shade a scene shade each polygon in each object by calculating the intensity based on a lighting model such as Phong

• Shading calculations are done for each vertex Vertex colors become vertex shades

Now you have a choice for computing the shade value for each interior (edge or polygon interior):

• By default, vertex shades are interpolated across the polygon glShadeModel(GL_SMOOTH);

• If we use glShadeModel(GL_FLAT); the color at the first vertex will determine

the shade of the whole polygon • Gouraud shading:

Use the average of the normals around a mesh vertex as the normal at the vertex (optically this is not correct but an easy approximation )

n = (n1+n2+n3+n4)/ |n1+n2+n3+n4

Page 14: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Gouraud versus Phong Shading

• Gouraud Shading

Find average normal at each vertex (vertex normals) Apply Phong model at each vertex Interpolate vertex shades across each polygon

• Phong shading

Find vertex normals Interpolate vertex normals across edges Interpolate edge normals across polygon Apply Phong model at each fragment

Page 15: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

5. Lighting and Shading in OpenGL What is shading?

Shading is the process of rendering fragments in a graphics model of a scene using a lighting model + a geometric model of objects in the world coordinate system

Geometric model is usually based on polygonal surfaces Real light sources

Finite area

Spectral characteristic

Emission dependent on direction Lighting in OpenGL

Point sources / Spotlights / Ambient sources Located at finite distance / infinity

Each source has diffuse / ambient / specular properties each with

different possible RGB specs Materials for each specified for each surface (back and front) in terms

of how much reflected, transmitted, emitted (because surface can also be a source)

Shading in OpenGL

Lighting model used is for shading one of the following types of polygonal geometry

Flat

Smooth

Gouraud

Page 16: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Steps in OpenGL shading Step 1 Enable lighting

glEnable(GL_LIGHTING);

All colors of objects in scene determined based on light sources

glColor*() not in effect

Enable each light source individually

glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); … Here LIGHT0 has defaults applied to it.

Step 2 Select light model

Specifies model for ambient, local viewer, two-sided surfaces glLightModeli(parameter, value) Here: param is an enumerated type { GL_LIGHT_MODEL_AMBIENT, GL_LIGHT_MODEL_LOCAL_VIEWER, GL_LIGHT_MODEL_TWO_SIDE } and value is boolean: TRUE or FALSE

GL_LIGHT_MODEL_LOCAL_VIEWER = do not use simplifying distant viewer assumption in calculation

GL_LIGHT_MODEL_TWO_SIDED = shades both sides of

polygons independently

Page 17: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Step 3 Specify all normals

In OpenGL the normal vector is part of the state Set normal by

glNormal3f(x, y, z); or glNormal3fv(p);

Specify normal in normalized form or let OpenGL do the normalization automatically:

glEnable(GL_NORMALIZE);

Example: Normal for Triangle n p2 Plane n ·(p - p0 ) = 0 n = (p2 - p0 ) ×(p1 - p0 ) p1 normalize n ← n/ |n| p0 Note that right-hand rule determines outward face.

Page 18: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Step 4 Specify all Light sources glLightv(<source n>, <property>, <value>); Here <source n> is GL_LIGHTn <property> is one of the following: GL_POSITION

GL_AMBIENT GL_DIFFUSE GL_SPECULAR <value> is 4-element array with R, G, B, 0 or 1 or x, y, z, 0 or 1 In the first case: 1 = opaque 0 = translucent In the second: 1 = source at finite distance 0 = source at infinite distance Different types of light sources can be specified in OpenGL:

point source

spotlight

ambient

global ambient

Page 19: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Point Light Source

For each light source, we can set an RGBA for the diffuse, specular, and ambient components, and for the position

GL float diffuse0[]={1.0, 0.0, 0.0, 1.0}; GL float ambient0[]={1.0, 0.0, 0.0, 1.0}; GL float specular0[]={1.0, 0.0, 0.0, 1.0}; Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0}; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightv(GL_LIGHT0, GL_POSITION, light0_pos); glLightv(GL_LIGHT0, GL_AMBIENT, ambient0); glLightv(GL_LIGHT0, GL_DIFFUSE, diffuse0); glLightv(GL_LIGHT0, GL_SPECULAR, specular0);

Global Ambient Light

Ambient light depends on color of light sources A red light in a white room will cause a red ambient term that

disappears when the light is turned off

OpenGL allows a global ambient term

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);

Page 20: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Step 4 Material Properties

Material properties are also part of the OpenGL state and match the terms in the modified Phong model

Set by glMaterialv()

GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0}; GLfloat diffuse[] = {1.0, 0.8, 0.0, 1.0}; GLfloat specular[] = {1.0, 1.0, 1.0, 1.0}; GLfloat shine = 100.0 glMaterialf(GL_FRONT, GL_AMBIENT, ambient); glMaterialf(GL_FRONT, GL_DIFFUSE, diffuse); glMaterialf(GL_FRONT, GL_SPECULAR, specular); glMaterialf(GL_FRONT, GL_SHININESS, shine);

Step 5 Select shade model: Flat or Gouraud

Set to GL_FLAT or GL_SMOOTH (the latter corresponds to

Gouraud) glShadeModel(GL_FLAT); Default is smooth.

Page 21: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

Other considerations Front and Back Faces

• The default is shade only front faces which works correctly for convex objects

• If we set two sided lighting, OpenGL will shade both sides of a surface

• Each side can have its own properties which are set by using GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK in glMaterialf

back faces not visible back faces visible

Guidelines: See

http://www.sjbaker.org/steve/omniv/opengl_lighting.html

Page 22: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

6. Example of sphere (taken from author’s website) /* Example 11: eleven-angel.c This program is taken from the book (also on the author's website at www.cs.unm.edu/~angel/BOOK/INTERACTIVE_COMPUTER_GRAPHICS/FOURTH_EDITION/PROGRAMS ). Nothing has been changed. */ /* Recursive subdivision of tetrahedron (Chapter 6). Three display modes: wire frame, constant, and interpolative shading. */ /* program also illustrates defining materials and light sources in init() */ /* mode 0 = wire frame, mode 1 = constant shading, mode 3 = interpolative shading */ #include <stdlib.h> #include <GL/glut.h> typedef float point[4]; /* initial tetrahedron */ float v[][3]={{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.33333}, {-0.816497, -0.471405, -0.333333}, {0.816497, -0.471405, -0.333333}}; static GLfloat theta[] = {0.0,0.0,0.0}; int n; int mode; void triangle( float *a, float *b, float *c) /* display one triangle using a line loop for wire frame, a single normal for constant shading, or three normals for interpolative shading */ { if (mode==0) glBegin(GL_LINE_LOOP); else glBegin(GL_POLYGON); if(mode==1) glNormal3fv(a); if(mode==2) glNormal3fv(a); glVertex3fv(a); if(mode==2) glNormal3fv(b); glVertex3fv(b); if(mode==2) glNormal3fv(c); glVertex3fv(c); glEnd(); } void normal(float *p) {

Page 23: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

/* normalize a vector */ double sqrt(); float d =0.0; int i; for(i=0; i<3; i++) d+=p[i]*p[i]; d=sqrt(d); if(d>0.0) for(i=0; i<3; i++) p[i]/=d; } void divide_triangle(float *a, float *b, float *c, int m) { /* Triangle subdivision using vertex numbers. Right-hand rule applied to create outward-pointing faces. */ float v1[3], v2[3], v3[3]; int j; if(m>0) { for(j=0; j<3; j++) v1[j]=a[j]+b[j]; normal(v1); for(j=0; j<3; j++) v2[j]=a[j]+c[j]; normal(v2); for(j=0; j<3; j++) v3[j]=b[j]+c[j]; normal(v3); divide_triangle(a, v1, v2, m-1); divide_triangle(c, v2, v3, m-1); divide_triangle(b, v3, v1, m-1); divide_triangle(v1, v3, v2, m-1); } else triangle(a,b,c); /* draw triangle at end of recursion */ } void tetrahedron( int m) { /* apply triangle subdivision to faces of tetrahedron */ divide_triangle(v[0], v[1], v[2], m); divide_triangle(v[3], v[2], v[1], m); divide_triangle(v[0], v[3], v[1], m); divide_triangle(v[0], v[2], v[3], m); } void display() { /* displays all three modes, side by side */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); mode=0; tetrahedron(n); mode=1; glTranslatef(-2.0, 0.0,0.0); tetrahedron(n);

Page 24: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

mode=2; glTranslatef( 4.0, 0.0,0.0); tetrahedron(n); glFlush(); } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho(-4.0, 4.0, -4.0 * (GLfloat) h / (GLfloat) w, 4.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0); else glOrtho(-4.0 * (GLfloat) w / (GLfloat) h, 4.0 * (GLfloat) w / (GLfloat) h, -4.0, 4.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); display(); } void myinit() { GLfloat mat_specular[]={1.0, 1.0, 1.0, 1.0}; GLfloat mat_diffuse[]={1.0, 1.0, 1.0, 1.0}; GLfloat mat_ambient[]={1.0, 1.0, 1.0, 1.0}; GLfloat mat_shininess={100.0}; GLfloat light_ambient[]={0.0, 0.0, 0.0, 1.0}; GLfloat light_diffuse[]={1.0, 1.0, 1.0, 1.0}; GLfloat light_specular[]={1.0, 1.0, 1.0, 1.0}; /* set up ambient, diffuse, and specular components for light 0 */ glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); /* define material properties for front face of all polygons */ glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialf(GL_FRONT, GL_SHININESS, mat_shininess); glShadeModel(GL_SMOOTH); /* enable smooth shading */ glEnable(GL_LIGHTING); /* enable lighting */ glEnable(GL_LIGHT0); /* enable light 0 */ glEnable(GL_DEPTH_TEST); /* enable z buffer */ glClearColor (1.0, 1.0, 1.0, 1.0); glColor3f (0.0, 0.0, 0.0); } int main(int argc, char **argv) {

Page 25: Chapter 6 Shading - RITgs/spring06/570-761/Notes/Ch6.pdf · 2006. 5. 15. · 3. Lighting models Reflection and shading Phong model: Phong reflection + ambient light + diffuse reflection

n=atoi(argv[1]); glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("sphere"); myinit(); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutMainLoop(); }

Shading type Constant Wireframe Interpolative No of subdivisions of triangle n = 3 n = 5 n = 8