1
$\begingroup$

I know how to generate the walls of a cylinder with triangle strips and the caps with triangle fans, but since I am working inside a geometry shader, I can only output triangle strips, so I am kind of lost here.

This here is my current code, which only generates the hull of the cylinder based on a line segment:

#version 330 core

    layout(lines) in;
    layout(triangle_strip, max_vertices = 32) out;

    uniform mat4 projection;
    uniform mat4 view;
    uniform mat4 model;

    in Vertex 
    {
        vec4 color;
        float width;
    } vertex[];

    out vec4 vertex_color;
    out vec3 normal_vector;

    vec3 createPerp(vec3 p1, vec3 p2)
    {
        vec3 invec = normalize(p2 - p1);
        vec3 ret = cross( invec, vec3(0.0, 0.0, 1.0) );
        if ( length(ret) == 0.0 )
        {
            ret = cross(invec, vec3(0.0, 1.0, 0.0) );
        }
        return ret;
    }


    void main()
    {
        mat4 mvp = projection * view * model;

        vec3 axis = gl_in[1].gl_Position.xyz - gl_in[0].gl_Position.xyz;

        vec3 perpx = normalize(createPerp(gl_in[1].gl_Position.xyz, gl_in[0].gl_Position.xyz));
        vec3 perpy = cross(normalize(axis), perpx);

        float r1 = vertex[0].width / 1000.0;
        float r2 = vertex[0].width / 1000.0;

        int segs = 16;
        for(int i=0; i<segs; i++) {
            float a = i/float(segs-1) * 2.0 * 3.14159;
            float ca = cos(a); float sa = sin(a);
            vec3 normal = vec3( ca*perpx.x + sa*perpy.x,
                            ca*perpx.y + sa*perpy.y,
                            ca*perpx.z + sa*perpy.z );



            vec3 p1 = gl_in[0].gl_Position.xyz + r1*normal;
            vec3 p2 = gl_in[1].gl_Position.xyz + r2*normal;

            gl_Position = mvp * vec4(p1, 1.0);
            vertex_color = vertex[0].color;
            normal_vector = normal;
            EmitVertex();

            gl_Position = mvp * vec4(p2, 1.0);
            vertex_color = vertex[0].color;
            normal_vector = normal;
            EmitVertex();       
        }
        EndPrimitive();   
    }

Is there some simple way to integrate the generation of the endcaps here?

$\endgroup$
2
  • $\begingroup$ Make point in middle of cylinder cap $\endgroup$
    – joojaa
    Commented Dec 23, 2019 at 6:39
  • $\begingroup$ @joojaa That wouldn't really change much about the problem of emulating a triangle fan with a strip that the question is facing. In fact it would make answer by Simon inapplicable even. $\endgroup$ Commented Feb 27, 2020 at 19:09

1 Answer 1

1
$\begingroup$

If the end of your cylinder is convex, with vertices $V_0, V_1, V_2,...V_{N-1}$ then just replace your fan order (i.e. as above) with this strip $V_0, V_1, V_{N-1}, V_2, V_{N-2}, V_3...$, i.e. just zig-zag across the polygon.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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