1
votes

I'm trying to add a fog effect to my scene in OpenGL 3.3. I tried following this tutorial. However, I can't seem to get the same effect on my screen. All that seems to happen is that my objects get darker, but there's no gray foggy mist on the screen. What could be the problem?

Here's my result.

enter image description here

When it should look like:

enter image description here

Here's my Fragment Shader with multiple light sources. It works fine without any fog. All GLSL variables are set and working correctly.

 for (int i = 0; i < NUM_LIGHTS; i++)
{   
    float distance = length(lightVector[i]);

    vec3 l; 

    // point light

    attenuation = 1.0 / (gLight[i].attenuation.x + gLight[i].attenuation.y * distance + gLight[i].attenuation.z * distance * distance);
    l = normalize( vec3(lightVector[i]) );

    float cosTheta = clamp( dot( n, l ), 0,1 );
    vec3 E = normalize(eyeVector);
    vec3 R = reflect( -l, n );
    float cosAlpha = clamp( dot( E, R ), 0,1 );

    vec3 MaterialDiffuseColor = v_color * materialCoefficients.diffuse;
    vec3 MaterialAmbientColor = v_color * materialCoefficients.ambient;

    lighting += vec3(
        MaterialAmbientColor
        + (
            MaterialDiffuseColor * gLight[i].color * cosTheta * attenuation
        )
        + (
            materialCoefficients.specular * gLight[i].color * pow(cosAlpha, materialCoefficients.shininess) 
        ) 
    );
}

float fDiffuseIntensity = max(0.0, dot(normalize(normal), -gLight[0].position.xyz));
color = vec4(lighting, 1.0f) * vec4(gLight[0].color*(materialCoefficients.ambient+fDiffuseIntensity), 1.0f);

float fFogCoord = abs(eyeVector.z/1.0f);
color = mix(color, fogParams.vFogColor, getFogFactor(fogParams, fFogCoord));
1

1 Answers

3
votes

Two things.

First you should verify your fogParams.vFogColor value is getting set correctly. The simplest way to do this is to just short-circut the shader and set color to fogParams.vFogColor and immediately return. If the scene is black, then you know your fog color isn't being sent to the shader correctly.

Second, you need to eliminate your skybox. You can simply set glClearColor() with the fog color and not use a skybox at all, since everywhere the skybox should be visible you should be seeing fog instead, right? More advanced usage could modify the skybox shader to move from fog to the skybox texture depending on the angle of the vec3 off of horizontal, so when looking up the sky is (somewhat) visible, but looking horizontally simply shows the fog, and have a smooth transition between the two.