Fog GLSL shaders

Hello!

I share 3 fog shaders for different sorts of objects:

All those shaders use the same equation:

const float LOG2 = 1.442695;
float z = gl_FragCoord.z / gl_FragCoord.w;
float fogFactor = exp2( -gl_Fog.density *
gl_Fog.density *
z *
z *
LOG2 );
fogFactor = clamp(fogFactor, 0.0, 1.0);

gl_FragColor = mix(gl_Fog.color, finalColor, fogFactor );

http://www.ozone3d.net/tutorials/glsl_fog/p02.php (sketch GL_EXP2)

The computation is made in the fragment shader (fog per pixel).

  • Stones: 1 Diffuse texture. Basic shader.
  • Ground: 1 Diffuse texture and 1 Normal texture (I played with normal texture coordinates to fake reliefs)
  • Trees: 2 materials including 1 with alpha texture.

I found the original code here: http://www.ozone3d.net/tutorials/glsl_fog/p05.php

Commands: ZSQD to move the Camera, WheelUpMouse and WheelDownMouse to increase or decrease red component of the fog.

I forgot to make object with specular… http://en.wikibooks.org/wiki/GLSL_Programming/Blender/Specular_Highlights

I’m not experimented in GLSL so if I made mistakes, thanks to inform me. And if you have errors, thanks to warn me. I’ll try to fix it.

Have a good day!

Looks great and runs very fast. Every day using the BGE I wish I’d studied harder in math class, it’s all Greek to me. :slight_smile:

There’s a bit of a visible line where the mist begins though, I don’t know how to fix that.

Thanks! About the “visible” line, it’s deliberate (that was to show the difference beetween no fog/fog effect)(if you don’t want to have it, you just have to replace:

if (z < 10.0)
{
gl_FragColor = finalColor;
}
else
{
gl_FragColor = mix(color, finalColor, fogFactor);
}

by:

gl_FragColor = mix(color, finalColor, fogFactor); )

(in the 3 shaders)(z is the distance beetween camera and pixel)

I don’t try to understand maths equations (sometimes but that’s often too complicate and it’s been a long time since I finished school). Just apply it. It’s more a copy/paste work. But each day, I understand one more thing :slight_smile:

Hello youle!

I see you apply some of my “advices” :wink:

Could you please post a screenshot of how the effect looks like on your machine, 'cause I’m not quite sure it is displayed right here… Thanks!

Hello Matpi!

Yes I try some little things in GLSL.


On the ground, I faked relief with wrong normal textures coordinates… (and that makes wrong lighting effects…) That’s what about you are shocked?

Have a good day!

Ok, I think it looked right anyways…

What I believe I’m surprised about is that the background isn’t affected by the fog… It would add a lot of realism to have that too. And maybe you should try a “blueish” color rather than white (kind of like as in your reference on ozone3d)?

Thanks, you too!

Yes It’s not realistic. The skyBox has its own shader. But I could add another semi transparent skybox with fog shader…

EDIT: http://www.mediafire.com/download/50bngnkdvmylvym/FogShaders2.blend

I could have add an uniform property to play with the density of the fog but…

Attachments


Much better :wink:

Something strange, though: the parts of leaves that are in front of the ground appear green, but others (in front of the sky) are totally grey, with an abrupt transition…

Yes I don’t know how to fix that… EDIT: No alpha problem when I apply the fog directly on the “sun skybox”… Or if the “sun skybox” is semi transparent and the “fog skybox” is larger than the “sun skybox”.