color

color color

caig.cs.nctu.edu.tw
from caig.cs.nctu.edu.tw More from this publisher

Lighting and Shading<br />

Steve Lin<br />

Department of Computer Science &<br />

Institute of Multimedia Engineering<br />

I-Chen Lin’CG Slides, Doug James’CG Slides,<br />

Angel, Interactive Computer Graphics, Chap 6<br />

Shirley, Fundamentals of Computer Graphics, Chap 9


Illumination and Shading<br />

•Is it a ball or a plate<br />

•What <strong>color</strong> should I set for each pixel<br />

DCP4516 Introduction to Computer Graphics 2


Why Do We Need Shading<br />

•Suppose we <strong>color</strong> a sphere model. We get<br />

something like<br />

•But we want<br />

DCP4516 Introduction to Computer Graphics 3


Shading<br />

•Why does the image of a real sphere look like<br />

•Light-material interactions cause each point to<br />

have a different <strong>color</strong> or shade<br />

•Need to consider<br />

–Light sources<br />

–Material properties<br />

–Location of the viewer<br />

–Surface orientation<br />

DCP4516 Introduction to Computer Graphics 4


Factors that affect the“<strong>color</strong>”of a pixel<br />

•Light sources<br />

–Emittance spectrum (<strong>color</strong>)<br />

–Geometry (position and direction)<br />

–Directional attenuation<br />

•Objects’surface properties<br />

–Reflectance spectrum (<strong>color</strong>)<br />

–Geometry (position, orientation, and micro-<br />

structure)<br />

–Absorption<br />

DCP4516 Introduction to Computer Graphics 5


Light and Matter<br />

•Light strikes A<br />

–Some reflected<br />

–Some absorbed<br />

•Some of reflected light strikes B<br />

–Some reflected<br />

–Some absorbed<br />

•Some of reflected light reflected back to A and B,<br />

and so on<br />

•Rendering equation describes this recursive<br />

process<br />

DCP4516 Introduction to Computer Graphics 6


Rendering Equation<br />

•The infinite reflection and absorption of light can be<br />

described by the rendering equation<br />

–[outgoing]-[incoming] = [emitted]-[absorbed]<br />

–[outgoing] = [emitted] + [reflected] (+[transmitted])<br />

–Cannot be solved in general<br />

–Ray tracing is a special case for perfectly reflecting surfaces<br />

•Rendering equation is global and includes<br />

–Shadows<br />

–Multiple reflection from object to object<br />

DCP4516 Introduction to Computer Graphics 7


Global Effects<br />

shadow<br />

transmission<br />

multiple reflection<br />

translucent surface<br />

DCP4516 Introduction to Computer Graphics 8


Local vs Global Illumination<br />

•Correct shading requires a global calculation<br />

–Incompatible with a pipeline model which shades<br />

each polygon independently (local rendering)<br />

•However, in computer graphics, especially<br />

real time graphics, we are happy if things<br />

“look right”<br />

–many techniques for approximating global effects<br />

exist<br />

DCP4516 Introduction to Computer Graphics 9


Local Illumination<br />

•Adequate for real-time graphics.<br />

•No inter-reflection, no refraction, no realistic<br />

shadow ….<br />

DCP4516 Introduction to Computer Graphics 10


Light Sources<br />

•General light sources are difficult to simulated<br />

–because we must integrate light coming from all points<br />

on the source<br />

DCP4516 Introduction to Computer Graphics 11


Simple Light Sources<br />

•Point source<br />

–Model with position and <strong>color</strong><br />

–Distant source = infinite distance away (parallel)<br />

•Spotlight<br />

–Restrict light from ideal point source<br />

•Ambient light<br />

–Same amount of light everywhere in scene<br />

–Can model contribution of many sources and reflecting<br />

surfaces<br />

DCP4516 Introduction to Computer Graphics 12


Surface Types<br />

•The smoother a surface, the more reflected<br />

light is concentrated in the direction<br />

•A very rough surface scatters light in all<br />

directions<br />

smooth surface<br />

rough surface<br />

DCP4516 Introduction to Computer Graphics 13


Phong Reflection (Lighting) Model<br />

•A simple model that can be computed rapidly<br />

•Has three components<br />

–Ambient<br />

–Diffuse<br />

–Specular<br />

•Uses four vectors<br />

–To source l<br />

–To viewer v<br />

–Normal n<br />

–Perfect reflector r<br />

r = 2 (l · n ) n- l<br />

DCP4516 Introduction to Computer Graphics 14


Ambient Light<br />

•The result of multiple interactions between<br />

(large) light sources and the objects in the<br />

environment.<br />

•I a = k a L a<br />

ambient light intensity<br />

DCP4516 Introduction to Computer Graphics 15


Diffuse Reflection<br />

•Light scatters equally in all directions<br />

•Reflected intensities vary with the direction<br />

of the light.<br />

•Lambertian Surface<br />

–Perfect diffuse reflector<br />

•I d = k d (l·n) L d<br />

DCP4516 Introduction to Computer Graphics 16


Specular Surfaces<br />

•Most surfaces are neither perfectly diffuse nor<br />

perfectly specular (ideal reflectors)<br />

•Incoming light reflects in concentrated directions<br />

close to the direction of a perfect reflection<br />

specular<br />

highlight<br />

DCP4516 Introduction to Computer Graphics 17


Modeling Specular Relections<br />

•Phong model<br />

I s = k s L s (r·v) α<br />

= k s L s cos α Φ<br />

shininess coefficient<br />

reflected<br />

intensity incoming intensity<br />

absorption coefficient<br />

<br />

DCP4516 Introduction to Computer Graphics 18


The Shininess Coefficient α<br />

•Values of αbetween 100 and 200 correspond to<br />

metals<br />

•Values between 5 and 10 give surfaces that look<br />

like plastic<br />

DCP4516 Introduction to Computer Graphics 19


Distance Terms<br />

•Account for attenuation<br />

•Original: Inversely proportional to the square of<br />

the distance d between light source and surface<br />

•Modified: add a factor of the form 1/(a + bd +cd 2 )<br />

to the diffuse and specular terms<br />

•The constant and linear terms soften the effect<br />

of the point source<br />

DCP4516 Introduction to Computer Graphics 20


Adding up the Components<br />

•A primitive virtual world with lighting can be<br />

shaded by combining the three light<br />

components:<br />

•I = I ambient<br />

+ I diffuse<br />

+ I specular<br />

= k a<br />

L a<br />

+ k d<br />

(l · n) L d<br />

+ k s<br />

L s<br />

(v · r ) α<br />

diffuse<br />

diffuse<br />

+<br />

specular<br />

diffuse<br />

+<br />

ambient<br />

DCP4516 Introduction to Computer Graphics 21


Coefficients<br />

•3 <strong>color</strong> channels and 3 surface types<br />

•9 coefficients for each point light source<br />

–L ra<br />

, L rd<br />

, L rs<br />

, L ga<br />

, L gd<br />

, L gs<br />

, L ba<br />

, L bd<br />

, L bs<br />

•Material properties<br />

–9 absorption coefficients<br />

•k ra , k rd , k rs , k ga , k gd , k gs , k ba , k bd , k bs<br />

–Shininess coefficient α<br />

DCP4516 Introduction to Computer Graphics 22


Modified Phong Model<br />

•Problem: In the specular component of<br />

Phong model, it requires the calculation of a<br />

new reflection vector and view vector for<br />

each vertex<br />

r = 2 (l · n ) n - l<br />

•Blinn suggested an approximation using the<br />

halfway vector that is more efficient<br />

DCP4516 Introduction to Computer Graphics 23


The Halfway Vector<br />

•h is the normalized vector halfway between<br />

l and v<br />

h = ( l + v )/ | l + v |<br />

DCP4516 Introduction to Computer Graphics 24


Using the Halfway Angle<br />

•Replace (v · r ) α by (n · h ) b<br />

•b is chosen to match shininess<br />

•Note that halfway angle is half of angle<br />

between r and v if vectors are coplanar<br />

l<br />

n h<br />

φ Θ-φ<br />

θ θ Φ<br />

r<br />

v<br />

<br />

<br />

<br />

2<br />

DCP4516 Introduction to Computer Graphics 25


Blinn lighting model<br />

•Resulting model is known as the modified<br />

Phong or Blinn lighting model<br />

•Specified in OpenGL standard and most<br />

real-time applications<br />

DCP4516 Introduction to Computer Graphics 26


Example<br />

Teapots with<br />

different<br />

parameters.<br />

DCP4516 Introduction to Computer Graphics 27


Computation of Vectors<br />

•l and v : specified by the application<br />

•r: computed from l and n<br />

•Determine n<br />

–OpenGL leaves determination of normal to<br />

application; exception for GLU quadrics and Bezier<br />

surfaces<br />

DCP4516 Introduction to Computer Graphics 28


Plane Normals<br />

•Equation of plane: ax+by+cz+d = 0<br />

•Normal can be obtained by<br />

n = (p 2 -p 0 ) × (p 1 -p 0 )<br />

n<br />

DCP4516 Introduction to Computer Graphics 29


Normal to Sphere<br />

•Implicit function f(x,y.z)=0<br />

•Normal given by gradient<br />

•Sphere<br />

–n = [∂f/∂x, ∂f/∂y, ∂f/∂z] T<br />

DCP4516 Introduction to Computer Graphics 30


Parametric Form<br />

•For sphere<br />

x = x(u,v) = cos u sin v<br />

y = y(u,v) = cos u cos v<br />

z = z(u,v) = sin u<br />

•Tangent plane determined by vectors<br />

∂p/∂u = [∂x/∂u, ∂y/∂u, ∂z/∂u] T<br />

∂p/∂v = [∂x/∂v, ∂y/∂v, ∂z/∂v] T<br />

•Normal given by cross product<br />

n = ∂p/∂u × ∂p/∂v<br />

DCP4516 Introduction to Computer Graphics 31


What’s next<br />

•So far, we know how to compute the <strong>color</strong> at<br />

a point given lighting model, illumination<br />

model, and surface normals<br />

•The required computation can be huge if we<br />

compute for every point on a surface<br />

•Exploit efficiency by decomposing curved<br />

surfaces into many small flat polygons<br />

DCP4516 Introduction to Computer Graphics 32


Polygonal Shading<br />

•Curved surfaces are approximated by polygons<br />

•How do we shade<br />

–Flat shading<br />

–Interpolative shading<br />

–Gouraud shading<br />

–Phong shading (not Phong reflection model)<br />

•Two questions:<br />

–How do we determine normals at vertices<br />

–How do we calculate shading at interior points<br />

DCP4516 Introduction to Computer Graphics 33


Flat Shading:<br />

•Normal: given explicitly before vertex<br />

glShadeModel(GL_SMOOTH);<br />

glNormal3f(nx, ny, nz);<br />

glVertex3f(x, y, z);<br />

•Shading constant across polygon<br />

•Single polygon: first vertex<br />

•Triangle strip: vertex n+2 for triangle n<br />

DCP4516 Introduction to Computer Graphics 34


Flat Shading Assessment<br />

•Inexpensive to compute<br />

•Appropriate for objects with flat faces<br />

•Less pleasant for smooth surfaces<br />

DCP4516 Introduction to Computer Graphics 35


Flat Shading and Perception<br />

•Lateral inhibition: exaggerates perceived<br />

intensity<br />

•Mach bands: perceived “stripes”along edges<br />

DCP4516 Introduction to Computer Graphics 36


DCP4516 Introduction to Computer Graphics 37


Interpolative Shading<br />

•Enable with glShadeModel(GL_SMOOTH);<br />

•Calculate <strong>color</strong> at each vertex<br />

•Interpolate <strong>color</strong> in interior<br />

•Compute during scan conversion<br />

(rasterization)<br />

•More expensive to calculate<br />

DCP4516 Introduction to Computer Graphics 38


Gouraud Shading<br />

•Special case of interpolative shading<br />

•How do we calculate vertex normals<br />

•Gouraud: average all adjacent face normals<br />

•Requires knowledge<br />

about which faces share<br />

a vertex—adjacency info<br />

DCP4516 Introduction to Computer Graphics 39


Gouraud Shading Algorithm<br />

•Find average normal at each vertex<br />

•Apply Phong reflection model at each vertex<br />

•Linearly interpolate vertex shades (<strong>color</strong>s)<br />

across each polygon I 1 (x 1 , y 1 )<br />

I<br />

I<br />

I<br />

a<br />

b<br />

s<br />

<br />

y<br />

<br />

y<br />

<br />

x<br />

1<br />

1<br />

b<br />

1<br />

y<br />

1<br />

y<br />

1<br />

x<br />

2<br />

4<br />

a<br />

I<br />

y<br />

y<br />

<br />

I y<br />

y<br />

<br />

1<br />

I<br />

y<br />

y<br />

<br />

I y<br />

y<br />

<br />

1<br />

I<br />

x<br />

x<br />

<br />

I x<br />

x<br />

<br />

a<br />

DCP4516 Introduction to Computer Graphics 40<br />

s<br />

s<br />

b<br />

2<br />

4<br />

s<br />

2<br />

4<br />

b<br />

1<br />

1<br />

s<br />

s<br />

s<br />

a<br />

I a (x a , y s )<br />

I 2 (x 2 , y 2 )<br />

I s (x s , y s )<br />

I 3 (x 3 , y 3 )<br />

I b (x b , y s )<br />

I 4 (x 4 , y 4 )


Phong Shading<br />

•Interpolate normals rather than <strong>color</strong>s<br />

•Significantly more expensive<br />

•Mostly done off-line (not supported in<br />

OpenGL)<br />

DCP4516 Introduction to Computer Graphics 42


Phong Shading Algorithm<br />

•Find vertex normals<br />

•Interpolate vertex normals across edges<br />

•Find shades along edges<br />

•Interpolate edge shades across polygons<br />

DCP4516 Introduction to Computer Graphics 43


Polygonal Shading Summary<br />

•Gouraud shading<br />

–Set vertex normals<br />

–Calculate <strong>color</strong>s at vertices<br />

–Interpolate <strong>color</strong>s across polygon<br />

•Must calculate vertex normals!<br />

•Must normalize vertex normals to unit length!<br />

DCP4516 Introduction to Computer Graphics 44

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!