Search code examples
javascriptglslwebgl

glsl vertex shader glGetUniformLocation return null


hello everyone i'm learning webgl and i can't get the location of the special variable here is the code

vertex-shader

  uniform vec2 u_translation;
  attribute vec2 a_position;
  uniform vec2 u_resolution;
  void main() {
    vec2 position = a_position + u_translation;
    vec2 zeroToOne = a_position / u_resolution;
    vec2 zeroToTwo = zeroToOne * 2.0;
    vec2 clipSpace = zeroToTwo - 1.0;
    gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);
  }

fragment-shader

  precision mediump float;
  uniform vec4 u_color;
  void main() {
    gl_FragColor = u_color;
  }

    var translationLocation = gl.getUniformLocation(program, "u_translation");
    var positionLocation = gl.getAttribLocation(program, "a_position");
    var resolutionLocation = gl.getUniformLocation(program, "u_resolution");
    var colorLocation = gl.getUniformLocation(program, "u_color");
// translationLocation is null  but i can get the others

enter image description here

does any one know what happend? i think i used the 'u_translation' in the shader


Solution

  • The local variable position is not used anywhere in the shader and thus u_translation is not needed and optimized out. So it does not become an active program resource and does not get a uniform location. Likely the following line is wrong

    vec2 zeroToOne = a_position / u_resolution;
    

    but should be

    vec2 zeroToOne = position / u_resolution;
    

    Now the local variable position is used in the shader program and thus u_translation becomes an active program resource.