Local Illum
Local Illum
Kalpathi Subramanian
Computer Science
The University of North Carolina at Charlotte
Learning Objectives
1 Explain the motivation behind shading in computer graphics
2 Distinguish between local and global illumination
3 List the illumination models and their rationale that make up the
local illumination model used in the graphics pipeline for shading
3D objects
4 Illustrate how the ambient, diffuse and specular reflection models
are evaluated through examples.
5 Distinguish between Phong and Gouraud shading models used to
shade polygonal meshes, and state their computational tradeoffs.
Illumination and Shading
Why Shade Objects?
We want renderings to be realistic, i.e. like how they would appear in
the real world.
Difficult to perceive objects in 3D with constant illumination, for eg.,
objects appear flat
Need to understand how light interacts with matter
Governed by the Rendering Equation (Kajiya ’86), a scattering
model.
Z
′ ′ ′ ′ ′′ ′ ′′ ′′
I (x, x ) = g (x, x ) ϵ(x, x ) + ρ(x, x , x )I (x , x )dx
S
′ ′′
where x, x , x are object points, ρ is surface reflectance, g is a
geometry term accounting for visible surface calculations
Cannot solve analytically, so must approximate the solution, leading
to a number of global illumination algorithms
We will initially focus on local illumination models.
Light Scattering/Absorption
Multiple Scattering
Local Illumination:
▶ Assumes a set of designated lights in the environment
▶ Accounts for only illumination at object points directly arriving
from designated light sources in scene that are visible to the viewer.
Global Illumination:
▶ Also consider illumination from all other object points, due to all other
light transport mechanisms, such as transmission, reflection.
▶ Each object point in the scene can act as a secondary light
source, receiving and sending illumination along various paths.
▶ Primary light sources are emitters, secondaray sources usually are not.
Most often, in computer graphics, we can get away with local
illumination models for a large number of applications, which can take
advantage of existing graphics hardware.
In recent years, significant advances have been made in supporting
global illumination algorithms in hardware.
What influences Shading on 3D objects?
Light-Material Interactions
Area Sources: Illumination comes from all points in the source, and
generally expensive to model/compute.
Point Source: Model with position, color
Distant Source: Model with orientation, all illumination rays are
parallel.
Spotlight: Restrict illumination to a specified angle around the point
source.
Ambient: Constant source of illumination, used to model illumination
from secondary sources (reflection, transmission), global illumination.
What influences Shading on 3D objects?
Surface Types
Smooth Surface: Reflected illumination is concentrated in a
direction, governed by the laws of reflection (approaching mirror)
Rough Surface: Scatters light in all directions with equal intensity.
Translucent Surface: Allows part of light to go through the surface,
and dependent on refractive index of the material. And some
incident reflection.
Iambient = ka Ia
Idiffuse = Ip Kd cos(θ)
⃗ ⃗L)
= Ip Kd (N.
Local Illumination Models in Computer Graphics
64
4 Cos α
Cos ( α ) Cos α
α 90 α 90 α 90
Ispec = ks cos n α
⃗ R)
where α = cos −1 (V. ⃗ is the angle between the viewer and reflection
direction.
Local Illumination Models in Computer Graphics
Modified Phong Model (Blinn’77)
⃗ = (⃗L + V
⃗)
H ,
|⃗L + V
⃗|
Ispec = ks cos n ψ = ks (H ⃗ n
⃗ • N)
Local Illumination Models in Computer Graphics
Motivation
In the early days of computer graphics, realtime rendering of polygons
was expensive
Graphics hardware was in its infancy, memory was expensive
Need for more efficient algorithms to render large polygonal meshes
Computing Normals: Polygons
Ax + By + Cz + D = 0
⃗ = (A, B, C ) to the polygon surface
where the normal vector N
If V1 , V2 , V3 are points on the polygon
⃗ = (V⃗2 − V⃗1 ) × (V⃗3 − V⃗1 )
N
= (A, B, C )
where V⃗1 , V⃗2 and V⃗3 are non-collinear points in the polygon
Polygonal Shading Models
Computing Normals: Sphere
Phong Shading
I (D) =?
I (E ) =?
Transforming Normals by an Affine Transform
Normals are direction vectors, not object points!
Applying an arbitrary affine transform M to a normal vector n⃗ can
result in incorrect normals.
Consider a tangent plan t and a normal vector ⃗n in that plane.
We want to ensure the transformed normal (by some matrix N) is
orthogonal to the plane that transform t by M, i.e.,
(M.t) • (Nn) = 0, transformation results in a new plane
(M • t.)T • N.⃗
n = 0, must take transpose for matrix vector multiply
(t T M T ).(N n⃗) = 0
t T (M T N)⃗
n = 0
v o i d main ( ) {
vec3 pos = ( modelViewMatrix ∗ v P o s i t i o n ) . xyz ;
vec3 l i g h t = l i g h t P o s . xyz ;
vec3 L = n o r m a l i z e ( l i g h t − pos ) ;
v e c 3 E = n o r m a l i z e (−p o s ) ; // camera i s a t o r i g i n
vec3 H = normalize (L + E ) ;
v e c 3 N = n o r m a l i z e ( ( modelView ∗ vNormal ) . x y z ) ; // t r a n s f o r m n o r m a l
// Compute t e r m s i n t h e i l l u m i n a t i o n e q u a t i o n
vec4 ambient = ambientProduct ;
f l o a t Kd = max ( d o t ( L , N) , 0 . 0 ) ;
vec4 d i f f u s e = Kd∗ d i f f u s e P r o d u c t ;
f l o a t Ks = pow ( max ( d o t (N, H) , 0 . 0 ) , s h i n i n e s s ) ;
vec4 s p e c u l a r = Ks∗ s p e c u l a r P r o d u c t ;
i f ( d o t ( L , N) < 0 . 0 ) { // l i g h t s o u r c e i s n o t v i s i b l e
s p e c u l a r = vec4 ( 0 . 0 , 0.0 , 0.0 , 1 . 0 ) ;
}
f C o l o r = ambient + d i f f u s e + s p e c u l a r ;
fColor . a = 1.0;
g l P o s i t i o n = p r o j e c t i o n ∗ modelView ∗ v P o s i t i o n ;
}
Per Vertex Shading in WebGL: Fragment Shader
v a r y i n g vec4 f C o l o r ;
v o i d main ( ) {
gl FragColor = fColor ;
}
a t t r i b u t e v e c 4 v P o s i t i o n , vNormal ;
v a r y i n g v e c 3 N, L , E ;
u n i f o r m mat4 modelView , p r o j e c t i o n ;
uniform vec4 l i g h t P o s ;
v o i d main ( ) {
vec3 pos = ( modelViewMatrix ∗ v P o s i t i o n ) . xyz ;
vec3 l i g h t = l i g h t P o s . xyz ;
vec3 L = n o r m a l i z e ( l i g h t − pos ) ;
v e c 3 E = n o r m a l i z e (− p o s ) ; // camera i s a t o r i g i n
vec3 H = normalize (L + E ) ;
v e c 3 N = n o r m a l i z e ( ( modelView ∗ vNormal ) . x y z ) ; // t r a n s f o r m n o r m a l
g l P o s i t i o n = p r o j e c t i o n ∗ modelView ∗ v P o s i t i o n ;
}
Per Fragment Shading in WebGL: Fragment Shader
Local lighting model is applied to each position using interpolated values of
vertex position, normal vector, light vector and vector to the viewer
i f ( d o t ( L , N) < 0 . 0 ) { // l i g h t s o u r c e i s n o t v i s i b l e
s p e c u l a r = vec4 ( 0 . 0 , 0.0 , 0.0 , 1 . 0 ) ;
}
f C o l o r = ambient + d i f f u s e + s p e c u l a r ;
fColor . a = 1.0;
gl FragColor = fColor ;
}