Tomasso
Tomasso

Reputation: 711

How to debug shader compiler error when error message line number is wrong?

My OpenGL application loads its vertex and fragment shaders from two ascii source files. Compiling the fragment shader generates these errors:

0:35(22): error: operands to arithmetic operators must be numeric
0:35(17): error: cannot construct `vec4' from a non-numeric data type
0:40(31): error: too many parameters to `vec4' constructor

So far I’m unable to find the cause of these errors, after many hours. Here is the shader source:

#version 330
/*
Adapted from phong shader demo at http://www.cs.toronto.edu/~jacobson/phong-demo/
*/
/// precision MEDIUMP float;  // Generates a syntax error on non-embedded OpenGL
varying vec3 normalInterp;  // Surface normal
varying vec3 vertPos;       // Vertex position
uniform int mode;   // Rendering mode
uniform float u_Ka;   // Ambient reflection coefficient
uniform float u_Kd;   // Diffuse reflection coefficient
uniform float u_Ks;   // Specular reflection coefficient
uniform float u_shininess; // Shininess

// Material color
/// uniform vec3 u_ambientColor;
uniform vec3 u_diffuseColor;
uniform vec3 u_specularColor;
uniform vec3 u_lightPos; // Light position

varying vec4 ambientColor;

void main() {
  vec3 N = normalize(normalInterp);
  vec3 L = normalize(u_lightPos - vertPos);

  // Lambert's cosine law
  float lambertian = max(dot(N, L), 0.0);
  float specular = 0.0;
  if(lambertian > 0.0) {
    vec3 R = reflect(-L, N);      // Reflected light vector
    vec3 V = normalize(-vertPos); // Vector to viewer
    // Compute the specular term
    float specAngle = max(dot(R, V), 0.0);
    specular = pow(specAngle, u_shininess);
  }  // This is line 35!
  gl_FragColor = vec4(u_Ka * ambientColor +
                      u_Kd * lambertian * u_diffuseColor +
                      u_Ks * specular * u_specularColor, 1.0);

But “line 35” of the shader source file is a closing ‘}’, not an actual statement. How to interpret the line numbers on compiler error messages? Can someone help me find these errors? How do people debug GLSL code when the error message line numbers are wrong?

Upvotes: 2

Views: 2221

Answers (1)

gkv311
gkv311

Reputation: 2982

Look around mentioned line - the next line has an error:

  gl_FragColor = vec4(u_Ka * ambientColor +
                      u_Kd * lambertian * u_diffuseColor +
                      u_Ks * specular * u_specularColor, 1.0);

ambientColor defined as vec4 is used in the math expecting vec3 as first argument to vec4(rgb, a) constructor.

So that particular lines may look like this:

gl_FragColor = vec4(ambientColor.rgb * u_Ka
                  + u_diffuseColor * lambertian * u_Kd
                  + u_specularColor * specular * u_Ks, 1.0);

GLSL logs vary from vendor to vendor, but normally line numbers look correct in logs, as long as there is no confusion in numbering of your own GLSL code.

0:35(22) normally refers to 0th portion of GLSL code passed to glShaderSource() (as it is possible passing an array of strings, not just a single one) 35th line and 22nd column in code.

Upvotes: 5

Related Questions