OpenGL 3.3 GLSL片段着色器雾效果不工作

OpenGL 3.3 GLSL片段着色器雾效果不工作,opengl,glsl,fog,Opengl,Glsl,Fog,我正试图在OpenGL 3.3中为我的场景添加雾效果。我试着跟着。然而,我似乎无法在我的屏幕上获得相同的效果。所有发生的事情似乎是我的物体变暗了,但屏幕上没有灰色的雾。有什么问题吗 这是我的结果 当它看起来像: 这是我的带有多个光源的片段着色器。它工作正常,没有雾。所有GLSL变量均已设置且工作正常 for (int i = 0; i < NUM_LIGHTS; i++) { float distance = length(lightVector[i]); v

我正试图在OpenGL 3.3中为我的场景添加雾效果。我试着跟着。然而,我似乎无法在我的屏幕上获得相同的效果。所有发生的事情似乎是我的物体变暗了,但屏幕上没有灰色的雾。有什么问题吗

这是我的结果

当它看起来像:

这是我的带有多个光源的片段着色器。它工作正常,没有雾。所有GLSL变量均已设置且工作正常

 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));
for(int i=0;i
两件事

首先,您应该验证您的
fogParams.vFogColor
值设置是否正确。最简单的方法是短切着色器,将
color
设置为
fogParams.vFogColor
并立即返回。如果场景为黑色,则您知道雾颜色未正确发送到着色器

 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));

其次,你需要消除你的skybox。您可以简单地将glClearColor()设置为雾颜色,而不使用天空盒,因为天空盒应该在任何地方都可见,您应该看到雾,对吗?更高级的使用可以修改skybox着色器,使其根据vec3偏离水平线的角度从雾移动到skybox纹理,因此,当仰视天空时(有些)可见,但水平观看仅显示雾,并在两者之间进行平滑过渡。

非常感谢!vFogColor不起作用。我加了glClearColor,看起来好多了。能否请您更详细地指导我如何根据视角修改雾的skybox着色器?你知道这方面的教程吗?