gpt4 book ai didi

opengl - GLSL 是否支持 C 风格的初始化程序?

转载 作者:行者123 更新时间:2023-12-04 13:21:30 30 4
gpt4 key购买 nike

从“Data Type (GLSL)”上的OpenGL Wiki 页面,我发现了关于初始化列表的相当广泛的声明。

...Therefore, GLSL takes a feature from C++11 and allows the initialization of any type by using an initializer list. An initializer list is a list of initializers (which themselves could be initializer lists) that are used to initialize a variable. Initializer lists are bounded by curly braces ("{" and "}").

Initializer lists do not require specifying the type name; the compiler will deduce it correctly. And this deduction extends down the hierarchy of objects in aggregates (structs/arrays). For example, the above code could be initialized with the following initializer list:

Data dataArray[3] = { {...}, ... }



我在初始化列表上尝试了许多变体,但我不相信。
# version 330
vec4 by_return(void) { return {1.0}; } // unexpected '{', expecting "::"
vec4 by_decl(void) { vec4 x = {1}; return x; } // too little data in initialization

好的,初始化列表不等同于推导类型的构造函数,例如 vec4(1.0) ...
vec4 by_decl(void) { vec4 x = {1,0,0,0}; return x; }
// OpenGL does not allow C style initializers

......但它没有实际意义。以防万一“任何类型”比预期的更广泛,我确保尝试一个变体,其中一个结构应该在外部初始化列表中推导,另一个具有显式构造函数但参数作为初始化列表给出:
struct Dual { vec4 u, v; }
Dual struct_outer(void) { return {vec4(1,0,0,0), vec4(0,0,0,0)}; } // unexpected '{', expecting "::"
Dual struct_inner(void) { return Dual({1,0,0,0}, {0,0,0,0}); } // unexpected '{', expecting "::"

将该结果与 Wiki 的接下来几行进行对比:

The compiler automatically deduces that the second element of each Data member is an vec2. This is based on the definition of Data.



最后,数组应该与结构没有区别,所以这应该不足为奇:
vec4[2] as_array(void) { return {{1,0,0,0},{0,0,0,0}}; } // unexpected '{', expecting "::"
vec4[2] as_array2(void) { vec4 x[2] = {{1,0,0,0},{0,0,0,0}}; return x; }
// OpenGL does not allow C style initializers
vec4[2] as_array3(void) { return {vec4(1), vec4(1)}; } // unexpected '{', expecting "::"
vec4[2] as_array4(void) { return vec4[2]{vec4(1), vec4(1)}; } // unexpected '{', expecting "::"
vec4[2] as_array5(void) { vec4[2] x; x[0] = {1,0,0,0}; x[1] = {0,0,0,0}; return x; } // unexpected '{', expecting "::"

从目前我所看到的一切来看,编译器似乎能够解析“C 风格的初始化程序”,但仅限于赋值,并且其唯一目的是更明确地拒绝它。

最佳答案

初始化列表是后来添加到 OpenGL 中的。 Wiki 在旁边指出此功能是“自 4.2 版以来的核心”。我确保包括我的 #version 330声明,因为我非常确定初始值设定项列表是比 GL 3.3 更早的添加项,并且直到我要提交问题时我什至没有质疑这一点。吸取了教训,我想。

关于opengl - GLSL 是否支持 C 风格的初始化程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52070904/

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