我在通过着色器(尤其是几何着色器)传递数据时遇到了一些麻烦。我以前从未使用过Geometry Shader,因此在理解数据传递方式时遇到了一些麻烦。
基本上,在“几何”着色器之前,我只需使用输入/输出限定符将数据从“顶点着色器”传递到“片段”。就像我有:
顶点着色器!
out vec3 worldNormal;
out vec3 worldView;
片段着色器:
in vec3 ex_worldNorm;
in vec3 ex_worldView;
因此,对于“几何着色器”,我是否必须执行类似的操作才能传递数据?
in vec3 ex_worldNorm[];
in vec3 ex_worldView[];
out vec3 ex_worldNorm[];
out vec3 ex_worldNorm[];
我的问题是,如何在每个着色器之间正确传递数据?就像,这是这样做的方式吗? (因为它对我不起作用!)
最佳答案
输入的无边界数组对于Geomtery着色器是正确的(大小将由布局限定符中的输入类型确定),但是输出不能是数组。
通过将值写入所有输出变量,然后调用EmitVertex (...)
,可以一次输出一个顶点。在两次调用EmitVertex (...)
之间未写入的任何输出变量都将是未定义的。
这是一个非常简单的示例几何着色器,可以满足您的需求:
#version 330
layout (triangles) in; // This will automatically size your two arrays to 3, and also
// defines the value of `gl_in.length ()`
in vec3 worldNormal [];
in vec3 worldView [];
// ^^^ Names match up with Vertex Shader output
layout (triangle_strip, max_vertices = 3) out;
out vec3 ex_worldNorm;
out vec3 ex_worldView;
// ^^^ Names match up with Fragment Shader input
void main (void)
{
// For each input vertex, spit one out
for (int i = 0; i < gl_in.length (); i++) {
ex_worldNorm = worldNormal [i];
ex_worldView = worldView [i];
EmitVertex ();
}
}
关于c++ - 通过GLSL着色器传递数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26790478/