glGetUniformLocation returns -1 for exisitng unifo

Hello…I am new to shaders and they seem pretty self explanatory but I am getting frustrated quickly. I am starting to work with uniforms but I cannot get the location call to return anything other than incorrect results.

BTW…Visual C++ w/ Freeglut 2.6 and GLee 5.4. Maybe it’s Glee?

Here’s the code…

C++ Segment
shaderPrg = glCreateProgram();
glAttachShader(shaderPrg, v);
glAttachShader(shaderPrg, f);
glLinkProgram(shaderPrg);

int loc1 = glGetUniformLocation(shaderPrg, “color”);
int loc2 = glGetUniformLocation(shaderPrg, “norm”);
int loc3 = glGetUniformLocation(shaderPrg, “topo”);

VERTEX SHADER
uniform sampler2D topo;
uniform sampler2D norm;
uniform sampler2D color;

varying vec2 texture_coordinate;

void main()
{
// Passing The Texture Coordinate Of Texture Unit
texture_coordinate = vec2(gl_MultiTexCoord0);

// Transforming The Vertex
gl_Position = ftransform();
}

FRAGMENT SHADER
uniform sampler2D topo;
uniform sampler2D norm;
uniform sampler2D color;

varying vec2 texture_coordinate;

void main()
{
// Sampling The Texture And Passing It To The Frame Buffer
gl_FragColor = texture2D(color, texture_coordinate);
}

OK…all I can get is…
Loc1 = 0 which is wrong as color is at 2
Loc2 = -1 which is wrong as normBILL is at 1
Loc3 = -1 which is wrong as topo is at 0

I am obviously missing something rather simple. The shader seems to work and when I tweak it I see changes. One other thing I noticed is if I change the fragment shader line to…

gl_FragColor = vec4(1); //texture2D(color, texture_coordinate);

…my geometry is all white, as expected, but all locations are -1. Is there a reference thing I am having a problem with? Do I need to reference everything in a certain way for the locations to properly report?

It removes unused uniforms, and since you don’t use “topo” or “norm” in the shader, they aren’t included and therefore don’t have a location.

If the uniform is determined to have no effect on the final result, then they are often removed.

Loc1 = 0 which is wrong as color is at 2

The location can be any number. The actual integer value that you assigned this uniform should be equal to the texture slot ID it’s bound to.

Well they are being ‘optimized’ and removed. I now have vars to access all of the uniforms and they have references. I also noticed that I have to use each uniform in both the vertex and fragment shader or it gets removed. I am collecting data from each texture, suming and passing along. Using that result in the output of fragment shader. THEN they all have references. If I don’t use the varying passed all get dropped. Also performance now suffers with these shaders. 150fps to 7fps. Thoughts…

Goal is to bump map a planet model of MARS. I have my own geometry and collected diffuse, topo(height) and normal maps from a site on the web. I never thought that this would be so time consuming. Anyone have an exmaple shader that would just plug in to bump map? Say with the diffuse and normal map only? Also I have seen mention of tangents, but tangetns to what?

OK…my current shaders…

VERTEX
uniform sampler2D diffuse;
uniform sampler2D norm;
uniform sampler2D topo;

varying vec2 texture_coordinate;
varying vec4 color;

void main()
{
// Passing The Texture Coordinate Of Texture Unit 0 To The Fragment Shader
texture_coordinate = vec2(gl_MultiTexCoord0);

vec4 x =  texture2D(diffuse, texture_coordinate);
vec4 y =  texture2D(norm, texture_coordinate);
vec4 z =  texture2D(topo, texture_coordinate);
color = x + y + z;

// Transforming The Vertex
gl_Position = ftransform();

}

FRAGMENT
uniform sampler2D diffuse;
uniform sampler2D norm;
uniform sampler2D topo;

varying vec2 texture_coordinate;
varying vec4 color;

void main()
{
vec4 x = texture2D(diffuse, texture_coordinate);
vec4 y = texture2D(norm, texture_coordinate);
vec4 z = texture2D(topo, texture_coordinate);

// Sampling The Texture And Passing It To The Frame Buffer
//gl_FragColor = texture2D(diffuse, texture_coordinate);
gl_FragColor = color;

}

I also noticed that I have to use each uniform in both the vertex and fragment shader or it gets removed

First, the only condition that 100% prevents removing of the uniform is the influence on your final output color (fragment shader).
Next, this optimization thing is completely up to GLSL compiler (driver) - you should not rely on it (and of course, driver doesn’t change the program behavior correctness, otherwise it’s a bug). Just write a shader that does what you need and check the uniform location before uploading a value.

Also performance now suffers with these shaders. 150fps to 7fps.

Maybe because you are doing 3 samples in a vertex shader and have an enormous vertex count.

Anyone have an exmaple shader that would just plug in to bump map? Say with the diffuse and normal map only? Also I have seen mention of tangents, but tangetns to what?

http://gpwiki.org/index.php/OpenGL:Tutorials:GLSL_Bump_Mapping
The tangent application is also covered there.

Good luck with GLSL,
Dmitry