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

标签 glsl shader

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

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/

相关文章:

opengl - 为什么这些不匹配的着色器变量不会产生链接器错误?

opengl - OpenGL GLSL 中从 RGB 到 HSV

c# - glGetUniformLocation 对于结构中的 2/4 字段失败?

opengl - 如何使用 OpenGL 将 RGBA 转换为 NV12?

opengl - 在 GLSL 中使用 glBindAttributeLocation 的目的是什么?

opengl - 计算着色器中的图像原子加法

opengl - GPU 手动 Mipmap 生成 [OpenGL 2.x]

opengl - 使用 imageAtomicCompSwap 的 GLSL 每像素自旋锁

android - 黑白模式负片滤镜

glsl - WebGL/GLSL 中间变量是否可以提高性能且没有任何缺点?