1 L Feb-22-05 SMM009, Shading Overview Theory - Shading for 3D - - PDF document

1 l
SMART_READER_LITE
LIVE PREVIEW

1 L Feb-22-05 SMM009, Shading Overview Theory - Shading for 3D - - PDF document

INSTITUTIONEN FR SYSTEMTEKNIK LULE TEKNISKA UNIVERSITET Shading David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E. Angel 1 L Feb-22-05 SMM009, Shading Overview Theory - Shading for 3D appearance -


slide-1
SLIDE 1

1

Feb-22-05 SMM009, Shading 1 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Shading

David Carr Virtual Environments, Fundamentals Spring 2005

Based on Slides by E. Angel

Feb-22-05 SMM009, Shading 2 L

Overview

  • Theory
  • Shading for 3D appearance
  • Light-material interactions
  • The Phong model
  • OSG
  • OSG methods
  • Gouraud Shading

Feb-22-05 SMM009, Shading 3 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Shading Theory

slide-2
SLIDE 2

2

Feb-22-05 SMM009, Shading 4 L

Why We Need Shading

  • Model of a sphere using many polygons and color it

with a single color:

  • We get something like
  • But we want

Feb-22-05 SMM009, Shading 5 L

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

Feb-22-05 SMM009, Shading 6 L

Scattering

  • Light strikes A
  • Some scattered
  • Some absorbed
  • Some of scattered light strikes B
  • Some scattered
  • Some absorbed
  • Some of this scattered

light strikes A and so on

slide-3
SLIDE 3

3

Feb-22-05 SMM009, Shading 7 L

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

Feb-22-05 SMM009, Shading 8 L

Global Effects

translucent surface shadow multiple reflection

Feb-22-05 SMM009, Shading 9 L

Local versus Global Rendering

  • Correct shading requires:
  • 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”

  • Many techniques for approximating global effects
slide-4
SLIDE 4

4

Feb-22-05 SMM009, Shading 10 L

Light-Material Interaction

  • Light that strikes an object is:
  • Partially absorbed and
  • Partially scattered (reflected)
  • The amount reflected determines the color and brightness
  • f 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
  • n the smoothness and orientation of the surface

Feb-22-05 SMM009, Shading 11 L

Light Sources

  • General light sources are difficult to work with because

we must integrate light coming from all points on the source

Feb-22-05 SMM009, Shading 12 L

Simple Light Sources

  • Point source
  • Model with position and color
  • 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

slide-5
SLIDE 5

5

Feb-22-05 SMM009, Shading 13 L

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

Feb-22-05 SMM009, Shading 14 L

Phong Model

  • A simple model that can be computed rapidly
  • Has three components
  • Diffuse
  • Specular
  • Ambient
  • Uses four vectors
  • To source
  • To viewer
  • Normal
  • Perfect reflector

Feb-22-05 SMM009, Shading 15 L

r = 2 (l · n ) n - l

Ideal Reflector

  • Normal is determined by local orientation
  • Angle of incidence = angle of reflection
  • The three vectors must be coplanar
slide-6
SLIDE 6

6

Feb-22-05 SMM009, Shading 16 L

Solving for the Reflection Vector

  • Note, let l, n, r be unit vectors
  • The angle of incidence = the angle of reflection
  • The reflection is co-planer with the normal and the vector to the light source.
  • Two equations and 2 unknowns
  • From the 1st, θi = θr, so cosθi = cosθr,

thus, l · n = r · n

  • From the 2nd, r is a linear combination of n, l

thus, r = αl + βn

  • Take the above and dot with n, solve for β

r · n = αl · n + β l · n = αl · n + β β = (α-1)(l · n )

  • But, r is of unit length, so 1 = r· r = α2 + 2αβl· n + β2
  • Solving gives r = 2(l· n)n - l

Feb-22-05 SMM009, Shading 17 L

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

Feb-22-05 SMM009, Shading 18 L

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 normalized
  • There are also three coefficients, kr, kb, kg that show how

much of each color component is reflected

slide-7
SLIDE 7

7

Feb-22-05 SMM009, Shading 19 L

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

  • f a perfect reflection

Specular highlight

Feb-22-05 SMM009, Shading 20 L

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 coefficient absorption coefficient incoming intensity reflected intensity

Feb-22-05 SMM009, Shading 21 L

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

  • 90
slide-8
SLIDE 8

8

Feb-22-05 SMM009, Shading 22 L

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 coefficient intensity of ambient light

Feb-22-05 SMM009, Shading 23 L

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 the

form 1/(a + bd +cd2) to the diffuse and specular terms

  • The constant and linear terms soften

the effect of the point source

Feb-22-05 SMM009, Shading 24 L

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
  • Idr, Idg, Idb, Isr, Isg, Isb, Iar, Iag, Iab
slide-9
SLIDE 9

9

Feb-22-05 SMM009, Shading 25 L

Material Properties

  • Material properties match light source properties
  • Nine absorption coefficients

+kdr, kdg, kdb, ksr, ksg, ksb, kar, kag, kab

  • Shininess coefficient α

Feb-22-05 SMM009, Shading 26 L

Adding up the Components

  • For each light source and each color component, the

Phong model can be written (without the distance terms) as I =kd Id l · n + ks Is (v · r )α + ka Ia

  • For each color component

we add contributions from all sources

Feb-22-05 SMM009, Shading 27 L

Example

  • Only differences in

these teapots are the parameters in the Phong model

slide-10
SLIDE 10

10

Feb-22-05 SMM009, Shading 28 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Shading in OSG

Feb-22-05 SMM009, Shading 29 L

Steps in OSG Shading

  • Specify material properties
  • setDiffuse,setSpecular, setAmbient

+ Take a 4D vector, RGBα + For RGB 1.0=100% reflectivity, 0.0=0% + For α, 1.0=opaque, 0.0=transparent

  • setShininess, bigger gives smaller specular highlights
  • Specify normal vectors
  • Default for OSG primitive objects
  • In OSG the normal vector is part of the drawable
  • Usually bound per vertex

Feb-22-05 SMM009, Shading 30 L

Steps in OSG Shading

  • Specify lights, point sources
  • setLightNum, different for each source, limited but at least 8
  • setPosition, 4D vector,

w=1 for finite distance, 0 for infinite

  • setDirection, 3D vector pointing in light’s direction
  • setDiffuse, setSpecular, setAmbient

+ 4D vectors RBGα, α is used for blending

  • Construct StateSet for
  • Materials, attached to objects (drawables, Geodes)
  • Root, gets the lights
  • Stick the lights in a LightSource and enable

LightSource.setStateSetModes();

  • See LightDemo.java
slide-11
SLIDE 11

11

Feb-22-05 SMM009, Shading 31 L

Video with Commentary

Feb-22-05 SMM009, Shading 32 L

Notes on Lights

  • Spotlights
  • Model cone
  • Variable intensity
  • Proportional to cosaf
  • 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

θ −θ

φ

Feb-22-05 SMM009, Shading 33 L

Moving Light Sources

  • Light sources are geometric objects whose positions or

directions are affected by their position in the scene graph

  • Depending on where we place them, 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
slide-12
SLIDE 12

12

Feb-22-05 SMM009, Shading 34 L

Front and Back Faces

  • The default is shade only front faces which works correct for

convex objects

  • If we set two sided lighting, OSG will shaded both sides of a

surface

  • Each side can have its own properties which are set by using

MATERIALFace.FRONT_AND_BACK, MATERIALFace.FRONT, MATERIALFaceBACK when setting the material

back faces not visible back faces visible

Feb-22-05 SMM009, Shading 35 L

Notes on Materials

  • Emissive Term
  • We can simulate a light source in OSG by giving a material

an emissive component

  • This color is unaffected by any sources or transformations
  • Transparency
  • Material properties are specified as RGBα values
  • The α value can be used to make the surface translucent
  • The default is that all surfaces are opaque regardless of α
  • Later we will enable blending and use this feature

Feb-22-05 SMM009, Shading 36 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Other Aspects of Shading

slide-13
SLIDE 13

13

Feb-22-05 SMM009, Shading 37 L

Polygonal Shading

  • Shading calculations are done for each vertex
  • Vertex colors become vertex shades
  • By default, vertex colors are interpolated across the

polygon

  • gl.glShadeModel(GL.GL_SMOOTH);
  • If we use gl.glShadeModel(GL.GL_FLAT); the

color at the first vertex will determine the color of the whole polygon

Feb-22-05 SMM009, Shading 38 L Feb-22-05 SMM009, Shading 39 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Gouraud Shading

slide-14
SLIDE 14

14

Feb-22-05 SMM009, Shading 40 L

Polygon Normal Vectors

  • Polygons have a single normal
  • Shades at the vertices as computed by the Phong model can

be almost same

  • Identical for a distant viewer (default)
  • r if there is no specular component
  • Consider model of sphere
  • Want different normal vectors at

each vertex even though this concept is not quite correct mathematically

Feb-22-05 SMM009, Shading 41 L

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
  • Note silhouette edge

Feb-22-05 SMM009, Shading 42 L

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 normal vectors around a mesh vertex

| n | | n | | n | | n | n n n n n

4 3 2 1 4 3 2 1

+ + + + + + =

slide-15
SLIDE 15

15

Feb-22-05 SMM009, Shading 43 L

Gouraud and Phong Shading

  • Gouraud Shading
  • Find average normal at each vertex (vertex normal vectors)
  • Apply Phong model at each vertex
  • Interpolate vertex shades across each polygon
  • Phong shading
  • Find vertex normal vectors
  • Interpolate vertex normal vectors across edges
  • Find shades along edges
  • Interpolate edge shades across polygons

Feb-22-05 SMM009, Shading 44 L

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

  • Usually not available in real time systems
  • Both need data structures to represent meshes so we

can obtain vertex normal vectors

Feb-22-05 SMM009, Shading 45 L

Questions?