我尝试在结构内初始化数组,如下所示:
struct myStruct {
vec3 data[20] = vec3[20] (vec3(1, 1, 1), vec3( 1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1),
vec3(1, 1, -1), vec3( 1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1),
vec3(1, 1, 0), vec3( 1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0),
vec3(1, 0, 1), vec3(-1, 0, 1), vec3( 1, 0, -1), vec3(-1, 0, -1),
vec3(0, 1, 1), vec3( 0, -1, 1), vec3( 0, -1, -1), vec3( 0, 1, -1));
};
但我收到此错误:
ERROR: 0:84: '=' : syntax error: syntax error
可以这样做吗?
最佳答案
struct
启动类型规范,而不是变量声明。您必须声明一个变量并使用结构构造函数(请参阅 Data Type (GLSL) - Struct constructors ):
struct myStruct {
vec3 data[20];
};
myStruct myVar = myStruct( vec3[20]( vec3(1, 1, 1), ..... ) );
请参阅GLSL Specification - 4.1.8 Structures
User-defined types can be created by aggregating other already defined types into a structure using the struct keyword. For example,
struct keyword. For example, struct light { float intensity; vec3 position; } lightVar;
Structures can be initialized at declaration time using constructors, as discussed in section 5.4.3 “Structure Constructors”
请参阅GLSL Specification - 5.4.3 Structure Constructors
Once a structure is defined, and its type is given a name, a constructor is available with the same name to construct instances of that structure. For example:
struct light { float intensity; vec3 position; }; light lightVar = light(3.0, vec3(1.0, 2.0, 3.0));
关于glsl - 如何在 GLSL 结构体中初始化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47159301/