Phong Shader equation
For each material in the scene, the following parameters are defined:
- , which is a specular reflection constant, the ratio of reflection of the specular term of incoming light,
- , which is a diffuse reflection constant, the ratio of reflection of the diffuse term of incoming light (Lambertian reflectance),
- , which is an ambient reflection constant, the ratio of reflection of the ambient term present in all points in the scene rendered, and
- , which is a shininess constant for this material, which is larger for surfaces that are smoother and more mirror-like. When this constant is large the specular highlight is small.
Furthermore, we have
- , which is the set of all light sources,
- , which is the direction vector from the point on the surface toward each light source ( specifies the light source),
- , which is the normal at this point on the surface,
- , which is the direction that a perfectly reflected ray of light would take from this point on the surface, and
- , which is the direction pointing towards the viewer (such as a virtual camera).
Then the Phong reflection model provides an equation for computing the illumination of each surface point :
where the direction vector is calculated as the reflection of on the surface characterized by the surface normal using
and the hats indicate that the vectors are normalized. The diffuse term is not affected by the viewer direction (). The specular term is large only when the viewer direction () is aligned with the reflection direction . Their alignment is measured by the power of the cosine of the angle between them. The cosine of the angle between the normalized vectors and is equal to their dot product. When is large, in the case of a nearly mirror-like reflection, the specular highlight will be small, because any viewpoint not aligned with the reflection will have a cosine less than one which rapidly approaches zero when raised to a high power.
Although the above formulation is the common way of presenting the Phong reflection model, each term should only be included if the term's dot product is positive. (Additionally, the specular term should only be included if the dot product of the diffuse term is positive.)
When the color is represented as RGB values, as often is the case in computer graphics, this equation is typically modeled separately for R, G and B intensities, allowing different reflections constants and for the different color channels.
Computationally more efficient alterations[edit]
When implementing the Phong reflection model, there are a number of methods for approximating the model, rather than implementing the exact formulas, which can speed up the calculation; for example, the Blinn–Phong reflection model is a modification of the Phong reflection model, which is more efficient if the viewer and the light source are treated to be at infinity.
Another approximation[3] that addresses the calculation of the exponentiation in the specular term is the following: Considering that the specular term should be taken into account only if its dot product is positive, it can be approximated as
where , and is a real number which doesn't have to be an integer. If is chosen to be a power of 2, i.e. where is an integer, then the expression can be more efficiently calculated by squaring times, i.e.
댓글
댓글 쓰기