opengl - 传递给几何着色器的顶点属性数据未正确设置

标签 opengl graphics glsl vertex-shader geometry-shader

这是代码:

顶点着色器:

#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/

相关文章:

c++ - 当我在 glTexImage2D 中选择与在着色器中采样不同的内部格式时,情况会怎样?

c++ - 对纹理进行增量更新的最佳方法? (渲染到纹理)

python - 使用pyinstaller时如何从pyqt5库中排除opengl32sw.dll?

java - 将 OpenGL 与 LWJGL 结合使用的方法

r - 我在 R 中的想象图 - 条形间距和条形高度之间的 map

opengl - 在 GLSL 着色器中进行翻译

ios - 前两个片段着色器输出不同

opengl - 如何在GLSL程序中识别intel显卡?

algorithm - 对于矩阵中的所有标记区域,找到它们的顶点进行绘制

r - 在 R 中根据值绘制不同指标的点图