Phong shading is a model for calculating the illumination of three-dimensional objects, including polygonal models and primitives, as well as the method of interpolating illumination over the entire object.
Content
Smooth shading
Models are usually defined by a set of flat convex faces , although most real three-dimensional objects have smooth curvilinear surfaces. Thus, the curved surface is drawn as a ribbed polygonal mesh; To make this grid look smooth, use one or another method of interpolation of the illumination of the vertices of the polygonal grid .
If Guro shading is used, the color is calculated at each vertex of each face, and then the calculated color is interpolated across the face. As a result, the glare , which should appear in the middle of the polygon, will not be drawn - when interpolating the vertex colors, a brighter illumination of the polygon center is impossible.
When shading Phong interpolates the normal vector [1] . To find the normal vector at an arbitrary point on the surface, we use the normalized weighted sum of the normal vectors of the faces to which this point belongs:
The computational cost of Gouraud or Phong shading depends on the number of vertices and the number of image fragments, respectively. Modern graphics equipment uses the second method, calculating the color of each fragment (ie, pixel), rather than each vertex.
Lighting Model
Phong lighting also includes the Phong lighting model, i.e. algorithm for calculating the lighting at a given point. This is a local lighting model, i.e. it takes into account only the properties of a given point and light sources, ignoring the effects of scattering, lensing, reflection from neighboring bodies.
Phong shading requires relatively few resources, but most optical phenomena are ignored or calculated with a rough approximation.
Other lighting models may better take into account material properties (local models of Oren-Nayar, Cook-Torrens, anisotropic models) or complex optical phenomena (global models), but they lead to an increase in overhead costs.
Lighting calculation method
The Phong lighting calculation requires calculating the color intensity of three lighting components: ambient (ambient), diffuse (diffuse) and glossy specular (glare) components. The background component is a rough approximation of the rays of light scattered by neighboring objects and then reaching a given point; the other two components mimic the scattering and reflection of direct radiation.
Where
- the surface normal vector at
- projection direction (direction to the light source)
- direction to the observer
- background illumination ratio
- coefficient of mirror lighting
- diffuse illumination coefficient
OpenGL Lighting
In the OpenGL pipeline, the fragment color intensity is calculated for each light source separately, then the results are added and the light emitted by the body is added (GL_EMISSION).
Phongue lighting calculation algorithm can be illustrated using the following shaders :
Vertex Shader
1 varying vec3 n ;
2 varying vec3 v ;
3
4 void main ( void )
5 {
6 v = vec3 ( gl_ModelViewMatrix * gl_Vertex );
7 n = normalize ( gl_NormalMatrix * gl_Normal );
8 gl_Position = ftransform ();
9 }
Fragment Shader
1 varying vec3 n ;
2 varying vec3 v ;
3
4 void main ( void )
5 {
6 vec4 result = vec4 ( 0.0 );
7 for ( int li = 0 ; li < gl_MaxLights ; ++ li )
8 {
9 vec3 viewPos = gl_LightSource [ li ]. position . w * v ;
10 vec3 l = normalize ( gl_LightSource [ li ]. Position . Xyz - viewPos );
11 vec3 e = normalize ( - v );
12 vec3 r = normalize ( - reflect ( l , n ));
13
14 vec4 Iamb = gl_FrontLightProduct [ li ]. ambient ;
15
16 vec4 Idiff = gl_FrontLightProduct [ li ]. diffuse * max ( dot ( n , l ), 0.0 );
17 Idiff = clamp ( Idiff , 0.0 , 1.0 );
18
19 vec4 Ispec = gl_FrontLightProduct [ li ]. specular
20 * pow ( max ( dot ( r , e ), 0.0 ),
21 gl_FrontMaterial . shininess );
22 Ispec = clamp ( Ispec , 0.0 , 1.0 );
23
24 result + = Iamb + Idiff + Ispec ;
25 }
26
27 gl_FragColor = gl_FrontLightModelProduct . sceneColor + result ;
28 }
Where is the value
gl_FrontLightModelProduct . sceneColor
equivalent to the expression
gl_FrontMaterial . emission + gl_FrontMaterial . ambient * gl_LightModel . ambient
Notes
- ↑ D. Rogers. Algorithmic Foundations of Computer Graphics = Procedural Elements for Computer Graphics. - M .: Mir, 1989. - p. 394. - 512 p. - ISBN 0-07-053534-5 .