OpenGL ES 2.0无法实现标准线性雾化功能

时间:2015-03-06 20:45:41

标签: opengl-es opengl-es-2.0

我一直在尝试实现一个基本的雾函数,我发现的一些资源让我尝试了线性雾函数,我的理解让我相信是基于原始的固定函数雾方程(尽管我可能错了)。但是,当我尝试实现它时,我的所有表面都变黑了。我很确定我的问题在于我(试图)如何计算眼距,因为其余的方程非常简单。

我的顶点着色器:

precision mediump float;

attribute vec4 position;
attribute vec3 normal;
attribute vec2 texCoordIn;

varying vec3 eyeNormal;
varying vec4 eyePos;
varying vec2 texCoordOut;
varying float eyeDistance;

uniform mat4 modelViewProjectionMatrix;
uniform mat4 modelViewMatrix;
uniform mat3 normalMatrix;
uniform vec3 cameraPosition;

void main()
{
    eyeNormal = (normalMatrix * normal);
    eyePos = modelViewMatrix * position;      
    eyeDistance = sqrt((eyePos.x - cameraPosition.x) * (eyePos.x - cameraPosition.x) + (eyePos.y - cameraPosition.y) * (eyePos.y - cameraPosition.y) + (eyePos.z - cameraPosition.z) * (eyePos.z - cameraPosition.z));
    texCoordOut = texCoordIn;
    gl_Position = modelViewProjectionMatrix * position;
}

我的片段着色器:

precision mediump float;

varying vec3 eyeNormal;
varying vec4 eyePos;
varying vec2 texCoordOut;
varying float eyeDistance;
uniform sampler2D texture;

uniform vec3 flashlightPosition;
uniform vec3 diffuseLightPosition;
uniform vec4 diffuseComponent;
uniform float shininess;
uniform vec4 specularComponent;
uniform vec4 ambientComponent;
uniform float maxFogDistance;
uniform float minFogDistance;
uniform vec4 fogColor;

void main()

{
    vec4 ambient = ambientComponent;

    vec3 N = normalize(eyeNormal);
    float nDotVP = max(0.0, dot(N, normalize(diffuseLightPosition)));
    vec4 diffuse = diffuseComponent * nDotVP;

    vec3 E = normalize(-eyePos.xyz);
    vec3 L = normalize(flashlightPosition - eyePos.xyz);
    vec3 H = normalize(L+E);
    float Ks = pow(max(dot(N, H), 0.0), shininess);
    vec4 specular = Ks*specularComponent;
    if( dot(L, N) < 0.0 ) {
        specular = vec4(0.0, 0.0, 0.0, 1.0);
    }

    float factor = (maxFogDistance - eyeDistance) / (maxFogDistance - minFogDistance);
    factor = clamp(factor, 0.0, 1.0);
    vec4 finalFogColor = factor * fogColor;

    vec4 finalColor = (ambient + specular) * texture2D(texture, texCoordOut);
    gl_FragColor = finalColor * factor + fogColor * (1.0 - factor);
    gl_FragColor.a = 1.0;
}

此外,最小雾距已设置为1f,最大雾距设置为10f(我的移动空间非常小,目前为5x5,因此这应该是合适的)并且雾颜色设置为白色。

任何人都可以提供任何帮助将非常感激。如果问题不在着色器中,我可以发布额外的代码(但是,我无法想象其他任何地方,在此之前着色器的其他方面工作正常)。

0 个答案:

没有答案