I'm trying to send a boolean to an OpenGL glsl shader.
Currently I have this in the shader:
uniform bool foo;
And I use this to set it:
glUniform1i(glGetUniformLocation(shader, "foo"), true);
There doesn't seem to be a glUniform1b
, so I'm setting it as an integer. This seems to work fine.
Is there any problem with this approach? Is it portable, or could it break on other graphics cards / drivers? I'm using OpenGL 4.3 at the moment.
Uniforms are intended to be set by the user from OpenGL, rather than within the shader. However, you can initialize them to a default value using standard GLSL initalizer syntax: uniform vec3 initialUniform = vec3(1.0, 0.0, 0.0); This will cause the uniform to have this vector as its value, until the user changes it.
vec4 is a floating point vector with four components. It can be initialized by: Providing a scalar value for each component. Providing one scalar value. This value is used for all components.
§ 4.1 Basic Types The OpenGL Shading Language supports the following basic data types, grouped as follows:
bool
a conditional type, taking on values of true or falsebvec2
a two-component Boolean vectorbvec3
a three-component Boolean vectorbvec4
a four-component Boolean vector
...
§ 4.1.2 Booleans To make conditional execution of code easier to express, the type bool is supported. There is no expectation that hardware directly supports variables of this type. (...)
As for setting:
§ 2.2.1 (...) When state values are specified using a different parameter type than the actual type of that state, data conversions are performed as follows:
- When the type of internal state is boolean, zero integer or floating-point values are converted to
FALSE
and non-zero values are converted toTRUE
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With