In most tutorials I see on how to achieve fog with shaders the fog is exponential and has a minimum / start distance from the camera. Why? Both seem counterintuitive, especially the minimum distance. Is this an artistic / design choice or is this how our eyes see fog in real life?

  • \$\begingroup\$ Does not seem counter-intuitive to me. In fog you see pretty clearly just in front of you, then in becomes a bit foggy and not far after that you cannot see much anymore \$\endgroup\$ – realUser404 Jan 23 '17 at 14:45

This is a legacy from fixed pipeline fog equations, where you typically had one of three options available:

  • Linear blended between a start distance and an end distance.
  • Exponential based on density.
  • Exponential-squared based on density.

For example, the documentation for glFog and for Direct3D 9 fog equations.

A sample tutorial making reference to these three equations (you can cross-check with the documentation and determine that they are the same).

Another sample tutorial referring to fog in the fixed pipeline versus implementing it yourself in a shader.

GLSL 1.10.59 specification containing documentation of built-in uniforms for fog, allowing glFog calls to be used with shaders.

Using shaders you are of course not restricted to these equations, and you can adapt them, modify them, or write your own as you see fit. However, tutorial material does tend to either emulate the older equations or take them as a starting point, because they are a familiar reference (and early versions of GLSL even provided access to glFog parameters via built-in uniforms). You see the very same with lighting, where tutorials typically follow the old fixed pipeline lighting models too (and likewise early versions of GLSL provided access to lighting state too).

(Interesting aside: fixed-pipeline fog did not support specifying a start distance with exponential fog which suggests that some tutorial authors may not have paid as close attention to the documentation as they could have).

| improve this answer | |
  • \$\begingroup\$ Fog formula use an exponential because exponential is close to the physics of what happen in real life. Tho, with shaders and powerful GPU, we can do better simulations of the atmospheric haze and light scattering in homogeneous media :) \$\endgroup\$ – galop1n Feb 1 '17 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.