illumination & reflectance dr. amy zhang. outline 2 illumination and reflectance the phong...

60
Illumination & Reflectance Dr. Amy Zhang

Upload: dominic-dawson

Post on 27-Dec-2015

234 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Illumination & Reflectance

Dr. Amy Zhang

Page 2: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Outline

2

Illumination and Reflectance The Phong Reflectance Model Shading in OpenGL

Page 3: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Two Components of Illumination Light sources with:

Emittance spectrum (color) Geometry (position and direction) Directional attenuation (falloff)

Surface properties with: Reflectance spectrum (color) Geometry (position, orientation, and micro-

structure) Absorption

3

Page 4: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Computer Graphics Jargon

4

Illumination: the transport of energy from light sources between points via direct and indirect paths

Lighting: the process of computing the light intensity reflected from a specific 3‐D point

Shading: the process of assigning a color to a pixel based on the illumination in the scene

Page 5: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Direct and Global Illumination

5

Direct illumination: A surface point receives light directly from all light sources in the scene Computed by the local illumination model Determine which light sources are visible

Global illumination: A surface point receives light after the light rays interact with other objects in the scene

I = Idirect + Ireflected + Itransmitted

Page 6: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Directional Light Sources

6

All of the rays from a directional light source have a common direction (parallel)

The direction is a constant at every point in the scene

It is as if the light source was infinitely far away from the surface that it is illuminating

Page 7: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Point Light Sources

7

The rays emitted from a point light radially diverge from the source

Direction to the light changes at each point

Page 8: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Other Light Sources

8

Spotlights

Area light sources Light source occupies a 2D

area (polygon) Generates soft shadows.

Page 9: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Linearity of Light

9

= +

+

Paul Haeberli, Grafica Obscura

Page 10: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

10

Page 11: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Outline

11

Illumination and Reflectance The Phong Reflectance Model Shading in OpenGL

Page 12: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

OpenGL Reflectance Model A simple model that can be computed

rapidly Has three components

Diffuse Specular Ambient

Uses four vectors To source To viewer Normal Perfect reflector

Page 13: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Ideal Diffuse Reflectance

13

Surface reflects light equally in all directions • Why? Examples?

Page 14: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Lambert’s Cosine Law

14

Diffuse reflectance scales with cosine of angle

Page 15: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Ideal Diffuse Reflectance

15

Lambertian reflection model IL: The incoming light intensity kd: The diffuse reflection coefficient N: Surface normal

cos i = N · L if vectors normalized There are also three coefficients, kdr, kdg, kdb that

show how much of each color component is reflected

Page 16: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Ideal Specular Reflectance

16

Normal is determined by local orientation Angle of incidence = angle of reflection The three vectors must be coplanar Ideal Specular Reflectance

Surface reflects light only at mirror angle

Page 17: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Reflection Vector R

17

The vector R can be computed from the incident ray direction L and the surface normal N

Note that all vectors have unit length

Page 18: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

18

How much light is seen? Depends on: Angle of incident light Angle to the viewer

ks is the absorption coef

Page 19: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Non-ideal Reflectors

19

Real materials tend to deviate significantly from ideal mirror reflectors

Introduce an empirical model that is consistent with our experience

The amount of reflected light is greatest in the direction of the perfect mirror reflection

The reflected light forms a “beam” pattern around this mirror direction

Page 20: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Phong Specular Reflection

20

Phong proposed using a term that dropped off as the angle between the viewer and the ideal reflection increased.

n is the shininess coefficient The cosine lobe gets more narrow with

increasing n. Values of between 100 and 200 correspond to metals Values between 5 and 10 give surface that look like

plastic

Page 21: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Blinn & Torrance Variation

21

The specular term in the Phong model is problematic because it requires the calculation of R and V for each vertex

Blinn suggested an more efficient approximation using the halfway vector halfway vector H between L and V

H is the normal to the (imaginary) surface that maximally reflects light in the V direction

Page 22: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

22

No need to compute reflection vector R at every point

Is is a function only of N, if: the viewer is very far away and V does not change

for all points on the object (e.g., orthographic projection)

L does not change for all points on the object (e.g., directional lights)

Resulting model is known as the modified Phong or Blinn lighting model Specified in OpenGL standard

Page 23: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Ambient Light Ambient light is the result of multiple

interactions between (large) light sources and the objects in the environment It represents the reflection of all indirect

illumination 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 termsreflection coef intensity of ambient light

Page 24: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Distance Terms The light from a point source that reaches a

surface is inversely proportional to the square of the distance between them

We can add a factor of the form 1/(a + bd +cd2) to the diffuse and specular terms

The constant and linear terms soften the effect of the point source

Page 25: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

The Phong Illumination Model

25

Sum of three components: diffuse reflection + specular reflection +

ambient

Ambient represents the reflection of all indirect illumination

Page 26: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

26

Page 27: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

27

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 match light source properties Nine absorption coefficients

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

Shininess coefficient

Page 28: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Phong Reflectance Model

28

Page 29: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Phong Examples

29

The direction of the light source and the n are varied

Page 30: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

The Plastic Look

30

The Phong illumination model is an approximation of a surface with a specular and a diffuse layer

E.g., shiny plastic, varnished wood, gloss paint

Page 31: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Phong Reflectance Model

31

Single light source:

Page 32: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Phong Reflectance Model

32

Multiple light sources:

Page 33: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Computation of Vectors L and V are specified by the application Can compute R from L and N Problem is determining N OpenGL leaves determination of normal to

application Exception for GLU quadrics and Bezier

surfaces

Page 34: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Plane Normals Equation of plane: ax+by+cz+d = 0 we know that plane is determined by three

points p0, p1, p2 or normal n and p0

Normal can be obtained by

n = (p1-p0) × (p2-p0) p0

p2

p1

Page 35: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Normal to Sphere Surface implicit function f(x, y, z) = 0 Normal given by gradient vector Unit sphere f(p)=p·p-1 n = [∂f/∂x, ∂f/∂y, ∂f/∂z]T=p

Page 36: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Parametric Form For unit sphere

Tangent plane determined by vectors

Normal given by cross product

x=x(u,v)=cos u cos vy=y(u,v)=cos u sin vz= z(u,v)=sin u

∂p/∂u = [∂x/∂u, ∂y/∂u, ∂z/∂u]T∂p/∂v = [∂x/∂v, ∂y/∂v, ∂z/∂v]T

n = ∂p/∂u × ∂p/∂v

Page 37: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

General Case We can compute parametric normals for other

simple cases Quadrics Parametric polynomial surfaces

Bezier surface patches

Page 38: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Outline

38

Illumination and Reflectance The Phong Reflectance Model Shading in OpenGL

Page 39: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Objectives Introduce the OpenGL shading functions Discuss polygonal shading

Flat Smooth Gouraud

Page 40: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Steps in OpenGL shading1. Specify normals2. Enable shading and select model3. Specify lights4. Specify material properties

Page 41: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Normals In OpenGL the normal vector is part of the

state Set by glNormal*()

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

Usually we want to set the normal to have unit length so cosine calculations are correct Length can be affected by transformations Note that scaling does not preserved length glEnable(GL_NORMALIZE) allows for

autonormalization at a performance penalty

Page 42: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Normal for Triangle

p1

p0

p2

n

plane n ·(p - p0 ) = 0

n = (p2 - p0 ) ×(p1 - p0 )

normalize n n/ |n|

p

Note that right-hand rule determines outward face

Page 43: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Enabling Shading

Shading calculations are enabled by glEnable(GL_LIGHTING) Once lighting is enabled, glColor() ignored

Must enable each light source individually glEnable(GL_LIGHTi) i=0,1….. At least 8 light sources

Can choose light model parameters glLightModeli(parameter, GL_TRUE)

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

Time consuming

Page 44: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Defining a 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);

Page 45: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Distance and Direction

The source colors are specified in RGBA The position is given in homogeneous

coordinates If w =1.0, a finite location If w =0.0, a parallel source with the given

direction vector The coefficients in the distance terms

(1/(a+bd+cd2)) by default a=1.0 (GL_CONSTANT_ATTENUATION), b=c=0.0 (GL_LINEAR_ATTENUATION,

GL_QUADRATIC_ATTENUATION ). Change bya= 0.80;glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, a);

Page 46: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Spotlights

Use glLightv to set Direction GL_SPOT_DIRECTION Cutoff GL_SPOT_CUTOFF Attenuation GL_SPOT_EXPONENT

Proportional to cos

Page 47: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

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 also allows a global ambient term that is often helpful for testing glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient)

Page 48: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Moving Light Sources

Light sources are geometric objects whose positions or directions are affected by the model-view matrix

Depending on where we place the position (direction) setting function, we can Move the light source(s) with the object(s) Fix the object(s) and move the light source(s) Fix the light source(s) and move the object(s) Move the light source(s) and object(s) independently

Page 49: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

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.0glMaterialf(GL_FRONT, GL_AMBIENT, ambient);glMaterialf(GL_FRONT, GL_DIFFUSE, diffuse);glMaterialf(GL_FRONT, GL_SPECULAR, specular);glMaterialf(GL_FRONT, GL_SHININESS, shine);

Page 50: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

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

Page 51: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Emissive Term We can simulate a light source in OpenGL by

giving a material an emissive component This component is unaffected by any sources

or transformations

GLfloat emission[] = 0.0, 0.3, 0.3, 1.0);glMaterialf(GL_FRONT, GL_EMISSION, emission);

Page 52: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Transparency Material properties are specified as RGBA

values The A value can be used to make the surface

translucent The default is that all surfaces are opaque

regardless of A Later we will enable blending and use this

feature

Page 53: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Efficiency Because material properties are part of the

state, if we change materials for many surfaces, we can affect performance

We can make the code cleaner by defining a material structure and setting all materials during initialization

We can then select a material by a pointer

typedef struct materialStruct { GLfloat ambient[4]; GLfloat diffuse[4]; GLfloat specular[4]; GLfloat shineness;} MaterialStruct;

Page 54: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Polygonal Shading Shading calculations are done for each vertex

Vertex colors become vertex shades 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

Page 55: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Polygon Normals Polygons have a single normal

Shades at the vertices as computed by the Phong model can be almost same

Identical for a distant viewer (default) or if there is no specular component

Consider model of sphere Want different normals at

each vertex even though this concept is not quite correct mathematically

Page 56: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Smooth Shading

We can set a new normal at each vertex

Easy for sphere model If centered at origin, n = p

Now smooth shading works Note silhouette edge

Page 57: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Mesh Shading The previous example is not general because

we knew the normal at each vertex analytically

For polygonal models, Gouraud proposed we use the average of the normals around a mesh vertex

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

Page 58: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Gouraud and Phong Shading

Gouraud Shading Find average normal at each vertex (vertex

normals) Apply modified 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 modified Phong model at each fragment

Page 59: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

Comparison If the polygon mesh approximates surfaces

with a high curvatures, Phong shading may look smooth while Gouraud shading may show edges

Phong shading requires much more work than Gouraud shading Until recently not available in real time systems Now can be done using fragment shaders

Both need data structures to represent meshes so we can obtain vertex normals

Page 60: Illumination & Reflectance Dr. Amy Zhang. Outline 2  Illumination and Reflectance  The Phong Reflectance Model  Shading in OpenGL

The end

60

Questions and answers