Gouraud Shading Gouraud Shading Normal Interpolation

Transcription

Gouraud Shading Gouraud Shading Normal Interpolation
Surface Normal Interpolation
Determining Vertex Normals
• If a normal at each point on the surface is available then we can apply
Lambertian shading as shown.
• Often we are approximating curved/smooth surfaces with polygons ⇒
we get edge artifacts at polygon boundaries:
• To combat this we determine average normals at the vertices of the
polygons by averaging the normals of each polygon that shares a
vertex and storing the result with that vertex.
Gouraud Shading
vertices shared between
both polygons
Nv =
1
1
(N1 + N 2 )
(N + N 2 )
2 1
2
vertices not shared
Gouraud Shading
• For each interior point in the polygon being shaded we
interpolate the intensity determined at the vertices.
• We do this in exactly the same way that we interpolated
colour across the surface of a polygon.
• This is known as Gouraud shading.
⇒ we need to do lighting calculations at vertices only
⇒ lighting is correct at vertices only
• This is OK if the polygons are small. As polygons
increase the errors also increase leading to often
unacceptable results:
Normal Interpolation
Interpolation errors with Gouraud shading:
• To improve upon Gouraud shading we can interpolate the
normals across the surface and apply the lighting model at
each point in the interior.
• This assumes we are working with polygonal models.
• If an analytic normal exists then we should use it instead:
– we do this in ray tracing, but for OpenGL® we are always working
with polygons
• Care must be taken to ensure that all interpolated normals
are of unit length before employing the lighting model.
• This is known as Phong shading (as opposed to the
Phong illumination model).
1
Phong Shading
Phong Shading
• Phong shading is capable of reproducing
highlights within the interior of a polygon that
Gouraud shading will miss:
Phong
Phong Illumination Model
• Specular surfaces exhibit a high degree of coherence in
their reflectance, i.e. the reflected radiance depends very
heavily on the outgoing direction.
– An ideal specular surface is optically smooth (smooth even at
resolutions comparable to the wavelength of light).
– Most specular surfaces (rough specular) reflect energy in a tight
distribution (or lobe) centered on the optical reflection direction:
The Cosn Function
Gouraud
Phong Illumination Model
• To simulate reflection we should examine surfaces in the
reflected direction to determine incoming flux
⇒ global illumination
• A local illumination approximation considers only
reflections of light sources.
• The Phong model is an empirical (based on observation)
local model of shiny surfaces (tend to appear like plastic).
• It is assumed that the BRDF of such surfaces may be
approximated by a spherical cosine function raised to a
power (known as the Phong exponent).
The Phong Model
The cosine function (defined on the sphere) gives us a lobe shape which
approximates the distribution of energy about a reflected direction controlled
by the shininess parameter n known as the Phong exponent.
light source irradiance
specular reflectivity
θ
r
Lr , s ( x,V ) =
r = cos θ
n
n+2
Φ
ρ s cos n θ S 2
2π
4πd
cosine lobe
x
In the limit (n → ∞) the function becomes a single spike (i.e. ideal specular).
This is sometimes called the delta function.
normalising term
Radiance of reflected light given by cosine function
2
Pure Lambertian vs. Phong
Ambient Illumination
• Local illumination models account for light scattered from
the light sources only.
• Light may be scattered from all surfaces in the scene
Lambertian Surface
Phong Illuminated Specular Surface
– we are missing some light; in fact we are missing a lot of light,
typically over 50%.
• Ambient term = a coarse approximation to this missing
flux
• Ambient term defined by the ambient coefficient ρa:
I a = ρa I s
• This is a constant everywhere in the scene.
• The ambient term is sometimes estimated from the total
powers and geometries of the light sources.
Ambient Illumination
Putting it all together...
• The complete Phong Illumination Model includes
ρa = 0
ρa > 0
– Lambertian model for diffuse reflection
– Cosine lobe for specular reflection
– Ambient term to approximate all other light
• An object must therefore have material data
associated with it to define how diffuse, specular
(and shiny) or ambient it is:
ρa = ambient reflectance
ρ = diffuse reflectance

Surface Data =  d
ρs = specular reflectance
n = phong exponent
Point Light Sources
• The irradiance E of a surface due to a point source obeys
the inverse square law: E = Φ S = I S
4πd 2 d 2
• However, this often makes it difficult to control the lighting
in the scene, so we employ a less accurate but more
flexible model of irradiance:
IS
E=
a + bd + cd 2
– a = constant attenuation factor
– b = linear attenuation factor
– c = quadratic attenuation factor
Phong Illumination Model
Lr ( x, V ) = Lr ,a ( x ) + Lr ,d ( x ) + Lr , s ( x, V )
= (ambient + diffuse + specular)
ρ
n+2
n
= Eρa + E d ( N ⋅ L ) + E 
ρ s (V ⋅ R )
π
 2π 
=
Φs
1
4π a + bd + cd 2
ρd

n+2
n
ρ a + π ( N ⋅ L ) +  2π ρ s (V ⋅ R ) 




For multiple light sources:
N
Φ

ρ
1
n+2
n 
ρ + d ( N ⋅ Li ) + 
Lr ( x, V ) = ∑  i
ρ s (V ⋅ Ri )  
2  a
π
 2π 
i =1  4 π a + bd i + cd i 

3
Phong Illumination Examples
Determination of the Reflected Vector
N (− I ⋅ N )
N (− I ⋅ N )
I
N (− I ⋅ N )
R = L + 2 N (− L ⋅ N )
= L − 2 N (L ⋅ N )
Implementation
• It is important to check to
see if the surface is facing
the lightsource prior to
employing the illumination
model.
• Otherwise we could get
negative dot-products:
if ( N ⋅ L ) > 0 then detemine Lr ,d
if (R ⋅ V ) > 0 then detemine Lr ,s
(N ⋅ L ) > 0
Half Vector Formulation
• Because determination of the
reflected vector R given V and
N is time consuming, an
alternative formulation employs
the half vector H in place of R:
H=
(N ⋅ L ) < 0
Other Light Source Types
• We can extend the functionality of the illumination model
by admitting a number of other light source types:
– directional: the source is assumed to be at infinity and therefore is
represented by a direction rather than a position.
– spot lights: we can admit illumination only within a restricted solid
angle.
V +L
2
• The Phong specular term is
then reformulated in terms of
H:
Lr ,s ∝ cosn (N ⋅ H )
Spot Lights
• Originally proposed by Warn (1983): a light source is
defined by a position, a direction, a cutoff angle and an
exponent.
• The radiant intensity of the spot light in a given direction is
defined by a model similar to Phong’s illumination model:
P−x
d
if cos -1 (− I ⋅ L ) < θ then
d = P−x
Directional Source
direction to light source
is constant everywhere
⇒ don’t need to calculate I for each xi
⇒ light radiant intensity is constant
(i.e. does not vary with distance)
Es =
I=
Φs
 (− I ⋅ L ) 
4π a + bd + cd 2  cos θ 
(
n
)
spotlight attentuation
4
Spot Lights
Incorporating Colour
smaller cutoff angle
n=0
• In fact, many of the systems that implement the Phong
model differ only in the manner in which colour is handled.
• Typically we handle diffuse and specular illumination
separately:
– diffusely reflected light results from the reflection via multiple
scattering events in the micro-scale geometry ⇒ reflected light is
coloured by selective absorption by the surface i.e. a green
surface absorbs all wavelengths except green
– specularly reflected light interacts once with the surface and is
thus not coloured by the surface i.e. the reflection of a light source
takes on the colour of the source
low n
n=1
Shading and OpenGL®
Incorporating Colour
• OpenGL supports 8 simultaneous light-sources, named
GL_LIGHT0 to GL_LIGHT7 which must be enabled for use:
• Therefore the Phong model becomes:
Lr ( x, V ) = Lr ,a ( x ) + Lr ,d ( x ) + Lr ,s ( x, V )
= ECambient ρ a + ECsurface
glEnable(GL_LIGHT1);
ρd
( N ⋅ L ) + EClight  n + 2 ρ s (V ⋅ R )n
π
 2π 
• Specification of light parameters using glLight*():
glLightf{iv}(GL_LIGHT0, param, value);
• Usually however, for flexibility, the ambient, diffuse and specular
reflections are scaled independently by colour vectors:
Lr ( x, V ) = ECambρ a + ECdiff
GL_AMBIENT
GL_DIFFUSE
Light source colour = intensity
GL_SPECULAR
GL_POSITION
GL_SPOT_DIRECTION
spotlight parameters
GL_SPOT_EXPONENT
GL_SPOT_CUTOFF
GL_CONSTANT_ATTENTUATION
GL_LINEAR_ATTENUATION
irradiance control
GL_QUADRATIC_ATTENUATION
ρd
(N ⋅ L ) + ECspecClight  n + 2 ρ s (V ⋅ R )n
π
 2π 
• The model is applied using colour vectors yielding the final colour vector
to assign to a pixel.
• Note that only the specular term is scaled by the light’s colour.
Shading and OpenGL®
• Note that light source positions and directions (for
spotlights) are affected as normal by OpenGL
transformations.
glEnable(GL_LIGHTING);
• To enable lighting use:
• OpenGL does not support true Phong shading; it
interpolates the intensities across each polygon ⇒
Gouraud shading glShadeModel(GL_SMOOTH);
• We then specify lighting model details:
glLightModel{if}v(param, value);
GL_LIGHT_MODEL_AMBIENT
GL_LIGHT_MODEL_LOCAL_VIEWER
GL_LIGHT_MODEL_TWO_SIDE
specifies a global ambient term
boolean specifying viewer at ∞
shade both sides of a polygon?
•
OpenGL® Material
Properties
Each polygon is assigned material properties to define
how it reflects light from each light source.
• We can assign different properties to each side of the
polygon:
glMaterial{if}v(face, param, value);
GL_FRONT
GL_BACK
GL_FRONT_AND_BACK
GL_AMBIENT
GL_DIFFUSE
GL_AMBIENT_AND_DIFFUSE
GL_SPECULAR
GL_EMISSION
GL_SHININESS
RGB Values
Phong Exponent
5
2-sided lighting
Materials
1-sided lighting
Look at Lightlab example
on samples page.
Coloured Spotlights
6