| 1 |
#version 400 |
| 2 |
precision highp float; |
| 3 |
// Modelview/projection matrix |
| 4 |
uniform mat4 mv; |
| 5 |
uniform mat4 proj; |
| 6 |
|
| 7 |
// anchor position |
| 8 |
uniform vec3 anchor; |
| 9 |
|
| 10 |
// Distance to project the label towards the camera |
| 11 |
uniform float radius; |
| 12 |
|
| 13 |
// Vertex attributes. |
| 14 |
in vec2 offset; |
| 15 |
in vec2 texCoord; |
| 16 |
|
| 17 |
// Viewport dimensions: |
| 18 |
uniform ivec2 vpDims; |
| 19 |
|
| 20 |
// Texture coordinate. |
| 21 |
out vec2 texc; |
| 22 |
|
| 23 |
// Given a clip coordinate, align the vertex to the nearest pixel center. |
| 24 |
void alignToPixelCenter(inout vec4 clipCoord) |
| 25 |
{ |
| 26 |
// Half pixel increments (clip coord span / [2*numPixels] = [2*w] / [2*l]): |
| 27 |
vec2 inc = abs(clipCoord.w) / vec2(vpDims); |
| 28 |
|
| 29 |
// Fix up coordinates -- pixel centers are at xy = (-w + (2*i + 1) * inc) |
| 30 |
// for the i'th pixel. First find i and floor it. Just solve the above for i: |
| 31 |
ivec2 pixels = ivec2(floor((clipCoord.xy + abs(clipCoord.ww) - inc) |
| 32 |
/ (2. * inc))); |
| 33 |
|
| 34 |
// Now reapply the equation to obtain a pixel centered offset. |
| 35 |
clipCoord.xy = -abs(clipCoord.ww) + (2. * vec2(pixels) + vec2(1., 1.)) * inc; |
| 36 |
} |
| 37 |
|
| 38 |
void main(void) |
| 39 |
{ |
| 40 |
// Transform to eye coordinates: |
| 41 |
vec4 eyeAnchor = mv * vec4(anchor, 1.0); |
| 42 |
|
| 43 |
// Apply radius; |
| 44 |
eyeAnchor += vec4(0., 0., radius, 0.); |
| 45 |
|
| 46 |
// Transform to clip coordinates |
| 47 |
vec4 clipAnchor = proj * eyeAnchor; |
| 48 |
|
| 49 |
// Move the anchor to a pixel center: |
| 50 |
alignToPixelCenter(clipAnchor); |
| 51 |
|
| 52 |
// Align offset to cell centers using the w coordinate: |
| 53 |
// Since w determines whether or not the vertex is clipped, (-w, w) spans |
| 54 |
// the width/height of the display. Using the viewport width/height in pixels, |
| 55 |
// we can properly convert the offset into pixel units. |
| 56 |
vec2 conv = (2. * abs(clipAnchor.w)) / vec2(vpDims); |
| 57 |
|
| 58 |
// Apply the offset: |
| 59 |
gl_Position = clipAnchor + vec4(offset.x * conv.x, offset.y * conv.y, 0., 0.); |
| 60 |
|
| 61 |
// Pass through the texture coordinate |
| 62 |
texc = texCoord; |
| 63 |
} |