Fog

I’m tryng to figure out, how to apply the fog in a fragment shader.
The fog is enabled before going into the shader, some operations are performed in the shader, but i need that the fog is applied in the FragColor too.

I tried, to:
gl_FragColor = gl_Fog.color * Output;
or
gl_FragColor = gl_Fog.color + Output;

Both of them don’t work, i guess there’s some other way to do this ?

thanks,
Bruno

When using a fragment shader you replace the fixed function fog computation and your shader needs to calculate the fog itself. Here is an example that shows how to do the EXP, EXP2, and LINEAR fog modes:

    float fogFactor = 1.0;

    if (fogMode == FogExp)
        fogFactor = exp(-gl_Fog.density * gl_FogFragCoord);
    else if (fogMode == FogExp2)
        fogFactor = exp(-pow((gl_Fog.density * gl_FogFragCoord), 2.0));
    else if (fogMode == FogLinear)
        fogFactor = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;

    fogFactor = clamp(fogFactor, 0.0, 1.0);

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

Thanks, but i’m unable to get it working right.,

Let’s imagine my original glFragment color output, is at a variable called outFrag.

I should do this, right ?

float fogFactor = 1.0;
fogFactor = exp(-gl_Fog.density * gl_FogFragCoord);
fogFactor = clamp(fogFactor, 0.0, 1.0);

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

However, i get no fog at all.
Is anything wrong with that code ?

thanks,
Bruno

Do you have a vertex shader as well? If so, you need to write to gl_FogFragCoord, like so:

vec4 eyePos = gl_ModelViewMatrix * gl_Vertex;
gl_FogFragCoord = abs(eyePos.z/eyePos.w);

Isn’t possible to do it without a vertex program ?
I’m asking because i’m not using one, and if i do, it will mess up my projection matrix.

If you aren’t using a vertex shader then the fixed function vertex pipeline will compute gl_FogFragCoord for you.

Are you setting the fog density in your app?

Example:

glFogf(GL_FOG_DENSITY, 0.35);

Does the fog work correctly if you don’t have a fragment shader?

Yeah i’am , i’m setting the fog before enabling the shader, and yes, i’m setting the fog density, fog_start, fog_end, fog equation
( GL_EXP) , etc

Can’t think of any reason it wouldn’t be working then.

I have a small example that does EXP, EXP2, and LINEAR fog and allows you to to switch between fixed function and shaders at runtime. You can download it here . Press ‘f’ to switch between the different fog modes and ‘s’ to toggle shaders and fixed function.

If this example works correctly on your computer (you shouldn’t be able to see any differences when switching between shaders and fixed function) then take a look at the code and see if you can locate what you are doing differently.

If you don’t see any fog in the shader mode then your might be seeing a driver bug.

Thanks,
Your application works fine here, i don’t get it.
Apparently i’m doing everything you do, i must be missing something…,
Oh well, i will eventually find out,

thanks again

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.