gpt4 book ai didi

opengl-es - OpenGL ES (2.0) 着色语言 : How to input boolean into vertex shader and pass to fragment shader?

转载 作者:行者123 更新时间:2023-12-02 19:50:11 39 4
gpt4 key购买 nike

我第一次尝试将 bool 值传递到我的顶点着色器中;到目前为止我只使用过 float 。

所讨论的 bool 值是特定于原语的,因此不能作为统一传递。然而,对于任何给定图元的所有顶点,它具有相同的值。

从 Khronos 规范看来,“variing”是将数据传递到片段着色器的唯一方法,但毫不奇怪地声明“variing bool my_bool;”在我的顶点着色器中定义时会导致解析器错误。

我将 bool 值传递到我的顶点着色器中:

attribute bool a_my_bool;

我定义了一个变量来尝试传递给片段着色器:

varying bool v_my_bool;
void main() {
// ...
v_my_bool = a_my_bool;
}

有人可以告诉我如何实现我的目的吗?

最佳答案

来自The OpenGL® ES Shading Language version 1.0.17 (PDF)的§4.3.5 :

The varying qualifier can be used only with the data types float, vec2, vec3, vec4, mat2, mat3, and mat4, or arrays of these.

从§4.3.3开始:

The attribute qualifier can be used only with the data types float, vec2, vec3, vec4, mat2, mat3, and mat4. Attribute variables cannot be declared as arrays or structures.

因此,根据规范,您不能拥有attribute bool,更不用说variing bool了。

如果您确实需要每个顶点一个 bool 值,您可以使用 0.0 表示 false,使用 1.0 表示 true。测试时,检查 x > 0.5。例如:

// vertex shader
attribute float a_my_bool;
varying float v_my_bool;
void main() {
// ...
v_my_bool = a_my_bool;
}

// fragment shader
varying float v_my_bool;
void main() {
if (v_my_bool > 0.5) {
// my_bool is true
// ...
} else {
// my_bool is false
// ...
}
}

只要每个三角形中的所有顶点都具有相同的值,就应该可以工作。如果它们不一致,那么同一个三角形的不同片段最终会表现不同。 (如果坚持使用 0.0 和 1.0,最接近“奇数”角的三角形的四分之一的行为将与其余部分不同。)

关于opengl-es - OpenGL ES (2.0) 着色语言 : How to input boolean into vertex shader and pass to fragment shader?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8863196/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com