Problem with glGetUniformLocation ?

when I try to assign values to a uniform variable
glGetUniformLocation doesnt work. It always returns
the -1 that mean the variable doesnt exist. But it does !
Thats driving me crazy

shader file

void main()
	// clip space position
	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
  // assign texture coordinates from texunit0
  gl_TexCoord[0] = gl_MultiTexCoord0;		
uniform sampler3D volumeTexture;
uniform sampler1D transferTexture;
uniform sampler2D randomTexture;
uniform vec2 tileSize;
uniform vec3 viewVector;

void main()
  vec4 color = vec4(0.0,0.0,0.0,0.0);  
  color = texture3D(volumeTexture,gl_TexCoord[0].str);
  //color = texture1D(transferTexture,color.r);

  // result
	gl_FragColor = color;  

and here the method for setting up the uniform variable

void CGLSLShaderObject::SetUniform1i(const char* pchParam, int i0)
  GLint iParam = glGetUniformLocation(m_ShaderProgramID, pchParam);
	glUniform1i(iParam, i0);

in my case, iParam (the location) is always -1. I dont know why.
OpenGL context exists.
I tried to give the variable name normal and with \0 terminition
i.e. “volumeTexture” , “volumeTexture\0”

Any ideas ?

Um, is that shader file all one file, or is that two files that you put into one code-block? If it’s all one file, GLSL doesn’t work that way.

The -1 signifies that the variable under concern is not getting used in shader program.

The shader compiler has this habit of scanning code at linking stage where it checks variable declared is being used or is just there as a wastage. The action is taken with a view to optimize code for less memory requirement.

This is required since , a shader module is exclusively loaded by the non-shader program.

Example: //Shader code:

int i;
int j; 
i = i + 1;

When you compile and try to get location of variables i and j
you will get 0 as location for i and -1 for j becuase ‘j’ has no role to play in adding a meaning to your shader code

In short do not worry the compiler and you are both right.There is no glitch in your code.

Other reason ‘-1’ can come up is when a variable with the name <name> is not there in your code.

Example: out side shader code if you try
GLint loc = glGetUniformLocation(p , “k”);

loc = -1 in this case.

I guess the first main() is meant for Vertex shade and the second one for fragment shader.

what awhig said about optimizing away thats your <transferTexture> f.e. make sure you bind the program before glGet anything.

However the volumeTexture sampler is used actually so it can’t be thrown away by a compiler optimization at link time.
lobbel, are you sure that the program is successfully linked before getting the uniform location? Check Opengl and compiler errors.

It could also be a text encoding problem, the shader code parsing may not occur correctly and some code is commented whereas it should not be.

In 3.1 you can use a named uniform block/buffer with a shared/std140 layout to preserve your uniform blocks. I think that so long as you reference at least one uniform in the block you’ll get the whole enchilada. Pretty much the same boat I guess, but at a coarser granularity.