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?