Shading & Lighting Shading Intro Light and Matter Light and Matter

22
Shading & Lighting Courtesy of Vincent Costa Shading Intro Move from flat to 3-D models Orthographic view of sphere was uniformly color, thus, a flat circle A circular shape with many gradations or shades of color Light and Matter Rendering is based on physics Physically either emit light or reflect it Multiple interactions among light sources and reflective surfaces determine the color point of a object Light and Matter View as a recursive process that results in an integral equation called the rendering equation which, in general, is unsolvable Best technique is to use ray tracing and radiosity, but they are presently too slow

Transcript of Shading & Lighting Shading Intro Light and Matter Light and Matter

Page 1: Shading & Lighting Shading Intro Light and Matter Light and Matter

1

Shading & Lighting

Courtesy of Vincent Costa

Shading Intro

Move from flat to 3-D modelsOrthographic view of sphere was uniformly color, thus, a flat circleA circular shape with many gradations or shades of color

Light and MatterRendering is based on physicsPhysically either emitlight or reflect itMultiple interactions among light sources and reflective surfaces determine the color point of a object

Light and Matter

View as a recursive process that results in an integral equation called the rendering equation which, in general, is unsolvableBest technique is to use ray tracing and radiosity, but they are presently too slow

Page 2: Shading & Lighting Shading Intro Light and Matter Light and Matter

2

Light and MatterUse a simpler rendering model based on PhongreflectionFollow rays of light from light-emitting surfaces, called light sourcesConsider only single interactions between light sources and surfacesViewer sees only the light that leaves the source and reaches the eye

Light and MatterViewer sees the color of the light reflected from the surfaceReplace the viewer by a projection planeClipping window is mapped to the screen, each rectangle is a pixelColor of light source and surfaces determine the color of one or more pixels in the frame bufferMost rays never contribute to the image

Light-Material Interaction

specularsurface

diffusesurface

translucentsurface

Light-Material InteractionsSpecular surfaces appear shiny because most of the light that is reflected is scattered in a narrow range of angles close to the angle of reflection (mirrors are perfect specular surfaces)Diffuse surfaces are characterized by reflected light being scattered in all directionsTranslucent surfaces allow some light to penetrate the surface and to emerge from another location on the object in a process called refraction

Page 3: Shading & Lighting Shading Intro Light and Matter Light and Matter

3

Material Properties

no ambient reflection

gray ambient reflection

blue ambient reflection

bluediffuse

nospecular

whitespecular

lowshininess

whitespecular

highshininess

bluediffuseremissive

Light SourcesLight can leave a surface through two fundamental processes: self-emission and reflectionLight source is an object with a surfaceEach point (x,y,x) on the surface can emit light that is characterized by the direction of emission (θ,φ) and the intensity of energy emitted at each wavelength λGeneral light source is a six variable illumination function I(x,y,z,θ,φ,λ)Easiest to model with polygons

Light Sources

u=θf=φ

I(x,y,z,θ,φ,λ)

Color SourcesLight sources emit different amounts of light at different frequenciesBest physical model is based on three color theoryWe use each of the three color sources (red, green, blue) to obtain a corresponding color component that a human seesWe describe a source through a three component intensity or luminance function:

=

b

g

r

III

I

Page 4: Shading & Lighting Shading Intro Light and Matter Light and Matter

4

Light Sources

Ambient LightingPoint SourcesSpotlightsDistant LightsThese are sufficient for rendering most scenes

Ambient LightAmbient Light is the uniform lighting in an environment – “background glow”Ambient illumination is characterized by an intensity, Ia, that is identical at every point in the sceneIt has three color components:

We use the scalar Ia to denote any on of the red, green or blue components

=

ab

ag

ar

a

III

I

Point Sources

An ideal point source emits light equally in all directionsCharacterize a point source located at a point p0 by:

=)()()(

)

0

0

0

0

ppp

p(I

b

g

r

III

Point SourcesThe intensity of illumination received from a point source is proportional to the inverse square of the distance between the source and the surfaceAt a point p, the intensity of light received from the point source is given by:

)(1

) 02

0

0 pIpp

p (pI−

=,

Distance between p and p0 can be replaced with: 12 )( −++ cdbda

Page 5: Shading & Lighting Shading Intro Light and Matter Light and Matter

5

Point SourcesPoint sources tend to have high contrastLarger the light source, softer the sceneSome areas in full shadow, umbra, others in partial shadow, penumbraMitigate high contrast by adding ambient light

Spotlights

Spotlights are characterized by a narrow range of angles through which light is emittedGiven a point source, limit the angles at which light from the source can be seenUse a cone whose apex is at ps, which points in the direction ls, and width is determined by an angle θθ-θ

θ

φ

Distant Light

Point on the surface to the light source doesn’t change much as the distance increasesReplace a point source with a parallel source that illuminates objects with parallel rays of lightReplace location of the light source with direction of the light source

Distant Light

=

1

0 zyx

p

=

0

0 zyx

p

pointsource

directionvectorsource

Page 6: Shading & Lighting Shading Intro Light and Matter Light and Matter

6

If the goal in shading a computersynthesized image is to simulate a real physical object, then the shading model should in some way imitate real physical shading situations.

Bui-Tuong Phong

Phong Reflection Model

Shading model was introduced in 1975Close approximation to physical realityModel supports three types of material-light interactions: ambient, diffuse and specular

aassdd LkLkLkcdbda

I +⋅+⋅++

= ))((1

2αvrnl

1975 Martin Newell of University of Utah creates the first environmentally mapped object

Utah Teapot Phong Reflection ModelModel uses four vectors to calculate a color for an arbitrary point p on a surfacen is the normal at pv is in the direction from p to the viewer or COPl is in the direction of a line from p to the light sourcer is in the direction that a perfectly reflected ray from l would take

Page 7: Shading & Lighting Shading Intro Light and Matter Light and Matter

7

Phong Reflection Model

Model supports three types of material-light interactions: ambient, diffuse and specularGiven a set of point sources, assume each has separate ambient, diffuse and specular components for each of the primary colors3x3 illumination matrix for the i th light source at any point p on the surface:

=

ibsigsirs

ibdigdird

ibaigaira

i

LLLLLLLLL

L

Phong Reflection Model

How much of the incident light is reflected at a pointAmount of reflection depends on the material properties, surface orientation, light source direction and distance3x3 reflection matrix for the i th light source at any point p on the surface:

=

ibsigsirs

ibdigdird

ibaigaira

i

RRRRRRRRR

R

Phong Reflection ModelNow we compute the contribution for each color source by adding the ambient, diffuse and specular componentse.g., the red intensity that we see at p from source i is

Obtain the total intensity by adding the contributions of all sources and a global ambient term. Thus, the red term is

irsirdirairsirsirdirdirairair IIILRLRLRI ++=++=

ari

irsirdirar IIIII +++=∑ )(

Phong Reflection ModelNote that the necessary computations are the same for each source and for each primary colorThey differ depending on whether we are considering the ambient, diffuse or specular termsWe can omit the subscript and write

understanding that the computation will be done for each of the primaries and each source

ssddaasda LRLRLRIIII ++=++=

Page 8: Shading & Lighting Shading Intro Light and Matter Light and Matter

8

Ambient Reflection

The intensity of ambient light La is the same at every point on the surfaceAmount reflected is given by the ambient reflection coefficient, Ra=ka

ThusIa= ka La

where 0≤ ka ≤1

Diffuse Reflection

A perfectly diffuse reflector scatters the light that it reflects equally in all directionsAmount of light reflected is dependent on the material and the orientation of the light source

Diffuse Reflection

The relationship between brightness and surface orientation is called Lambert’s law

θ

θ

Diffuse Reflection

Lambert’s law statesRd ∝ cosθ,

where θ is the angle between the normal nand the direction of the light source l. If these are unit vectors, then

cosθ = l ⋅ nIf we add the diffuse reflection coefficient kd,we have the diffuse reflection term:

Id = kd(l ⋅ n)Ld

Page 9: Shading & Lighting Shading Intro Light and Matter Light and Matter

9

Specular Reflection

We need to add highlights that are reflected from shiny objectsColor is different from the color of the reflected ambient and diffuse light

Specular Reflection

Surface is smoothSmoothest surface resembles a mirrorReflected light is concentrated in a smaller range of angles as the surface gets smootherModeling is complex, (wavelength of light and reflection angle)

Specular ReflectionPhong proposed an approximate model similar to the one used for diffuse surfaces

Is=ksLscosαφwhere the coefficient ks (0≤ ks ≤1) is the fraction of the incoming specular light that is reflected, φ is the angle between the direction of the perfect reflector, r, and the direction of the viewer, v, and α is a shininess coefficientIf we normalize r and v we can use the dot product and the specular reflection term is:

Is=ksLs(r ⋅ v)α

Specular ReflectionAs it is increased, the reflected light is concentrated in a narrower region, centered on the angle of a perfect reflector

Page 10: Shading & Lighting Shading Intro Light and Matter Light and Matter

10

Phong Reflection Model

We can add a quadratic attenuation term for distance from the light source and finally arrive at the Phong model:

This formula is computed for each light source and for each primary color

aassdd LkLkLkcdbda

I +⋅+⋅++

= ))((1

2αvrnl

Teapots, Teapots, …

Computation of VectorsSmooth curved surfaces are approximated by a large number of small flat polygons. Vectors perpendicular to these polygons can be used as the surface normalsThe surface appears faceted, since the normal direction is discontinuous across the polygonal boundaries. In many cases, however, an exact mathematical description exists for the surface, and true surfacenormals can be calculated at every point. Using the true normals improves the rendering considerably

Computation of Vectors

Page 11: Shading & Lighting Shading Intro Light and Matter Light and Matter

11

Normal VectorsFor smooth, flat surfaces, the vector normal to the surface exists at every pointPlane:

ax + by + cz + d = 0Also,

n ⋅ (p - p0) = 0where p0 is a point on the plane, p is any point (x,y,z) on the plane and n is the normal to the plane

Normal VectorsGiven three noncollinear points, p0, p1, p2…and vectors p2 - p0 and p1 - p0 are parallel to the planeNormal:

n = (p2 - p0 ) ×(p1 - p0 )Order of the vectors in the cross product is important, since it determines pointing in or outOpenGL does not determine the normal automatically

Normal VectorsFor curved surfaces, there are different methods for representation (bezier, b-spline, NURBS)Unit Sphere, Implicit form:

Also,f(p) = p ⋅ p – 1 = 0

01),,( 222 =−++= zyxzyxf

Normal VectorsNormal is given by the gradient vector

pn 2222

=

=

∂∂∂∂∂∂

=zyx

zfyfxf

Page 12: Shading & Lighting Shading Intro Light and Matter Light and Matter

12

Normal Vectors

We usually have to work with a collection of vertices and approximate a normal vectorOpenGL takes the “do-it-yourself” approach to normalsThe user program must determine the normalWe associate a normal with a vertex through the function

glNormal3f(nx, ny, nz);All subsequent glVertex calls use that normal

Angle of ReflectionWe use the normal n and the direction of the light source l to determine the direction of a perfect reflectionIdeal mirror: The angle of incidence is equal to the angle of reflection

θi θr

angle ofincidence

angle ofreflection

Angle of Reflection

For three dimensions: At a point p on the surface, the incoming light ray, the reflectedlight ray, and the normal at the point must all lie in the same planeWe are concerned with the direction of rUnit-length vectors make rendering calculations easierNormalize l, n, and r such that

1== nl and 1=r

Halfway VectorWith the phong model, r⋅ v must be recalculated for every point of the surfaceHalfway vectors are used to simplify this calculation, replace r⋅ v with n⋅ hh = (l + v )/|l + v|, it is a unit vector halfway

Page 13: Shading & Lighting Shading Intro Light and Matter Light and Matter

13

Transmitted Light

Ideal condition is a surface that transmits all the light that strikes itLight is bent at the surface due to the difference in the speed of light in the materialsηl and ηt are the indices of refraction, the relative speed of light in two materialsSnell’s law,

l

t

t

l

ηη

θθ=sin

sin

Transmitted Light

ideal reality

θt

θl

Phong Shading

Assuming we can compute normal vectors, we can use our lighting models and apply them to every point on the surfaceComputationally expensivePolygons simplify this processDecompose curved surfaces into many small polygons where each polygon is flat and it has a well defined normal vector

Polygonal Shading

Polygonal Mesh

Page 14: Shading & Lighting Shading Intro Light and Matter Light and Matter

14

Flat Shadingl, n, and v vary from point to point on a surfaceFor a flat polygon, n is constantOver a distance, l and v are constantDistance can be interpreted by relative size of the polygon to the viewer

Flat ShadingIf the three vectors are constant, then the shading calculation needs to be carried out only once for each polygonEach point on the polygon is assigned the same shadeThis technique is called flat shading

glShadeModel(GL_FLAT);OpenGL uses the normal associated with the first vertex of a single polygon

Flat Shading

Flat shading will show differences in shading for the polygons in our meshModeling a smooth surface will be disappointingHuman visual system is very sensitive to small differences in light intensity because of lateral inhibition

Flat Shading

Page 15: Shading & Lighting Shading Intro Light and Matter Light and Matter

15

Mach BandsThe solid black curverepresents the amount of light being reflected from the figure at the top. The red curve represents the brightness of this figure as it is usually perceived.

To the left of the point wherethe figure just starts to get lighter people usually see a dark bar that is slightly darker that the area to the left of it

This phenomenon was discovered by the famous physicist, Ernst Mach

Mach Bands

Interpolative ShadingOpenGL interpolates colors assigned to vertices across a polygon (rotating cube)If the light source and viewer are distant, then we can use interpolative shading to shade a polygon in a constant shadeOpenGL interpolates colors for primitives other than vertices via

glShadeModel(GL_SMOOTH)Assign to each vertex the normal of the polygon being shaded

Interpolative ShadingMultiple polygons meet at interior vertices of the mesh, each with its own normalThe normal at the vertex is discontinuous

Page 16: Shading & Lighting Shading Intro Light and Matter Light and Matter

16

Gourand ShadingIn Gourand shading, we define the normal at a vertex to be the normalized average of the normals of the polygons that share the vertexIn the previous diagram, the vertex normal is given by

Gourand shading is deceptively simple

4321

4321

nnnn

nnnnn

+++

+++=

Gourand Shading

How do we find the normals that we should average?We need a data structure to represent the meshKey info: which polygons meet at each vertex

Phong ShadingPhong proposed interpolating normals across each polygonCompute vertex normals by interpolating over the normals of the polygons that share the vertexUse bilinear interpolation to interpolate the normals over the polygonDetermine the normal at any interior pointwith

n(α, β)=(1-β)nC+βnDMake independent shading operation for each point and combine with scan conversion

Phong Shading

Produces smoother renderings but at a significant costGourand shading is built into hardwarePhong Shading is usually done off line

Page 17: Shading & Lighting Shading Intro Light and Matter Light and Matter

17

Approximating A Sphere

No sphere in OpenGLPolygonal approximation to a sphereRecursive subdivision is a powerful technique for generating approximations to curves and surfaces to any desired accuracyStart with a tetrahedron

Tetrahedron

http://www.math.umn.edu/~roberts/java.dir/JGV/tetrahedron1.html

Recursive Subdivision

Four equilateral trianglesFour verticesAll four lie on the unit sphere centered at the origin

)31,3

2,36(),3

1,32,3

6(),31,3

22,0(),1,0,0( −−−−−−

Recursive Subdivision…but it’s not a sphereCloser approximation by subdividing each facet of the tetrahedron into smaller trianglesConnect bisectors of the sides of the triangle, forming four equilateral triangles

Page 18: Shading & Lighting Shading Intro Light and Matter Light and Matter

18

Recursive Subdivision…but four new triangles are still in the same plane as the original triangleMove the new vertices created by bisection to the unit sphereNormalize each bisected vertex

Subdividing Continues

Light SourcesOpenGL supports the four types of light sources and up to 8 light sourcesFormat: glLightfv(source, parameter, pointer_to_array);

Glfloat light0_pos[ ]={1.0, 2.0, 3.0, 1.0}; Position of the sourceGlfloat diffuse0[ ]={1.0, 0.0, 0.0, 1.0}; Amount of diffuseGlfloat ambient0[ ]={1.0, 0.0, 0.0, 1.0}; Amount of ambientGlfloat specular0[ ]={1.0, 1.0, 1.0, 1.0}; Amount of specular

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

Light and Position

Nate Robins' Light and Position Tutorial

www.xmission.com/~nate/tutors.html

Page 19: Shading & Lighting Shading Intro Light and Matter Light and Matter

19

MaterialsMaterial properties in OpenGL match up directly with the supported light sources and the Phong reflection modelCan specify different material properties for front and back faces of a surfaceMaterial properties are modal: values remain the same until changedFormat: glMaterialfv(face, type, pointer_to_array);

Glfloat light0_pos[ ]={1.0, 2.0, 3.0, 1.0};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};

glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, 100.0);

Light and Material

Nate Robins' Light and Material Tutorial

www.xmission.com/~nate/tutors.html

Global RenderingLimitation to the local modelSpheres close to the source block some of the lightIn local model, each sphere is shaded independently

Ray Tracing

Only rays that contribute to our images are those that enter the lens and pass through the COPMost rays do not contribute to our imageReverse the direction and consider only those ray that start at the COP, cast rays

Page 20: Shading & Lighting Shading Intro Light and Matter Light and Matter

20

Ray Tracing

cast rays

Ray Tracing

Rule the image plane into pixel-sized areasCast at least one ray through each pixelIntersect a surface or light source, or go off to nowhere, getting a background colorCalculate a shade for surfaces that a ray strikes

Ray Tracing

shadow ray

handledrecursively

Ray Tracing

ray tree

Page 21: Shading & Lighting Shading Intro Light and Matter Light and Matter

21

Ray Tracing

Easiest way to describe a ray tracer is recursivelyMost of the work in ray tracing goes into the calculation of intersections between rays and surfacesBest suited for highly reflective environments

Radiosity

Radiosity is ideal for a scene consisting of only perfectly diffuse surfacesDiffuse-diffuse interactions shading modelAssume all surfaces are perfectly diffuseSimulates the actual behavior of lightThe goal of the radiosity method is to find the point of equilibrium of all the radiation in the system.

Radiosity Radiosity

Page 22: Shading & Lighting Shading Intro Light and Matter Light and Matter

22

Radiosity One Last Ugly Teapot