0

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!

rtavakko
  • 31
  • 4
  • I think you need to do a quick reading / refresh on homogenous coordinates and what W is used for in the rasterisation step. Any 3D textbook or guide should do, it isn't specific to OpenGL. Easy mistake to make, I've done so myself many times. – Hugh Fisher Feb 20 '22 at 20:56
  • @HughFisher I've checked against multiple OpenGL guides. I'm looking for thoughts on the overall approach in the geometry shader – rtavakko Feb 24 '22 at 12:41
  • Let me add that if I switch the output of the geometry shader to "line_strip" (vertex count of 2) and push the input vertices without changes, I see no clipping. The waves are shown correctly without clipping – rtavakko Feb 24 '22 at 13:02
  • My apologies for the W coordinate comment. I misread your code, that is not the problem. – Hugh Fisher Mar 02 '22 at 05:16
  • I have tried writing my own lines to triangle strip geo shader. It works fine if I shift the X coord by +/- th, or the Y coord by +/- th. But if I change the Z coord, it gets really weird with single points or lines appearing. Hmmm. – Hugh Fisher Mar 02 '22 at 05:17

1 Answers1

1

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.

Hugh Fisher
  • 2,321
  • 13
  • 8
  • I solved this issue earlier this week exactly the same way! I was applying a single matrix (combined projection & model) in my vertex shader. Moving that step to the geomtry shader solved the issue so I can confirm you are correct. Thank you for your help! – rtavakko Mar 03 '22 at 13:03