这是代码:
顶点着色器:
#version 330
layout(std140) uniform;
layout(location = 6) in vec4 worldPosition;
layout(location = 7) in int FIndex;
flat out int[] passFIndex;
uniform Projection
{
mat4 view;
mat4 projection;
};
uniform mat3[6] FMatrix;
void main()
{
gl_Position = worldPosition;
passFIndex = int[](FIndex);
}
几何着色器:
#version 330
layout(std140) uniform;
layout(points) in;
layout(triangle_strip, max_vertices = 136) out;
flat in int[] passFIndex;
uniform Projection
{
mat4 view;
mat4 projection;
};
uniform mat3[6] FMatrix;
void main()
{
vec3 newPos = FMatrix[passFIndex[0]] * vec3(-0.5f, -0.5f, 0.5f);
...
所以问题是,当我将 passFIndex 设置为仅包含位置 7 处的顶点属性的数组时,passFIndex 未正确设置。问题是,它始终设置为 0。但是,我知道底层顶点FIndex 的数据不为 0,因为我可以使用不同的着色器程序(没有 GS 的着色器程序)并且 FIndex 会相应变化。
我可以将“passFIndex = int [ ] (FIndex)”替换为“passFIndex = int [ ] (2)”或其中的其他一些常量,这可以创建一个非零数组,并且它索引正如几何着色器中所预期的那样。这似乎告诉我顶点着色器出了问题,但我无法想象它是什么。
最佳答案
您在顶点着色器中输出数组的尝试令人困惑。这应该会在您的 GLSL 程序中生成链接错误,因为顶点着色器和几何着色器之间的接口(interface)不匹配。
几何着色器输入始终采用数组的形式,因为它们采用多个变换的顶点来构造图元。然而,顶点着色器中的输出通常不是数组。如果您想在顶点着色器中输出数组,则会出现几何着色器需要 2D 数组来匹配接口(interface)的情况 - 这是不支持的。
GLSL 3.30 Specification - 4 个变量和类型 - 第 30 页
For the interface between a vertex shader and a geometry shader, vertex shader output variables and geometry shader input variables of the same name must match in type and qualification, except that the vertex shader name cannot be declared as an array while the geometry shader name must be declared as an array. Otherwise, a link error will occur.
If the output of a vertex shader is itself an array to be consumed by a geometry shader, then it must appear in an output block (see interface blocks below) in the vertex shader and in an input block in the geometry shader with a block instance name declared as an array. This is required for arrays output from a vertex shader because two-dimensional arrays are not supported.
我会考虑重写你的着色器:
顶点着色器:
#version 330
layout(std140) uniform;
layout(location = 6) in vec4 worldPosition;
layout(location = 7) in int FIndex;
flat out int passFIndex;
uniform Projection
{
mat4 view;
mat4 projection;
};
uniform mat3 FMatrix [6];
void main()
{
gl_Position = worldPosition;
passFIndex = FIndex;
}
几何着色器:
#version 330
layout(std140) uniform;
layout(points) in;
layout(triangle_strip, max_vertices = 136) out;
flat in int passFIndex [];
uniform Projection
{
mat4 view;
mat4 projection;
};
uniform mat3 FMatrix [6];
void main()
{
vec3 newPos = FMatrix[passFIndex[0]] * vec3(-0.5f, -0.5f, 0.5f);
...
关于opengl - 传递给几何着色器的顶点属性数据未正确设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21521060/