4

我编写了一个非常简单的直通几何着色器。我的输入原语是点,输出原语也是点。我还想通过几何着色器将颜色和法线从顶点着色器转发到片段着色器。着色器被完美地编译和链接,但最终的颜色非常奇怪。我认为这个转发有问题。谁能指出问题?这是我的着色器:

顶点着色器:

#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;
}
4

1 回答 1

17

我想通了!输入/输出变量必须具有相同的名称,即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;
}

快乐编码!

于 2013-02-18T04:28:24.880 回答