View Full Version : glGetUniformLocation returns -1

08-28-2011, 02:22 PM
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.

#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.

Chris Lux
08-28-2011, 02:50 PM
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...

08-28-2011, 03:12 PM
Here the simple fragment shader.

#version 140

in vec3 ex_Color;
out vec4 out_Color;

void main(void)
out_Color = vec4(ex_Color,1.0);

normally it using it.

08-28-2011, 03:51 PM
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?

08-28-2011, 04:27 PM
Now I see my mistake! In the vertex shader it is called "ex_color" and in the fragment shader "ex_Color"...

Alfonse Reinheart
08-28-2011, 05:01 PM
Are you sure you didn't get a linker error? Did you check to see if your program linked correctly?

08-29-2011, 01:54 AM
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