Uniform float doesn't work

hi !

I need to pass two parameters to my fragment shader, but I never get the right variable’s values. So I tried to change the uniform to const variables like this :


/*******************/
/* FRAGMENT SHADER */
/*******************/

uniform sampler2D fishTex;
//uniform float aspectRatio;
//uniform float fishRadius;

const float aspectRatio = 1.5;
const float fishRadius = 0.5204475309;
...

and the shader works perfectly. Now if i try to do the same things with uniform :


/*******************/
/* FRAGMENT SHADER */
/*******************/

uniform sampler2D fishTex;
uniform float aspectRatio;
uniform float fishRadius;

...

And send the three values like this :


  glUseProgram(texProgram);

  GLint texVar = glGetUniformLocation(texProgram, "fishTex");
  printf("fishTex : %d
", texVar);
  glUniform1i(texVar, 0);

  
  texVar = glGetUniformLocation(texProgram, "aspectRatio");
  printf("aspectRatio : %d
", texVar);
  glUniform1f(texVar, 1.5);
  
  
  texVar = glGetUniformLocation(texProgram, "fishRadius");
  printf("fishRadius : %d
", texVar);
  glUniform1f(texVar, 0.5204475309f);

I get positive variable’s identifier (with the printf) but the values inside the shader doesn’t change. Maybe they stay to 0. I don’t understand where is the problem. I have wrote another shader with three uniform sampler2D and it works …

You seem to be activating your shader before you try to set a uniform value,
so that common mistake is not the cause of your problems.
In some Nvidia drivers, I have experienced that glUniform1f and
glUniform1i were broken, so I had to work around the problem by
using glUniform1fv and glUniform1iv instead, and pass the pointer to
a scalar value. That was quite some time ago, though, and it would
surprise me if then haven’t fixed it now. I did report it as a bug.

You seem to be activating your shader before you try to set a uniform value, so that common mistake is not the cause of your problems.

That’s not a mistake - having a current (or active) program object is mandatory to be able to use glUniform*() functions.

Although I doubt that the thread starter still hasn’t solved the problem I’d like to ask, are there any GL errors generated?

[QUOTE=thokra;1243478]That’s not a mistake - having a current (or active) program object is mandatory to be able to use glUniform*() functions.
[/QUOTE]
That was his point (although a fairly useless one, aka “the problem is NOT this”). The common mistake is doing it in the wrong order - which OP did not as StefanG noted.

Yes, you’re right. I got confused by StefanG’s formulation.

test,code:glUseProgram(texProgram); GLint texVar = glGetUniformLocation(texProgram, “fishTex”); printf("fishTex : %d
", texVar); glUniform1i(texVar, 0); texVar = glGetUniformLocation(texProgram, “aspectRatio”); printf("aspectRatio : %d
", texVar); glUniform1f(texVar, 1.5); texVar = glGetUniformLocation(texProgram, “fishRadius”); printf("fishRadius : %d
", texVar); glUniform1f(texVar, 0.5204475309f);

No, My problem is still not resolved…

StefanG, my graphic card is an old geforce 6600gt … may my drivers never have been fixed ? I will try to use glUniform1fv and return the result.

Thokra, I have not checked GL errors because glGetUniformLocation give me good values. I will try …

Thanks !

Which OS do you use?

Gentoo linux with nvidia-drivers-295.71

I finally found the error !

When I compile my program I get the following warnings :


fishdisplay.c:300:3: warning: implicit declaration of function ‘glUniform1i’
fishdisplay.c:306:3: warning: implicit declaration of function ‘glUniform1f’

I thought that was not a problem because the glUniform functions was found when linking.

Next I tried with glUniform1fv and it works !


  int fishTex = 0;
  GLfloat aspectRatio = 5184.0/3456.0;
  GLfloat fishRadius = 0.5204475309;

  GLint texVar = glGetUniformLocation(texProgram, "fishTex");
  glUniform1iv(texVar, 1, &fishTex);
  
  texVar = glGetUniformLocation(texProgram, "aspectRatio");
  printf("tex2 : %d
", texVar);
  glUniform1fv(texVar, 1, &aspectRatio);
  
  texVar = glGetUniformLocation(texProgram, "fishRadius");
  printf("tex2 : %d
", texVar);
  glUniform1fv(texVar, 1, &fishRadius);
  

I think this is because “gcc” haven’t the right fonction prototype at compile time, so

glUniform1f(texVar, 0.5204475309f);

doesn’t pass correctly the float as GLfloat. With glUniform1fv, fishRadius is explicitly declared as GLfloat so it work.

After using glew to get the right function prototype, everything works. So sorry for this stupid error !

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.