1

我有一些以前编译好的旧 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

我检查了一些以前的问题,似乎在使用 GLSL 1.2 或更早版本时会出现问题,因为它不进行自动转换。但我不知道这里可能出现什么问题,因为我使用的是 OPENGL Core 3.3,它应该映射到 GLSL 3.30请帮助我。

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

编辑:见下面的答案,也改变

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

vec3 ambient = ambientStrength * ambLig;
4

1 回答 1

1

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

改变:

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

FragColor = texture(texture0, TexCoord) * vec4(ambient + diffuse + specular, 1.0);
于 2021-11-11T12:15:03.647 回答