Game Development Reference
In-Depth Information
r e s u l t . r g b
=
i n p u t . d i f f C o l o r ;
/ /
modulate
by
d i f f u s e +ambient
l i g h t i n g
r e s u l t . r g b +=
m a t e r i a l S p e c . r g b i n p u t . s p e c C o l o r ;
/ /
add
spec ,
i g n o r e
map
a l p h a
/ /
Modulate
i t
by
t h e
c o n s t a n t
and
o u t p u t
r e t u r n
r e s u l t c o n s t a n t C o l o r ;
}
Listing 10.19
Pixel shader for Gouraud shading for any lighting environment
As the caption for Listing 10.19 indicates, this pixel shader does not
depend on the number of lights, or even the lighting model, since all lighting
calculations are done in the vertex shader. Listing 10.20 shows a vertex
shader that could be used with this same pixel shader, but it implements a
different lighting environment: ambient plus three directional lights. This
is a very useful lighting environment in editors and tools, since it's easy to
create one lighting rig that works decently well for practically any object
(although we would usually use it with per-pixel shading).
/ /
Mesh
i n p u t s .
{
s t r u c t
I n p u t
f l o a t 4
p o s
:
POSITION ;
/ /
p o s i t i o n
i n
model
space
f l o a t 3
n o r m a l
: NORMAL ;
/ /
v e r t e x
normal
i n
model
space
f l o a t 2
uv
:
TEXCOORD0 ;
/ /
t e x t u r e
c o o r d s
f o r
d i f f u s e ,
spec
maps
} ;
/ /
V e r t e x
s h a d e r
o u t p u t
s t r u c t
O u t p u t
{
f l o a t 4
c l i p P o s
:
POSITION ;
/ /
c l i p space
p o s i t i o n
f l o a t 2
uv
:
TEXCOORD0 ;
/ /
t e x t u r e
c o o r d s
f o r
d i f f u s e ,
spec
maps
f l o a t 3
d i f f C o l o r
:
TEXCOORD1 ;
/ /
d i f f u s e
l i g h t i n g
RGB
f l o a t 3
s p e c C o l o r
:
TEXCOORD2 ;
/ /
s p e c u l a r
l i g h t i n g
RGB
} ;
/ /
Model −> c l i p
t r a n s f o r m
m a t r i x .
uniform
f l o a t 4 x 4
m o d e l T o C l i p ;
/ /
Th re e
l i g h t
d i r e c t i o n s
( i n MODEL space ) .
These
p o i n t
/ /
i n
t h e
o p p o s i t e
d i r e c t i o n
t h a t
t h e
l i g h t
i s
s h i n i n g .
uniform
f l o a t 3
l i g h t D i r [ 3 ] ;
/ /
Th re e
l i g h t
RGB
c o l o r s
uniform
f l o a t 3
l i g h t C o l o r [ 3 ] ;
/ /
C o n s t a n t
ambient
l i g h t
c o l o r
uniform
f l o a t 3
a m b i e n t L i g h t C o l o r ;
/ /
View
p o s i t i o n ,
i n MODEL space
uniform
f l o a t 3
v i e w P o s ;
/ /
M a t e r i a l
g l o s s i n e s s
( phong
exponent )
uniform
f l o a t
s p e c E x p o n e n t ;
/ /
The
body
o f
our
v e r t e x
s h a d e r
O u t p u t
main ( I n p u t
i n p u t )
{
O u t p u t
o u t p u t ;
/ /
T r a n s f o r m
v e r t e x
p o s i t i o n
t o
c l i p
space .
o u t p u t . c l i p P o s
= mul ( i n p u t . pos ,
m o d e l T o C l i p ) ;
Search WWH ::




Custom Search