Professional Documents
Culture Documents
2IV60 8 Shading
2IV60 8 Shading
H&B 17:531-532
Introduction 2
Example:
• Illumination model gives color
vertices,
• Surface is displayed via
interpolation of these colors.
H&B 17:531-532
Introduction 3
Illumination:
• Physics:
– Material properties, light sources, relative
positions, properties medium
• Psychology:
– Perception, what do we see
– Color!
• Often approximating models
H&B 17:531-532
Light sources 1
Light source: object that radiates energy.
H&B 17-1:532-536
Light sources 2
Simple model: point light source
- position P and intensity I
- Light rays along straight lines
- Good approximation for small
light sources
H&B 17-1:532-536
Light sources 3
Simpler yet: point light source at infinity
- Direction V and intensity I
- Sunlight
H&B 17-1:532-536
Light sources 4
Damping: intensity of light decreases with distance
Energy is distributed over area sphere, hence
Il = I / d 2 ,
d
with d distance to light source.
In practice often too ‘agressive’,
hence Il = I / (a0 +a1d+a2d2)
P
Q
l
light cone
Vlight H&B 17-1:532-536
Surface illumination 1
• When light hits a surface, three things can
happen:
reflection
absorption
transmission
H&B 17-2:536-537
Surface illumination 2
• Suppose, a light source radiates white light,
consisting of red, green and blue light.
reflection
If only red light is reflected,
absorption then we see a red surface.
transmission
H&B 17-2:536-537
Surface illumination 3
• Diffuse reflection: Light is uniformly reflected in
all directions
• Specular reflection: Light is stronger reflected in
one direction.
specular reflection
diffuse reflection
H&B 17-2:536-537
Surface illumination 4
• Ambient light: light from the environment. Undirected
light, models reflected light of other objects.
H&B 17-2:536-537
Basic illumination model 1
Basic illumination model:
• Ambient light;
• Point light sources;
• Ambient reflection;
• Diffuse reflection;
• Specular reflection.
Ia
I l , P of V
ka k a , k d , k s : reflection coefficients
kd k p (k p,red , k p,green , k p,blue )
k s , ns
H&B 17-3:537-546
Basic illumination model 2
• Ambient light: environment light. Undirected light,
models reflected light of other objects.
I amb k a I a
H&B 17-3:537-546
Basic illumination model 3
Perfect diffuse reflector: light is reflected
uniformly in all directions.
dA energy
Intensity
projected area.
dA
dA / cos
dA/cos cos
H&B 17-3:537-546
Basic illumination model 4
Perfect diffuse reflector: light is reflected uniformly in all directions.
.
dA N Lambert’s law:
Reflected energy is
L
proportional with cos , where
denotes the angle between the
normal N and a vector to the light
dA/cos source L.
H&B 17-3:537-546
Basic illumination model 5
Perfect diffuse reflector: light is reflected
uniformly in all directions.
Psource N Graphics model diffuse reflection :
Il k d I l (N L) if N L 0
I l,diff
0 if N L 0
L
Psurf with 0 k d 1 and
Psource Psurf
L
| Psource Psurf |
H&B 17-3:537-546
Basic illumination model 6
Perfect specular reflector: light is only reflected in one
direction. Angle of incidence is angle of reflection.
N
L R
H&B 17-3:537-546
Basic illumination model 7
Imperfect specular reflector: light is distributed in the
direction of the angle of reflection, dependent on the
roughness of the surface.
N N
L R L R
I l , spec W ( ) I l cos ns ,
N
with W ( ) k s ,
L R
ns smoothness (1 ruw, 100 glad),
V : angle between N and L,
: angle between R and V,
R : direction reflected ray of light
V : direction viewer
H&B 17-3:537-546
Basic illumination model 9
Phong model: empirical model for specular reflection
N ns if V R 0
k
s l I ( V R )
L R and N L 0
I l , spec
V 0 if V R 0
or N L 0
H&B 17-3:537-546
Basic illumination model 10
Phong model: calculating the vectors
L R L ( 2N L) N
L N R hence
V R ( 2N L) N L
N.L
Pview Psurf
V
| Pview Psurf |
H&B 17-3:537-546
Basic illumination model 11
Phong model: variant with halfway vector H.
Use instead of
N LV
H H
L R |LV|
V I l , spec k s I l (N H ) ns
If light source and viewer far away:
H constant.
H&B 17-3:537-546
Basic illumination model 12
All together:
I I amb I dif I spec
k a I a k d I l (max(0, N L)) k s I l (max(0, N H )) n s
H&B 17-3:537-546
Basic illumination model 13
Color (reprise):
Light intensity I and reflection coefficients k: (r,g,b) triplets
So for instance: I dif , R k d , R I l , R (max(0, N L))
H&B 17-4:546-549
Transparancy 2
i
Snell’s law of refraction: sin r sin i , : index of refraction
r
i
N T cos i cos r N i L
r r
L R
i i Derivation :
Use Snell's law,
T.T 1,
T N cos r ,
T N L,
and solve for and
r T
H&B 17-4:546-549
Transparancy 3
Thin surface:
- double refraction
- shift of light ray
H&B 17-4:546-549
Transparancy 3
Very thin surface:
- Discard shift
Simple model :
I (1 kt ) I refl kt I trans
0 kt 1
kt : transparancy
1 kt: opacity
H&B 10-5:549-550
Atmospheric effects 2
Attenuation by atmosphere :
f atmo (d ) e d , with
d : distance
: attenuation factor
d d min
Simpler : f atmo (d )
d max d min
Perceived intensity :
I f atmo (d ) I obj [1 f atmo (d )]I atmo
H&B 17-10:559-564
Rendering polygons 2
Constant-intensity rendering aka flat surface rendering:
• Determine color for center of polygon;
• Fill the polygon with a constant color.
Ok if:
• Object consists of planar faces, and
• Light sources are far away, and
• Eye point is far away,
or
• Polygons are about a pixel in size.
H&B 17-10:559-564
Rendering polygons 2
Constant-intensity rendering aka flat surface rendering:
• Determine color for center of polygon;
• Fill the polygon with a constant color.
Angel (2000)
H&B 17-10:559-564
Rendering polygons 2
Gouraud surface rendering:
• Determine average normal on vertices;
• Determine color for vertices;
• Interpolate the colors per polygon (incrementally).
N2
n
N3 Nk
N1 NV k 1
V
n
N4 k 1
Nk
H&B 17-10:559-564
Rendering polygons 3
Gouraud surface rendering:
• Much better result for curved surfaces
• Errors near highlights
• Linear interpolation still gives Mach banding
• Silhouettes are still not smooth
Gouraud Flat
Rendering polygons 4
Phong surface rendering:
• Determine average normal per vertex;
• Interpolate normals per polygon (incrementally);
• Calculate color per pixel.
H&B 17-10:559-564
Rendering polygons 5
Phong surface rendering:
• Even better result for curved surfaces
• No errors at high lights
• No Mach banding
• Silhouettes remain coarse
• More expensive than flat or Gouraud shading
H&B 17-10:559-564
Rendering polygons 5
Flat
Gouraud
Phong
H&B 17-10:559-564
OpenGL Illumination
Glfloat lightPos[] = {2.0, 0.0, 3.0, 0.0};
Glfloat whiteColor[] = {1.0, 1.0, 1.0, 1.0};
Glfloat pinkColor[] = {1.0, 0.5, 0.5, 1.0};
H&B 17-11:564-574
OpenGL Light-sources 1
First, enable lighting in general:
glEnable(GL_LIGHTING);
Two-sided lighting:
glLightModelI(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
H&B 17-11:564-574
OpenGL Surface properties 1
Surface reflection parameters:
H&B 17-11:564-574
OpenGL Surface properties 2
If colors are changed often (for instance, per vertex):
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glBegin(…);
for i = ...
for j = ...
glColor3f(red(i,j), green(i,j), blue(i,j));
glVertex3f(x(i,j), y(i,j), z(i,j));
glEnd(…);
H&B 17-11:564-574
OpenGL Surface properties 3
Transparent surfaces:
• First, draw all opaque surfaces;
• Next, draw transparent surfaces, back to front*, using
something like:
glDisable(GL_BLEND);
* OpenGL cannot automatically handle transparency, because of the
z-buffer algorithm used for hidden surface removal. More on this
later. H&B 17-11:564-574
OpenGL Surface properties 4
Color Blending (see also H&B: 135-136):
Source: the new graphics object to be drawn;
Destination: the current image built up.
glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
// Use alpha of source as transparency
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Use alpha of source as opacity
H&B 17-11:564-574
More options available for special effects.
OpenGL Surface-Rendering 1
glShadeModel(m): specify the rendering method
m = GL_FLAT or m = GL_SMOOTH (Gouraud,
default)
glNormal*(Nx, Ny, Nz) : specify the normal vector
Smooth version:
Flat version:
glBegin(GL_TRIANGLES);
glNormal3fv(nV); glNormal3fv(nV1);
glBegin(GL_TRIANGLES); glVertex3fv(V1);
glVertex3fv(V1); glNormal3fv(nV2);
glVertex3fv(V2); glVertex3fv(V2);
glVertex3fv(V3); glNormal3fv(nV3);
glEnd(); glVertex3fv(V3);
glEnd(); H&B 17-11:564-574
OpenGL Surface-Rendering 2
glShadeModel(m): specify the rendering method
m = GL_FLAT or m = GL_SMOOTH (Gouraud,
default)
glNormal*(Nx, Ny, Nz) : specify the normal vector
H&B 17-11:564-574
Next