Hi! I’m currently working on a custom line renderer for my 3d editor. The idea is to get constant width lines in pixel units. I’m using a geometry shader to create a triangle strip from a line by offseting the vertices positions a little in screen space according to the desired width.

Vertex shader:

```
#version 330 core
layout (location = 0) in vec3 aPosition;
uniform mat4 uView;
uniform mat4 uProjection;
void main()
{
gl_Position = uProjection * uView * vec4(aPosition, 1.0);
}
```

Geometry shader:

```
#version 330 core
layout (lines) in;
layout (triangle_strip, max_vertices = 4) out;
uniform float uThickness;
uniform vec2 uViewport;
vec2 toScreenSpace(vec4 vertex, vec2 viewport) {
return vec2(vertex.xy / vertex.w) * viewport;
}
void main()
{
vec2 p0 = toScreenSpace(gl_in[0].gl_Position, uViewport);
vec2 p1 = toScreenSpace(gl_in[1].gl_Position, uViewport);
vec2 dir = normalize(p1 - p0);
vec2 normal = vec2(-dir.y, dir.x);
gl_Position = vec4((p0 + uThickness * normal) / uViewport, 0.0, 1.0);
EmitVertex();
gl_Position = vec4((p0 - uThickness * normal) / uViewport, 0.0, 1.0);
EmitVertex();
gl_Position = vec4((p1 + uThickness * normal) / uViewport, 0.0, 1.0);
EmitVertex();
gl_Position = vec4((p1 - uThickness * normal) / uViewport, 0.0, 1.0);
EmitVertex();
EndPrimitive();
}
```

Fragment shader:

```
#version 330 core
layout (location = 0) out vec4 FragColor;
uniform vec4 uColor;
void main()
{
FragColor = uColor;
}
```

The line gets rendered correctly when I’m far from it and everything looks good but when I get too close to it and the line intersects (I think) with the near plane, I start getting errors. I assume it’s due to the division by the w component. I’ve been trying to fix it for two whole days now and I don’t know how to do make it work. Any ideas?

I recorded a video but I’m not allowed to embed it. This is the YouTube link:

`https://youtu.be/y-JnEvyeIG0`

Sorry for that.

Thank you very much in advance!