具有数组输入的顶点着色器 [英] Vertex shader with array inputs
问题描述
给出一个看起来像这样的顶点着色器
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];
...
将数据映射到正确的顶点属性的正确方法是什么?我正在尝试使用VBO,但是我不知道如何传递这么大的值.glVertexAttribPointer
最多采用大小为4的向量.将顶点属性放入着色器的正确方法是什么? ?
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?
推荐答案
顶点属性数组在OpenGL中是合法的.但是,数组的每个成员都占用一个 separate 属性索引.它们从您赋予patch_data
的任何属性索引开始连续分配.由于您使用的是GLSL 4.00,因此还应该使用layout(location = #)
明确指定属性位置.
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 = #)
.
因此,如果您这样做:
layout(location = 1) in vec3 patch_data[1 + 2 * max_valence];
然后patch_data
将覆盖从1到1 + (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)
.
从OpenGL来看,每个数组条目都是一个单独的属性.因此,您需要为每个单独的数组索引单独调用 glVertexAttribPointer
.
Each array entry is, from the OpenGL side, a separate attribute. Therefore, you need a separate glVertexAttribPointer
call for each separate array index.
因此,如果您的内存中的数组数据看起来像是紧密封装的13个vec3的数组,那么您需要执行以下操作:
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.
);
}
这篇关于具有数组输入的顶点着色器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!