我正在使用我自己的(不是内置的 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 变量中。如何计算多个灯光的单个环境和单个漫反射颜色变量,这样我会得到一个真实的结果?