How to do it...

To create a shader that produces a fog-like effect, use the following code for the fragment shader:

in vec3 Position; 
in vec3 Normal; // Light and material uniforms ...uniform struct FogInfo {  float MaxDist;  float MinDist;  vec3 Color;} Fog;layout( location = 0 ) out vec4 FragColor;vec3 blinnPhong( vec3 position, vec3 n ) {   // Blinn-Phong reflection model ...}void main() {    float dist = abs( Position.z );    float fogFactor = (Fog.MaxDist - dist) /                      (Fog.MaxDist - Fog.MinDist);    fogFactor = clamp( fogFactor, 0.0, 1.0 );    vec3 shadeColor = blinnPhong(Position, normalize(Normal));    vec3 color = mix( Fog.Color, shadeColor, fogFactor );    FragColor = vec4(color, 1.0);}

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.