Vertex shader with array inputs

倾然丶 夕夏残阳落幕 提交于 2019-12-05 13:40:06

Arrays of vertex attributes are legal in OpenGL. However, each member of the array takes up a separate attribute index. They are allocated contiguously, starting from whatever attribute index you give patch_data. Since you're using GLSL 4.00, you should also be specifying attribute locations explicitly with layout(location = #).

So if you do this:

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

Then patch_data will cover all attribute indices on the half-open range from 1 to 1 + (1 + 2 * max_valence).

Each array entry is, from the OpenGL side, a separate attribute. Therefore, you need a separate glVertexAttribPointer call for each separate array index.

So if your array data in memory looks like an array of 13 vec3's, tightly packed, then you need to do this:

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.
  );
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!