Lesson 15 of 15
Phong Lighting
Phong Lighting
The Phong reflection model breaks lighting into three components:
- Ambient — constant background illumination (prevents pure black shadows)
- Diffuse — scattered light based on surface orientation toward light
- Specular — mirror-like highlight based on the reflection direction
The Formula
color = ambient + diffuse + specular
Given:
- eyev: unit vector from surface toward camera
- normalv: unit normal vector of the surface
- lightv: unit vector from surface toward light
ambient = material.ambient
lightDotNormal = dot(lightv, normalv)
if (lightDotNormal < 0):
diffuse = 0 // light is behind the surface
else:
diffuse = material.diffuse * lightDotNormal
reflectv = reflect(-lightv, normalv) // mirror direction
reflectDotEye = dot(reflectv, eyev)
if (reflectDotEye <= 0):
specular = 0
else:
factor = pow(reflectDotEye, material.shininess)
specular = material.specular * factor
Reflect Formula
reflect(in, normal) = in - normal * 2 * dot(in, normal)
Test Cases from the Book
Using: color=(1,1,1), ambient=0.1, diffuse=0.9, specular=0.9, shininess=200
| Scene | Result |
|---|---|
| Eye directly facing light, light directly above | 1.9 |
| Light behind surface | 0.1 |
Your Task
Implement phong(eyex, eyey, eyez, nx, ny, nz, lx, ly, lz) that returns the combined lighting intensity.
Expected output:
1.9
0.1JSCPP loading...
Loading...
Click "Run" to execute your code.