1
votes

I am just messing around with some geometry shaders taking a list of GL_POINTS and outputting a box with triangle strips. i have it basically working but when i zoom in/out or pan around the triangle strips go all over the place and do not maintain their posistion in the world but are still correctly drawing the box.

for example if i give the input (5,5,0) it will draw a triangle strip with these points to make a box:

(5 , 5 , 0)
(5.5, 5 , 0)
(5 , 5.5, 0)
(5.5, 5.5, 0)

Vertex Shader:


// Vertex Shader
#version 130

in vec4 vVertex;

void main(void)
{ 
    gl_Position = gl_ModelViewProjectionMatrix * vVertex;
}

Geometry Shader:


version 130 
#extension GL_EXT_geometry_shader4 : enable

void main(void)
{
    vec4 a;
    vec4 b;
    vec4 c;
    vec4 d;

    int i = 0;
    for(i = 0; i  gl_VerticesIn; i++)
    {
        a = gl_PositionIn[i];
        //a.x -= 0.5;
        //a.y -= 0.5;
        //a.z = 0.0;
        gl_Position = a;
        EmitVertex();

        b = gl_PositionIn[i];
        b.x += 0.5;
        //b.y -= 0.5;
        //b.z = 0.0;
        gl_Position = b;
        EmitVertex();

        d = gl_PositionIn[i];
        //d.x -= 0.5;
        d.y += 0.5;
        //d.z = 0.0;
        gl_Position = d;        
        EmitVertex();

        c = gl_PositionIn[i];
        c.x += 0.5;
        c.y += 0.5;
        //c.z = 0.0;
        gl_Position = c;
        EmitVertex();

    }
    EndPrimitive();

}

im probably missing something dumb.

2

2 Answers

4
votes

Multiply each vertices by gl_ModelViewMatrix in your vertex shader instead. It's far more easy to reason in world space.

After that you can do what you do in the geometry shader, but don't forget to multiply vertices by your projection matrix, before emiting them. This should fix your issue.

Edit: I forget about ModelViewMatrix which transforms to view space, sorry. Just pass the vertex in the VS without doing nothing on it. That means you still will be in model space in the GS. Do your offset work in GS, then before emiting, transform with gl_ModelViewProjectionMatrix.

1
votes

The geometry shader runs after the vertex shader. So those changes you're making to the vertices are being made in screen coordinates, not world.