具有数组输入的顶点着色器

cob*_*bal 4 opengl shader glsl vbo vertex-shader

给定一个看起来像的顶点着色器

#version 400 compatibility

const int max_valence = 6;

in int valence;
in vec3 patch_data[1 + 2 * max_valence];

...
Run Code Online (Sandbox Code Playgroud)

将数据映射到正确的顶点属性的正确方法是什么?我正在尝试使用VBO,但我无法弄清楚如何传递大量的值.glVertexAttribPointer最多需要一个大小为4的向量.将顶点属性放入着色器的正确方法是什么?

Nic*_*las 9

顶点属性数组在OpenGL中是合法的.但是,数组的每个成员都会占用一个单独的属性索引.它们是连续分配的,从您提供的任何属性索引开始patch_data.由于您使用的是GLSL 4.00,因此您还应该明确指定属性位置layout(location = #).

所以,如果你这样做:

layout(location = 1) in vec3 patch_data[1 + 2 * max_valence];
Run Code Online (Sandbox Code Playgroud)

然后patch_data将涵盖从1到1的半开范围的所有属性索引1 + (1 + 2 * max_valence).

从OpenGL端,每个数组条目都是一个单独的属性.因此,您需要glVertexAttribPointer为每个单独的数组索引单独调用.

因此,如果内存中的数组数据看起来像13个vec3的数组,紧密包装,那么你需要这样做:

for(int attrib = 0; attrib < 1 + (2 * max_valence); ++attrib)
{
  glVertexAttribPointer(attrib + 1, //Attribute index starts at 1.
    3, //Each attribute is a vec3.
    GL_FLOAT, //Change as appropriate to the data you're passing.
    GL_FALSE, //Change as appropriate to the data you're passing.
    sizeof(float) * 3 * (1 + (2 * max_valence)), //Assuming that these array attributes aren't interleaved with anything else.
    reinterpret_cast<void*>(baseOffset + (attrib * 3 * sizeof(float))) //Where baseOffset is the start of the array data in the buffer object.
  );
}
Run Code Online (Sandbox Code Playgroud)