C++ OpenGL布尔统一?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33690186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenGL bool uniform?
提问by Jan Rüegg
I'm trying to send a boolean to an OpenGL glsl shader.
我正在尝试向 OpenGL glsl 着色器发送一个布尔值。
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.
似乎没有 a glUniform1b,所以我将其设置为整数。这似乎工作正常。
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.
这种方法有什么问题吗?它是便携式的,还是会在其他显卡/驱动程序上损坏?我目前正在使用 OpenGL 4.3。
采纳答案by Bartek Banachewicz
§ 4.1 Basic TypesThe OpenGL Shading Language supports the following basic data types, grouped as follows:
boola conditional type, taking on values of true or falsebvec2a two-component Boolean vectorbvec3a three-component Boolean vectorbvec4a four-component Boolean vector
§ 4.1 基本类型OpenGL 着色语言支持以下基本数据类型,分组如下:
bool条件类型,取值为真或假bvec2二元布尔向量bvec3三分量布尔向量bvec4四分量布尔向量
...
...
§ 4.1.2 BooleansTo 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. (...)
§ 4.1.2 Booleans为了使代码的条件执行更容易表达,支持 bool 类型。不期望硬件直接支持这种类型的变量。(……)
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
FALSEand non-zero values are converted toTRUE.
§ 2.2.1(...) 当使用与该状态的实际类型不同的参数类型指定状态值时,数据转换执行如下:
- 当内部状态类型为布尔值时,零整数或浮点值转换为
FALSE,非零值转换为TRUE。

