6

我正在使用我自己的(不是内置的 opengl)灯。这是我的片段着色器程序:

#version 330

in vec4 vertexPosition;
in vec3 surfaceNormal;
in vec2 textureCoordinate;
in vec3 eyeVecNormal;

out vec4 outputColor;

uniform sampler2D texture_diffuse;
uniform bool specular;
uniform float shininess;

struct Light {
    vec4 position;
    vec4 ambientColor;
    vec4 diffuseColor;
    vec4 specularColor;
};

uniform Light lights[8];

void main()
{
    outputColor = texture2D(texture_diffuse, textureCoordinate);
    for (int l = 0; l < 8; l++) {
        vec3 lightDirection = normalize(lights[l].position.xyz - vertexPosition.xyz);
        float diffuseLightIntensity = max(0, dot(surfaceNormal, lightDirection));

        outputColor.rgb += lights[l].ambientColor.rgb * lights[l].ambientColor.a;
        outputColor.rgb += lights[l].diffuseColor.rgb * lights[l].diffuseColor.a * diffuseLightIntensity;
        if (specular) {
            vec3 reflectionDirection = normalize(reflect(lightDirection, surfaceNormal));
            float specular = max(0.0, dot(eyeVecNormal, reflectionDirection));
            if (diffuseLightIntensity != 0) {
                vec3 specularColorOut = pow(specular, shininess) * lights[l].specularColor.rgb;
                outputColor.rgb += specularColorOut * lights[l].specularColor.a;
            }
        }
    }
}

现在的问题是,当我有 2 个具有环境颜色的光源时vec4(0.2f, 0.2f, 0.2f, 1.0f),模型上的环境颜色将是vec4(0.4f, 0.4f, 0.4f, 1.0f)因为我只是将它添加到 outputColor 变量中。如何计算多个灯光的单个环境和单个漫反射颜色变量,这样我会得到一个真实的结果?

4

1 回答 1

12

这是一个有趣的事实:现实世界中的灯光没有环境、漫反射或镜面反射颜色。它们发出一种颜色。时期(好吧,如果你想成为技术人员,灯光会发出很多颜色。但是单个光子没有“环境”属性。它们只是具有不同的波长)。你所做的只是抄袭那些抄袭 OpenGL 关于环境光和漫射光颜色的废话的人。

不要再抄袭别人的代码,做了。

每盏灯都有一种颜色。您使用该颜色来计算该光对对象的漫反射和镜面反射贡献。就是这样。

Ambient is a property of the scene, not of any particular light. It is intended to represent indirect, global illumination: the light reflected from other objects in the scene, as taken as a general aggregate. You don't have ambient "lights"; there is only one ambient term, and it should be applied once.

于 2013-03-20T23:07:15.720 回答