gpt4 book ai didi

opengl - GLSL 从多个灯光计算颜色矢量

转载 作者:行者123 更新时间:2023-12-03 04:41:31 25 4
gpt4 key购买 nike

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

最佳答案

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

停止复制别人的代码并正确行事。

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

环境是场景的属性,而不是任何特定光线的属性。它旨在代表 indirect, global illumination :从场景中其他物体反射的光,作为一般聚合。你没有环境“灯”;只有一个环境术语,并且应该应用一次。

关于opengl - GLSL 从多个灯光计算颜色矢量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15535984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com