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

Hybrid View

  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,128
    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,128
    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);

Posting Permissions

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