Blinn–Phong shading model

From Wikipedia, the free encyclopedia
Jump to: navigation, search

The Blinn–Phong reflection model (also called the modified Phong reflection model) is a modification to the Phong reflection model developed by Jim Blinn.[1]

Blinn–Phong is the default shading model used in OpenGL[2] and Direct3D's fixed-function pipeline (before Direct3D 10 and OpenGL 3.1), and is carried out on each vertex as it passes down the graphics pipeline; pixel values between vertices are interpolated by Gouraud shading by default, rather than the more computationally-expensive Phong shading.

Description[edit]

Vectors for calculating Phong and Blinn–Phong shading

In Phong shading, one must continually recalculate the scalar product R \cdot V between a viewer (V) and the beam from a light-source (L) reflected (R) on a surface.

If, instead, one calculates a halfway vector between the viewer and light-source vectors,


H = \frac{L + V}{\left| L + V \right|}

we can replace R \cdot V with N \cdot H, where N is the normalized surface normal. In the above equation, L and V are both normalized vectors, and H is a solution to the equation V=P_H(-L), where P_H is the Householder matrix that reflects a point in the hyperplane that contains the origin and has the normal H.

This dot product represents the cosine of an angle that is half of the angle represented by Phong's dot product if V, L, N and R all lie in the same plane. This relation between the angles remains approximately true when the vectors don't lie in the same plane, especially when the angles are small. The angle between N and H is therefore sometimes called the halfway angle.

Considering that the angle between the halfway vector and the surface normal is likely to be smaller than the angle between R and V used in Phong's model (unless the surface is viewed from a very steep angle for which it is likely to be larger), and since Phong is using \left( R \cdot V \right)^{\alpha}, an exponent can be set \alpha^\prime > \alpha such that \left(N \cdot H \right)^{\alpha^\prime} is closer to the former expression.

For front-lit surfaces (specular reflections on surfaces facing the viewer), \alpha^\prime = 4\,\alpha will result in specular highlights that very closely match the corresponding Phong reflections. However, while the Phong reflections are always round for a flat surface, the Blinn–Phong reflections become elliptical when the surface is viewed from a steep angle. This can be compared to the case where the sun is reflected in the sea close to the horizon, or where a far away street light is reflected in wet pavement, where the reflection will always be much more extended vertically than horizontally.

Visual comparison: Blinn–Phong highlights are larger than Phong with the same exponent, but by lowering the exponent, they can become nearly equivalent.

Additionally, while it can be seen as an approximation to the Phong model, it produces more accurate models of empirically determined bidirectional reflectance distribution functions than Phong for many types of surfaces. (See: Experimental Validation of Analytical BRDF Models, Siggraph 2004)

Efficiency[edit]

This rendering model is less efficient than pure Phong shading in most cases, since it contains a square root calculation. While the original Phong model only needs a simple vector reflection, this modified form takes more into consideration. However, as many CPUs and GPUs contain single and double precision square root functions (as standard features) and other instructions that can be used to speed up rendering, the time penalty for this kind of shader will not be noticed in most implementations.

However, Blinn-Phong will be faster in the case where the viewer and light are treated to be at infinity. This is the case for directional lights. In this case, the half-angle vector is independent of position and surface curvature. It can be computed once for each light and then used for the entire frame, or indeed while light and viewpoint remain in the same relative position. The same is not true with Phong's original reflected light vector which depends on the surface curvature and must be recalculated for each pixel of the image (or for each vertex of the model in the case of vertex lighting).

In most cases where lights are not treated to be at infinity, for instance when using point lights, the original Phong model will be faster.

Code sample[edit]

This sample in High Level Shader Language is a method of determining the diffuse and specular light from a point light. The light structure, position in space of the surface, view direction vector and the normal of the surface are passed through. A Lighting structure is returned;

Note that the below would also need to clamp certain dot products to zero in the case of negative answers. Without that, light heading away from the camera is treated the same way as light heading towards it. For the specular calculation, an incorrect "halo" of light glancing off the edges of an object and away from the camera might appear as bright as the light directly being reflected towards the camera.

struct Lighting
{
    float3 Diffuse;
    float3 Specular;
};
 
struct PointLight
{
	float3 position;
	float3 diffuseColor;
	float  diffusePower;
	float3 specularColor;
	float  specularPower;
};
 
Lighting GetPointLight( PointLight light, float3 pos3D, float3 viewDir, float3 normal )
{
	Lighting OUT;
	if( light.diffusePower > 0 )
	{
		float3 lightDir = light.position - pos3D; //3D position in space of the surface
		float distance = length( lightDir );
		lightDir = lightDir / distance; // = normalize( lightDir );
		distance = distance * distance; //This line may be optimised using Inverse square root
 
		//Intensity of the diffuse light. Saturate to keep within the 0-1 range.
		float NdotL = dot( normal, lightDir );
		float intensity = saturate( NdotL );
 
		// Calculate the diffuse light factoring in light color, power and the attenuation
		OUT.Diffuse = intensity * light.diffuseColor * light.diffusePower / distance;
 
		//Calculate the half vector between the light vector and the view vector.
		//This is faster than calculating the actual reflective vector.
		float3 H = normalize( lightDir + viewDir );
 
		//Intensity of the specular light
		float NdotH = dot( normal, H );
		intensity = pow( saturate( NdotH ), specularHardness );
 
		//Sum up the specular light factoring
		OUT.Specular = intensity * light.specularColor * light.specularPower / distance; 
	}
	return OUT;
}

See also[edit]

References[edit]

  1. ^ James F. Blinn (1977). "Models of light reflection for computer synthesized pictures". Proc. 4th annual conference on computer graphics and interactive techniques: 192–198. doi:10.1145/563858.563893. CiteSeerX: 10.1.1.131.7741. 
  2. ^ Shreiner, Dave; The Khronos OpenGL ARB Working Group (2010). "The Mathematics of Lighting". OpenGL programming guide : the official guide to learning OpenGL, version 3.0 and 3.1 (7th ed.). Pearson Education, Inc. pp. 240–245. ISBN 978-0-321-55262-4.