39
Lighting and Shading I-Chen Lin’ CG Slides, Doug James’ CG Slides, Angel, Interactive Computer Graphics, Chap 6 Shirley, Fundamentals of Computer Graphics, Chap 9 Wen-Chieh (Steve) Lin Department of Computer Science

Lighting and Shading I-Chen Lin’ CG Slides, Doug James’ CG Slides, Angel, Interactive Computer Graphics, Chap 6 Shirley, Fundamentals of Computer Graphics,

  • View
    251

  • Download
    0

Embed Size (px)

Citation preview

Lighting and ShadingLighting and Shading

I-Chen Lin’ CG Slides, Doug James’ CG Slides,

Angel, Interactive Computer Graphics, Chap 6

Shirley, Fundamentals of Computer Graphics, Chap 9

Wen-Chieh (Steve) Lin

Department of Computer Science

ILE5014 Computer Graphics 10F 2

Illumination and ShadingIllumination and Shading

• Is it a ball or a plate?

• What color should I set for each pixel?

ILE5014 Computer Graphics 10F 3

Why Do We Need Shading?Why Do We Need Shading?

• Suppose we color a sphere model. We get something like

• But we want

ILE5014 Computer Graphics 10F 4

ShadingShading

• 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 the viewer

– Surface orientation

ILE5014 Computer Graphics 10F 5

Factors that affect the “color” of a pixelFactors that affect the “color” of a pixel

• Light sources

– Emittance spectrum (color)

– Geometry (position and direction)

– Directional attenuation

• Objects’ surface properties

– Reflectance spectrum (color)

– Geometry (position, orientation, and micro-structure)

– Absorption

ILE5014 Computer Graphics 10F 6

• Light strikes A

– Some reflected

– Some absorbed

• Some of reflected light strikes B

– Some reflected

– Some absorbed

• Some of reflected light reflected back to A and B, and so on

• Rendering equation describes this recursive process

Light and MatterLight and Matter

ILE5014 Computer Graphics 10F 7

Rendering EquationRendering Equation

• The infinite reflection and absorption of light can be described by the rendering equation

– [outgoing] = [emitted] + [reflected] (+[transmitted])

– Cannot be solved in general

– Ray tracing is a special case for perfectly reflecting surfaces

• Rendering equation is global and includes

– Shadows

– Multiple reflection from object to object

ILE5014 Computer Graphics 10F 8

translucent surface

shadow

multiple reflection

Global EffectsGlobal Effects

transmission

ILE5014 Computer Graphics 10F 9

Local vs Global IlluminationLocal vs Global Illumination

• Correct shading requires a global calculation

– Incompatible with a pipeline model which shades each polygon independently (local rendering)

• However, in computer graphics, especially real time graphics, we are happy if things “look right”

– many techniques for approximating global effects exist

ILE5014 Computer Graphics 10F 10

Local IlluminationLocal Illumination

• Adequate for real-time graphics.

• No inter-reflection, no refraction, no realistic shadow ….

ILE5014 Computer Graphics 10F 11

Light SourcesLight Sources

• General light sources are difficult to simulated

– because we must integrate light coming from all points on the source

ILE5014 Computer Graphics 10F 12

Simple Light SourcesSimple Light Sources

• Point light

– Model with position and color

• Directional light

– Distant source = infinite distance away (parallel)

• Spotlight

– a point light source with a small light cone radius

• Ambient light

– Same amount of light everywhere in scene

– Can model contribution of many sources and reflecting surfaces

ILE5014 Computer Graphics 10F 13

smooth surface rough surface

Surface TypesSurface Types

• The smoother a surface, the more reflected light is concentrated in the direction

• A very rough surface scatters light in all directions

ILE5014 Computer Graphics 10F 14

Phong Reflection (Lighting) ModelPhong Reflection (Lighting) Model

• A simple model that can be computed rapidly• Has three components

– Ambient

– Diffuse

– Specular

• Uses four vectors

– To source l

– To viewer v

– Normal n

– Perfect reflector rr = 2 (l · n ) n - l

ILE5014 Computer Graphics 10F 15

Ambient LightAmbient Light

• The result of multiple interactions between (large) light sources and the objects in the environment.

• Ia = kaLa

ambient light intensity

ILE5014 Computer Graphics 10F 16

Diffuse ReflectionDiffuse Reflection

• Light scatters equally in all directions

• Reflected intensities vary with the direction of the light.

• Lambertian Surface

– Perfect diffuse reflector

• Id = kd (l·n) Ld

ILE5014 Computer Graphics 10F 17

specularhighlight

Specular SurfacesSpecular Surfaces

• Most surfaces are neither perfectly diffuse nor perfectly specular (ideal reflectors)

• Incoming light reflects in concentrated directions close to the direction of a perfect reflection

ILE5014 Computer Graphics 10F 18

• Phong model

Is = ks Ls (r·v)α

= ks Ls cosαΦ

shininess coefficient

absorption coefficient

incoming intensityreflectedintensity

Modeling Specular RelectionsModeling Specular Relections

ILE5014 Computer Graphics 10F 19

The Shininess Coefficient αThe Shininess Coefficient α

• Values of α between 100 and 200 correspond to metals

• Values between 5 and 10 give surfaces that look like plastic

ILE5014 Computer Graphics 10F 20

Distance TermsDistance Terms

• Account for attenuation

• Original: Inversely proportional to the square of the distance d between light source and surface

• Modified: 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

ILE5014 Computer Graphics 10F 21

diffuse diffuse +

ambient

diffuse +

specular

Adding up the ComponentsAdding up the Components

• A primitive virtual world with lighting can be shaded by combining the three light components:

• I = Iambient + Idiffuse + Ispecular

= kaLa + kd (l · n) Ld + ks Ls (v · r )α

ILE5014 Computer Graphics 10F 22

CoefficientsCoefficients

• 3 color channels and 3 surface types

• 9 coefficients for each point light source

– Lra, Lrd, Lrs, Lga, Lgd, Lgs, Lba, Lbd, Lbs

• Material properties

– 9 absorption coefficients

• kra, krd, krs, kga, kgd, kgs, kba, kbd, kbs

– Shininess coefficient α

ILE5014 Computer Graphics 10F 23

Modified Phong ModelModified Phong Model

• Problem: In the specular component of Phong model, it requires the calculation of a new reflection vector and view vector for each vertex

• Blinn suggested an approximation using the halfway vector that is more efficient

r = 2 (l · n ) n - l

ILE5014 Computer Graphics 10F 24

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

The Halfway VectorThe Halfway Vector

• h is the normalized vector halfway between l and v

ILE5014 Computer Graphics 10F 25

2

n

l r

vθ θ Φ

β Θ-β

h

Using the Halfway AngleUsing the Halfway Angle

• Replace (v · r )α by (n · h )b

• b is chosen to match shininess

• Note that halfway angle is half of angle between r and v if vectors are coplanar

ILE5014 Computer Graphics 10F 26

Blinn lighting modelBlinn lighting model

• Resulting model is known as the modified Phong or Blinn lighting model

• Specified in OpenGL standard and most real-time applications

ILE5014 Computer Graphics 10F 27

Teapots with different parameters.

ExampleExample

ILE5014 Computer Graphics 10F 28

What’s next?What’s next?

• So far, we know how to compute the color at a point given lighting model, illumination model, and surface normals

• The required computation can be huge if we compute for every point on a surface

• Exploit efficiency by decomposing curved surfaces into many small flat polygons

ILE5014 Computer Graphics 10F 29

Polygonal ShadingPolygonal Shading

• Curved surfaces are approximated by polygons

• How do we shade?

– Flat shading

– Interpolative shading

– Gouraud shading

– Phong shading (not Phong reflection model)

• Two questions:

– How do we determine normals at vertices?

– How do we calculate shading at interior points?

ILE5014 Computer Graphics 10F 30

Flat Shading:Flat Shading:

• Normal: given explicitly before vertex

• Shading constant across polygon

• Single polygon: first vertex

• Triangle strip: vertex n+2 for triangle n

glShadeModel(GL_FLAT);glNormal3f(nx, ny, nz);glVertex3f(x, y, z);

ILE5014 Computer Graphics 10F 31

Flat Shading AssessmentFlat Shading Assessment

• Inexpensive to compute

• Appropriate for objects with flat faces

• Less pleasant for smooth surfaces

ILE5014 Computer Graphics 10F 32

Flat Shading and PerceptionFlat Shading and Perception

• Lateral inhibition: exaggerates perceived intensity

• Mach bands: perceived “stripes” along edges

ILE5014 Computer Graphics 10F 33

ILE5014 Computer Graphics 10F 34

Interpolative ShadingInterpolative Shading

• Enable with glShadeModel(GL_SMOOTH);

• Calculate color at each vertex

• Interpolate color in interior

• Compute during scan conversion (rasterization)

• More expensive to calculate

ILE5014 Computer Graphics 10F 35

Gouraud ShadingGouraud Shading

• Special case of interpolative shading

• How do we calculate vertex normals?

• Gouraud: average all adjacent face normals

• Requires knowledge

about which faces share

a vertex—adjacency info

ILE5014 Computer Graphics 10F 36

Gouraud Shading AlgorithmGouraud Shading Algorithm

• Find average normal at each vertex

• Apply Phong reflection model at each vertex

• Linearly interpolate vertex shades (colors) across each polygon I1(x1, y1)

Ib(xb, ys)

I2(x2, y2)

I3(x3, y3)

Ia(xa, ys)

I4(x4, y4)

Is(xs, ys)

asbsbaab

s

ssb

ssa

xxIxxIxx

I

yyIyyIyy

I

yyIyyIyy

I

1

1

1

144141

122121

ILE5014 Computer Graphics 10F 37

Problem with Gouraud ShadingProblem with Gouraud Shading

• See the artifact of specular highlight on a rotating sphere

– Highlight lies inside a polygon will not be apparent

– Highlight occurs at vertex will be spread unnaturally across all neighboring polygons

ILE5014 Computer Graphics 10F 38

Phong ShadingPhong Shading

• Interpolate normals rather than colors

• Significantly more expensive

• Not supported in OpenGL

• Can be implemented using pixel shader

ILE5014 Computer Graphics 10F 39

Phong Shading AlgorithmPhong Shading Algorithm

• Find vertex normals

• Interpolate vertex normals across edges

• Interpolate edge normals across each line

• Compute shades at each pixels of a polygon