Search code examples
c#openglopentk

Boolean uniform in OpenTK


How can I pass bool value to shader via OpenTK? I have not seen such overload for Uniform1. How my purposes I use float variable and than check something like

if (floatValue > 0.5) {}

But it doesn't looks like nice way to do things. Any ideas with bool uniform?


Solution

  • You can use either glUniform1i() or glUniform1f() to set a bool uniform. For both of these, the uniform value will be set to FALSE if you pass zero, and TRUE otherwise. So to set the uniform to FALSE, use either one of these two:

    glUniform1i(0);
    gUniform1f(0.0f);
    

    To set it to TRUE, use either one of these:

    glUniform1i(1);
    gUniform1f(1.0f);
    

    Any other non-zero value instead of 1 or 1.0f would be legal as well. I would personally prefer the integer version with values 0 and 1, since the int type is more closely related to bool.

    The spec language defining this behavior is (copied from section 2.11.4 of OpenGL 3.3 spec):

    When loading values for a uniform declared as a boolean, a boolean vector, an array of booleans, or an array of boolean vectors, the Uniform*i{v}, Uniform*ui{v}, and Uniform*f{v} set of commands can be used to load boolean values. Type conversion is done by the GL. The uniform is set to FALSE if the input value is 0 or 0.0f, and set to TRUE otherwise.