Search code examples
openglglslgeometry-shader

Pass-through geometry shader for points


I'm having some problems writing a simple pass-through geometry shader for points. I figured it should be something like this:

#version 330
precision highp float;

layout (points) in;
layout (points) out;

void main(void)
{
    gl_Position = gl_in[0].gl_Position;
    EmitVertex();
    EndPrimitive();
}

I have a bunch of points displayed on screen when I don't specify a geometry shader, but when I try to link this shader to my shader program, no points show up and no error is reported.

I'm using C# and OpenTK, but I don't think that is the problem.

Edit: People requested the other shaders, though I did test these shaders without using the geometry shader and they worked fine without the geometry shader.

Vertex shader:

void main()
{
    gl_FrontColor = gl_Color;
    gl_Position = ftransform();
}

Fragment shader:

void main()
{
    gl_FragColor = gl_Color;
}

Solution

  • I'm not that sure sure (have no real experience with geometry shaders), but don't you have to specify the maximum number of output vertices. In your case it's just one, so try

    layout (points, max_vertices=1) out;
    

    Perhaps the shader compiles succesfully because you could still specify the number of vertices by the API (at least in compatibility, I think).

    EDIT: You use the builtin varying gl_FrontColor (and read gl_Color in the fragment shader), but then in the geometry shader you don't propagate it to the fragment shader (it doesn't get propagated automatically).

    This brings us to another problem. You mix new syntax (like gl_in) with old deprecated syntax (like ftransform and the builtin color varyings). Perhaps that's not a good idea and in this case you got a problem, as gl_in has no gl_Color or gl_FrontColor member if I remember correctly. So the best thing would be to use your own color variable as out variable of the vertex and geometry shaders and as in variable of the geometry and fragment shaders (but remember that the in has to be an array in the geometry shader).