# Learn OpenGL. Lesson 5.1 - Advanced lighting. Model Blinn-Fong

# Advanced lighting

In the lesson dedicated to the basics of lighting we briefly analyzed Fong's lighting model, which allows us to give a substantial share of realism to our scenes. Fong's model looks quite good, but has several drawbacks, on which we will concentrate in this lesson.

OpenGL

Creating the window.

Hello Window

Hello Triangle

Shaders

Textures

Transformations

Coordinate systems

Camera

Part 2. Basic lighting

The colors are

Basics of lighting

Materials

Texture maps

Light sources

Several light sources

Part 3. Loading 3D models

Library Assimp

The polygon mesh class is Mesh

The model class is Model

Part 4. Advanced features of OpenGL

The depth test is

Stencil test

Blending colors

Clipping of faces

The frame buffer is

Cubic cards

Advanced work with data

Advanced GLSL

Geometrichesky shader

Instance

Antialiasing

Part 5. Advanced lighting

Advanced lighting. The Blinn-Fong model.

## The Blinn-Fong model

The Fong model is a very effective approximation for calculating illumination, but under certain conditions it can lose part of the component of specular highlights. This can be seen at low values of the light intensity ( ? shininess [/b] ) When the specular reflection region becomes quite large. The figure below shows what happens when we use the mirror gloss strength 1.0 for a flat textured surface:

As you can see, the mirror image area has a sharply delineated boundary. This is because the angle between the view vector and the reflection vector should not exceed 90 degrees, otherwise their scalar product becomes negative, which makes the component of specular reflections equal to zero. You might think that this is okay, because we should not get any light at corners over 90 degrees, right?

Not really. This is applicable only to the diffuse component, where the angle above 90 degrees between the normal vector and the direction of light means that the light source is below the illuminated surface, and therefore the contribution of diffuse illumination should be zero. However, in the case of a mirror component, we measure not the angle between the direction of light and the normal, but the angle between the viewing and reflection vectors. Take a look at the following two figures:

Now the problem becomes obvious. On the left, we see the familiar picture of the Fongov reflection with θ less than 90 degrees. In the right figure, the angle θ between the directions of sight and reflection is greater than 90 degrees, as a result of which the contribution of the mirror illumination is canceled. This is usually not a cause for concern, since the view vector is often noticeably removed from the reflection vector. But for small values of the mirror-gloss strength, the radius of the reflection region becomes sufficiently large, and makes an appreciable contribution to the overall picture. Using the Fong model, we cancels this contribution at angles greater than 90 degrees (as seen in the first image).

In 197? James F. Blinn (James F. Blinn) presented the Blinn-Fong Lighting Model as an addition to the Phong model, which we have used so far. It is similar in many respects to the Fong model, but uses a slightly different approach to calculating the mirror component, which allows us to solve our problem. Instead of relying on the reflection vector, we use the so-called

**the median vector is**( ? halfway vector [/b] ), Which is a unit vector exactly in the middle between the direction of the view and the direction of the light. The closer this vector to the normal of the surface, the greater the contribution of the mirror component.

When the direction of the survey completely coincides with the (now imaginary) reflection vector, the median vector coincides with the normal to the surface. Thus, the closer the viewing direction to the direction of reflection, the stronger the mirror shine becomes.

Obviously, regardless of the direction from which the observer is looking, the angle between the median vector and the normal to the surface will never exceed 90 degrees (unless, of course, the light source is below the surface). Due to this, we get slightly different results compared to the Fongov reflection, and in general the picture looks more visually plausible, especially at low values of the mirror gloss strength. It was Blinn-Fong's lighting model that was used in the earlier, fixed OpenGL pipeline.

Finding a median vector is easy: you need to add the vector of the direction of light with the view vector and normalize the result:

$$ display $$ vec H = frac {vec L + vec V} {|| vec L + vec V ||} $$ display $$

In GLSL it looks like this:

` vec3 lightDir = normalize (lightPos - FragPos);`

vec3 viewDir = normalize (viewPos - FragPos);

vec3 halfwayDir = normalize (lightDir + viewDir);

Thus, the calculation of the mirror component reduces to a simple calculation of the scalar product between the normal to the surface and the median vector in order to obtain the cosine of the angle between them, which we again raise to the power of specular luminosity:

` `` float spec = pow (max (dot (normal, halfwayDir), 0.0), shininess);`

vec3 specular = lightColor * spec;

Actually, it's all about the Blinn-Fong model. The only difference between mirror reflections in Blinn-Fong and Phong models is that now we measure the angle between the normal and the median vector instead of the angle between the direction of the view and the reflection vector.

Using the median vector to calculate the components of the specular highlights, we will no longer have a problem with the sharp boundary of the specular reflection region that is characteristic of the Fong model. The image below shows the mirror image region of both methods with a mirror gloss strength of 0.5:

Another small difference between the Phong and Blinn-Phong models is that the angle between the median vector and the normal to the surface is often smaller than the angle between the viewing and reflection vectors. Therefore, in order to obtain similar results for Fong's model, the value of the mirror gloss strength should be slightly higher. It is empirically established that it is somewhere between 2 and 4 times greater than the Fong model.

Below is a comparison of the mirror component between the models with a mirror-gloss strength equal to 8 for the Fong model and 32 for the Blinn-Fong model:

As can be seen, the mirror component of Blinn-Fong is sharper. Typically, a small adjustment is required to obtain results similar to those obtained previously using the Fong model, but in general, Blinn-Fong's coverage gives a more plausible picture.

For this demonstration, we used a simple fragment shader that switches between the usual Phong reflection and the Blinn-Fong reflection:

`void main ()`

{

[]

float spec = 0.0;

if (blinn)

{

vec3 halfwayDir = normalize (lightDir + viewDir);

spec = pow (max (dot (normal, halfwayDir), 0.0), 16.0);

}

else

{

vec3 reflectDir = reflect (-lightDir, normal);

spec = pow (max (dot (viewDir, reflectDir), 0.0), 8.0);

}

[]

The source code for this lesson is here . By pressing the b key, you can switch from the Phong model to the Blinn-Fong model and back.

Original of the article .