顶点着色器与数组输入

5

给定一个类似下面的顶点着色器:

#version 400 compatibility

const int max_valence = 6;

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

...

如何将数据正确地映射到相应的顶点属性?我正在尝试使用VBO,但是我无法弄清楚如何传递那么大的值。glVertexAttribPointer最多只能处理大小为4的向量。那么,将顶点属性传递到着色器的正确方法是什么?

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.
  );
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接