How to implement ground fog of a GLSL shader

I am trying to implement a ground fog shader for my terrain rendering engine. The technique is described in this article: http://www.iquilezles.org/www/articles/fog/fog.htm

The idea is to look at the beam going from the camera to the fragment and integrate the fog density function along this beam.

Here is my shader code:

#version 330 core in vec2 UV; in vec3 posw; out vec3 color; uniform sampler2D tex; uniform vec3 ambientLightColor; uniform vec3 camPos; const vec3 FogBaseColor = vec3(1., 1., 1.); void main() { vec3 light = ambientLightColor; vec TexBaseColor = texture(tex,UV).rgb; //***************************FOG******************************************** vec3 camFrag = posw - camPos; float distance = length(camFrag); float a = 0.02; float b = 0.01; float fogAmount = a * exp(-camPos.z*b) * ( 1.0-exp( -distance*camFrag.z*b ) ) / (b*camFrag.z); color = mix( light*TexBaseColor, light*FogBaseColor, fogAmount ); } 

Firstly, I don’t understand how to choose a and b and what is their physical role in the function of fog density.

Then the result is not what I expect ... I have an earthen fog, but the fogAmount transition from 0 to 1 is always focused on the height of the camera. I tried a lot of different a and b, but when I do not have a transition to the height of the camera, I am either completely fogged or not fogged in any area.

I checked the data that I use and everything is correct:

  • camPos.z is the height of my camera.
  • camFrag.z - the vertical component of the vector going from the camera to the fragment

I cannot understand which part of the equation is causing this.

Any ideas on this?

EDIT: Here's the effect I'm looking for: image1 image2

+6
source share
2 answers

I found a method that gives the result I was looking for.

The method is described in this article by Eric Lengiel: [link] http://www.terathon.com/lengyel/Lengyel-UnifiedFog.pdf

It explains how to create a layer of fog with density and height parameters. You can fly through it, it gradually mixes all the geometry over the fog.

+4
source

This is a fairly standard atmospheric dispersion application.

This is usually discussed under general volumetric lighting, which includes the transmission of light through various media (e.g., smoke, air, water). In a modern graphical shell based on shaders, this can be achieved in real time using beam marching or if there is only one evenly participating medium (as in this case, the fog is applied only to air), simplified to integration at a certain distance.

Usually you pass the marker through the participating media to determine the light transmission properties, but this application is simplified to take an environment that has clearly defined distribution characteristics, and it is here that the coefficients that you confuse come from, The fog density exponentially changes with distance, and this that controls b also changes also with height ( not shown in the equation immediately below).

http://www.iquilezles.org/www/articles/fog/for00.png

However, this article is a discussion of the weakly named coefficients a and b . They control scattering and extinction. The author repeatedly refers to the extinction coefficient as an extension, which actually does not make any sense to me - I hope this is simply because English was not the mother tongue of the author. Extinction can be considered as how quickly light is absorbed, and describes the opacity of the medium. If you want a more theoretical foundation for all of this, see the next article .

With this in mind, take another look at the code from your article:

 vec3 applyFog( in vec3 rgb, // original color of the pixel in float distance, // camera to point distance in vec3 rayOri, // camera position in vec3 rayDir ) // camera to point vector { float fogAmount = c*exp(-rayOri.y*b)*(1.0-exp(-distance*rayDir.y*b))/rayDir.y; vec3 fogColor = vec3(0.5,0.6,0.7); return mix( rgb, fogColor, fogAmount ); } 

You can see that c in this code is actually a from the original equation.

More importantly, there is an additional expression:

LaTeX Equation

This additional expression controls density in height. Judging by your shader implementation, you have incorrectly executed the second expression. camFrag.z very likely not a height, but a depth. Also, I don't understand why you multiply it by a coefficient b .

+3
source

All Articles