• Advertisement
Sign in to follow this  

odd behavior with glGetUniformLocation

This topic is 3628 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So my GLSL shader has 5 uniform variables in it and when I call glGetUniformLocation on one of the variables it returns -1 for the location. All the other variables return a valid address but this one. I read the docs and it states: "This function returns -1 if name does not correspond to an active uniform variable in program or if name starts with the reserved prefix "gl_". The variable name is "vsfog" and I have checked and rechecked and rechecked to make sure I am spelling it correctly...what gives? Keep in mind I am passing the correct constant type as well... here is the shader code:
uniform vec4 vsfog;
uniform mat4 wvp_matrix;
uniform mat4 w_matrix;
uniform mat4 texture_matrix;
uniform mat4 cullplane;

varying float fogFactor;   // used by FS

void main(void)
{
   vec4 cam2vert  = vec4(0.0, 0.0, 0.0, 0.0);
   
   vec4 r1        = w_matrix * gl_Vertex;
   gl_Position    = wvp_matrix * gl_Vertex;
   gl_TexCoord[2] = texture_matrix * gl_Vertex;

   gl_TexCoord[0] = cullplane * r1;

   cam2vert       = r1 + (-vsfog);

   r1    = cam2vert * cam2vert;
   r1.x  = dot(r1.xyz, vec3(1.0, 1.0, 1.0));

   float rsqrt = inversesqrt(r1.x);

   fogFactor = r1.x * rsqrt;
}

Share this post


Link to post
Share on other sites
Advertisement
Have you tried changing the variable to something like "uniform vec4 smileyface;"
just to make sure there isn't some strange variable naming overlap?

Also, you might to call glGetInfoLog() on your shader, just to make sure it's compiled correctly or if it produced some errors.

Share this post


Link to post
Share on other sites
As always make sure that no unexpected gl errors are generated. A tool like glIntercept can help with this. Also check the shader compile status and maybe also the info log for possible problems.

It can also happen that even though you declare uniforms or attributes in your vertex shader that retrieving their location yields -1. This situation occurs when the compiler determined that the variables are not actually needed and thus not active (since glGetUniformLocation only returns the location of active uniforms)

Share this post


Link to post
Share on other sites
I tried changing the name and also making 4 floats and modifying them instead of a vec4. And I am also checking the glGetInfoLog...no messages back compiles fine =/

If the compiler did in fact optimize them out then that makes no sense since clearly that uniform IS being using on every vertex. Ugh this is frustrating =(

Share this post


Link to post
Share on other sites
I can see that you are using vsfog. Have you tested your shader in Rendermonkey?
What GPU/driver/OS do you use?

Share this post


Link to post
Share on other sites
GeForce 9600 GT, latest nvidia drivers, have not yet tested in RenderMonkey...

small update:

calling glGetObjectParameterivARB(program, GL_ACTIVE_UNIFORMS, &active); returns 6 active uniform variables...however there should be 7 total since the fragment shader has 2 uniforms as well. I just don't understand why it is being flagged as inactive >.<

final update:

Issue resolved...I finally realized that the fogFactor variable was not being used on the final fragment output color therefore the compiler optimized it out. Ugh pilot error as usual...thanks for all the input.



[Edited by - thedigga007 on March 17, 2008 2:03:46 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement