virtual realism lighting and shading. lighting & shading approximate physical reality ray...

37
CSE 410 Computer Graphics Sessional Virtual Realism LIGHTING AND SHADING

Upload: iris-sabina-farmer

Post on 16-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

CSE 410 Computer Graphics Sessional

Virtual Realism

LIGHTING AND SHADING

Page 2: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Lighting & Shading

Approximate physical realityRay tracing:

Follow light rays through a scene Accurate, but expensive (off-line)

Radiosity: Calculate surface inter-reflection approximately Accurate, especially interiors, but expensive (off-line)

Phong Illumination model (this lecture): Approximate only interaction light, surface, viewer Relatively fast (on-line), supported in OpenGL

Page 3: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Geometric IngredientsThree ingredients

Normal vector m at point P of the surfaceVector v from P to the viewers eyeVector s from P to the light source

m

s

v

P

Page 4: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Types of Light Sources

Ambient light: no identifiable source or directionDiffuse light - Point: given only by pointDiffuse light - Direction: given only by directionSpot light: from source in direction

Cut-off angle defines a cone of light Attenuation function (brighter in center)

Light source described by a luminance Each color is described separately I = [I r I g I b ] T (I for intensity) Sometimes calculate generically (applies to r, g, b)

Page 5: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Ambient Light

Global ambient light Independent of light source Lights entire scene

Local ambient light Contributed by additional light sources Can be different for each light and primary color

Computationally inexpensive

Page 6: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Diffuse LightPoint Source

Given by a point Light emitted equally in all directions Intensity decreases with square of distance Point source [x y z 1]T

Directional Source Given by a direction Simplifies some calculations Intensity dependents on angle between

surface normal and direction of light Distant source [x y z 0]T

Page 7: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Spot Lights

Spotlights are point sources whose intensity falls off directionally. Requires color, point

direction, falloffparameters

d

P

αβ

Intensity at P = I cosε(β)

Page 8: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

This model is based on modeling surface reflection as a combination of the following

components:

Used to model objects that glow

A simple way to model indirect reflection

The illumination produced by dull smooth surfaces

The bright spots appearing on smooth shiny surfaces

Phong illumination model

Page 9: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Diffuse Reflection Ideal diffuse reflection An ideal diffuse reflector, at the microscopic level, is

a very rough surface (real-world example: chalk) Because of these microscopic variations, an incoming

ray of light is equally likely to be reflected in any direction over the hemisphere

What does the reflected intensity depend on?

Page 10: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Computing Diffuse Reflection Independent of the angle between m and v Does depend on the direction s (Lambertian surface)

ms

ms diffusesourcediffuse II

)0,max(ms

ms diffusesourcediffuse II

Diffuse Reflection Coefficient

Adjustment for ‘inside’ face

)cos(diffusesourcediffuse II

Therefore, the diffuse component is:

Page 11: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Specular Reflection Shiny surfaces exhibit specular reflection Polished metal Glossy car finish

A light shining on a specular surface causes a bright spot known as a specular highlight Where these highlights appear is a function of the viewer’s position, so specular reflectance is view dependent

Page 12: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Specular Reflection

Perfect specular reflection (perfect mirror) The smoother the surface, the closer it becomes

to a perfect mirror Non-perfect specular reflection: Phong Model most light reflects according to Snell’s Law as we move from the ideal reflected ray, some light is

still reflected

Page 13: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Non-Ideal Specular Reflectance: Phong Model

An illustration of this angular falloff

θ

ms

r

Page 14: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Phong Lighting

θ

ms

r

The Specular Intensity, according to Phong model:

)(cos fspecularsourcespecular II

Specular Reflection Coefficient

Shininess factor

f

specularsourcespecular II

vr

vr

Page 15: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Phong Lighting Examples

These spheres illustrate the Phong model as s and f are varied:

Page 16: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Blinn and Torrence VariationIn Phong Model, r need to be found

computationally expensiveInstead, halfway vector h = s + v is used

angle between m and h measures the falloff of intensity

β

ms h

v

f

specularsourcespecular II

mh

mh

Page 17: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Combining Everything

Simple analytic model: diffuse reflection +specular reflection +ambient

Surface

Page 18: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

The Final Combined Equation

Single light source:

m

sr

v

Viewer

φqq

fsspddaa phongIlambertIII )(

ms

ms,0maxlambert

mh

mh,0maxphong

Page 19: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Adding ColorConsider R, G, B components individuallyAdd the components to get the final color of

reflected light

fsrsprdrdrarar phongIlambertIII )(

fsgspgdgdgagag phongIlambertIII )(

fsbspbdbdbabab phongIlambertIII )(

Page 20: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Shading Models

Page 21: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Applying Illumination

We have an illumination model for a point on a surface

Assuming that our surface is defined as a mesh of polygonal facets, which points should we use?

Page 22: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Polygon ShadingTypes of Shading Model

Flat Shading

Gouraud Shading

Phong Shading

Smooth Shading

Page 23: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Flat ShadingFor each polygon

Determines a single intensity value

Uses that value to shade the entire polygon

AssumptionsLight source at infinityViewer at infinityThe polygon represents the

actual surface being modeled

Page 24: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Wire-frame Model

Flat Shading

Flat Shading

Page 25: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Smooth ShadingIntroduce vertex normals at each

vertexUsually different from facet normalUsed only for shadingThink of as a better approximation of the real

surface that the polygons approximateTwo types

Gouraud ShadingPhong Shading (do not confuse with Phong

Lighting Model)

Page 26: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Gouraud Shading

This is the most common approach Perform Phong lighting at the vertices Linearly interpolate the resulting colors over

faces Along edges Along scanlines

Page 27: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Gouraud Shading

xright

ys

ytop

ybott

xleft

color1

color2

color3

color4y4

bott

bottsleft yy

yycolorcolorcolorcolor

4

141

bott

bottsright yy

yycolorcolorcolorcolor

2

121

rightleft

leftleftrightleftx xx

xxcolorcolorcolorcolor

Page 28: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Wire-frame Model

Gouraud Shading

Flat ShadingGouraud Shading

Page 29: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Gouraud Shading

ArtifactsOften appears dullLacks accurate specular component

If included, will be averaged over entire polygon

C1

C2

C3

Can’t shade the spot light

Page 30: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Phong Shading

ys

x

m1

m2

m3

m4 mleftmright

m

Interpolate normal vectors at each pixel

Page 31: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Wire-frame Model

Phong Shading

Flat ShadingGouraud ShadingPhong Shading

Page 32: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

If a highlight does not fall on a vertex Gouraud shading may miss it completely,

but Phong shading does not.

Phong vs Gouraud Shading

Page 33: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Shading Models (Direct lighting)

Flat ShadingCompute Phong lighting once for entire

polygonGouraud Shading

Compute Phong lighting at the vertices and interpolate lighting values across polygon

Phong ShadingInterpolate normals across polygon and

perform Phong lighting across polygon

Page 34: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Lighting in OpenGL

Page 35: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Lighting in OpenGL [1/2]

Enabling shadingglShadeModel(GL_FLAT)glShadeModel(GL_SMOOTH); // Gouraud

Shading onlyUsing light sources

Up to 8 light sourcesTo create a light

GLfloat light0_position[] = { 600, 40, 600, 1.0}; glLightfv(GL_LIGHT0, GL_POSITION,

light0_position); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING);

Page 36: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

Lighting in OpenGL [2/2]

Changing light properties GLfloat light0_ambient[] = { 0.4, 0.1, 0.0, 1.0 }; GLfloat light0_diffuse[] = { 0.9, 0.3, 0.3, 1.0 }; GLfloat light0_specular[] = { 0.0, 1.0, 1.0, 1.0 }; glLightfv(GL_LIGHT0, GL_AMBIENT,

light0_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR,

light0_specular);

For more detailSee Red Book (Ch 5)

Page 37: Virtual Realism LIGHTING AND SHADING. Lighting & Shading Approximate physical reality Ray tracing: Follow light rays through a scene Accurate, but expensive

References

Hill § 8.1 ~ 8.3