dashed_thick_lines.gs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #version 150
  2. // see as reference: https://github.com/mhalber/Lines/blob/master/geometry_shader_lines.h
  3. // https://stackoverflow.com/questions/52928678/dashed-line-in-opengl3
  4. layout(lines) in;
  5. layout(triangle_strip, max_vertices = 4) out;
  6. const float aa_radius = 0.5;
  7. uniform vec2 viewport_size;
  8. uniform float width;
  9. in float coord_s[];
  10. out float line_width;
  11. // x = v tex coord, y = s coord
  12. out vec2 seg_params;
  13. void main()
  14. {
  15. vec2 ndc_0 = gl_in[0].gl_Position.xy / gl_in[0].gl_Position.w;
  16. vec2 ndc_1 = gl_in[1].gl_Position.xy / gl_in[1].gl_Position.w;
  17. vec2 dir = normalize((ndc_1 - ndc_0) * viewport_size);
  18. vec2 normal_dir = vec2(-dir.y, dir.x);
  19. line_width = max(1.0, width) + 2.0 * aa_radius;
  20. float half_line_width = 0.5 * line_width;
  21. vec2 normal = vec2(line_width / viewport_size[0], line_width / viewport_size[1]) * normal_dir;
  22. seg_params = vec2(-half_line_width, coord_s[0]);
  23. gl_Position = vec4((ndc_0 + normal) * gl_in[0].gl_Position.w, gl_in[0].gl_Position.zw);
  24. EmitVertex();
  25. seg_params = vec2(-half_line_width, coord_s[0]);
  26. gl_Position = vec4((ndc_0 - normal) * gl_in[0].gl_Position.w, gl_in[0].gl_Position.zw);
  27. EmitVertex();
  28. seg_params = vec2(half_line_width, coord_s[1]);
  29. gl_Position = vec4((ndc_1 + normal) * gl_in[1].gl_Position.w, gl_in[1].gl_Position.zw);
  30. EmitVertex();
  31. seg_params = vec2(half_line_width, coord_s[1]);
  32. gl_Position = vec4((ndc_1 - normal) * gl_in[1].gl_Position.w, gl_in[1].gl_Position.zw);
  33. EmitVertex();
  34. EndPrimitive();
  35. }