Simple pass-through geometry shader with normal and color

I wrote a very simple geometry pass-through shader. My input primitive is points, and the primitive output is also points. I also want to pass the color and normal from the vertex shader to the fragment shader through the geometry shader. The shaders are compiled and linked flawlessly, but the final color is very strange. I think there is something wrong with this expedition. Can anyone point out the problem? Here are my shaders:

Vertex shader:

#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();
}

      

Geometric Shader:

#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();
}

      

Fragment shader:

#version 330

struct vData
{
    vec3 normal;
    vec4 color;
};

in vData frag;

void main()
{
    gl_FragColor = frag.color;
}

      

+3


source to share


1 answer


I understood that! in / out variables must have the same name, i.e. vertices[]

in geometry the shader should be vertex[]

. What is it!

My refined and working code looks like this:

Vertex shader:

#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();
}

      

Geometric Shader:



#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();
}

      

Fragment shader:

#version 330 compatibility

in fData
{
    vec3 normal;
    vec4 color;
};

void main()
{
    gl_FragColor = frag.color;
}

      

Happy coding!

+14


source







All Articles