Game Development Reference
In-Depth Information
Listing 4.1
Phong Reflection Model
// Vector3 N = normal of surface
// Vector3 eye = position of camera
// Vector3 pos = position on surface
// float a = specular power (shininess)
Vector3
V
= Normalize(
eye
-
pos
)
// FROM surface TO
camera
Vector3
Phong
=
AmbientColor
foreach
Light
light
in
scene
if
light
affects surface
Vector3
L
= Normalize(
light.pos
-
pos
)
//
FROM surface TO light
Phong
+=
DiffuseColor
* DotProduct(
N
,
L
)
Vector3
R
= Normalize(
Reflect
(-
L
,
N
))
// Re-
flection of -L about N
Phong
+=
SpecularColor
* pow(DotProduct(
R
,
V
),
a
)
end
end
Shading
Separate from the reflection model is the idea of
shading
, or how the surface of
a triangle is filled in. The most basic type of shading,
flat shading
, has one color
applied to the entire triangle. With flat shading, the reflection model is calculated
once per each triangle (usually at the center of the triangle), and this computed
color is applied to the triangle. This works on a basic level but does not look par-
ticularly good.