5

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

#version 400 compatibility

const int max_valence = 6;

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

...

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

4

1 回答 1

9

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

所以如果你这样做:

layout(location = 1) in vec3 patch_data[1 + 2 * max_valence];

然后patch_data将覆盖从 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.
  );
}
于 2012-11-24T04:44:53.783 回答