gpt4 book ai didi

glsl - 如何在 GLSL 结构体中初始化数组

转载 作者:行者123 更新时间:2023-12-02 03:44:44 33 4
gpt4 key购买 nike

我尝试在结构内初始化数组,如下所示:

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/

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