65
1 CSC 830 Computer CSC 830 Computer Graphics Graphics Lecture 5 Lecture 5 Shading Shading Course Note Credit: Some of slides are extracted from the course notes of prof. Mathieu Desburn (USC) and prof. Han-Wei Shen (Ohio State University).

CSC 830 Computer Graphics Lecture 5 Shading

  • Upload
    field

  • View
    43

  • Download
    0

Embed Size (px)

DESCRIPTION

CSC 830 Computer Graphics Lecture 5 Shading. Course Note Credit: Some of slides are extracted from the course notes of prof. Mathieu Desburn (USC) and prof. Han-Wei Shen (Ohio State University). Shading. Determining the light traveling from a point in the scene to the viewer’s eye. - PowerPoint PPT Presentation

Citation preview

Page 1: CSC 830 Computer Graphics Lecture 5 Shading

1

CSC 830 Computer CSC 830 Computer GraphicsGraphicsLecture 5Lecture 5ShadingShading

CSC 830 Computer CSC 830 Computer GraphicsGraphicsLecture 5Lecture 5ShadingShading

Course Note Credit: Some of slides are extracted from the course notes of prof. Mathieu Desburn (USC) and prof. Han-Wei Shen (Ohio State University).

Page 2: CSC 830 Computer Graphics Lecture 5 Shading

2

ShadingDetermining the light traveling from a point in the scene to the viewer’s eye

Images courtesy of Watt, Watt & Watt, and Foley & van Dam

Page 3: CSC 830 Computer Graphics Lecture 5 Shading

3

ShadingLight comes from many sources:

absorbedscattered

dtransmitte

reflectedemittedlight

scattering

absorption

reflection

transmissionemission

Page 4: CSC 830 Computer Graphics Lecture 5 Shading

4

Local versus Global Illumination

Global Illumination Considers

indirect illumination

Reflection Refraction Shadows

Local Illumination Only considers direct

illumination No reflection No refraction Shadows possible

Page 5: CSC 830 Computer Graphics Lecture 5 Shading

5

Local versus Global Illumination

2P1P

Indirect Illumination

Direct Illumination

We will do local only for this lecture…

Page 6: CSC 830 Computer Graphics Lecture 5 Shading

6

Local illumination• Only consider the light, the observer position,

and the object material properties

Page 7: CSC 830 Computer Graphics Lecture 5 Shading

7

Local versus Global Illumination

Images courtesy of Francois Sillion

To understand shading properly, we need to review some basic notions of physics…

Page 8: CSC 830 Computer Graphics Lecture 5 Shading

8

Basic Illumination Model• Simple and fast method for calculating surface

intensity at a given point• Lighting calculation are based on:

– The background lighting conditions– The light source specification: color, position– Optical properties of surfaces:

• Glossy OR matte• Opaque OR transparent (control refection and

absorption)

Page 9: CSC 830 Computer Graphics Lecture 5 Shading

9

RadianceRadiance: Power per unit projected area

perpendicular to the ray, per unit solid angle in the direction of the ray

d

dA

N),( xI

Think of it as a flux of photons leaving the surface…

Page 10: CSC 830 Computer Graphics Lecture 5 Shading

10

IrradianceIrradiance: Radiant energy per unit area

dxIdEE cos),(

),( xI

Think of it as a flux of photons bombarding the surface…

Page 11: CSC 830 Computer Graphics Lecture 5 Shading

11

BRDFBidirectional Reflection Distribution Function

Determines the fraction of light from an incoming direction reflected to an outgoing direction

),(

),(),,(

i

rri xdE

xIxf

Image courtesy of Watt, 3D Computer Graphics

Relates reflected radiance to incoming irradiance…

Page 12: CSC 830 Computer Graphics Lecture 5 Shading

12

Properties of the BRDFReciprocity

),,(),,( irri xfxf In photography and holography, reciprocity refers to the relationship between the intensity of the light and duration of the exposure that result in identical exposure. Within the normal range for intensity and time for the film, the reciprocity law states that exposure = intensity × time. Outside the normal range the reciprocity law breaks down, which is known as reciprocity failure.

Page 13: CSC 830 Computer Graphics Lecture 5 Shading

13

Properties of the BRDFAnisotropy

),,,,(),,,,( rriirrii xfxf

Anisotropy (the opposite of isotropy) is the property of being directionally dependent.In the field of computer graphics, an anisotropic surface will change in appearance as it is rotated about its geometric normal, as is the case with velvet. Anisotropic scaling occurs when something is scaled by different amounts in different directions.

Page 14: CSC 830 Computer Graphics Lecture 5 Shading

14

Reflectance EquationBRDF allows us to calculate outgoing light, given incoming light

),(),,(),( irirfrom xdExfxIi

Image courtesy of Watt, 3D Computer Graphics

Page 15: CSC 830 Computer Graphics Lecture 5 Shading

15

Splitting the BRDFIn practice, BRDF can be really funky…To simplify, we divide the BRDF into 3 components:

specularidealdiffuseldirectionadiffuseideal ffff

NL NL

MNL

M

Page 16: CSC 830 Computer Graphics Lecture 5 Shading

16

Reflectance• 3 forms

Slide Courtesy of Dutre et. al on SIGGRAPH 2001

Page 17: CSC 830 Computer Graphics Lecture 5 Shading

17

Ideal Specular Reflection

Reflection is only in the mirror direction

NL

M

Page 18: CSC 830 Computer Graphics Lecture 5 Shading

18

Ideal Diffuse ReflectionReflection is equal in all directions

diffuserixf ),,(NL

Page 19: CSC 830 Computer Graphics Lecture 5 Shading

19

Directional Diffuse Reflection

Reflection is concentrated around the mirror direction

NL

M

Page 20: CSC 830 Computer Graphics Lecture 5 Shading

20

Ambient LightIf a surface is visible from the eye, but not a light, it will be rendered black (if indirect light is not considered).

Ambient light is an approximation to indirect light

Difficult to compute Approximate this as a constant Or a light source at the eye

Page 21: CSC 830 Computer Graphics Lecture 5 Shading

21

Phong Reflection

speculardiffuseambient IIII

Assume point lights and direct illumination only

Page 22: CSC 830 Computer Graphics Lecture 5 Shading

22

Diffuse Light

• The illumination that a surface receives from a light source and reflects equally in all directions

• This type of reflection is called Lambertian Reflection (thus, Lambertian surfaces)

• The brightness of the surface is indepenent of the observer position (since the light is reflected in all direction equally)

Page 23: CSC 830 Computer Graphics Lecture 5 Shading

23

Lambert’s Law• How much light the surface receives from a

light source depends on the angle between its angle and the vector from the surface point to the light (light vector)

• Lambert’s law: the radiant energy ’Id’ from a small surface da for a given light source is:

Id = IL * cos)IL : the intensity of the light source is the angle between the surface normal (N) and light vector (L)

Page 24: CSC 830 Computer Graphics Lecture 5 Shading

24

Phong Diffuse Component

)(

cos

LNkI

kII

dl

dldiffuse

Diffuse component depends only on incident angle.

Image courtesy of Watt, 3D Computer Graphics

Note: L and N are unit…

Page 25: CSC 830 Computer Graphics Lecture 5 Shading

25

Examples

Sphere diffusely lighted from various angles !

Page 26: CSC 830 Computer Graphics Lecture 5 Shading

26

Specular Light

These are the bright spots on objects (such as polished metal, apple ...)

Light reflected from the surface unequally to all directions.

The result of near total reflection of the incident light in a concentrated region around the specular reflection angle

Page 27: CSC 830 Computer Graphics Lecture 5 Shading

27

Specular Highlights

• Shiny surfaces change appearance when viewpoint is changed• Specularities are caused by microscopically smooth surfaces.• A mirror is a perfect specular reflector

Page 28: CSC 830 Computer Graphics Lecture 5 Shading

28

Phong Specular Component

nsl

nslspecular

VRkI

kII

)(

cos

Phong combines directional diffuse & ideal specular

Image courtesy of Watt, 3D Computer Graphics

Page 29: CSC 830 Computer Graphics Lecture 5 Shading

29

Half Vector• An alternative way of computing phong

lighting is: Is = ks * Is * (N*H)n • H (halfway vector): halfway between V and L:

(V+L)/2• Fuzzier highlight• Check

http://www.lighthouse3d.com/opengl/glsl/index.php?ogldir2 L

N H

V

Page 30: CSC 830 Computer Graphics Lecture 5 Shading

30

Phong Illumination

Moving Light

Change n

Page 31: CSC 830 Computer Graphics Lecture 5 Shading

31

Phong Ambient Component

aaambient kII

Treat it as a constant:

Where is the ambient light in the scene.aI

Page 32: CSC 830 Computer Graphics Lecture 5 Shading

32

Adding Color

bsbl

gsgl

rsrln

bdbl

gdgl

rdrl

baba

gaga

rara

b

g

r

kI

kI

kI

HN

kI

kI

kI

LN

kI

kI

kI

I

I

I

,,

,,

,,

,,

,,

,,

,,

,,

,,

)()(

Page 33: CSC 830 Computer Graphics Lecture 5 Shading

33

Adding Lights

lightsll

nsdaa

lightslbsbl

gsgl

rsrln

bdbl

gdgl

rdrl

babal

gaga

rara

b

g

r

IHNkLNkIkI

kI

kI

kI

HN

kI

kI

kI

LN

kI

kI

kI

I

I

I

)()(

)()(

,,

,,

,,

,,

,,

,,

,,

,,

,,

Page 34: CSC 830 Computer Graphics Lecture 5 Shading

34

Phong Reflection

Image courtesy of Watt, 3D Computer Graphics

Page 35: CSC 830 Computer Graphics Lecture 5 Shading

35

Aluminium

Page 36: CSC 830 Computer Graphics Lecture 5 Shading

36

Bronze

Page 37: CSC 830 Computer Graphics Lecture 5 Shading

37

Chrome

Page 38: CSC 830 Computer Graphics Lecture 5 Shading

38

Stainless Steel

Page 39: CSC 830 Computer Graphics Lecture 5 Shading

39

OpenGL Materials

GLfloat white8[] = {.8, .8, .8, 1.}, white2 = {.2,.2,.2,1.},black={0.,0.,0.}; GLfloat mat_shininess[] = {50.}; /* Phong exponent */ glMaterialfv( GL_FRONT_AND_BACK, GL_AMBIENT, black);

glMaterialfv( GL_FRONT_AND_BACK, GL_DIFFUSE, white8);

glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, white2);

glMaterialfv( GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);

Page 40: CSC 830 Computer Graphics Lecture 5 Shading

40

OpenGL Lighting

GLfloat white[] = {1., 1., 1., 1.}; GLfloat light0_position[] = {1., 1., 5., 0.}; /* directional light (w=0) */

glLightfv(GL_LIGHT0, GL_POSITION, light0_position); glLightfv(GL_LIGHT0, GL_DIFFUSE, white); glLightfv(GL_LIGHT0, GL_SPECULAR, white); glEnable(GL_LIGHT0);

glEnable(GL_NORMALIZE); /* normalize normal vectors */ glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);/* two-sided lighting*/

glEnable(GL_LIGHTING);

Page 41: CSC 830 Computer Graphics Lecture 5 Shading

41

Vertex Normals vs. Face Normals

What are the normals to the surface?

Each polygonal face has a normal.

We call these face normals.

a

c

b

N = (b - a) x (c - b)

Page 42: CSC 830 Computer Graphics Lecture 5 Shading

42

Flat ShadingAssume a constant color across the polygon

Uses face normalsEquivalent to single point sampling…

a

c

b

Polygon mesh is only an approximation.Can we do better?

Page 43: CSC 830 Computer Graphics Lecture 5 Shading

43

Vertex Normals vs. Face Normals

Should use the actual surface’s normals

Usually stored at the vertices of the objectCan calculate as averages of face normals

Page 44: CSC 830 Computer Graphics Lecture 5 Shading

44

Mach Band ?

Page 45: CSC 830 Computer Graphics Lecture 5 Shading

45

Mach Band ?

Page 46: CSC 830 Computer Graphics Lecture 5 Shading

46

Shading Models for Polygons Constant Shading (flat shading)

Compute illumination at any one point on the surface. Use face or one normal from a pair of edges. Good for far away light and viewer or if facets approximate surface well.

Per-Pixel Shading

Compute illumination at every point on the surface.

Interpolated Shading

Compute illumination at vertices and interpolate color

Page 47: CSC 830 Computer Graphics Lecture 5 Shading

47

Interpolation

Gouraud Interpolation Phong Interpolation

Given vertex normals, how to color the interior:

Page 48: CSC 830 Computer Graphics Lecture 5 Shading

48

Un-lit

Page 49: CSC 830 Computer Graphics Lecture 5 Shading

49

Flat Shading

Page 50: CSC 830 Computer Graphics Lecture 5 Shading

50

Gouraud Interpolation – Interpolated Shading

Page 51: CSC 830 Computer Graphics Lecture 5 Shading

51

Phong Interpolation – Per pixel Shading

Page 52: CSC 830 Computer Graphics Lecture 5 Shading

52

Gouraud InterpolationCalculate the color at each vertex

Interpolate the colors

C(a)

C(c)

C(b)

C(a)

C(c)

C(b)

C = t C(b) + (1 – t) C(c)

Page 53: CSC 830 Computer Graphics Lecture 5 Shading

53

Gouraud Interpolation Problems

Misses some highlights

light

Shading is not linear

shading will be constant!

Page 54: CSC 830 Computer Graphics Lecture 5 Shading

54

Phong InterpolationInterpolate the normals, then compute the colors

light

Interpolation is usually done component-wise

Page 55: CSC 830 Computer Graphics Lecture 5 Shading

55

Phong Shading Model Gouraud shading does not properly handle specular

highlights, specially when the n parameter is large (small highlight).

Reason: colors are interpolated.

Solution: (Phong Shading Model)

1. Compute averaged normal at vertices.

2. Interpolate normals along edges and scan-lines. (component by component)

3. Compute per-pixel illumination.

Page 56: CSC 830 Computer Graphics Lecture 5 Shading

56

Page 57: CSC 830 Computer Graphics Lecture 5 Shading

57

Interpolation

Image courtesy of Watt & Watt, Advanced Animation and Rendering Techniques

Page 58: CSC 830 Computer Graphics Lecture 5 Shading

58

Interpolated Shading - Problems

Problems at shared vertices – shared by right polygons and not by one on left and hence discontinuity

Incorrect Vertex normals – no variation in shade

Page 59: CSC 830 Computer Graphics Lecture 5 Shading

59

Shade Trees

dot

N L

dot

N H

*

Il

*

Il pow

n

+

Phong Shade Tree:

Ia

*

ka

kd

ks

Page 60: CSC 830 Computer Graphics Lecture 5 Shading

60

Shading LanguageA language for implementing shading models

State is passed to/from the shader by global variablesCi – Outgoing ray colorOi – Outgoing ray opacity

Cs – Surface colorOs – Surface opacityP – Surface pointN - Surface normalI - Direction of viewing (eye ray)L - Direction to the light source

I N

L

Page 61: CSC 830 Computer Graphics Lecture 5 Shading

61

Shading Languagesurfaceplastic(float Ks = .5, float Kd = .5, float Ka = 1,

float roughness = .1, color specularColor = 1){ point Nf = faceforward(N,I);

Oi = Os; Ci = Os*(Cs*(Ka*ambient()+Kd*diffuse(Nf)

+ specularColor*Ks*specular(Nf,-I,roughness);}

Phong Surface Shader:

Page 62: CSC 830 Computer Graphics Lecture 5 Shading

62

Light Sources• Point light source• Directional light source: e.g. sun

light• Spot light

Page 63: CSC 830 Computer Graphics Lecture 5 Shading

63

Spot Light

• To restrict a light’s effects to a limited area of the scene• Flap: confine the effects of the light to a designed range in

x, y, and z world coordinate• Cone: restrict the effects of the light using a cone with a

generating angle

Page 64: CSC 830 Computer Graphics Lecture 5 Shading

64

Light Source Attenuation

• Takes into account the distance of the light from the surface

I’L = I L * fatt (d)

I’L: the received light after attenuation

I L: the original light strengthfatt: the attenuation factord: the distance between the light source

and the surface point

• fatt = max ( 1/(c1 + c2*d + c3*d2) , 1) • C1, C2, C3 are user defined constants

associated with each light source

Page 65: CSC 830 Computer Graphics Lecture 5 Shading

65

Hints for HW4• Shading can be done in easily in view space or in

world space where all light, view, & surface normal vectors are described in same coordinate space. – how can you achieve this effect?

• For normal transformation, use only 3x3 rotation matrix out of the 4x4 transformation & inverse transpose of it – remember normal transformation from the lecture 2?

• For Phong shading, interpolate normal & compute shading for each pixel. For Gouraud shading, compute shading at only vertex and interpolate the color.