Phong Illumination Model

A local illumination model EECS 487: Interactive • one bounce: light → surface → viewer

Lighting a single point Computer Graphics At the point: n Lecture 16: : surface normal (orientation of surface) l: light vector (surface to light) n • Phong Illumination Model v: viewing vector (surface to eye) ϕ • θ: light angle of incidence θ l ϕ: viewing angle v

Phong Illumination Model Surface Reflection Coefficients Approximate surface color as Light sources: sum of three components: Approximate BRDFs: not physically based sd diffuse light intensity • an ideal diffuse component ss specular light intensity • a glossy/blurred specular component Modify surface color by changing reflection sa ambient light intensity • an ambient term coefficients based on: • material type m ambient color reflection • surface finish a m diffuse color reflection + = • texture maps d • what looks good ms specular color reflection

• artistic license mshi shininess (blurriness) • trial and error me emissive color intensity • personal library + = Diffuse Example Ideal Diffuse Reflectance Ideal diffuse surface reflects light equally in all directions, according to Lambert's cosine law: • amount of light energy that falls on surface and gets Where is the light? reflected is proportional to the incidence angle, θ • perceived brightness (reflectance) is view independent Where’s the normal direction at the brightest point? θ

Durand

Ideal Diffuse Reflectance Ideal Diffuse Model Amount of light energy that falls At the microscopic level, an ideal on surface is proportional to diffuse surface is a very rough surface incidence angle, θ: • microfacets are oriented in any which way cd = sd md cos θ • examples: chalk, clay, surface of moon Curless For normalized n and l: c = s m max((n•l), 0) d d d RTR2 • (n•l) < 0 cos θ < 0 θ > 90º, light source is behind surface • why must n and l be normalized? l n u·v θ u·v = u v cosθ ⇒ cosθ = u v Durand Diffuse Reflectance and Viewing Angle Ideal Specular/Mirror Reflectance

Lambert’s Cosine Law: amount of light energy that falls on Accounts for highlight seen on objects surface and gets reflected is proportional to incidence angle, θ i with smooth, shiny surfaces, such as: • cos θi = dot product of light vector with normal (both normalized) • metal • polished stone Shouldn’t the amount of energy reflected also be proportional • plastics to viewing angle, θ ? r • apples • no, whereas larger θi means • n skin energy arriving over area Recall: dA cos θi is spread across the sin (π/2 – θ) = cos θ larger area dA, larger θr simply means collecting energy from n the same area dA, but channelling it through the smaller area dA cos θ dA cos θ r θ Surface 2 π/2 − θ Surface 1

dA TP3, FvD94 Curless,Zhang

n Ideal Specular/Mirror Reflectance Phong Specular Reflection r

l v Reflection only at mirror angle Simulates a highlight θ θ φ • highlight intensity depends on viewing direction Reflection angle = incidence angle = θ Most intense specular reflection when v = r Model: all microfacets of mirror surface are oriented in the same How to compute r? direction as the surface itself • examples: mirrors, highly polished r = −l + 2(n•l)n n (in OpenGL, specular term is 0 if l • n = 0) metals r l 2(n ⋅ l)n (n ⋅ l)n

-l Durand Glossy Reflectors Glossy Real materials tend to deviate significantly from ideal Reflectors mirror reflectors ⇒ highlight and reflections are blurry Simple empirical model: Also known as: “rough specular”, “directional diffuse”, • we expect most of the reflected light to travel in or “glossy” reflection the direction of the ideal reflection ray • but due to variations in microfacet orientations, (there are no ideal diffuse surfaces either …) some of the light will be reflected just slightly off from the ideal reflected direction • as we angle away from the reflected ray, we expect to see less light reflected

Durand Durand

Surface Roughness Phong “Glossy” Reflection Model

Schlick As v angles away from r, specular reflection falls specular diffuse off, simulating “glossy” reflection:

Glossy Surface Less Shiny Surface Diffuse Surface Perfect Mirror v v increasing roughness

TP3 less glossy/ more glossy/blurry more specular Phong “Glossy” Reflection Model Material Glossiness

Phong specular model: To account for the shininess of different material: mshi cs = ss ms max((r • v), 0) cs = ss ms cos φ = ss ms max((r • v), 0) larger mshi gives tighter and shinier highlight, with sudden dropoff, simulating a more mirror-like surface cos φ

v n r r l v intensity φ specular

angle

mshi: RTR3

Phong Specular Reflection Blinn-Phong (Blinn-Torrance) Model

larger mshi , tighter highlight Back to micro facets:

• model surface by a collection of tiny mirrors

• specular reflectance comes from mirrors oriented halfway between l and v, in direction of h , shinier s

m l n h v ω ω larger

TP3 Manocha Blinn-Phong Illumination Models

h A rendering process can be modeled as an integral Model specular reflection with “halfway” l l equation representing the transport of light through vector (h) instead of r v the environment the rendering equation • consider the microfacet through point p that p Local illumination: an approx. of the rendering eqn. reflects light perfectly to the viewer • assumes light is scattered only once: light from light source is reflected by a surface and modulated on its way towards • h is the normal of this plane, it is halfway l + v h = the eye between l and v (by definition), h normalized l + v :

• angle between h and n is β; v = r when h = n • light rays traveling from light to surface may be v r n h (β is not the angle between and ) l r • blocked by intervening surfaces (shadows) or • specular reflection modeled as: β v • bent or scattered by intervening material (refraction and atmospheric 4m 4m effects) or c = s m (n • h) shi = s m (cos β) shi s s s s s p • light arriving at a surface may come indirectly via another surface (reflection and color bleed)

Phong Ambient Term Putting It All Together Phong Illumination Model “Approximates” the contribution of all indirect m c = m s + (m (n⋅l) + m (r ⋅v) shi ) f (d)s illumination t a a d s Phong ρa ρd ρs ρtotal Surface uniformly lit θ = 60º • areas with no direct illumination are not completely dark • independent of: θ = 25º • light direction • surface normal • viewing angle

θ = 0º Spheres rendered with ambient reflection only

Durand OpenGL Light Sources OpenGL Lighting and Reflectance glLightfv(lightname,param,value) /* Initialize material property, light source, • parameters lighting model, and depth buffer. */ • GL_AMBIENT void init(void) • GL_DIFFUSE { • GL_SPECULAR GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; • GL_POSITION GLfloat mat_shininess[] = { 50.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; • GL_SPOT_DIRECTION glClearColor(0.0, 0.0, 0.0, 0.0); • GL_SPOT_CUTOFF glShadeModel(GL_SMOOTH); • GL_SPOT_EXPONENT glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); • GL_CONSTANT_ATTENUATION glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); • GL_LINEAR_ATTENUATION • GL_QUADRATIC_ATTENUATION glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHTING); Turning on the lights: glEnable(GL_LIGHT0); • glEnable(GL_LIGHTING) glEnable(GL_DEPTH_TEST); }

• glEnable(GL_LIGHT0)

OpenGL Material Properties Choosing the Parameters

glMaterial(face,param,value) Experiment with different parameter settings • face • GL_FRONT A few suggestions: • GL_BACK • ma + md + ms < 1 • GL_FRONT_AND_BACK • use a small m (~0.1) • parameters a • GL_AMBIENT • try mshi ∈ [0, 100] • GL_DIFFUSE

• GL_AMBIENT_AND_DIFFUSE TP3 • GL_SPECULAR • GL_SHININESS material m m m • GL_EMISSION d s shi See Redbook Chapter 5 for techniques to minimize metal small, color of metal large, color of metal large performance costs associated medium, color of medium, color of plastic medium with changing material object light (or white) properties planet varying 0 0

Curless Highlight Color Emissive Term For non-metals, e.g., plastics, highlight color is color Material’s emissive parameter me of light (plastics has a transparent/white coating) • assumed not lighting other objects

For metals, e.g., brass, highlight depends on surface m c = m + m s + (m (n⋅l) + m (v ⋅r) shi ) f (d)s color t e a a d s

Surface

plastic metal clay

Apodaca&Gritz Funkhouser

Attenuation Model Global Ambient and Multiple Lights Whereas the ambient term simulates indirect lighting, attenuation model and fog simulate scattering effect Global ambient light source: cg = sg ma • sg set with: Intensity attenuation: light falls off the further away glLightModel*(GL_LIGHT_MODEL_AMBIENT,ambient_light) one gets from the source • distinguishes overlapping surfaces Multiple lights: n having the same reflection parameters (k) (k) (k) (k) 2 c = c + m + c + f (d )(c + c ), • radiant energy disperses as 1/d d t g e ∑ a d s 1 k=1 • d is the distance from the light source to surface k:!light!number,!not!exponentiation • reason for the inverse square falloff? d 2 ! 2 • attenuation function f (d) = 1/(a0+a1d+a2d ) • user defined constants a0 , a1 , a2 • since we’re not modeling atmospheric scattering, 1/d 2 often appears too harsh

• instead, we use f (d) = 1/(a0+a1d) or no attenuation Lighting with attenuation: c’ = f (d )c Fog Fog Simple atmospheric effect f :!fog!factor!at!point!p color of fog: cf p • a little better realism color of surface: cs zend − zstart : the!foggy!range • fog effect: help to convey distances d f : fog!density cp = f cs + (1 − f ) cf , f ∈ [0,1] ! • 2 forms of depth cueing (ignoring scattering) • light-to-object distance conveyed by light attenuation How to compute f ? • object-to-viewer atmospheric scattering simulated by fog zend − zp • linear: f = p z − z end start

−d f zp • exponential: fp = e RTR3 • exponential-squared: 2 −(d f zp ) fp = e

can also be a function of height off the ground radial- or range-based, not simply z-based Akenine-Möller02

Fog in OpenGL Lighting and Shading Lighting: computing interaction between light and surfaces of glEnable(GL_FOG); different materials, and interaction with the geometry of glHint(GL_FOG_HINT,GL_[NICEST|FASTEST]); objects to determine the luminous intensity reflected from a specified 3D point // per fragment or per vertex glFog*(GL_FOG_MODE, GL_[LINEAR|EXP|EXP2]); Shading: performing the lighting computations on polygonal glFog*(GL_FOG_[START|END|DENSITY], param); objects and coloring the pixels glFog*(GL_FOG_COLOR, param); flat shading Phong shading

To specify a fog depth for a given vertex: glFog*(GL_FOG_COORD_SRC, GL_FOG_COORD); glFogCoord*(z); glVertex(…);

RTR3 Problem with Flat Shading: Flat Shading Mach Band Illusion Entire polygon displayed with the same intensity • calculate intensity from the reflection model In 1865, Mach discovered that • use the surface normal (for triangles) human eyes perceive a surface • compute an average normal (for >3 vertex polygons) with flat shading of different hues as having a “fluted” aspect Is a valid shading model when: due to the abrupt change in • object is truly planar shades of color (discontinuity in Mach Bands (not an approximation of a curved surface) the first derivative of the shading function) • all k light sources are far enough away that measured signal (k) n • l is constant over the polygon surface • viewing position is far enough away that n • h is constant over the polygon surface perceived signal

Riesenfeld06

Gouraud Shading Lateral Inhibition Match intensity across polygon edges Mach Band Illusion is due to lateral inhibition of Linear interpolation of color across polygon surface the human visual system: • neighboring receptors are connected Gouraud shading algorithm:

• when one fires, it inhibits its neighbors from firing •determine average normal at each vertex (averaged over the normals of all polygons that share the vertex) • compute color at each vertex using the average normal Effect: • linearly interpolate color across a single polygon surface • eye sensitive to difference from surrounding area (GL_SMOOTH does only the last step! GL_FLAT colors polygon with the color of the last vertex) n • good for edge and motion detection n ∑ i n = i=1 avg n n ∑ i i=1 n = all polygons that share the vertex

Riesenfeld06 Gouraud Shading Example Mach Band not Eliminated when there’s an abrupt change in the orientation of two polygons and there’s discontinuity in the 1st derivative of 1. Compute average normal at each vertex (1, 2, 3) 2. Compute colors c , c , c the shading function 1 2 3 3. Compute colors c4, c5 3 y - y y - y c = 4 2 c + 1 4 c 4 y - y 1 y - y 2 1 2 1 2 1 p scanline 4. Compute color cp x - x x - x 4 5 5 p p 4 2 c p = c4 + c5 x5 - x4 x5 - x4

Or use barycentric coordinates! Steps 2-4 were cheap enough to be implemented in hardware even for fixed pipeline GPU effect can be ameliorated with increased polygon count or Phong shading …

Limitation: Wrong Highlights Limitation: Wrong Highlights Highlights depend on polygonal shape Why does Gouraud give inaccurate simulation of highlights? Does not capture highlight in the • color at vertex may not be (highlight) color inside the polygon middle of polygon What would give the most accurate simulation of highlights? • c1 = 0 because (n•h) < 0 • compute normal of actual surface (not polygon-approximated • c2 = 0 because (n•l) < 0 surface) at each pixel and color accordingly very expensive! • any interpolation of c1 and c2 will be 0

c1 = 0 c2 = 0 area of desired Phong shading: interpolate normal of vertices across polygon highlight surface and color each pixel according to interpolated normals Shows up in as flashing highlights between frames as orientation of polygons change n3 Phong Shading OpenGL Lighting and Reflectance n1 Algorithm: scanline /* Initialize material property, light source, n lighting model, and depth buffer. */ • determine average normal at each vertex n 2 void init(void) • linearly interpolate per-pixel normal y - y y - y  2 1 { n = n1 + n2 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; across surface y1 - y2 y1 - y2 GLfloat mat_shininess[] = { 50.0 }; • compute color for each pixel using the GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; value of the approximated per-pixel normal glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_SMOOTH); // or GL_FLAT // interpolate color of vertices, but does not // average normals at the vertices

glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);

Phong illumination model ≠ glLightfv(GL_LIGHT0, GL_POSITION, light_position);

Phong specular reflection ≠ c = 0 c = 0 glEnable(GL_LIGHTING); Phong shading 1 2 glEnable(GL_LIGHT0); area of desired glEnable(GL_DEPTH_TEST); highlight }

Where in the Pipeline?

Application vertex colors computed Vertex Processing

vertex colors Primitive Processing interpolated across polygon (Gouraud shading) Rasterization

Fragment Processing

per-pixel Framebuffer Phong shading (expensive)

Display