 # Texcoords for line in geometry shader

Hi all,

This is my first time posting here so i hope i ask this in the right section of the forum.

I use a geometry shader to create a line between to points like so:

``````vec4 p1 = gl_in.gl_Position;
vec4 p2 = gl_in.gl_Position;
vec2 dir = normalize((p2.xy - p1.xy) * viewport);
vec2 offset = vec2(-dir.y,dir.x) * line_width / viewport;

texcoord = vec2(0.,0.);
gl_Position = p1 + vec4(offset.xy * p1.w, 0.0, 0.0);
EmitVertex();

texcoord = vec2(1.,0.);
gl_Position = p1 - vec4(offset.xy * p1.w, 0.0, 0.0);
EmitVertex();

texcoord = vec2(0., 1.);
gl_Position =  p2 + vec4(offset.xy * p2.w, 0.0, 0.0);
EmitVertex();

texcoord = vec2(1., 1.);
gl_Position = p2 - vec4(offset.xy * p2.w, 0.0, 0.0);
EmitVertex();
``````

I now want to create texture coordinates for my created line.
You can see my attempt in the code above. This works a bit but when i rotate the camera the coordinates don’t always work.

Does anyone know a solution to my problem? Thanks!y

For a start, you should divide by W here, e.g.

``````vec2 dir = normalize((p2.xy/p2.w - p1.xy/p1.w) * viewport);
``````

Without that, the line direction will be wrong.

Thanks for the quick response! Already looking a lot better but the texture coordinates still look a bit weird when my camera is roated. Do you have any pointers?

Can you elaborate?

You should get a rectangle centred on the line, with the corners of the rectangle mapped to the corners of the texture. Note that the texture mapping will be projective, i.e. the scale factor will reduce with distance. If you want an affine mapping, add the `noperspective` qualifier to the `texcoord` variable.

Ah, Yes noperspective is what i’m looking for :).

Thanks a lot!

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.