Vertex shader with array inputs

不问归期 提交于 2019-12-07 11:33:44

问题


Given a vertex shader that looks something like

#version 400 compatibility

const int max_valence = 6;

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

...

What is the the proper way to map the data to the correct vertex attribs? I'm trying to use a VBO, but I can't figure out how to pass that large of a value in. glVertexAttribPointer takes at most a vector of size 4. What's the correct way to get the vertex attributes into the shader?


回答1:


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


来源:https://stackoverflow.com/questions/13538091/vertex-shader-with-array-inputs

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