5. lighting & shading · 5. lighting & shading 3 real-world vs. opengl •r wdllaeor...
TRANSCRIPT
5. Ligh
ting
& Sh
ad
ing
5. Ligh
ting
& S
ha
din
g 2
Ligh
ting
•Lig
ht so
urce
s
•Lig
ht re
flectio
n
5. Ligh
ting
& S
ha
din
g 3
Rea
l-Wo
rld vs. O
pen
GL
•R
ea
l wo
rld
–co
mp
lex
com
pu
tatio
ns
–se
e o
ptics te
xtb
oo
ks, ph
oto
realistic re
nd
erin
g
•O
pe
nG
L
–sim
plifie
d m
od
el
–a
mb
ien
t, diffu
se a
nd
spe
cula
rlig
ht so
urce
s an
d re
flectio
ns
–e
asy to
tun
e
–fa
st to co
mp
ute
5. Ligh
ting
& S
ha
din
g 4
Op
enG
LGLfloatlight_pos[] = {x,y,z, 1};
GLfloatlight_Ia[] = {IaR, IaG, IaB, IaA};
GLfloatlight_Id[] = {IdR, IdG, IdB, IdA};
GLfloatlight_Is[] = {IsR, IsG, IsB, IsA};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_Ia);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_Id);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_Is);
GLfloatmaterial_Ka[] = {kaR, kaG, kaB, kaA};
GLfloatmaterial_Kd[] = {kdR, kdG, kdB, kdA};
GLfloatmaterial_Ks[] = {ksR, ksG, ksB, ksA};
glMaterialfv(GL_FRONT, GL_AMBIENT, material_Ka);
glMaterialfv(GL_FRONT, GL_DIFFUSE, material_Kd);
glMaterialfv(GL_FRONT, GL_SPECULAR, material_Ks);
glMaterialfv(GL_FRONT, GL_SHININESS, Se);
•Lig
ht so
urce
:
•R
efle
ction
(ma
teria
l):
5. Ligh
ting
& S
ha
din
g 5
Ligh
t Po
sition
•P
ositio
n in
wo
rld-co
ord
ina
tes
•Lig
ht sta
tion
ary
gluLookAt(eyeX, eyeY, eyeZ,
centerX, centerY, centerZ,
upX, upY, upZ);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
•P
ositio
n in
cam
era
-coo
rdin
ate
s
•Lig
ht m
ovin
g w
ith ca
me
ra (h
ea
dlig
ht)
glLightfv(GL_LIGHT0, GL_POSITION, pos);
gluLookAt(eyeX, eyeY, eyeZ,
centerX, centerY, centerZ,
upX, upY, upZ);
5. Ligh
ting
& S
ha
din
g 6
Am
bien
t Ligh
t
•S
catte
red
by e
nviro
nm
en
t
•C
om
ing
from
all d
irectio
ns
•R
efle
ction
ind
ep
en
de
nt o
f
–C
am
era
po
sition
–Lig
ht p
ositio
n (n
o lig
ht p
ositio
n)
–S
urfa
ce o
rien
tatio
n
•R
efle
cted
inte
nsity:
ligh
t so
urce
ma
teria
l p
ara
meter
aa kI
I=
5. Ligh
ting
& S
ha
din
g 7
Diffu
se Reflectio
n
•D
irecte
d lig
ht Ip
•R
efle
ction
de
pe
nd
en
t on
–o
rien
tatio
n o
f surfa
ce
–lig
ht so
urce
po
sition
•In
de
pe
nd
en
t of
–ca
me
ra p
ositio
n
(refle
cted
eq
ua
lly in a
ll dire
ction
s)
•R
efle
cted
inte
nsity:
θk
II
dp
cos=
L
N
θ
)(
kI
Id
pL
N⋅
=
5. Ligh
ting
& S
ha
din
g 8
Tw
o R
ad
ian
t Surfa
ce Pa
tches
•Illu
min
atio
n B
[lux
]
2dA dF
B=
5. Ligh
ting
& S
ha
din
g 9
Diffu
se Reflectio
n
•D
iffuse
refle
ction
scale
s with
an
gle
Surfa
ce 1Su
rface 2
dA
dA
/ cosθ
N
θ
90-θ
N
illum
ina
tion
dA dFθ
θdA/ dF
Bcos
cos=
=
5. Ligh
ting
& S
ha
din
g 10
A Sim
ple M
od
el
•S
um
up
am
bie
nt lig
ht
an
d d
iffuse
refle
ction
:
)L
N⋅
+=
(k
Ik
II
dp
aa
5. Ligh
ting
& S
ha
din
g 11
Atten
ua
tion
•Q
ua
dra
tic atte
nu
atio
n d
ue
to sp
atia
l rad
iatio
n
2
1Latt
df
=
),
dc
dc
c(
fL
Latt
11
min
23
21
++
=
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, c1);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, c2);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, c3);
)(
kI
fk
II
dp
atta
aL
N⋅
+=
•A
mo
de
l ofte
n u
sed
in G
rap
hics (O
pe
nG
L)
•In
clud
e a
tten
ua
tion
5. Ligh
ting
& S
ha
din
g 12
Wa
veleng
th D
epen
den
cy
)(
Ok
If
Ok
II
λλ
λλ
dd
patt
da
aλ
LN⋅
+=
Restrictio
n o
f spectra
l sam
plin
g to
RG
B ca
nlea
d to
sub
stan
tial co
lor a
rtifacts
)(
kI
fk
II
)(
kI
fk
II
)(
kI
fk
II
BB
RB
GG
RG
RR
RR
dp
atta
aB
dp
atta
aG
dp
atta
aR
LN
LN
LN
⋅+
=
⋅+
=
⋅+
=
•C
olo
red
ligh
t an
d re
flectio
n a
s fun
ction
s of
wa
vele
ng
th λ
•R
estrictio
n to
RG
B (O
pe
nG
L)
5. Ligh
ting
& S
ha
din
g 13
Dep
th C
uein
g (Fo
g)
•Lin
ea
r de
pth
cue
by b
len
din
g w
ith th
e co
lor o
f th
e p
articip
atin
g m
ed
ium
λdc
λλ
Is)
(Is
I−
+=
1
fb
bf
bf
bb
zz
zz
z)
s(s)
z(z
ss
≤≤
−
−−
+=for
1sf
Scale
facto
rsb
zb
Dep
thz
f1
z
•S0
is scalin
g fa
ctor
(sigm
oid
alfu
nctio
n)
5. Ligh
ting
& S
ha
din
g 14
Specu
lar
Reflectio
n
•D
ep
en
ds o
n a
ng
le b
etw
ee
n re
flectio
n a
nd
vie
win
g ra
y
L
N
θθ
α
R
V
5. Ligh
ting
& S
ha
din
g 15
Reflected
Ra
y
•C
om
pu
ted
usin
g sim
ple
vecto
r alg
eb
ra
VL
LN
NV
RL
LN
NL
NR
SN
R
⋅−
⋅=
⋅=
−⋅
=−
=+
=
))
((
α)
(θ θ
2cos
2cos
2cos
L
N
θθ
R
Nco
sθ SS
V
α
5. Ligh
ting
& S
ha
din
g 16
Ph
on
gIllu
min
atio
n M
od
el
•A
pp
rox
ima
tes sp
ecu
lar
refle
ction
by co
sine
p
ow
ers
]k
O[k
If
Ok
II
ns
dd
patt
da
aλ
λλ
λλ
)(
)(
VR
LN
⋅+
⋅+
=
5. Ligh
ting
& S
ha
din
g 17
Am
bien
t + D
iffuse +
Specu
lar
Am
bie
nt
Diffu
seS
pe
cula
r
5. Ligh
ting
& S
ha
din
g 18
Exten
sion
s
•S
pe
cula
rco
lors
•H
alfw
ay-V
ecto
r (faste
r)
•M
ultip
le Lig
ht S
ou
rces
L
N
θθ
α
R
V
H
β
])
(O
k)
(O
[kI
fO
kI
In
ss
dd
patt
da
aλ
λλ
λλ
λV
RL
N⋅
+⋅
+=
nn
)(
βH
N⋅
=cos
VL
VL
H+ +
=
∑≤
≤
⋅+
⋅+
=m
i
ni
ss
id
dp
attd
aa
λ]
)(
Ok
)(
O[k
If
Ok
II
λλ
λii
λλ
1V
RL
N
5. Ligh
ting
& S
ha
din
g 19
Hig
hlig
hts
•ks/kd
de
term
ine
s hig
hlig
hte
d su
rface
reg
ion
s
5. Ligh
ting
& S
ha
din
g
20
Ligh
t Sou
rces
•D
irecte
d S
po
tligh
ts
λI
pL'λ cos
pL'
L')L
(I
λ⋅
−
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, L’)
;
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, p)
;
L
NL’
γ
Spo
t po
sition
Spo
t directio
n
5. Ligh
ting
& S
ha
din
g 21
Inten
sity Dia
gra
ms
•Im
ple
me
nte
d in
mo
st mo
de
rn g
rap
hics A
PIs
5. Ligh
ting
& S
ha
din
g 22
Sha
din
g
•S
ha
din
g re
qu
ires m
an
y eva
lua
tion
s of a
ligh
ting
mo
de
l
•Q
ue
stion
s: –
wh
ere
to eva
lua
te ?
–w
he
n to
eva
lua
te ?
5. Ligh
ting
& S
ha
din
g 23
Sha
din
g M
od
els
•Fla
t sha
din
g(co
nsta
nt sh
ad
ing
)
–o
ne
colo
r pe
r prim
itive
•G
ou
rau
dsh
ad
ing
–lin
ea
r inte
rpo
latio
n o
f verte
x in
tensities
•P
ho
ng
sha
din
g
–lin
ea
r inte
rpo
latio
n o
f verte
x n
orm
als
•G
ou
rau
d&
Ph
on
gsh
ad
ing
ne
ed
verte
x
no
rma
ls
5. Ligh
ting
& S
ha
din
g 24
Go
ura
ud
Sha
din
g –Procedure
1.C
alcu
late
face
no
rma
ls
2.C
alcu
late
verte
x n
orm
als
3.E
valu
ate
ligh
ting
mo
de
l for e
ach
verte
x
4.In
terp
ola
te ve
rtex
inte
nsitie
s acro
ssp
rimitive
(bilin
ea
r inte
rpo
latio
n)
N1
Nv
N4
N3
N2
∑ ∑= ==
nii
nii
v
N NN
1 1
5. Ligh
ting
& S
ha
din
g 25
Go
ura
ud
Sha
din
g –
Interpolation•
Line
ar in
terp
ola
tion
on
curre
nt sca
n lin
e
•D
on
e d
urin
g sca
n co
nve
rsion
•H
ard
wa
re a
ccele
ratio
n p
ossib
le
Scan
line
I1
Ia
I3
I2
IbIp
y1
ys
y2
y3
y
x
Ia=
I1 -(I1 -I2 ) (y1 -y
s )(y1 -y
2 )
Ib=
I1 -(I1 -I3 ) (y1 -y
s )(y1 -y
3 )
Ip=
Ib -(Ib -Ia )(x
b -xp )
(xb -x
a )
5. Ligh
ting
& S
ha
din
g
26
Go
ura
ud
Sha
din
g –Quality
•S
ha
din
g q
ua
lity de
pe
nd
s on
size o
f pro
jecte
d
prim
itives (re
lative
to p
ixe
l size)
5. Ligh
ting
& S
ha
din
g 27
Ph
on
gSh
ad
ing
•Lin
ea
r inte
rpo
latio
n o
f no
rma
ls(n
ot
inte
nsitie
s) on
curre
nt sca
n lin
e
•E
valu
atio
n o
f ligh
ting
mo
de
l at e
ach
pix
el
•M
ore
accu
rate
bu
t slow
er th
an
Go
ura
ud
P0
Pa
Pb
Pc
P1
N0
Na
Nb
Nc
N1
5. Ligh
ting
& S
ha
din
g 28
Sha
din
g M
etho
ds –Overview
•S
ha
din
g in
ima
ge
spa
ce:
–Fla
t sha
din
g (Constant shading
)
–G
ou
rau
d sh
ad
ing
•S
ha
din
g in
ob
ject / scre
en
spa
ce:
–P
ho
ng
sha
din
g
•Im
pa
ct on
gra
ph
ics ha
rdw
are
...
5. Ligh
ting
& S
ha
din
g
29
Tra
nsp
aren
cy
•T
ran
spa
ren
cy do
ne
with
α-b
len
din
g…
•Lin
ea
rizing
ex
po
ne
ntia
l atte
nu
atio
n o
f in
ten
sity in m
ed
iaP1
P2
Iλ2
Iλ
Iλ1
∆t
thickn
ess
view
α1
α2
= 1
(op
aq
ue)
α: a
bso
rptio
n
5. Ligh
ting
& S
ha
din
g
30
Tra
nsp
aren
cy –Basic Laws
•P1
filters in
ten
sity acco
rdin
g to
•Lin
ea
rizatio
n yie
lds
•E
missio
n o
f P1
•S
um
min
g u
p (fo
r un
it len
gth
)
•Fo
r N p
olyg
on
s (α-b
lend
ing
)
∆tα
λλ
eI
I'1
22
−=
∆t)α
(I
I'λ
λ1
12
2−
=∆tα
II'
λλ
11
1 =
)α
(I
αI
I'I'
Iλ
λλ
λλ
12
11
12
1−
+=
+=
∑∏
=
−=
−⋅
=Ni
ibb
λi
λ)
α(
Iα
Ii
1
11
1