0
\$\begingroup\$

I've written a very simple pass-through geometry shader. My input and output primitives are points. I also want to forward the color and normal from vertex shader to fragment shader through geometry shader. The shaders are compiled and linked flawlessly but the final color is very weird. I think there is something wrong with this forwarding. 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();
}

Geometry 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;
}
\$\endgroup\$
4
  • \$\begingroup\$ I think this won't help the problem per se, but gl_FragColor is deprecated in 3.3. You should use "out vec3 out_color" or something similar and bind it in your code. :) \$\endgroup\$
    – manabreak
    Commented Feb 16, 2013 at 14:05
  • \$\begingroup\$ I tried changing the version 330 compatibility. But the version was not an issue here. I gave the in/our pair a different name. That's why it wasn't working. However I replaced this structure with interface block which is more sophisticated. And thank you manabreak for your response! \$\endgroup\$ Commented Feb 18, 2013 at 4:45
  • \$\begingroup\$ @manabreak: could please tell me how did you indent and tokenize these codes so nicely. They looked weird when I posted. \$\endgroup\$ Commented Feb 18, 2013 at 4:48
  • \$\begingroup\$ Umm, I didn't stylize the code, but you can achieve it by leaving an empty line before the code and then indenting all code with four spaces (or more if you want to actually indent it). The four spaces marks the line as source code. \$\endgroup\$
    – manabreak
    Commented Feb 19, 2013 at 16:19

1 Answer 1

0
\$\begingroup\$

My refined and working code goes as follows:

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

Geometry 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++)
    {
        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;
}frag_in;

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

Happy coding!

\$\endgroup\$
1
  • \$\begingroup\$ in fData { vec3 normal;vec4 color}frag; \$\endgroup\$ Commented May 18, 2015 at 9:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .