lighting and shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · lighting and shading...

15
Lighting and Shading 4 th 5 th W k 2009 4 th ~ 5 th Week , 2009 Why We Need Shading Why We Need Shading Suppose we build a model of a sphere using Suppose we build a model of a sphere using many polygons and color it with glColor W t thi lik We get something like But we want Why does the image of a real sphere look like?

Upload: others

Post on 20-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

Lighting and Shading

4th 5th W k 20094th~ 5th Week, 2009

Why We Need ShadingWhy We Need Shading

■ Suppose we build a model of a sphere using■ Suppose we build a model of a sphere using many polygons and color it with glColor

W t thi lik■ We get something like

■ But we want

■ Why does the image of a real sphere look like?

Page 2: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

ShadingShading

■ Determining a color for each filled pixel■ Determining a color for each filled pixel■ Light-material interactions cause each point to

h diff t l h dhave a different color or shade■ Need to consider

■ Light sources■ Material properties■ Location of viewer■ Surface orientation

Light Material InteractionLight-Material Interaction

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

■ The amount reflected determines the color and b i ht f th bj tbrightness of the object■ Ex) red surface under white light

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

Page 3: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

Light SourcesLight Sources

■ General light sources are difficult to work with■ General light sources are difficult to work with because we must integrate light coming from all points on the surfaceall points on the surface

Si l th ti l d l■ Simple mathematical models:■ Point light■ Distant light (directional light)■ Spotlight■ Ambient light

Point Light SourcesPoint Light Sources

■ Emitting light equally in all directions■ Emitting light equally in all directions■ p0: the location of a point light source

( )⎤⎡ pI

( )( )( )( )⎥

⎥⎥

⎢⎢⎢

⎡= 0

0

0 p

p

pI g

r

I

I

I

■ AttenuationProportional to the inverse square distance

( )⎥⎦⎢⎣ 0pbI

■ Proportional to the inverse square distance

( ) ( )020

1 , pI

ppppI

−=

0pp

( ) ( )020

1, pIppI =( ) ( )020, ppp

dkdkk qlc ++

Page 4: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

Distant Light SourcesDistant Light Sources

■ Parallel direction of lights■ Parallel direction of lights■ Infinite distance away from the surface

Location direction■ Location direction

⎤⎡x ⎤⎡x

⎥⎥⎥⎤

⎢⎢⎢⎡

=0 z

y

x

p⎥⎥⎥⎤

⎢⎢⎢⎡

=0 z

y

x

p

⎥⎥

⎦⎢⎢

⎣1z

⎥⎥

⎦⎢⎢

⎣0z

Spotlight SourcesSpotlight Sources

■ Characterized by a narrow range of angle■ Characterized by a narrow range of angle through which light is emitted

p : apex of a cone■ ps: apex of a cone■ ls : direction of pointingθ : angle to determine width■ θ : angle to determine width

Di t ib ti f li ht■ Distribution of light■ Concentrating in the center

■ Light intensity drop off

sls ⋅=φcos

( )ese ls ⋅=φcos

Page 5: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

Ambient Light SourcesAmbient Light Sources

■ Uniform lighting■ Uniform lighting■ Same amount of light everywhere in scene

Contribution of many sources and reflecting■ Contribution of many sources and reflecting surfaces

Phong Reflection ModelPhong Reflection Model

■ A simple model that can compute rapidly■ A simple model that can compute rapidly■ Three components (light-material interactions)

A bi■ Ambient■ Diffuse■ Specular

■ Using four vectors■ n: normal■ v: to the viewer or COP■ l: to light source■ r: perfect reflector

Page 6: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

Ambient ReflectionAmbient Reflection

■ Same at every point on the surface■ Same at every point on the surface■ Ambient reflection coefficient

( ) 10 ≤≤ kkkkkkk

■ Amount reflected S i b b d d i fl d

( ) 1 , ,0, , , ≤≤= abagarabagara kkkkkkk

■ Some is absorbed and some is reflected

■ Three components (red, green, blue)

A bi fl i i d i i■ Ambient reflection term in rendering equationaaa LkI =

■ Can be any of the individual light sources■ Can be a global ambient term

Diffuse ReflectionDiffuse Reflection

■ Light scattered equally in all directions■ Light scattered equally in all directions■ Perfectly diffuse surface

So rough that there is no preferred angle ofSo rough that there is no preferred angle of reflection

■ Lambert’s law■ Amount of light reflected is proportional to

vertical component of incoming light■ Reflected light

Diff se reflection term

ucos∝nl ⋅=ucos

■ Diffuse reflection term

Incorporating a distance term( ) ddd LnlkI ⋅=

■ Incorporating a distance term( ) d2

dd Lnl

kI ⋅

++=

dkdkk qlc

Page 7: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

Specular ReflectionSpecular Reflection

■ Smooth surfaces show specular highlights■ Smooth surfaces show specular highlights

SpecularHighlight

■ Specular reflection term

Highlight

( )■ α: shininess coefficient

φαcos( ) sss LvrkI α⋅=

■ α infinite : mirror■ 100 < α < 200 : metal■ 5< α < 10 : plastic■ 5< α < 10 : plastic

–90 90φ

Computation of ReflectionComputation of Reflection

■ Light sources■ Light sources■ Each light source has separate ambient, diffuse, and

specular terms + separate red green bluespecular terms + separate red, green, blue components = nine coefficients

( )bdbddb LLLLLLLLL ,,,,,,,,

■ Material properties■ Matching light source properties

( )sbsgsrdbdgdrabagar LLLLLLLLL ,,,,,,,,

■ Matching light source properties■ Nine coefficients:

( )kkkkkkkkk■ Shininess coefficient: α

( )sbsgsrdbdgdrabagar kkkkkkkkk ,,,,,,,,

Page 8: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

Adding up the ComponentsAdding up the Components

■ For each light source and each color■ For each light source and each color component, the Phong model can be written

Most surfaces are neither ideal diffusers nor■ Most surfaces are neither ideal diffusers nor perfectly specular

IIII

( ) ( ) ssddaa

sda

LvrkLnlkLk

IIIIα⋅+⋅+=

++=

■ Including the distance term

( ) ( )( )ssddqlc

aa dkdkkLvrkLnlkLkI α⋅+⋅

+++=

2

1

Computation of VectorsComputation of Vectors

■ Normal vectors pn

■ Normal vectors

Id l fl t

p0

p1

( ) ( )0201 ppppn −×−=

■ Ideal reflector■ Angle of incidence == angle of reflection

p2

( )( )lll 2 ( )( )( ) lnnl

lnnllr

−⋅=−⋅+=

2

2

vl +■ Halfway vector

■ Half-angle: ( ) φψθψθ +−=+vl

vlh

++

=

φψ =∴2

( ) sss LvrkI α⋅= ( ) sss LhnkI β⋅=Blinn ModelPhong Model

( ) sss ( ) sss

Page 9: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

Utah Teapots with Different Material Properties

Polygonal ShadingPolygonal Shading

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

■ In OpenGL, vertex shades are interpolated th l b d f lt

Polygonal Mesh

across the polygon by default■ glShadeModel(GL_SMOOTH);

■ If we use glShadeModel(GL_FLAT); the color at the first vertex will determine the shade of the whole polygon

Page 10: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

Flat Shading (1)Flat Shading (1)

■ Constant shading – polygons have a single■ Constant shading – polygons have a single normal

Flat polygon■ Flat polygon n: constant

Distant light source■ Distant light source l: constant

Di t t i■ Distant viewer v: constant

O h di l l i f h lO h di l l i f h l■■ One shading calculation for each polygonOne shading calculation for each polygon■ Consider model of sphere

Flat Shading (2)Flat Shading (2)

■ Disappointing for a smooth surface■ Disappointing for a smooth surface■ Lateral inhibition

■ Human visual system has a remarkable sensitivity■ Human visual system has a remarkable sensitivity

■ Mach bands■ Perceive the increases in brightness along the edges

Herman Grid■ Perceive the increases in brightness along the edges

Step chart

Flat shading of polygonal mesh Perceived and actual intensities at an edge

Page 11: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

Smooth Shading (1)Smooth Shading (1)

■ Gouraud shading – we can set a new normal■ Gouraud shading – we can set a new normal at each vertexO h di l l ti f h tO h di l l ti f h t■■ One shading calculation for each vertexOne shading calculation for each vertex■ Bilinear interpolation of colors

■ Defining vertex normal as the average of the normals around a mesh vertex

4321 nnnnn

+++=

4321 nnnn +++

Note silhouette edge

Smooth Shading (2)Smooth Shading (2)

■ Even the smoothness introduced by Gouraud■ Even the smoothness introduced by Gouraud shading may not prevent the appearance of Mach bandsMach bands

■ If a polygonal mesh is too coarse to capture ill mination effects in pol gon inte io s?illumination effects in polygon interiors?

Flat Shading Smooth ShadingFlat Shading Smooth Shading

Page 12: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

Phong ShadingPhong Shading

■ Interpolating normals across each polygon■ Interpolating normals across each polygon instead of interpolating vertex intensitiesO h di l l ti f h i lO h di l l ti f h i l■■ One shading calculation for each pixelOne shading calculation for each pixel

off-line■ Computing vertex normal at each point

( ) ( ) BA nnn 1 ααα +−=

( ) ( ) DC nnn 1 , βββα +−=

Normals on an edge Interpolation of normals

Gouraud vs Phong ShadingGouraud vs. Phong Shading

■ Gouraud shading■ Gouraud shading■ Find average normal at each vertex (vertex normals)

Apply modified Phong model at each vertex■ 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 13: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

Color Plate 2: WireframeColor Plate 2: Wireframe

Color Plate 3: Flat ShadingColor Plate 3: Flat Shading

Page 14: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

Color Plate 4: Smooth ShadingColor Plate 4: Smooth Shading

Color Plate 1: Bump MappingColor Plate 1: Bump Mapping

Page 15: Lighting and Shadinggraphics.hallym.ac.kr/teach/2009/cga/src/06shade.pdf · Lighting and Shading 4th~ 5th W k 2009Week, 2009 WhyWeNeedShadingWhy We Need Shading

Color Plate 7: Environmental MappingColor Plate 7: Environmental Mapping