VIEWS: 12 PAGES: 28

• pg 1
```									         Lighting and Shading

Light
Incoming
Source                                          Reflected light
(Incident) light

Object
Surface

Refracted light
Light Source
Point Light Source

Light emits from a single point
in all directions

Parallel (Directional) Light Source

Light source is at infinity.
Light rays are parallel lines.                                 V

V:   Light direction vector
Spotlight
Light emits from a single point. Light rays are confined to a cone.

V
c

c:      Cut-off angle
V:       Light direction vector (Cone Axis)

Extended Light Source
Light source with a finite surface area.
Can be approximated as multiple point light sources.
Light intensity decreases as light travels away from the light source.

1
f r (d ) 
a0  a1d  a2 d 2

d      Distance from the light source
a0     Constant attenuation coefficient
a1     Linear attenuation coefficient

Radial intensity attenuation only applies to point light and spotlight.
Angular Intensity Attenuation
R (light ray)


V (Cone axis)
c

For spotlight, light intensity decreases as the light ray deviates from
the cone axis.

Angular intensity attenuation factor
cosm  , 0     c
f a ( )                             cos  R V
0,         c
         Light ray angle from cone axis
m         Angular intensity attenuation exponent
Phong Reflection Model

Ambient             Diffuse

Specular            Emissive
Ambient Light

Ambient Light: Background light that comes from all directions
and has constant intensity.

Oa  ka I a

Ia       Intensity of incoming ambient light
Oa       Intensity of reflected ambient light
ka       Ambient reflectivity of object surface
Diffuse Reflection
• Incoming light comes from a certain direction.
• Reflected light is scattered in all directions.
Incident
N
light
N Unit Surface Normal
                   L Unit Direction Vector to Light
L                           incident angle

kd I d ( N  L), if N  L  0
Od  
0,               if N  L  0
Id       Intensity of incoming diffuse light
Od       Intensity of reflected diffuse light
kd       Diffuse reflectivity of object surface

For most materials, kd = ka
Specular Reflection
• Incoming light comes from a certain direction.
• Reflected light concentrates around a certain direction.

Incident
light                 N
R
L                                     Viewer
             V

R Unit Vector in Ideal Specular Reflection Direction
V Unit Direction Vector to the Viewer

R  ( 2 N  L) N  L
Phong Model

ks I s ( V  R )n , if V  R  0 and N  L  0
Os  
0,                  if V  R  0 or N  L  0

Is         Intensity of incoming specular light
Os         Intensity of specular reflection light
ks         Specular reflectivity of object surface
n          Specular reflection exponent

Simplified Phong Model
Halfway vector
N H
LV
R             H
                            LV
L
    V
ks I s ( N  H)n , if N  H  0 and N  L  0
Os  
0,                 if N  H  0 or N  L  0
n controls the shininess of the surface

N                                      N
L                R
L                 R

Shiny surface (large n)                 Dull surface (small n)
Small specular highlight                Large specular highlight
Surface Light Emission
Light emitted by the object itself.             I e  constant

Combine Everything Together

                                      
nl
O  I e  ka I a   f rl f al kd I d ( N  L)  k s I sl ( N  H) n
l

l 1

nl: Number of light sources
Colored Light and Material
Light Intensity and Material Reflectivity are specified in RGB triples

I a  ( I aR , I a , I aB ), I d  ( I dR , I d , I dB ), I s  ( I sR , I sG , I sB )
G                            G

ka  ( kaR , ka , kaB ), kd  ( kdR , kd , kdB ), k s  ( k sR , k sG , k sB )
G                        G

Lighting calculation is performed for each RGB component separately

 OaR  k aR I aR               OdR  kdR I dR ( N  L)                OsR  k sR I sR ( N  H) n
 G                             G                                      G
Oa  k a I a
G G
Od  kd I d ( N  L)
G G
Os  k sG I sG ( N  H ) n
O B  k B I B                 O B  k B I B ( N  L)                 O B  k B I B ( N  H)n
 a       a a                   d      d d                             s       s s

Polygon surface is rendered using a single color.
The color usually comes from a vertex or polygon centroid under
certain illumination model.
The color for each vertex of the polygon is calculated under certain
illumination model. The surface color of the polygon is interpolated
using the vertices’ colors.

1. Determine unit normal vector at each vertex.
2. Calculate the color of each vertex using an illumination model.
3. Linearly interpolate vertex colors across projected polygon.
Interpolate vertex normal vectors instead of vertex colors

1.   Determine unit normal vector at each vertex.
2.   Linearly interpolate vertex normals across projected polygon.
3.   Apply an illumination model and calculate the color of each
projected pixel using the interpolated normal.

• No Mach bands effect (bright and dark intensity streaks)
• Better specular highlight
Vertex Normal Calculation
Polygon Surface Normal and Plane Equation
z        N=(A, B, C)                 Three points on the          P1  ( x1 , y1 , z1 )

polygon surface:             P2  ( x2 , y2 , z 2 )
Outside                                         P  ( x , y , z )
P1                                                    3      3    3     3
P2
In right-handed coordinate system, P1, P2, P3
P3                   are specified in counter-clockwise direction
y
when viewed from outside to inside
Inside
x
T
Surface normal:          T  P1P2  P1P3  (P2  P1 )  (P3  P1 )  ( A, B, C )        N
T
Plane equation:          T  P  D          or    Ax  By  Cz  D  0
D  T  P1  T  P2  T  P3
1   y1   z1          x1 1 z1                 x1   y1 1                 x1    y1     z1
A  1 y2       z2     B  x2 1 z 2           C  x2     y2 1         D   x2      y2     z2
1 y3     z3          x3 1 z3                 x3   y3 1                x3     y3     z3
if Ax + By + Cz + D < 0, point (x, y, z) is inside.
if Ax + By + Cz + D > 0, point (x, y, z) is outside.
Averaged Vertex Normal of Polygon Surfaces
N2                      Average normal vectors of all
N1              N                 polygons that share the vertex
N3
n
T
T   Ni        N
N4                             i 1          T

True Normal of a Curved Surface

Non-parametric surface equation               f f f 
f ( x, y, z )  0              T   , ,   f
 x y z 
          
Parametric surface equation                   P P
( x, y, z )  P(u, v)          T     
u v
T
N
T
Lighting in OpenGL
Create Light Source

void glLightf(GLenum light, GLenum pname, GLfloat pvalue)
void glLightfv(GLenum light, GLenum pname, GLfloat *pvalue)
Create a light source
light:     Light source identifier.
Values are GL_LIGHTi, (i = 0, 1, 2, … , 7)
pname: Parameter name
pvalue: Parameter value

To enable a light source, use glEnable(GL_LIGHTi);
To disable a light source, use glDisable(GL_LIGHTi);
To enable lighting calculation, use glEnable(GL_LIGHTING);
To disable lighting calculation, use glDisable(GL_LIGHTING);
Parameter name             Parameter value Meaning
GL_AMBIENT                 (Ra, Ga, Ba, Aa)   ambient intensity
GL_DIFFUSE                 (Rd, Gd, Bd, Ad)   diffuse intensity
GL_SPECULAR                (Rs, Gs, Bs, As)   specular intensity
GL_POSITION                (x, y, z, h)       light source position
h=0 indicates parallel
light source
GL_SPOT_DIRECTION          (x, y, z)          spotlight direction
GL_SPOT_CUTOFF             c                 spotlight cutoff angle
(in degree)
GL_SPOT_EXPONENT           m                  spotlight exponent
GL_CONSTANT_ATTENUATION    a0                 constant attenuation
coefficient
GL_LINEAR_ATTENUATION      a1                 linear attenuation
coefficient
coefficient
The light source position specified by GL_POSITION is transformed by
current Modelview matrix and stored in eye coordinates.

To make a light source               To make a light source
relatively fixed to the viewer       relatively fixed to the scene
Specify light source position         Viewing Transformation

Viewing Transformation            Specify light source position

Modeling Transformation              Modeling Transformation
Define Surface Material Properties

void glMaterialf(GLenum face, GLenum pname, GLfloat pvalue)
void glMaterialfv(GLenum face, GLenum pname, GLfloat *pvalue)
face:   Indicates which face the material properties are applied to.
GL_FRONT, GL_BACK, GL_FRONT_AND_BACK

pname                              pvalue               Meaning
GL_AMBIENT                         (Ra, Ga, Ba, Aa)     ambient reflectivity
GL_DIFFUSE                         (Rd, Gd, Bd, Ad)     diffuse reflectivity
GL_SPECULAR                        (Rs, Gs, Bs, As)     specular reflectivity
reflectivity
GL_EMISSION                        (Re, Ge, Be, Ae)     emissive color
GL_SHININESS                       0  n  128          Specular reflection
exponent
Use Color Material Mode

void glColorMaterial( GLenum face, GLenum mode)
Cause material property specified by mode of polygon faces specified
by face to track current color specified by glColor*() command.
face: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK
mode: GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR,
GL_AMBIENT_AND_DIFFUSE, GL_EMISSION

To enable color material mode,
use glEnable(GL_COLOR_MATERIAL);
To disable color material mode,
use glDisable(GL_COLOR_MATERIAL);
Select Lighting Model

void glLightModel{if}(GLenum pname, TYPE pvalue)
void glLightModel{if}v(GLenum pname, TYPE *pvalue)

Global Ambient Light
pname = GL_LIGHT_MODEL_AMBIENT
pvalue = (R, G, B, A)
Defines global ambient light intensity that is not from any
particular light source. Default value is (0.2, 0.2, 0.2, 1.0)

Two-Sided Lighting
pname = GL_LIGHT_MODEL_TWO_SIDE
pvalue = GL_FALSE  One-sided lighting. Only perform lighting
calculation on front faces. (default)
pvalue = GL_TRUE   Two-sided lighting. Perform lighting
calculation on both front and back faces
Local or Infinite Viewpoint
pname = GL_LIGHT_MODEL_LOCAL_VIEWER
pvalue = GL_FALSE           Infinite viewpoint (default)
pvalue = GL_TRUE            Local viewpoint
This parameter affects how direction to viewer vector V is calculated
for specular reflection.
For infinite viewpoint, V is a constant (0, 0, 1) in eye coordinates.
For local viewpoint, the viewpoint is placed at the origin in eye
coordinates and V is different for each vertex.

pname = GL_LIGHT_MODEL_COLOR_CONTROL
pvalue = GL_SINGLE_COLOR
Specular color is added to ambient, diffuse and emissive colors
before texture mapping. (default)
pvalue = GL_SEPARATE_SPECULAR_COLOR
Specular color is calculated separately and added after texture
mapping.

mode:
Specify Vertex Normal

void glNormal3{bsifd}(TYPE x, TYPE y , TYPE z)
void glNormal3{bsifd}v(TYPE * v)
Specify current normal vector

• Current normal vector is considered an OpenGL state variable.
Therefore the normal vector of a vertex should be specified before its
coordinates.
• Normal vector should be unit vector. By default, OpenGL does not
automatically normalize a normal vector. To enable automatically
normalization, use glEnable(GL_NORMALIZE).
Use glNormal*() in primitive rendering

glBegin(mode);
glNormal*(N0);
glVertex*(V0);
glNormal*(N1);
glVertex*(V1);
...
glNormal*(Nn1);
glVertex*(Vn1);
glEnd();

```
To top