链接错误在顶点和片段着色器之间添加几何着色器

时间:2014-04-07 16:37:49

标签: opengl glsl geometry-shader

当我尝试在工作顶点和片段着色器之间添加几何着色器时,我收到链接错误:

Fragment shader(s) failed to link,  vertex shader(s) failed to link. 
ERROR: error(#280) Not all shaders have valid object code
ERROR: error(#280) Not all shaders have valid object code

所有三个着色器编译都没有错误。我想进出不适合信息流管道。内置的细节使我感到困惑,所以我无法发现错误。

着色器的来源:

vertex_source =
    "#version 330\n"
    "in vec3 Position;\n"
    "in vec2 TexCoord;\n"
    "out vec3 oColor;\n"
    "out vec2 oTexcoord;\n"
    "void main() {\n"
    "    oTexcoord = TexCoord;\n"
    "    gl_Position = gl_ModelViewProjectionMatrix*vec4(Position, 1.0);\n"
    "}\n";

geometry_source =
    "#version 330\n";
    "layout (triangles) in;\n";
    "layout (triangle_strip,  max_vertices=3) out;\n";
    "in vec3 Color;\n";
    "in vec2 TexCoord;\n";
    "out vec3 oColor;\n";
    "out vec2 oTexCoord;\n";
    "void main() {\n";
    "   oColor = Color;\n";
    "   oTexCoord = TexCoord;\n";
    "   gl_Position = gl_in[0].gl_Position;\n";
    "   EmitVertex();\n";
    "   gl_Position = gl_in[1].gl_Position;\n";
    "   EmitVertex();\n";
    "   gl_Position = gl_in[2].gl_Position;\n";
    "   EmitVertex();\n";
    "   EndPrimitive();\n";
    "}\n";

fragment_source =
    "#version 330\n"
    "in vec2 oTexcoord;\n"
    "out vec4 oColor;\n"
    "uniform sampler2D tex;\n"
    "uniform sampler2D tex_norm;\n"
    "uniform sampler2D tex_spec;\n"
    "void main() {\n"
    "   vec4 lightpos = normalize(-gl_ModelViewProjectionMatrix*vec4(1.0, -1.0, -1.5, 1.0));\n"
    "   vec3 tmpNorm  = normalize(texture2D(tex_norm, oTexcoord).rgb * 2.0 - 1.0);\n"
    "   float a       = dot(tmpNorm, lightpos.xyz);\n"
    "   float difuse  = max(a, 0.1);\n"
    "   float spec    = texture2D(tex_spec, oTexcoord).r * pow(a, 2.0);\n"
    "   vec3 tmpcolor = difuse * texture2D(tex, oTexcoord).rgb;\n"
    "   oColor        = vec4(tmpcolor+tmpcolor*spec, 1.0);\n"
    "}\n";

我在几何着色器中做错了什么?

我尝试跳过未使用的oColor并更改为几何着色器中的数组,如下所示:

#define GLSL(src) "#version 330 core\n" #src

vertex_source = GLSL(
    in vec3 Position;
    in vec2 TexCoord;
    out vec2 oTexcoord;
    void main() {
        gl_Position = gl_ModelViewProjectionMatrix*vec4(Position, 1.0);
        oTexcoord = TexCoord;
    }
);

geometry_source = GLSL(
    layout (triangles) in;
    layout (triangle_strip,  max_vertices=3) out;
    in vec2 gsTexCoord[];
    out vec2 gsoTexCoord;
    void main() {
        gsoTexCoord = gsTexCoord[0];
        gl_Position = gl_in[0].gl_Position;
        EmitVertex();
        gsoTexCoord = gsTexCoord[1];
        gl_Position = gl_in[1].gl_Position;
        EmitVertex();
        gsoTexCoord = gsTexCoord[2];
        gl_Position = gl_in[2].gl_Position;
        EmitVertex();
        EndPrimitive();
    }
);

fragment_source = GLSL(
    in vec2 oTexcoord;
    out vec4 oColor;
    uniform sampler2D tex;
    uniform sampler2D tex_norm;
    uniform sampler2D tex_spec;
    void main() {
       vec4 lightpos = normalize(-gl_ModelViewProjectionMatrix*vec4(1.0, -1.0, -1.5, 1.0));
        vec3 tmpNorm  = normalize(texture2D(tex_norm, oTexcoord).rgb * 2.0 - 1.0);
       float a       = dot(tmpNorm, lightpos.xyz);
       float difuse  = max(a, 0.1);
       float spec    = texture2D(tex_spec, oTexcoord).r * pow(a, 2.0);
       vec3 tmpcolor = difuse * texture2D(tex, oTexcoord).rgb;
       oColor        = vec4(tmpcolor+tmpcolor*spec, 1.0);
    }
);

这给了我以下链接错误:

Fragment shader(s) failed to link,  vertex shader(s) failed to link. 
ERROR: error(#277) Symbol 'gsTexCoord[0]' usage doesn't match between two stages
ERROR: error(#277) Symbol 'oTexcoord' usage doesn't match between two stages
ERROR: error(#277) Symbol 'gsTexCoord[0]' usage doesn't match between two stages
ERROR: error(#277) Symbol 'oTexcoord' usage doesn't match between two stages

1 个答案:

答案 0 :(得分:3)

您的几何着色器输入需要是数组值并与顶点着色器中的名称匹配:

// existing vertex shader outputs:
out vec3 oColor;
out vec2 oTexcoord;

// wrong geometry shader inputs:
in vec3 Color;
in vec2 TexCoord;

// correct geometry shader inputs:
in vec3 oColor[];
in vec2 oTexCoord[];