算术运算符的 GLSL 操作数必须是数字

GLSL operands to arithmetic operators must be numeric

我有一些旧的 GLSL 代码,以前编译得很好,但在切换到 Linux 的 gcc 编译器后,它突然抛出错误。来自碎片着色器:

#version 330 core
out vec4 FragColor;

in vec2 TexCoord;
in vec3 Normal;
in vec3 FragPos;

// texture samplers
uniform sampler2D texture0;
uniform vec3 lightPos;
uniform vec3 viewPos;
uniform vec3 lightColor;

void main()
{
//ambient lighting
vec3 ambLig = vec3(1.0,1.0,1.0);
float ambientStrength = 0.15;
vec4 ambient = ambientStrength * vec4(ambLig,1.0);

//diffuse lighting
vec3 norm = normalize(Normal);
vec3 lightDir = normalize(lightPos - FragPos);  

float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = diff * lightColor;

//specular lighting
float specularStrength = 0.5;

vec3 viewDir = normalize(viewPos - FragPos);
vec3 reflectDir = reflect(-lightDir, norm);  

float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
vec3 specular = specularStrength * spec * lightColor;  

FragColor = texture(texture0, TexCoord) * (ambient + diffuse + specular);
}

这会在我计算 FragColor:

的最后一行引发错误
Using ALSA driver
ERROR::SHADER_COMPILATION_ERROR of type: FRAGMENT
0:37(45): error: vector size mismatch for arithmetic operator
0:37(45): error: operands to arithmetic operators must be numeric
0:37(14): error: operands to arithmetic operators must be numeric

我查了一些previous questions and it seems the problem occurs when using GLSL 1.2 or earlier, since it doesn't do automatic casts. But I have no idea what the problem could be here, since I'm using OPENGL Core 3.3, which should map to GLSL 3.30请帮助我。

注意:为了方便起见,我在构建中包含了一个 glad 版本,其中 api 指定为 3.3。这可能是问题的根源,但我不确定,因为我相信我构建的一切都是正确的。

编辑:请参阅下面的答案,同时更改

vec4 ambient = ambientStrength * vec4(ambLig,1.0);

vec3 ambient = ambientStrength * ambLig;

texture(texture0, TexCoord) 的 return 值是 vec4 类型。但是,ambient + diffuse + specular 的类型是 vec3

变化:

FragColor = texture(texture0, TexCoord) * (ambient + diffuse + specular);

FragColor = texture(texture0, TexCoord) * vec4(ambient + diffuse + specular, 1.0);