# Simple pass-through geometry shader with normal and color

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:

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

• 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. :) – manabreak Feb 16 '13 at 14:05
• 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! – Partha Bera Feb 18 '13 at 4:45
• @manabreak: could please tell me how did you indent and tokenize these codes so nicely. They looked weird when I posted. – Partha Bera Feb 18 '13 at 4:48
• 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. – manabreak Feb 19 '13 at 16:19

My refined and working code goes as follows:

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

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


Happy coding!

• in fData { vec3 normal;vec4 color}frag; – Radu Toader May 18 '15 at 9:14