   Fog calculations
  Hiya.I'm working on a fog shader to solve the issues I have with dynamic lighting. What I do is basically disable fog when I render my maps using two passes. After all the elements have been rendered, I render the whole map again without textures, and I enable alpha blending.Using a GLSL shader, I set alpha values based on pixel distance from the view, and the pixel's colors are the same as those of the fog. This all seems to be working fine, but it seems that my fog calculations don't really match with the Standard OpenGL functionality, because fog distances seem to be handled differently.The same thing appears on the models too, because I had to rebuild the fog algorythm for those too, after I started using shaders on them. The fog distances simply don't seem to match, so my objects rendered with custom fog fade out less, and it looks odd if they are suddenly clipped by the view frustum.This is how the shader currently looks like:uniform vec3 fogcolor;uniform float fogend;uniform float fogstart;void main(){float z = gl_FragCoord.z / gl_FragCoord.w;float fogFactor = (fogend - z) / (fogend - fogstart);fogFactor = (1.0 - clamp(fogFactor, 0.0, 1.0));gl_FragColor = vec4(fogcolor, fogFactor);}Normally, I'd just try to use custom fog on all of my rendered polys, but to be honest I'd like to know if someone has a true solution to this. Anyway, the same problem would remain with the view frustum's far clip distance, and I'm hoping I don't have to use some random added number to the actual distance.Thanks.Andrew.   
  Hello Andrew!You render the map with texture and fog disabled first and then render the map with no texture and fog enabled! Don't you !And the problem is the fragCoords in the same pixel is same in you two rendering passes! And I think it will result in a unexcepted effect.I think you can get a fog effect with only one pass.in your vertex shader code , you can calculate the fog coord with z value and use the interpolated fog coord value in you fragment shader code,this is very simple and you can get the full code with <<The OpenGL shade language>>.This book introduce how to use GLSL to implement Standard OpenGL functionality.   ;  Thanks ruzhenchao!You really helped me with this. The reason I need to calculate fog like this is because of two things:1: My decals always caused graphics bugs, because of how they are blended with the lightmaps.2: I need to render the map in two passes whenever I have dynamic lights working ingame. This causes fog colors to be multiplied, and so fogging on the whole map appears completely incorrectly fogged.To be honest, I would have been happy if I could've found another way, because rendering all that geometry again is really expensive for the game. I tried using a rectangle texture, but depth values weren't available for alpha blended objects, and so It wasn't an option.Thanks for your help again, I finally have fog working 100%.   