Lighting and Shading

trexpeeverSoftware and s/w Development

Dec 13, 2013 (3 years and 10 months ago)

74 views

Lighting and Shading

An introduction to lighting and shading using OpenGL

From OpenGL’s perspective,

Shading is the process of
filling in the pixels that make
up lines and polygons

Lighting is the process of
calculating illumination at
the vertices

OpenGL’s shading model

OpenGL can work in two colour modes


RGBA



Color
-
index mode

Only going to consider RGBA mode within this course

Basic way to specify a colour is with the commands:

glColor3f

r g b


alpha value, a, defaults to 1.0

glColor4f

r g b a

Colours can be specified per
-
vertex; a shading model is selected that
specifies how colours are generated between the vertices

glShadeModel

GL_FLAT




GL_SMOOTH

Shading

glBegin
(
GL_TRIANGLES

);

glColor3f
(

1
,

0
,

0

);

glVertex2f
(

0
,

0.5

);

glColor3f
(

0
,

1
,

0

);

glVertex2f
(

0.5
,

-
0.5

);

glColor3f
(

0
,

0
,

1

);

glVertex2f
(

-
0.5
,

-
0.5

);

glEnd
();

glShadeModel(GL_SMOOTH);

glShadeModel(GL_FLAT);

Lighting on or off?

By default, OpenGL’s lighting model is turned off

Turn it on with


glEnable(GL_LIGHTING);

If lighting is off, you specify vertex colours directly:


glColor3f(0.1,0.8,0.2);

If lighting is on, you
don’t

specify vertex colours directly, but
rather specify material properties and illumination sources;
OpenGL calculates vertex colours itself


glColor3f(0.1,0.8,0.2); // redundant


glEnable(GL_LIGHT0);


glLightfv(GL_LIGHT0,
some parameters for light0
);


glMaterialfv(GL_FRONT,
some parameters for material
);

How do surfaces reflect light?

In very complex ways...



Different wavelengths (colours) are reflected to differing amounts



Light is scattered; very complex functions relate the intensity of
light reflected at a particular angle to the incident angle



Fluorescence and phosphorescence may occur



Light may be multiply scattered within the surface layer

What light is present in the environment?

Ambient light

Point light sources

Large light sources

Self
-
luminous objects

How to approximate realistic surfaces in
computer graphics

Give surfaces textures (a later lecture...)

Give surfaces roughness ( later course...)

“Let there be light”

Specify light source characteristics

{luminance, colour, direction, spreading characteristics}

Specify surface material characteristics

Use a lighting model that takes account of the relative positioning
and orientation of:



each surface relative to the light source[s]



each surface relative to the observer (viewpoint)

{reflectance, colour, shininess}

A Lit sphere

Basic lit sphere

With polygons

With surface normals

A sphere can be drawn with the utility function


glutSolidSphere( fRadius,nSlices,nStacks );

OpenGL light sources

OpenGL supports at least 8 lights, which may be positioned
within the world, as well as global ambient illumination

Lights themselves are invisible...!!

Their effects are only seen when they illuminate
something (a polygon or a line)


Combination of illumination and surface

How a surface is rendered (or, in the real world, perceived) is a function
of both the surface material and the illumination characteristics

Light characteristics








Diffuse light characteristics

Specular light characteristics

Ambient light characteristics

OpenGL’s model


Emissive characteristics

×

Ambient reflection measures

×

Diffuse reflection measures

×

Specular reflection measures

Ambient lighting

Ambient reflectance affects the overall
colour of the object

Since ambient light is considered to be
light that has been scattered so many
times that its direction is impossible to
determine, the intensity of the ambient
reflection is independent of the surface
orientation

An object’s total ambient reflectance is proportional to the sum of the
global ambient light and ambient light from individual light sources

The ambient reflectance isn’t affected by the viewpoint position

Diffuse reflections

Diffuse reflections play the most
important role in perception of colour

Diffuse reflections are maximised
when light falls directly on a surface...

The intensities of the diffuse reflections are identical in both cases

..and reduce with
oblique illumination...

surface normal



direct illumination

direct

illumination

Viewpoint makes no difference:

This surface is
seen obliquely

This surface is seen
more or less face
-
on

Specular reflections

Specular reflections are the
mirror like reflections that add
highlights to a shiny object

OpenGL does not consider
a scene reflected in a mirror
surface to be comprised of
specular reflections: other
advanced techniques are
available to handle such
levels of realism

Reflected light

angle of = angle of

incidence reflection





Light source

mirror

imperfect mirror
= narrow cone

OpenGL’s model






ht
EnabledLig
i
i
m
l
m
V
Cv
A
A
E
C
A vertex’s colour, C
v
, is sum of the contributions from:



a material emission term, E
m



global ambient lighting, A
l
, and a material ambient term, A
m



individual light’s contributions, Cv
i






ht
EnabledLig
i
ibution
LightContr
Individual
balTerm
AmbientGlo
rm
EmissionTe
ur
VertexColo








where,









m
i
shininess
m
i
m
i
i
i
i
S
Sl
n
s
D
Dl
n
L
A
Al
actor
SpotlightF
n
Attenuatio
Cv
*
*
0
,
.
max
*
*
0
,
.
max






*
*







Other lighting models

In the literature, three types of shading model referred to are:


flat
-
shading


Gourand
-
shading


Phong
-
shading

The first two correspond to OpenGL’s lighting
model with
glShadeModel

set to
GL_FLAT

and
GL_SMOOTH
, respectively

Phong
-
shading is not directly supported by
OpenGL. To create an equivalent image, would
require that all polygons were, at most, one
pixel wide on the screen.

Distance attenuation

OpenGL models how illumination falls with distance to a light source

In physics, the amount of illumination from a point light source at
distance, d, follows an inverse square law:

2
1
d
I

d

d

If the light were perfectly collimated (such as from an idealised laser),
distance would have no effect on illumination:

1
1

I
d

d

2
1
d
k
d
k
k
AF
q
l
c



d

d

OpenGL allows factors to be set that mimic these extremes (or
intermediate states):

constant linear quadratic

Spotlight effect



d
v
d
v









cos



and


vectors
normalised
between

,

angle,

of

cosine




EXPONENT
SPOT
GL
d
v
actor
SpotlightF
_
_
0
,
max




Spotlights point in a particular direction, ,

d

1

d

V
1

V
2




d

v

V
1

receives some light

V
2

receives no light

Define to be the direction vector from the light to a vertex

v

The amount of illumination received at a vertex depends on the angle
between

v
d



and

Distance fading or fogging

Fogging can be used to recreate more natural scenes, by having
distant objects merge into the colour of the ‘background’

f
i
C
f
fC
C
'


where,

f
f


1
'
C
i

is the incoming colour; C
f

is the fog colour

Setting C
f

to black
may be used to make
near features stand
out more

Setting C
f

to white
would recreate the
haziness of looking a
large distance through
the atmosphere

OpenGL fogging functions

f

can be generated by selecting one of three functions of distance
(of the vertex from the camera position):

GLfloat fogColour[4] = {1.0, 1.0, 1.0, 1.0};

glFogi( GL_FOG_MODE, GL_LINEAR );

glFogfv(GL_FOG_COLOR, fogColour);

glFogf( GL_FOG_START, 5.0 );

glFogf( GL_FOG_END, 10.0 );

glFogi( GL_FOG_MODE,
FogMode

);

GL_LINEAR

linear

start
end
z
end
f



GL_EXP

inverse exponential



z
density
e
f



GL_EXP2

inverse exponential

squared



2
z
density
e
f



Light local / global approximations

For accurate rendering of the specular reflections within a scene it
is necessary to take account of the position and orientation of a
surface relative to both the light source[s] and the observer

If the viewpoint is a long way away from the vertices that make up
the scene, then this direction is approximately constant

By default, OpenGL assumes that this is the case and saves time by
assuming an infinite viewpoint


which means that it is not necessary
to calculate a vertex
-
viewpoint direction for every vertex in the scene

To get accurate rendering, it is, however, necessary, to turn on the
correct [local] method of calculation:

glLightModel( GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE );

In OpenGL, one of these aspects is defined by the
direction from the vertex to the viewpoint