Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: glGetUniformLocation returns -1

  1. #1
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    36

    glGetUniformLocation returns -1

    Hello guys,
    I am working right now with GLSL and trying to get the location of some uniform variables. So far I found out that the glsl compiler makes some optimizations to these variables if they are not in use, i.e. it results in -1 location.
    In my case I cannot understand it really because all variables have influence in the result.
    Code :
    #version 140
     
    uniform vec3 lightPosition;
    uniform vec3 skyColor;
    uniform vec3 groundColor;
    uniform mat4 mvMatrix;
    uniform mat4 mvpMatrix;
    uniform mat3 normalMatrix;
     
    in vec3 in_position;
    in vec3 in_normal;
     
    out vec3 ex_color;
     
    void main(void)
    {
    	vec3 ecPosition = vec3(mvMatrix * vec4(in_position, 1.0));
    	vec3 tnorm = normalize(normalMatrix * in_normal);
    	vec3 lightVec = normalize(lightPosition - ecPosition);
    	float costheta = dot(tnorm, lightVec);
    	float a = costheta * 0.5 + 0.5;
    	ex_color = mix(groundColor, skyColor, a);
    	gl_Position = mvpMatrix * vec4(in_position, 1.0);
    }
    This code I have taken from the orange book. The variable "mvpMatrix" has a positive location, all others no. I hope somebody can explain me, where the problem is.

  2. #2
    Member Regular Contributor
    Join Date
    Nov 2003
    Location
    Germany
    Posts
    289

    Re: glGetUniformLocation returns -1

    let me guess, ex_color is not used in the fragment shader (or other connected shader like geometry shader). if not, the linker will remove the calculation to ex_color and so the uniforms are unused and will return an invalid location...

  3. #3
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    36

    Re: glGetUniformLocation returns -1

    Here the simple fragment shader.
    Code :
    #version 140
     
    in  vec3 ex_Color;
    out vec4 out_Color;
     
    void main(void)
    {
    	out_Color = vec4(ex_Color,1.0);
    }
    normally it using it.

  4. #4
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    36

    Re: glGetUniformLocation returns -1

    Hello again,
    With this fragment shader above it doesn't work. Then I removed this fragment shader, so that I have only one vertex shader and then glsl recognized my uniform variables. The output of the uniform count was 6 and the attribute count was 2 (as expected). So I am a little bit confused... why it is working without the fragment shader?

  5. #5
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    36

    Re: glGetUniformLocation returns -1

    Sorry!
    Now I see my mistake! In the vertex shader it is called "ex_color" and in the fragment shader "ex_Color"...

  6. #6
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,723

    Re: glGetUniformLocation returns -1

    Are you sure you didn't get a linker error? Did you check to see if your program linked correctly?

  7. #7
    Intern Newbie
    Join Date
    Dec 2010
    Posts
    36

    Re: glGetUniformLocation returns -1

    Yes I checked it. The linker was OK. As I said, I corrected the variable name in the fragment shader. Or corrected in the vertex shader.

    out ex_Color ---> in ex_Color

    Then it worked well. Stupid mistake... :P

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •