我写了一个非常简单的传递几何着色器。我的输入原语是点,输出原语也是点。我还想通过几何着色器将颜色和法线从顶点着色器转发到片段着色器。着色器的编译和链接完美无瑕,但最终的颜色非常奇怪。我认为这种转发有问题。任何人都可以指出这个问题吗?这是我的着色器:
顶点着色器:
#version 330 compatibility
struct vData
{
vec3 normal;
vec4 color;
};
out vData vertex;
void main()
{
vertex.normal = gl_NormalMatrix * gl_Normal;
vertex.color = gl_Color;
gl_Position = ftransform();
}
几何着色器:
#version 330
layout (points) in;
layout (points) out;
layout (max_vertices = 1) out;
struct vData
{
vec3 normal;
vec4 color;
};
in vData vertices[];
out vData frag;
void main()
{
int i;
for(i = 0;i < gl_in.length();i++)
{
frag.normal = vertices[i].normal;
frag.color = vertices[i].color;
gl_Position = gl_in[i].gl_Position;
EmitVertex();
}
EndPrimitive();
}
片段着色器:
#version 330
struct vData
{
vec3 normal;
vec4 color;
};
in vData frag;
void main()
{
gl_FragColor = frag.color;
}
答案 0 :(得分:15)
我想出来了!输入/输出变量必须具有相同的名称,即几何着色器中的vertices[]
应为vertex[]
。就是这样!
我的精炼和工作代码如下:
顶点着色器:
#version 330 compatibility
out vData
{
vec3 normal;
vec4 color;
}vertex;
void main()
{
vertex.normal = normalize(gl_NormalMatrix * gl_Normal);
vertex.color = gl_Color;
gl_Position = ftransform();
}
几何着色器:
#version 330
layout (points) in;
layout (points) out;
layout (max_vertices = 1) out;
in vData
{
vec3 normal;
vec4 color;
}vertices[];
out fData
{
vec3 normal;
vec4 color;
}frag;
void main()
{
int i;
for(i = 0;i < gl_in.length();i++)// gl_in.length() = 1 though!
{
frag.normal = vertices[i].normal;
frag.color = vertices[i].color;
gl_Position = gl_in[i].gl_Position;
EmitVertex();
}
EndPrimitive();
}
片段着色器:
#version 330 compatibility
in fData
{
vec3 normal;
vec4 color;
};
void main()
{
gl_FragColor = frag.color;
}
快乐的编码!