72
Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Embed Size (px)

Citation preview

Page 1: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Illumination and ShadingAngel, Chapter 6

slides from AW, etc.

CSCI 6360/4360

Page 2: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Overview

• Photorealism and complexity– Light interactions with a solid

• Rendering equation– “infinite scattering and absorption of light”– Local vs. global illumination

• Local techniques– Flat, Gouraud, Phong

• An illumination model– “describes the inputs, assumptions, and outputs that we will use to calculate

illumination of surface elements”– Light– Reflection characteristics of surfaces

• Diffuse reflection, Lambert’s law, specular reflection– Phong approximation – interpolated vector shading

Page 3: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Angel on Speed for Interactivity

• “… our goal is to create realistic shading effects in as close to real time as possible, rather than trying to model the BRDF as accurately as possible. Hence, we use tricks with a local model to simulate effects that can be global in nature.”

– P. 298

• “Because we cannot solve the full rendering equation, we instead use various tricks in an attempt to obtain realistic renderings, …”

– P. 307

• Good enough … for “fast” (interactive) graphics– Things change with time– Moore’s law

Page 4: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Photorealism and Complexity• Recall, from 1st lecture … examples below exhibit range of “realism”

• In general, trade off realism for speed – interactive computer graphics– Wireframe – just the outline– Local illumination models, polygon based

• Flat shading – same illumination value for all of each polygon• Smooth shading (Gouraud and Phong) – different values across polygons

– Global illlumination models• E.g., Raytracing – consider “all” interactions of light with object

Polygons – Flat shading Ray tracingWireframe Polygons - Smooth shading

Page 5: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Shading• So far, just used OpenGL pipeline for vertices

• Polygons have all had constant color– glColor(_)– Not “realistic” – or computationally complex

• Of course, OpenGL can (efficiently) provide more realistic images

• 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

• Terminology– “Lighting”

• modeling light sources, surfaces, and their interaction– “Shading”

• how lighting is done with polygon

Page 6: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Rendering Equation

• Light travels …– 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 …

• Infinite scattering and absorption of light can be described by rendering equation – Bidirectional reflection distribution function– Cannot be solved in general– Ray tracing is a special case for perfectly

reflecting surfaces

• Rendering equation is global, includes:– Shadows– Multiple scattering from object to object– … and everything

translucent surface

shadow

multiple reflection

Page 7: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Elements of Global Illumination

• Global Illumination– Simulating what happens when other objects effect light

reaching a surface element• e.g., ray tracing

• Lights and Shadows– Most light comes directly light sources– Light from a source may be blocked by other objects

• In “shadow” from that light source, so darker• All Non – global can’t do shadows

• Inter-Object Reflection– Light strikes other objects, bounces toward surface element– When that light reaches surface element from other surface

elements, brightens surface element (indirect illumination)

• Expensive to Compute– Many objects in scene affect light reaching surface elements

• But, necessary for some applications

translucent surface

shadow

multiple reflection

Page 8: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Global Illumination – Ray Tracing• Image formed from all light reaching view

• Ray tracing (or casting)– Basically, “running things backwards,

constrained by pixels …”– Follow rays from center of projection until

they either are absorbed by objects or go off to infinity

– Can handle global effects• Multiple reflections• Translucent objects

– Slow– Must have whole data base available at

all times

• Radiosity– Energy based approach– Very slow

Page 9: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Light Interactions with a Solid• Will focus on surface properties and light-material interactions• But, there are complexities in modeling light …

– but good enough is good enough…– Below from Watt, “3d Computer Graphics”

Page 10: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Light – Material Interactions for CG(quick look)

• Will examine each of these in detail

• Diffuse surface– Matte, dull finish– Light “scattered” in all directions

• Specular surface– Shiny surface– Light reflected (scattered) in narrow range

of directions

• Translucent surface– Light penetrates surface and emerges in

another location on object

Page 11: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Physical Model: BRDF Bidirectional Reflectance Distribution Function

• Light energy can arrive at any point on a surface from any direction and be reflected and leave from any direction

– For every pair of input and output directions, will be a value that is the fraction of incoming light that is reflected and leave in output direction

• BDRF – Bidirectional reflectance distribution function– Describes reflectance, absorption, and transmission of light at surface of material

• BDRF is a function of– Frequency of light– 2 angles to describe light in– 2 angles to describe light out

Page 12: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Physical Model: BRDF• Light energy can arrive at any point on a surface from any direction

and be reflected and leave from any direction– For every pair of input and output directions, will be a value that is the fraction of

incoming light that is reflected and leave in output direction

• Obtaining BDRF’s empirically

• … and how many points are there to consider???

Page 13: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

“Surface Elements” for Interactive CG(a big idea)

• A computer graphics issue/orientation:– Consider everything or just “sampling a scene”?

• Again, global view considers all light coming to viewer:– From each point on each surface in scene - object precision

– Points are smallest units of scene

– Can think of points having no area or infinitesimal area

• i.e., there are an infinite number of visible points.

• Of course, computationally intractable

• Alternatively, consider surface elements– Finite number of differential pieces of surface

• E.g., polygon

– Figure out how much light comes to viewer from each of these pieces of surface

– Often, relatively few (vs. infinite) is enough

• Reduction of computation through use of surface

elements is at core of tractable/interactive cg

Page 14: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Surface Elements and Illumination, 1

• Tangent Plane Approximation for Objects– Most surfaces are curved: not flat– Surface element is area on that surface

• Imagine breaking up into very small pieces • Each of those pieces is still curved,

– but if we make the pieces small enough, – then we can make them arbitrarily close to being flat

– Can approximate this small area with a tiny flat area

• Surface Normals– Each surface element lies in a plane.– To describe plane, need a point and a normal– Area around each of these vertices is a surface element

where we calculate “illumination”

• Illumination

Page 15: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Surface Elements and Illumination, 2

• Tangent Plane Approximation for Objects

• Surface Normals

• Illumination– Again, light rays coming from rest of scene

strike surface element and head out in different directions

– Light that goes in direction of viewer from that surface element

• If viewer moves, light will change

– This is “illumination” of that surface element– Will see model for cg later

Page 16: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

In Sum: Local vs. Global Rendering

• Correct shading requires a global calculation involving all objects and light sources

– Recall “rendering equation”• infinite scattering and absorption of light

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

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

– Exist many techniques for approximating global effects

– Will see several

Page 17: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Light-Material Interaction, 1

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

• Amount reflected determines color and brightness of object

– Surface appears red under white light because the red component of light is reflected and rest is absorbed

– Can specify both light and surface colors

rough surface

Livingstone, “Vision and Art”

Page 18: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Light-Material Interaction, 2

• Diffuse surfaces– Rough (flat, matte) surface scatters

light in all directions– Appear same from different viewing

angles

• Specular surfaces– Smoother surfaces, more reflected

light is concentrated in direction a perfect mirror would reflected the light

– Light emerges at single angle– … to varying degrees – Phong

shading will model

• Reflected light is scattered in a manner that depends on the smoothness and orientation of surface to light source

Page 19: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Light Sources

• General light sources are difficult to work with because must integrate light coming from all points on the source

• Use “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– A real convenience – recall, “rendering equation” – but real nice– Same amount of light everywhere in scene– Can model (in a good enough way) contribution of many sources and reflecting

surfaces

Page 20: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Overview: Local Rendering Techniques

• Will consider– Illumination (light) models focusing on following elements:

• Ambient• Diffuse• Attenuation• Specular Reflection

– Interpolated shading models:• Flat, Gouraud, Phong, modified/interpolated Phong (Blinn-Phong)

Page 21: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

About (Local) Polygon Mesh Shading(it’s all in how many polygons there are)

• Angel example of approximation of a sphere…

• Recall, any surface can be illuminated/shaded/lighted (in principle) by:

1. calculating surface normal at each visible point and

2. applying illumination model

… or, recall surface model of cg!

• Again, where efficiency is consideration, e.g., for interactivity (vs. photorealism) approximations are used

– Fine, because polygons themselves are approximation

– And just as a circle can be considered as being made of “an infinite number of line segments”,

• so, it’s all in how many polygons there are!

Page 22: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

About (Local) Polygon Mesh Shading(interpolation)

• Interpolation of illumination values are widely used for speed– And can be applied using any illumination model

• Will see three methods - each treats a single polygon independently of others (non-global)

– Constant (flat)– Gouraud (intensity interpolation)– Interpolated Phong (normal-vector interpolation)

• Each uses interpolation differently

Page 23: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Flat/Constant Shading, About• Single illumination value per polygon

– Illumination model evaluated just once for each polygon • 1 value for all polygon, Which is as fast as it gets!

– As “sampling” value of illumination equation (at just 1 point)– Right is flat vs. smooth (Gouraud) shading

• If polygon mesh is an approximation to curved surface, – faceted look is a problem– Also, facets exaggerated by mach band effect

• For fast, can (and do) store normal with each surface– Or can, of course, compute from vertices

• But, interestingly, approach is valid, if:– Light source is at infinity (is constant on polygon)– Viewer is at infinity (is constant on polygon)– Polygon represents actual surface being modeled (is not an

approximation)!

Page 24: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Flat/Constant Shading, Light Source

• In cg lighting, often don’t account for angle of rays

• Approach is valid, if …– Light source is at infinity (is constant on polygon)– Viewer is at infinity (is constant on polygon)– Polygon represents actual surface being modeled (is not an

approximation)

• Consider point light sources at right

– Close to surface: L1 <> L2 <> L3

– Farther from surface: L1 <> L2 <> L3, but closer --------------------

– At “infinity” can consider: L1 = L2 = L3! – same for V!, • so and are constant on polygon

Page 25: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

But, … Mach Banding

• May or may not be apparent here …

Page 26: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Mach Banding (Chevreul illusion)

Page 27: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Mach Banding• Mach banding

– Exaggerated differences in perceived intensities

• At adjacent edges of differing gintensities

– Non-intuitive and striking• An “illusion” in sense that

perception not veridical (true)

• In fact, physiological cause– Actual and perceived intensities due to

cellular bilateral inhibition• sensation (response of retinal cells)

depends on how cell neighbors are stimulated

• Eye’s photoreceptors responds to light

– according to intensity of light falling on it minus the activation of its neighbors

– Great for human edge detection– A challenge for computer graphics

Ernst Mach

Page 28: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Mach Bands and Receptor Fields

• Point where uniform area meets luminance ramp, bright band is perceived – Another way, appear where abrupt change in 1st derivative of brightness

profile– Particularly a problem for uniformly shaded polygons in computer graphics

• Hence, various methods of smoothing are applied

• … any number of “unintended” things can happen with perception

Page 29: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Simultaneous Brightness Contrastfyi

• Gray patch on a dark background looks lighter than the same patch on a light background

Page 30: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Simultaneous Brightness Contrastfyi

• Background removed! (honest, no change in foreground)

Page 31: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Gouraud Shading, About• Recall, for flat/constant shading, single illumination value per polygon

• Gouraud (or smooth, or interpolated intensity) shading overcomes problem of discontinuity at edge exacerbated by Mach banding

– “Smooths” where polygons meet– H. Gouraud, "Continuous shading of curved surfaces,"

IEEE Transactions on Computers, 20(6):623–628, 1971.

• Linearly interpolate intensity along scan lines– Eliminates intensity discontinuities at polygon edges– Still have gradient discontinuities,

• So mach banding is improved, but not eliminated– Must differentiate desired creases from tessellation artifacts

• (edges of a cube vs. edges on tesselated sphere)

Page 32: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Gouraud Shading, About• To find illumination intensity, need intensity of

illumination and angle of reflection– Flat shading uses 1 angle– Gouraud estimates – …. Interpolates

1. Use polygon surface normals to calculate “approximation” to vertex normals

– Average of surrounding polygons’ normals– Since neighboring polygons sharing vertices and edges

are approximations to smoothly curved surfaces – So, won’t have greatly differing surface normals

• Approximation is reasonable one

2. Interpolate intensity along polygon edges

3. Interpolate along scan lines– i.e,, find:

• Ia, as interpolated value between I1 and I2• Ib, as interpolated value between I1 and I3• Ip, as interpolated value between Ia and Ib

– formulaically, next slide

Page 33: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

(S) Gouraud Shading, Formulaically

• What goes in (firmware) - straightforward

• 3: interpolate along scan lines– e.g., find:

• Ia, as interpolated value between I1 and I2• Ib, as interpolated value between I1 and I3• Ip, as interpolated value between Ia and Ib

– i.e., formulaically:

or

Page 34: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

(S) Gouraud Shading

• Can also interpolate color– Though less straightforward

• Summary: Interpolating intensity – Gouraud shading– Gouraud versus constant shading– integrates nicely with scan line

algorithm:• constant along polygon edge:

Page 35: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

What Gouraud Shading Misses

• Misses specular highlights in specular objects – because interpolates vertex colors instead of vertex

normals– interpolating normal comes closer to what actual

normal of surface being “polygonally” approximated would be

• Illumination model following, and its implementation in Phong shading, does handle

• Below:– Flat/constant, Gouraud/interpolated intensity, Phong

Page 36: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Illumination Model, Describing Light, 1

• Will be looking at model of illumination for cg– Why are things relatively brighter or darker

depending on orientation to light source and viewer?

• Start with light …

• Units of light– light incident on a surface and exiting from a

surface is measured in specific terms defined later

• for now, consider the ratio:Light exiting surface from the viewer Light incident on surface from light

• Another way to conceptualize– Quick take ( note diff angle, theta, at right):

• Just not as much “light” (energy) per surface area unit – for non “straight on” light

• dA “shorter” (less) on left, than right• And can describe quantitatively

Page 37: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Describing Light, 2

• Factors in computing “light exiting surface” (how bright)

– physical properties of the surface (material)– geometric relationship of surface with respect to

viewer– geometric relationship of surface with respect to

lights– light incident on the surface (color and intensity of

lights in the scene)– polarization, fluorescence, phosphorescence

• Difficult to define some of these inputs– not sure what all categories of physical properties

are …, • and the effect of physical properties on light is

not totally understood– polarization of light, fluorescene, phosphorescene

difficult to keep track of– Light exiting surface toward viewer– Light incident on surface from lights

Page 38: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Simple Illumination Model

• One of first models of illumination that “looked good” and could be calculated efficiently

– simple, non-physical, non-global illumination model– describes some observable reflection characteristics of surfaces– came out of work done at the University of Utah in the early 1970’s– still used today, as it is easy to do in software and can be optimized in hardware

• Later, will put all together with normal interpolation

• Components of a simple model– Reflection characteristics of surfaces

• Diffuse Reflection• Ambient Reflection• Specular Reflection

• Model not physically-based, and does not attempt to accurately calculate global illumination

– does attempt to simulate some of important observable effects of common light interactions

– can be computed quickly and efficiently

Page 39: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Reflection Characteristics of Surfaces, Diffuse Reflection (1/7)

• Diffuse Reflection– Diffuse (Lambertian) reflection

• typical of dull, matte surfaces – e.g. carpet, chalk plastic

• independent of viewer position• dependent on light source position

– (in this case a point source, again a non-physical abstraction)

• Vecs L, N used to determine reflection– Value from Lambert’s cosine law … next slide

Page 40: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

• Lambert’s cosine law:– Specifies how much energy/light reflects

toward some point– Computational form used in equation for

illumination model

• Now, have intensity (I) calculated from:

– Intensity from point source– Diffuse reflection coefficient (arbitrary!)– With cos-theta calculated using

normalized vectors N and V• For computational efficiency

• Again:

Reflection Characteristics of Surfaces, Lambert’s Law (2/7)

Page 41: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Reflection Characteristics of Surfaces, Energy Density Falloff (3/7)

• Less light as things are farther away from light source

• Reflection - Energy Density Falloff– Should also model inverse square law

energy density falloff

• Formula often creates harsh effects

– However, this makes surfaces with equal differ in appearance ¾ important if two surfaces overlap

– Do not often see objects illuminated by point lights

• Can instead use formula at right– Experimentally-defined constants – Heuristic

Page 42: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Reflection Characteristics of Surfaces, Ambient Reflection (4/7)

• Ambient Reflection

• Diffuse surfaces reflect light

• Some light goes to eye, some to scene– Light bounces off of other objects and

eventually reaches this surface element – This is expensive to keep track of accurately – Instead, we use another heuristic

• Ambient reflection– Independent of object and viewer position– Again, a constant – “experimentally determined”– Exists in most environments

• some light hits surface from all directions • Approx. indirect lighting/global illumination

– A total convenience• but images without some form of ambient lighting

look stark, they have too much contrast– Light Intensity = Ambient + Attenuation*Diffuse

Page 43: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Reflection Characteristics of Surfaces, Color (5/7)

• Colored Lights and Surfaces

• Write separate equation for each component of color model

– Lambda - wavelength– represent an object’s diffuse color by one

value of for each component• e.g., in RGB• are reflected in proportion to• e.g., for the red component

• Wavelength dependent equation– Evaluating the illumination equation at only 3

points in the spectrum is wrong, but often yields acceptable pictures.

– To avoid restricting ourselves to one color sampling space, indicate wavelength dependence with (lambda).

Page 44: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Reflection Characteristics of Surfaces, Specular Reflection (6/7)

• Specular Reflection

• Directed reflection from shiny surfaces– typical of bright, shiny surfaces, e.g. metals– color depends on material and how it scatters light energy

• in plastics: color of point source, in metal: color of metal• in others: combine color of light and material color

– dependent on light source position and viewer position– Early model by Phong neglected effect of material color on specular highlight

• made all surfaces look plastic– for perfect reflector, see light iff– for real reflector, reflected light falls off as increases– Below, “shiny spot” size < as angle view >

Page 45: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Reflection Characteristics of Surfaces, Specular Reflection (7a/7)

• Phong Approximation– Again, non-physical, but works– Deals with differential “glossiness” in a

computationally efficient manner

• Below shows increasing n, left to right– “Tightness” of specular highlight– n in formula below (k, etc., next slide)

Page 46: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Reflection Characteristics of Surfaces, Specular Reflection (7b/7)

• Yet again, constant, k, for specular component

• Vectors R and V express viewing angle and so amount of illumination

• n is exponent to which viewing angle raised

– Measure of how “tight”/small specular highlight is

Page 47: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Putting it all together:A Simple Illumination Model

• Non-Physical Lighting Equation– Energy from a single light

reflected by a single surface element

• For multiple point lights, simply sum contributions

• An easy-to-evaluate equation that gives useful results

– It is used in most graphics systems,

• but it has no basis in theory and does not model reflections correctly!

Page 48: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Interpolated Vector Shading/Model

• Calculating normal at each point is computationally expensive

• Can interpolate normal– As interpolated vertices with Gouraud

shading

• Interpolated Vector Model:– Rather than recalculate normal at each at

each step, interpolate normal for calculation

– Much more computationally efficient

• Bui Tuong Phong, "Illumination for Computer Generated Images," Comm. ACM, Vol 18(6):311-317, June 1975

Page 49: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Phong Shading

• Normal vector interpolation– interpolate N rather than I– especially important with specular

reflection– computationally expensive at each

pixel to recompute• must normalize, requiring

expensive square root

• Looks much better than Gouraud and done in commodity hardware

– OpenGL default

• Bui Tuong Phong, "Illumination for Computer Generated Images," Comm. ACM, Vol 18(6):311-317, June 1975

Page 50: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Demo Program

• Calculates Phong shading at each point on sphere

Page 51: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

End

• .

Page 52: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

OpenGL Shading Functions

• Polygonal shading– Flat– Smooth– Gouraud

• Steps in OpenGL shading1. Enable shading and select model

2. Specify normals

3. Specify material properties

4. Specify lights

Page 53: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

53

Normals

• Normal – vector perpendicular to a surface– Recall, typically set by application program for each vertex or polygon

• In OpenGL the normal vector is part of the state

• Set by glNormal*()– glNormal3f(x, y, z);– glNormal3fv(p);

• Usually we want to set the normal to have unit length so cosine calculations are correct

– Length can be affected by transformations– Note that scaling does not preserved length– glEnable(GL_NORMALIZE) allows for autonormalization at a performance

penalty

Page 54: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

FYI - Normal for Triangle

plane n ·(p - p0 ) = 0

n = (p2 - p0 ) ×(p1 - p0 )

normalize n n/ |n|p0

p

1

p

2

n

p

Note that right-hand rule determines outward face

Page 55: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Enabling Shading

• Shading calculations are enabled by– glEnable(GL_LIGHTING)– Once lighting is enabled, glColor() ignored

• Must enable each light source individually– glEnable(GL_LIGHTi) i=0,1…..

• Can choose light model parameters:– glLightModeli(parameter, GL_TRUE)

• GL_LIGHT_MODEL_LOCAL_VIEWER – do not use simplifying distant viewer assumption in calculation

• GL_LIGHT_MODEL_TWO_SIDED – shades both sides of polygons independently

Page 56: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Defining a Point Light Source• For each light source, can set an RGBA (A for alpha channel)

– For diffuse, specular, and ambient components, and – For the position– Code below from Angel, other ways to do it (of course)

GL float diffuse0[]={1.0, 0.0, 0.0, 1.0};GL float ambient0[]={1.0, 0.0, 0.0, 1.0};GL float specular0[]={1.0, 0.0, 0.0, 1.0};Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0};

glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glLightv(GL_LIGHT0, GL_POSITION, light0_pos);glLightv(GL_LIGHT0, GL_AMBIENT, ambient0);glLightv(GL_LIGHT0, GL_DIFFUSE, diffuse0);glLightv(GL_LIGHT0, GL_SPECULAR, specular0);

Page 57: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Distance and Direction

• Position is given in homogeneous coordinates– If w =1.0, we are specifying a finite location– If w =0.0, we are specifying a parallel source with the given direction vector

• Coefficients in distance terms are by default a=1.0 (constant terms), b=c=0.0 (linear and quadratic terms)

– Change by: • a= 0.80;• glLightf(GL_LIGHT0, GLCONSTANT_ATTENUATION, a);

GL float diffuse0[]={1.0, 0.0, 0.0, 1.0};GL float ambient0[]={1.0, 0.0, 0.0, 1.0};GL float specular0[]={1.0, 0.0, 0.0, 1.0};Glfloat light0_pos[]={1.0, 2.0, 3,0, 1.0};

Page 58: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Spotlights

• Use glLightv to set – Direction GL_SPOT_DIRECTION– Cutoff GL_SPOT_CUTOFF– Attenuation GL_SPOT_EXPONENT

• Proportional to cosaf

Page 59: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

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

• OpenGL also allows a global ambient term that is often helpful for testing– glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient)

Page 60: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Moving Light Sources

• Light sources are geometric objects whose positions or directions are affected by the model-view matrix

• Depending on where we place the position (direction) setting function, 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

Page 61: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Material Properties

• Material properties are also part of the OpenGL state and match the terms in the modified Phong model

• Set by glMaterialv()GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};

GLfloat diffuse[] = {1.0, 0.8, 0.0, 1.0};

GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};

GLfloat shine = 100.0

glMaterialf(GL_FRONT, GL_AMBIENT, ambient);

glMaterialf(GL_FRONT, GL_DIFFUSE, diffuse);

glMaterialf(GL_FRONT, GL_SPECULAR, specular);

glMaterialf(GL_FRONT, GL_SHININESS, shine);

Page 62: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Front and Back Faces

• The default is shade only front faces which works correctly for convex objects

• If we set two sided lighting, OpenGL will shade both sides of a surface

• Each side can have its own properties which are set by using– GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK in glMaterialf

back faces not visible back faces visible

Page 63: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Emissive Term

• We can simulate a light source in OpenGL by giving a material an emissive component

• This component is unaffected by any sources or transformations– GLfloat emission[] = 0.0, 0.3, 0.3, 1.0);– glMaterialf(GL_FRONT, GL_EMISSION, emission);

Page 64: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Transparency

• Material properties are specified as RGBA values

• The A value can be used to make the surface translucent

• The default is that all surfaces are opaque regardless of A

• Later we will enable blending and use this feature

Page 65: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Efficiency

• Because material properties are part of the state, if we change materials for many surfaces, we can affect performance

• We can make the code cleaner by defining a material structure and setting all materials during initialization

typedef struct materialStruct {

GLfloat ambient[4];

GLfloat diffuse[4];

GLfloat specular[4];

GLfloat shineness;

} MaterialStruct;

• We can then select a material by a pointer

Page 66: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Polygonal Shading

• Shading calculations are done for each vertex– Vertex colors become vertex shades

• By default, vertex shades are interpolated across the polygon– glShadeModel(GL_SMOOTH);

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

Page 67: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Polygon Normals

• 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) or if there is no specular component

• Consider model of sphere

• Want different normals at each vertex even though this concept is not quite correct mathematically

Page 68: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

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

Page 69: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

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 the normals around a mesh vertex

– n = (n1+n2+n3+n4)/ |n1+n2+n3+n4|

Page 70: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

Gouraud and Phong Shading

• Gouraud Shading– Find average normal at each vertex (vertex normals)– 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 71: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

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– Until recently not available in real time systems– Now can be done using fragment shaders (see Chapter 9)

• Both need data structures to represent meshes so we can obtain vertex normals

Page 72: Illumination and Shading Angel, Chapter 6 slides from AW, etc. CSCI 6360/4360

End

• .