CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light...

92
CISC 3620 Lecture 7 Lighng and shading Topics : Exam review part 2 Lighting and shading Phong-Blinn model Computing necessary vectors Lighting and shading in WebGL Polygonal shading Per vertex and per fragment shading

Transcript of CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light...

Page 1: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

CISC 3620 Lecture 7Lighting and shading

Topics:

Exam review part 2Lighting and shadingPhong-Blinn model

Computing necessary vectorsLighting and shading in WebGL

Polygonal shadingPer vertex and per fragment shading

Page 2: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Exam review part 2

Page 3: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Midterm instructions

● This is a closed-book, closed-note exam, except for one 8.5x11” sheets of notes.

● You may not consult with any other person during the exam.● You may not use any communication device or computer during the exam.● You have 125 minutes to finish.● Write all work on the exam paper. Use reverse side if needed (but clearly

indicate this if you do)● The exam will be graded out of 100 points, but there are 110 points available,

thus there are 10 available bonus points. ● It is highly recommended that you provide some answer for every question so

you can receive partial credit. Unanswered questions will receive 0 points.

Page 4: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Example questions

● What is the projection of point (x,y,z) in the pinhole camera model with the plane of projection at zp=-d through a center of projection of the origin?

● What is the difference between a pinhole camera and the synthetic camera model?

● What is the difference between global and local lighting?● What are the components of our graphics pipeline and what does each do?

Which are programmable?● Describe an input device that provides absolute position. Describe one that

provides relative position● What is the difference between request mode and event mode for user input? For

which of these are callbacks more appropriate?● Name 3 techniques for picking primitives under a cursor

Page 5: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

More example questions

● Which of the following objects are convex? Sphere, coffee mug, donut, axel, shoe box, fork, football

● Name two parametric ways to define a plane● What is the length of the vector [1 2 2]?● What is the distance between the points [2 3 4] and [4 4 6]?● What is the dot product between the vectors [-1 1 3]

and [2 -1 1]? What is the angle between them?● Let Tx = T(1, 0, 0) and Ty = T(0, 2, 0). Compute Tx Ty.

Compute Ty Tx. Are they the same?

Page 6: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

What kinds of projections are these?

Page 7: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Lighting and shading

Page 8: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

8

Lighting and Shading: Objectives

● Learn to shade objects so their images appear three-dimensional

● Introduce the types of light-material interactions● Build a simple reflection model---the Phong

model--- that can be used with real time graphics hardware

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 9: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

9

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

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 10: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

10

Shading

● 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 –

– E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 11: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

11

Shading

● 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

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 12: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

12

Scattering

● Light strikes A – Some scattered– Some absorbed

● Some of scattered light strikes B– Some scattered– Some absorbed

● Some of this scatteredlight strikes A

● and so on

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 13: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

13

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

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 14: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

14

Global Effects

translucent surface

shadow

multiple reflection

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 15: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

15

Local vs Global Rendering● 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”– Exist many techniques for approximating global effects

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 15

https://commons.wikimedia.org/w/index.php?curid=31812

Page 16: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

16

Local vs Global Rendering● 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”– Exist many techniques for approximating global effects

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 16

https://commons.wikimedia.org/w/index.php?curid=31812

GlobalLocal

Page 17: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

17

Light-Material Interaction

● Light that strikes an object is partially absorbed and partially scattered (reflected)

● 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

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

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 18: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

18

Light-Material Interaction

● Light that strikes an object is partially absorbed and partially scattered (reflected)

● The amount reflected determines the color and brightness of the object– A surface appears red under white light because

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 19: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

19

Light Sources

● General light sources are difficult to work with because we must integrate light coming from all points on the source

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 20: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

20

Simple Light Sources

● Point source– Model with position and color– Can be 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

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 21: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

21

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

smooth surface rough surface

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 22: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

22

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

smooth surface rough surface

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 23: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

23

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

smooth surface rough surface

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 24: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

The Phong Model

Page 25: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

25

Phong Model● A simple model that can be computed rapidly● Has three components

– Diffuse– Specular– Ambient

● Uses four vectors – To source: l– To viewer: v– Normal: n– Perfect reflector: r

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 26: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

26

Ideal Reflector

● Normal is determined by local orientation● Angle of incidence = angle of reflection● The three vectors must be co-planar

r =

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 27: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

27

Ideal Reflector

● Normal is determined by local orientation● Angle of incidence = angle of reflection● The three vectors must be co-planar

r = 2 (l · n ) n - l

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 28: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

28

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 are normalized

– There are also three coefficients, kr, kg, kb that show how much of each color component is reflected

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

https://www.labsphere.com/site/assets/files/3005/cm6page.pdf

Page 29: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

29

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

specularhighlight

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 30: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

30

Modeling Specular Reflections

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

Ir ~ ks I cos

shininess coef

absorption coef

incoming intensityreflectedintensity

E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 31: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

31

The Shininess Coefficient

● Values of between 100 and 200 correspond to metals

● Values between 5 and 10 give surface that look like plastic

cos

90-90E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

Page 32: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

32

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

reflection coef intensity of ambient light

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 33: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

33

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 theform 1/(a + bd +cd2) tothe diffuse and specular terms

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

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 34: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

34

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

– Diffuse: Idr, Idg, Idb

– Specular: Isr, Isg, Isb

– Ambient: Iar, Iag, Iab

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 35: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

35

Material Properties

● Material properties match light source properties– Nine absorbtion coefficients

● Diffuse: kdr, kdg, kdb ● Specular: ksr, ksg, ksb ● Ambient: kar, kag, kab

– Plus shininess coefficient

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 36: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

36

Adding up the Components

● For each light source and each color component, the Phong model can be written as (assuming unit vectors)

I =

● For each color component we add contributions fromall light sources

● Id and Is terms depend on distance

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 37: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

37

Adding up the Components

● For each light source and each color component, the Phong model can be written as (assuming unit vectors)

I = kd Id l · n + ks Is (v · r )+ ka Ia

● For each color component we add contributions fromall light sources

● Id and Is terms depend on distance

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 38: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

38

Blinn-Phong Model

● The specular term in the Phong model is problematic because it is truncated for view directions more than 90 degrees from the reflection direction

● Blinn suggested an approach using the halfway vector that is more realistic for low values of alpha

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

https://learnopengl.com/Advanced-Lighting/Advanced-Lighting

Page 39: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

39

The Halfway Vector

•h is the normalized vector halfway between l (light) and v (viewer)

h = ( l + v )/ | l + v |

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 40: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

40

Using the halfway vector

● h = ( l + v )/ | l + v | and r = 2 (l · n ) n - l● Replace (v · r )by●

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 41: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

41

Using the halfway vector

● h = ( l + v )/ | l + v | and r = 2 (l · n ) n - l● Replace (v · r )by (n · h )● is chosen to match shininess● Note that halfway angle is half of angle between r

and v if vectors are coplanar● Resulting model is known as the modified Phong or

Blinn-Phong lighting model– Specified in OpenGL standard

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 42: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

42

Phong Examples

Only differences between these teapots are the Parameters inthe modifiedPhong model

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 43: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Computing necessary vectors

Page 44: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

44

Computation of Vectors

● l and v are specified by the application● Can compute r from l and n● Problem is determining n● For simple surfaces it can be determined, but how we

determine n differs depending on underlying representation of surface

● OpenGL leaves determination of normal to application

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 45: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Computing Reflection Direction

● Angle of incidence = angle of reflection● Normal, light direction and reflection direction are

coplaner● Want all three to be unit length

45Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 46: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Computing Reflection Direction

● Angle of incidence = angle of reflection● Normal, light direction and reflection direction are

coplaner● Want all three to be unit length

46

r=2( l·n )n−l

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 47: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

47

Plane Normals

● Equation of plane: ax+by+cz+d = 0● From Chapter 4 we know that plane is determined

by three points p0, p1, p2 or normal n and p0

● Normal can be obtained using cross-product

n =

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 48: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

48

Plane Normals

● Equation of plane: ax+by+cz+d = 0● From Chapter 4 we know that plane is determined

by three points p0, p1, p2 or normal n and p0

● Normal can be obtained using cross-product

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

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 49: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

49

Normal to sphere: implicit form

● Implicit function f(x, y, z)=0● Normal given by gradient● Sphere f(p)=p·p-1

n = [∂f/∂x, ∂f/∂y, ∂f/∂z]T=p

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 50: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

50

Normal to sphere: parametric form

● For sphere

● Tangent plane determined by vectors

● Normal given by cross product

x=x(u,v)=cos u sin vy=y(u,v)=cos u cos 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 = p

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 51: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

51

General Case

● We can compute parametric normals for other simple cases– Quadrics– Parametric polynomial surfaces

● Bezier surface patches (Chapter 11)

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 52: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Lighting and shading in WebGL

Page 53: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

53

Objectives

● Introduce the WebGL shading methods– Light and material functions on MV.js– per vertex vs per fragment shading– Where to carry out

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 54: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

54

WebGL lighting

● Need – – –

● Compute in application or in shaders

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 55: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

55

WebGL lighting

● Need – Normals– Material properties– Lights

● Compute in application or in shaders

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 56: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

56

Normalization

● Cosine terms in lighting calculations can be computed using dot product

● Unit length vectors simplify calculation● Usually we want to set the magnitudes to have unit

length but– Length can be affected by transformations– Note that scaling does not preserve length

● GLSL has a normalization function: normalize()

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 57: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

57

Normal for Triangle

p0

p1

p2

n

plane n ·(p - p0 ) = 0

n =

normalize n n/ |n|

p

Note that right-hand rule determines outward face

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 58: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

58

Normal for Triangle

p0

p1

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

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 59: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

59

Specifying a Point Light Source

● Each light source needs one RGBA for:

● And a

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 60: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

60

Specifying a Point Light Source

● Each light source needs one RGBA for:

– Ambient component

– Diffuse component

– Specular component

● And a position

var diffuse0 = vec4(1.0, 0.0, 0.0, 1.0);var ambient0 = vec4(1.0, 0.0, 0.0, 1.0);var specular0 = vec4(1.0, 0.0, 0.0, 1.0);var light0_pos = vec4(1.0, 2.0, 3,0, 1.0);

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 61: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

61

Distance and Direction

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

– If w =1.0, we are specifying a finite location– If w =0.0, we are specifying a parallel source with the

given direction vector

● The coefficients in distance terms are usually quadratic (1/(a+b*d+c*d*d)) where d is the distance from the point being rendered to the light source

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 62: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

62

Spotlights

● Derive from point source– Direction– Cutoff– Attenuation Proportional to cos

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 63: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

63

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

● A global ambient term is often helpful for testing

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 64: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

64

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

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 65: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Light Properties

65Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

var lightPosition = vec4(1.0, 1.0, 1.0, 0.0 );var lightAmbient = vec4(0.2, 0.2, 0.2, 1.0 );var lightDiffuse = vec4( 1.0, 1.0, 1.0, 1.0 );var lightSpecular = vec4( 1.0, 1.0, 1.0, 1.0 );

Page 66: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

66

Material Properties

● Material properties should match the terms in the light model● Reflectivities● w component gives opacity

var materialAmbient = vec4( 1.0, 0.0, 1.0, 1.0 );var materialDiffuse = vec4( 1.0, 0.8, 0.0, 1.0);var materialSpecular = vec4( 1.0, 0.8, 0.0, 1.0 );var materialShininess = 100.0;

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 67: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Using MV.js for Products

67Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

var ambientProduct = mult(lightAmbient, materialAmbient);var diffuseProduct = mult(lightDiffuse, materialDiffuse);var specularProduct = mult(lightSpecular, materialSpecular);

gl.uniform4fv(gl.getUniformLocation(program, "ambientProduct"), flatten(ambientProduct));gl.uniform4fv(gl.getUniformLocation(program, "diffuseProduct"), flatten(diffuseProduct) );gl.uniform4fv(gl.getUniformLocation(program, "specularProduct"), flatten(specularProduct) );gl.uniform4fv(gl.getUniformLocation(program, "lightPosition"), flatten(lightPosition) );gl.uniform1f(gl.getUniformLocation(program, "shininess"), materialShininess);

Page 68: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Adding Normals for Quads

68Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

function quad(a, b, c, d) { var t1 = subtract(vertices[b], vertices[a]); var t2 = subtract(vertices[c], vertices[b]); var normal = cross(t1, t2); var normal = vec3(normal); normal = normalize(normal);

pointsArray.push(vertices[a]); normalsArray.push(normal); . .

.

Page 69: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

69

Front and Back Faces

● Every face has a front and back● For many objects, we never see the back face so we don’t

care how or if it’s rendered● If it matters, we can handle it in the shader

back faces not visible back faces visible

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 70: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

70

Emissive Term

● We can simulate a light source in WebGL by giving a material an emissive component

● This component is unaffected by any sources or transformations

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 71: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

71

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● Later we will enable blending and use this feature● However with the HTML5 canvas, A<1 will mute

colors

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 72: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Polygonal Shading

Page 73: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

73

Polygonal Shading

● In per-vertex shading, shading calculations are done for each vertex– Vertex colors become vertex shades and can be sent to the vertex shader

as a vertex attribute– Alternately, we can send the parameters to the vertex shader and have it

compute the shade

● By default, vertex shades are interpolated across an object if passed to the fragment shader as a varying variable (smooth shading)

● We can also use uniform variables to shade with a single shade (flat shading)

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 74: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

74

Polygon Normals

● Triangles have a single normal– Shades at the vertices as computed by the modified

Phong model can be almost the same – Identical for a distant viewer (default) or if there is no

specular component

● Consider a model of a sphere● Want different normals at

each vertex even thoughthis concept is not quitecorrect mathematicallyAngel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 75: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

75

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● But note silhouette edge

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 76: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

76

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|

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 77: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

77

Gouraud and Phong Shading

● Gouraud Shading– Find average normal at each vertex (vertex normals)– Apply modified Phong model at each vertex– Interpolate vertex shades (colors) across each polygon

● Phong shading– Find vertex normals– Interpolate vertex normals across each polygon– Apply modified Phong model at each fragment (pixel)

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 78: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Comparison

https://www.youtube.com/watch?v=VRw3GuVdldo

Page 79: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

79

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

Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Page 80: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Per vertex and per fragment shading

Page 83: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Gouraud (vertex lighting) shaders I

83Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

// vertex shader

attribute vec4 vPosition;attribute vec4 vNormal;varying vec4 fColor;uniform vec4 ambientProduct, diffuseProduct, specularProduct;uniform mat4 modelViewMatrix;uniform mat4 projectionMatrix;uniform vec4 lightPosition;uniform float shininess;

Page 84: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Gouraud (vertex lighting) shaders II

84Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

void main(){ vec3 pos = -(modelViewMatrix * vPosition).xyz; vec3 light = lightPosition.xyz; vec3 L = normalize( light - pos ); vec3 E = normalize( -pos ); vec3 H = normalize( L + E );

// Transform vertex normal into eye coordinates vec3 N = normalize( (modelViewMatrix*vNormal).xyz);

Page 85: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Gouraud (vertex lighting) shaders III

85Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

// Compute terms in the illumination equation vec4 ambient = AmbientProduct;

float Kd = max( dot(L, N), 0.0 ); vec4 diffuse = Kd*DiffuseProduct; float Ks = pow( max(dot(N, H), 0.0), Shininess ); vec4 specular = Ks * SpecularProduct; if( dot(L, N) < 0.0 ) specular = vec4(0.0, 0.0, 0.0, 1.0); gl_Position = Projection * ModelView * vPosition;

color = ambient + diffuse + specular; color.a = 1.0;

}

Page 86: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Gouraud (vertex lighting) shaders IV

86Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

// fragment shader

precision mediump float;

varying vec4 fColor;

voidmain(){ gl_FragColor = fColor;}

Page 87: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Phong-Blinn shading

Page 88: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Phong-blinn (fragment lighting) Shaders I

88Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

// vertex shader attribute vec4 vPosition;attribute vec4 vNormal;varying vec3 N, L, E;uniform mat4 modelViewMatrix;uniform mat4 projectionMatrix;uniform vec4 lightPosition;

void main(){ vec3 pos = -(modelViewMatrix * vPosition).xyz; vec3 light = lightPosition.xyz; L = normalize( light - pos ); E = -pos; N = normalize( (modelViewMatrix*vNormal).xyz); gl_Position = projectionMatrix * modelViewMatrix * vPosition;

};

Page 89: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Phong-blinn (fragment lighting) Shaders II

89Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

// fragment shader

precision mediump float;

uniform vec4 ambientProduct;uniform vec4 diffuseProduct;uniform vec4 specularProduct;uniform float shininess;varying vec3 N, L, E;

Page 90: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Phong-blinn (fragment lighting) Shaders III

90Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

void main(){ vec4 fColor; vec3 H = normalize( L + E ); vec4 ambient = ambientProduct; float Kd = max( dot(L, N), 0.0 ); vec4 diffuse = Kd*diffuseProduct; float Ks = pow( max(dot(N, H), 0.0), shininess ); vec4 specular = Ks * specularProduct; if( dot(L, N) < 0.0 ) specular = vec4(0.0, 0.0, 0.0, 1.0); fColor = ambient + diffuse + specular; fColor.a = 1.0; gl_FragColor = fColor;

}

Page 91: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Teapot Examples

91Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015

Gouraud Phong-Blinn

Page 92: CISC 3620 Lecture 7 Lighting and shadingm.mr-pc.org/t/cisc3620/2019sp/lecture07.pdf · Light Sources In the Phong Model, we add the results from each light source Each light source

Summary

● Exam review part 2● Lighting and shading● Phong-Blinn model● Computing necessary vectors● Lighting and shading in WebGL● Polygonal shading● Per vertex and per fragment shading