Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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;
}
share|improve this question
    
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

1 Answer 1

up vote 0 down vote accepted

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

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

Happy coding!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.