Date post: | 17-Jan-2016 |
Category: |
Documents |
Upload: | tyrone-evans |
View: | 217 times |
Download: | 1 times |
CS 450: COMPUTER GRAPHICS
BASIC ILLUMINATIONSPRING 2015
DR. MICHAEL J. REALE
INTRODUCTION
• Let’s say we have a triangle on the screen
• We’ve already projected it and let’s assume it’s entirely within the clipping window
• The rasterizer needs to fill in each of the fragments/pixels inside of this triangle
• If we need to fill in one color straightforward, but not realistic-looking
• How do we make surfaces look realistic?
VERTEX ATTRIBUTES PIXEL/FRAGMENT VALUES
• Most of the time, a model is broken up into a triangle mesh
• For a given triangle, each vertex can be given different values (attributes):
• Color
• Texture coordinates
• Normal vector
• Etc.
• These values are interpolated across the triangle to get the individual pixel/fragment values
BARYCENTRIC COORDINATES
BARYCENTRIC COORDINATES
• Barycentric coordinates – parameterizes the space that can be formed as a weighted combination of a set of reference points
• In other words any location is a weighted average of the reference points
• Weights all sum to 1
• For 2 reference points:
• (u,v) = barycentric coordinates of P with respect to A and B
• P is on segment AB IFF 0 <= u <= 1 AND 0 <= v <= 1
• Barycentric coordinates of A = (1,0)
• Barycentric coordinates of B = (0,1)
1
vu
vBuAP
BARYCENTRIC COORDINATES:TRIANGLES• For 3 reference points (triangles):
• (u,v,w) = barycentric coordinates of P with respect to A, B, and C
• A (1,0,0)
• B (0,1,0)
• C (0,0,1)
• Inside or on triangle IFF 0 <= u,v,w <= 1
• Centroid or barycenter = (1/3, 1/3, 1/3)
http://en.wikipedia.org/wiki/Barycentric_coordinate_system#/media/File:TriangleBarycentricCoordinates.svg
1
wvu
wCvBuAP
BARYCENTRIC COORDINATES: WHY DO WE CARE?
• Given a point P in a triangle, the barycentric coordinates give us a weight for each vertex in the triangle can use to interpolate vertex attribute values!
• Example: if each vertex has a different color (C0 , C1 , C2 , respectively), and the barycentric coordinates for P are (u,v,w), the interpolated color for P is given by:
210 wCvCuCC p
BARYCENTRIC COORDINATES AS A PLANE
• One can rearrange the previous barycentric equations to form a point and two axes (effectively defining a plane):
• Note: We don’t really need u
1
wvu
wCvBuAP
)()(
)1(
11
ACwABvAP
wCvBwAvAAP
wCvBAwvP
wCvBuAP
wvuwvu
SOLVING FOR BARYCENTRIC COORDINATES• Let’s say we have a point P and we want to find the barycentric coordinates for it
• Let the following be true:
• Then we can rewrite our previous equation:
APVACVABV 210
210
10
10
)()(
)()(
)()(
)()(
VVwVv
APVwVv
VwVvAP
ACwABvAP
SOLVING FOR BARYCENTRIC COORDINATES
• We can turn this into a 2 x 2 system of linear equations by taking the dot product of both sides first with V0 and then with V1:
210 )()( VVwVv
121110121110
020100020100
)()()()(
)()()()(
VVVVwVVvVVVVwVVv
VVVVwVVvVVVVwVVv
SOLVING FOR BARYCENTRIC COORDINATES
• Fortunately, this can be solved using Cramer’s Rule!
wvu
DDw
DDv
dd
dd
VVVV
VVVVD
dd
dd
VVVV
VVVVD
d
d
VV
VVE
dd
dd
VVVV
VVVVD
w
v
wv
1
/
/
)()(
)()(
)()(
)()()(
)(
)()(
)()(
2101
2000
1210
0200
1121
0120
1112
0102
21
20
12
02
1101
0100
1110
0100
121110
020100
)()(
)()(
VVVVwVVv
VVVVwVVv
SOLVING FOR BARYCENTRIC COORDINATES
• The following code is taken from “Real-Time Collision Detection” by Christer Ericson (pg. 47-48)
void Barycentric(Point a, Point b, Point c, Point p, float &u, float &v, float &w) {Vector v0 = b – a, v1 = c – a, v2 = p – a;float d00 = Dot(v0,v0);float d01 = Dot(v0,v1);float d11 = Dot(v1,v1);float d20 = Dot(v2,v0);float d21 = Dot(v2,v1);float denom = d00 * d11 – d01 * d01;v = (d11 * d20 – d01 * d21) / denom;w = (d00 * d21 – d01 * d20) / denom;u = 1.0f – v – w;
}
LIGHT SOURCES
LIGHTING MODEL AND LIGHTS
• Lighting model (or shading model) = used to calculate the color of an illuminated position on the surface of an object
• Light source (or “light”) = emits light (radiant energy)
TYPES OF LIGHT SOURCES
• There are three basic types of light sources:
• Point lights
• Directional lights
• “Spotlights”
POINT LIGHTS
• Point light
• Located at a single point in space
• Emit light in all directions
DIRECTIONAL LIGHT (“SUN”)
• Directional Light
• Emits light in one direction
• Position of light doesn’t matter only the direction matters
• Simulates a light “infinitely” far away (e.g., the sun)
“SPOTLIGHTS”
• Spotlight
• Has a position, direction, and angle θ defining how wide the cone of light is
• If Vlight = direction of the light and Vobj = direction to object, then:
cos lightobj VV
conelight inside Otherwise,
conelight outsidecos lightobj VV
RADIAL INTENSITY ATTENUATION
• Let’s say we have a point light source
• The farther an object is from the light the less light reaches the object (less intense)
• Officially: attenuation factor = 1/d2
• In practice, however, this doesn’t look realistic:
• Objects close to light too much intensity variation
• Objects far from light very little variation
• Reason why it doesn’t look right: lights aren’t REALLY infinitesimal points only an approximation
• Alternative: use inverse quadratic function with tunable parameters:
2210
1)(
dadaadAradial
ATTENUATION AND DIRECTIONAL LIGHTS
• Attenuation doesn’t make sense with directional lights since light is “infinitely” far away, all points in scene equidistant from light
ANGULAR INTENSITY ATTENUATION
• For spotlights, as angle between object and light direction gets larger cosine gets smaller
• Can use as part of attenuation factor
otherwiseVV
VVA a
lightobj
lightobjangular )(
coscos if0
BASIC LIGHTING MODEL
INTRODUCTION
• When light hits an opaque surface, part of it reflected and part of it absorbed
• For transparent surfaces, light is also transmitted through the surface
• We’ll start by assuming a monochrome world (i.e., only black-and-white-and-shades-of-gray)
• So, we’ll refer to the intensity of the light as a single value (we’ll deal with RGB color later)
• For simplicity, we often determine the shading of an object based on three factors:
• Diffuse reflection
• Ambient light
• Specular reflection
DIFFUSE REFLECTION
• Diffuse reflection = when white light hits an object, what we see as the “color” of an object
• Example: apple absorbs all frequencies except red has red diffuse color
• Underlying physics: surfaces with microfacets (bumpy, grainy, matte) reflects light in lots of different directions
• Ideal diffuse reflectors or Lambertian reflectors
• Incident light scattered with equal intensity in ALL directions, INDEPENDENT of viewing angle
• Depends on angle between NORMAL and DIRECTION-to-LIGHT angle of incidence
DIFFUSE REFLECTION: SIMPLE VERSION
• kd = diffuse intensity (“color”) of surface
• I = light intensity
• N = normal
• L = vector to light from point
• The larger the angle between N and L the less light hits the surface (and gets reflected off)
• Use dot product of N and L (as long as it’s greater than 0)
0)(if 0
0)( if )(cos
LN
LNLNIkIkDiffuse d
d
DIFFUSE REFLECTION: DIFFERENT LIGHT SOURCES
• Point lights L = vector from current position to light position
• Directional light L = direction vector for light
AMBIENT LIGHT
• Ambient light = sets the overall brightness of the scene
• Applied to ALL objects equally
• Approximating global illumination (in real world, light bouncing around different objects)
• To compute ambient light contribution to point on surface:
• Sometimes graphics packages will use a separate ambient coefficient for the surface:
intensitylight ambient aI
ad IkAmbient
aa IkAmbient
DIFFUSE + AMBIENT LIGHT
• Note: all these factors vary from 0 to 1
0)( if
0)( if )(
LNIk
LNLNIkIkAmbientDiffuse
aa
daa
SPECULAR REFLECTION
• Specular reflection = when all (or almost all) of the incident light is reflected back
• A shiny or reflective spot
• Depends on:
• Normal N
• Light vector L
• View vector V = vector from point to camera
• R = ideal reflection angle
• When R is in line with V most reflective
SPECULAR REFLECTION: DULL AND SHINY
• Different surfaces reflect light over finite range of viewing positions
• Shiny surfaces narrow range
• Duller surfaces wider range
SPECULAR RELFECTION:PHONG SPECULAR-REFLECTION MODEL
• Phong specular-reflection model
• Angle φ = angle between R and V
• ns = specular-reflection exponent
• Sometimes called “shininess”
• If we use a constant coefficent for specular reflection, then:
sns IkSpecular cos
SPECULAR REFLECTION: GETTING R
• L projected onto N is the same as R projected onto N; therefore:
• Our specular component can then be computed by:
LNLNR
NLNLR
)2(
)2(
0)(or 0)( if 0
0)( and 0)( if )(
LNRV
LNRVRVIkSpecular
sns
SPECULAR REFLECTION: A SIMPLER WAY• A simplified way to calculate specular reflection is to look at the
halfway vector H between L and V:
• Now, we just look at the angle between N and H (or rather the dot product between them):
• Problem: if V, L, and N are not coplanar slightly off
• Another problem: need to check if V and L are on same side of N (L · V > L · N) if so, don’t use specular effect
VL
VLH
sns HNIkSpecular )(
GETTING THE VIEW VECTOR
• To get the vector from the current point to the viewer, you need:
• 3D WORLD position P of the point on the polygon
• PROBLEM: In vertex/pixel shader, have NORMALIZED DEVICE COORDINATES
• SOLUTION: Also pass in INVERSE Model-View-Projection transform to UNDO transformation
• P = invMVP*position
• 3D position of camera E
• To get the vector to the camera:
PE
PEV
)(
DIFFUSE + AMBIENT + SPECULAR
• So, for a single point light source, our combined diffuse, ambient, and specular reflections from a position on an illuminated surface are given by:
• NOTE: If light is behind surface, only use ambient light
• ALSO NOTE: If V and L are on the same side of N, no specular effects
snsdaa HNIkLNIkIk
SpecularDiffuseAmbient
)()(
MULTIPLE LIGHT SOURCES
• To deal with multiple light sources, just sum all values up for diffuse and specular components (ambient light is only added once, however):
n
i
nsdiaa
sHNkLNkIIkFinal1
)()(
SURFACE LIGHT EMISSIONS
• In addition to lights, one can also have surfaces that emit light
• Examples: fluorescent light bulbs, neon signs, anything from Tron,…
• Have addition term ke that is handled like an ambient term (except that it is specific to the surface)
• TECHNICALLY, light coming from the surface should affect other objects in the world:
• Use directional light with cone/box around it to determine area of effect
• Use lots of point lights
• Ignore it to save time (more common)
• Radiosity model (covered later)
http://thedisneyblog.com/wp-content/uploads/2012/06/tron-uprising-art.jpg
DIFFUSE + AMBIENT + SPECULAR + SURFACE EMISSION + ATTENUATION
• The general, monochromatic illumination model for surface reflection that includes:
• Multiple light sources
• Attenuation factors
• Point lights, directional lights, spotlights, and surface emissions
• …is given by:
n
ispeculardiffuseangularradialaae IIAAIkkFinal
1
)(
0)(if 0
0)( if )(
LN
LNLNIkI ddiffuse
otherwise 0
0.0)( if }))(,0(max{ LNHNIkI
sns
specular
2210
1
dadaaAradial
otherwiseVV
VVA a
lightobj
lightobjangular )(
coscos if0
THE ILLUMINATION FROM THIS PIXEL IS TOO HIGH!
• The final output intensity has to be between 0 and 1 how to do we keep it in that range when we have lots of lights, etc.?
• Set a max value for each term in equation
• If exceed max value, set to max
• Normalize individual terms by dividing each by magnitude of largest term
• More complicated: get ALL pixel intensities for scene, and then scale set of intensities to range [0,1]
n
ispeculardiffuseangularradialaae IIAAIkkFinal
1
)(
RGB COLOR
• For color lights and surfaces, our intensity values now become vectors:
• Examples:
• Our equations now become vector equations
aBaGaRa
dBdGdRd
IIII
kkkk
,,
,,
COLOR REFLECTIONS?
• In the original specular-reflection model, ks was set to a constant value INDEPENDENT of the surface color usually same color as light
• Gives surface a “plastic” appearance
• For nonplastic surfaces specular color may be different from both light color and diffuse color of object!
POLYGON SURFACE RENDERING
INTRODUCTION
• Given a surface’s color information (diffuse, specular, etc.) and the light color information, how do we actually render the polygon?
• There are three major approaches for this, and the difference between them is at what level we compute the lighting equation:
• Flat shading per primitive (triangle)
• Gouraud shading per vertex
• Phong shading per pixel/fragment
FLAT SHADING
• Flat shading = compute shading per primitive (triangle)
• All pixels/fragments have the same color
• Basically use the same normal for the whole triangle
• Advantages:
• Fastest
• Accurate if surface that the polygon is part of is NOT a curved surface
• Disadvantages:
• Curved surfaces look terrible transitions between polygons is sudden
•
GOURAUD SHADING
• Gouraud shading = compute shading per-vertex
• Interpolate COLOR values to fill in pixels
• Use per-vertex normal
• Pronounced “guh-row”
GOURAUD SHADING: PER-VERTEX NORMAL?• Usually, we think of the POLYGON as having a normal
• For a CURVED surface:
• To get the per-vertex normal, we get the average of all normals from the polygons the vertex is a part of:
• For a NON-CURVED surface:
• Sometimes we actually don’t want the surface to smoothly transition from one polygon to another
• To address this, duplicate the vertex so that you get two different average normals:
n
kk
n
kk
v
N
NN
1
1
Curved surface Duplicate vertex to create crease
GOURAUD SHADING: PROS AND CONS
• Advantages:
• Looks better than flat shading for curved surfaces
• Fairly efficient
• Disadvantages:
• Depending on vertex tessellation (i.e., how many vertices/polygons), specular highlights don’t look right
• Kind of smearing the highlight across the polygon
Fewer polygons More polygons
PHONG SHADING
• Phong shading = compute shading per-pixel/fragment
• Interpolate NORMALS across surface (from vertex normals)
• Advantages:
• Highest quality rendering
• Disadvantages:
• More computationally intensive (have to compute shading formula at each pixel)
• Overkill for non-curved surfaces (and we will have to duplicate vertices to get the creases we want)
FLAT VS. GOURAUD VS. PHONG
Flat shading- Fastest- Lowest quality- Bad for curved surfaces
Gouraud shading- Not as fast- Better quality- Specular highlights may not look right
Phong shading- Slowest - Highest quality
OPENGL?
• In legacy, handling lights, what kind of shading you were using, etc. was a LOT more complicated
• In modern OpenGL, you pretty much handle this yourself with the vertex/pixel shaders:
• Gouraud shading:
• Compute per-vertex color
• Pass to fragment shader to be interpolated
• Use interpolated color directly for fragment
• Phong shading:
• Pass per-vertex NORMAL to fragment shader to be interpolated
• Compute fragment color in fragment shader
• For flat shading, pass in same normal for all vertices that are part of the same triangle