Reputation: 47
I have some old GLSL code that compiled fine previously, but after switching to Linux's gcc compiler it's suddenly throwing errors. From the fragement shader:
#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);
}
This throws an error on the final line, where I calculate 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
I checked out some 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 Help me please.
Note: I've included a version of glad
in the build with api specified as 3.3
for convenience. That could be the root of the problem, but I'm not sure, since I believe I build everything correctly.
EDIT: See answer below, also change
vec4 ambient = ambientStrength * vec4(ambLig,1.0);
to
vec3 ambient = ambientStrength * ambLig;
Upvotes: 1
Views: 610
Reputation: 210877
The return value of texture(texture0, TexCoord)
is of type vec4
. However, ambient + diffuse + specular
is of type vec3
.
Change:
FragColor = texture(texture0, TexCoord) * (ambient + diffuse + specular);
FragColor = texture(texture0, TexCoord) * vec4(ambient + diffuse + specular, 1.0);
Upvotes: 1