| 1 |
#version 400 |
| 2 |
|
| 3 |
// Per-vertex attributes |
| 4 |
in vec3 position; // this endpoint |
| 5 |
in vec3 otherEnd; // the other endpoint of the line segment |
| 6 |
in vec4 color; // RGBA color at this endpoint |
| 7 |
in float widthSide; // half-width * side (-1 or +1) |
| 8 |
in float lineParam; // dash parameter (0 for solid lines) |
| 9 |
|
| 10 |
uniform mat4 modelView; |
| 11 |
uniform mat4 projection; |
| 12 |
|
| 13 |
out vec4 outColor; |
| 14 |
out float fragLineParam; |
| 15 |
|
| 16 |
void main() |
| 17 |
{ |
| 18 |
// Transform both endpoints to view space |
| 19 |
vec4 viewPos = modelView * vec4(position, 1.0); |
| 20 |
vec3 viewOther = (modelView * vec4(otherEnd, 1.0)).xyz; |
| 21 |
|
| 22 |
// Line direction in view space |
| 23 |
vec3 lineDir = viewOther - viewPos.xyz; |
| 24 |
float lineLen = length(lineDir); |
| 25 |
|
| 26 |
// View direction in view space (camera looks along -Z) |
| 27 |
vec3 viewDir = vec3(0.0, 0.0, 1.0); |
| 28 |
|
| 29 |
vec3 offset; |
| 30 |
if (lineLen > 1e-6) { |
| 31 |
lineDir /= lineLen; |
| 32 |
// Perpendicular to both line direction and view direction |
| 33 |
offset = normalize(cross(lineDir, viewDir)) * widthSide; |
| 34 |
} else { |
| 35 |
// Degenerate line segment - offset in X |
| 36 |
offset = vec3(widthSide, 0.0, 0.0); |
| 37 |
} |
| 38 |
|
| 39 |
// Extend the endpoint outward by halfWidth for a square cap. |
| 40 |
// This fills gaps at corners where two wide lines meet. |
| 41 |
float halfWidth = abs(widthSide); |
| 42 |
viewPos.xyz += offset - lineDir * halfWidth; |
| 43 |
|
| 44 |
gl_Position = projection * viewPos; |
| 45 |
outColor = color; |
| 46 |
fragLineParam = lineParam; |
| 47 |
} |