openglglslgeometry-shader

OpenGL - Geometry Shader - Lines to Triangle Strips


My goal is to render a sine wave in 3D space. The wave would be made up of small line segments. So I have a vertex shader that passes the points of the waves as lines to my geometry shader.

The geometry shader takes the two vertices in each line segment and uses them to draw two triangles. This allows me to adjust the thickness of the wave in the z direction.

My geometry shader is set up this way:

layout (lines, invocations = WAVES_PER_GROUP) in;
layout (triangle_strip, max_vertices = 4) out;

And this is the core code:

float th = 0.1;

vec4 quadCL = gl_in[0].gl_Position;
vec4 quadCR = gl_in[1].gl_Position;
    
vec4 quadTL = quadCL + vec4(0.0,0.0,-th,0.0);
vec4 quadTR = quadCR + vec4(0.0,0.0,-th,0.0);
vec4 quadBL = quadCL + vec4(0.0,0.0,th,0.0);    
vec4 quadBR = quadCR + vec4(0.0,0.0,th,0.0);

//Push the quad

//Quad
//0: TL
gl_Position = quadTL;
EmitVertex();
//1: BL
gl_Position = quadBL;
EmitVertex();
//2: TR
gl_Position = quadTR;
EmitVertex();
//3: BR
gl_Position = quadBR;
EmitVertex();

EndPrimitive(); 

I've made sure the all my position variables have a w value of 1.0. I'm getting what seems to be some kind of clipping in the z direction. If this is the right approach, then I will need to troubleshoot other parts of my code.

I would appreciate thoughts &&|| prayers on this.

Cheers!


Solution

  • OK after some experimentation I found a solution that seems to work.

    I started with a vertex shader that multiplies the position by projection and modelview matrices before passing on to the geometry shader. Like you, I found that adding an offset to the Z coord to generate triangles caused strange things. But adding an offset to the X and/or Y did work.

    Now I've rewritten my vertex shader to pass through the position unchanged and the geometry shader to multiply each of quadTL quadTR quadBL quadBR by the projection and modelview matrices just before EmitVertex(). And that creates visible triangles even when it's just a Z offset.

    I think this is something to do with the different range of values in Z vs X and Y, at least in my program, when normalized into device space. But not entirely sure.