Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 10

Thread: uniform float doesn't work

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2009
    Location
    France, Bordeaux.
    Posts
    24

    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 :
    Code :
    /*******************/
    /* 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 :
    Code :
    /*******************/
    /* FRAGMENT SHADER */
    /*******************/
     
    uniform sampler2D fishTex;
    uniform float aspectRatio;
    uniform float fishRadius;
     
    ...
    And send the three values like this :
    Code :
      glUseProgram(texProgram);
     
      GLint texVar = glGetUniformLocation(texProgram, "fishTex");
      printf("fishTex : %d\n", texVar);
      glUniform1i(texVar, 0);
     
     
      texVar = glGetUniformLocation(texProgram, "aspectRatio");
      printf("aspectRatio : %d\n", texVar);
      glUniform1f(texVar, 1.5);
     
     
      texVar = glGetUniformLocation(texProgram, "fishRadius");
      printf("fishRadius : %d\n", 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 ...

  2. #2
    Junior Member Regular Contributor
    Join Date
    Aug 2001
    Location
    Norrkoping, Sweden
    Posts
    112
    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.
    Last edited by StefanG; 10-16-2012 at 08:33 AM. Reason: Didn't read the question carefully enough...

  3. #3
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    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.

    Quote Originally Posted by The API Reference
    glUniform operates on the program object that was made part of current state by calling glUseProgram.
    Although I doubt that the thread starter still hasn't solved the problem I'd like to ask, are there any GL errors generated?

  4. #4
    Intern Contributor
    Join Date
    Jul 2006
    Posts
    72
    Quote Originally Posted by thokra View Post
    That's not a mistake - having a current (or active) program object is mandatory to be able to use glUniform*() functions.
    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.

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    Yes, you're right. I got confused by StefanG's formulation.

  6. #6
    Junior Member Newbie
    Join Date
    May 2012
    Posts
    10
    test,code:glUseProgram(texProgram); GLint texVar = glGetUniformLocation(texProgram, "fishTex"); printf("fishTex : %d\n", texVar); glUniform1i(texVar, 0); texVar = glGetUniformLocation(texProgram, "aspectRatio"); printf("aspectRatio : %d\n", texVar); glUniform1f(texVar, 1.5); texVar = glGetUniformLocation(texProgram, "fishRadius"); printf("fishRadius : %d\n", texVar); glUniform1f(texVar, 0.5204475309f);

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2009
    Location
    France, Bordeaux.
    Posts
    24
    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 !

  8. #8
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,099
    Quote Originally Posted by prunkdump
    my graphic card is an old geforce 6600gt .... may my drivers never have been fixed ?
    Which OS do you use?

  9. #9
    Junior Member Newbie
    Join Date
    Feb 2009
    Location
    France, Bordeaux.
    Posts
    24
    Quote Originally Posted by thokra View Post
    Which OS do you use?
    Gentoo linux with nvidia-drivers-295.71

  10. #10
    Junior Member Newbie
    Join Date
    Feb 2009
    Location
    France, Bordeaux.
    Posts
    24

    Post

    I finally found the error !

    When I compile my program I get the following warnings :
    Code :
    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 !

    Code :
      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\n", texVar);
      glUniform1fv(texVar, 1, &aspectRatio);
     
      texVar = glGetUniformLocation(texProgram, "fishRadius");
      printf("tex2 : %d\n", texVar);
      glUniform1fv(texVar, 1, &fishRadius);

    I think this is because "gcc" haven't the right fonction prototype at compile time, so
    Code :
    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 !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •