diff options
author | Wieland Hagen <wieland.hagen@kdab.com> | 2016-12-28 13:25:38 +0700 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2017-01-30 18:56:16 +0000 |
commit | 18ad771187f3d9a7d14d1a98d71ce64ed8ff15d4 (patch) | |
tree | c5e82336db09f911066912f011771ff1ca35fb77 /src/extras/shaders/gl3 | |
parent | 6c94c0395d060b4f4e07da1b86348f3e0e7a9b65 (diff) |
QDistanceFieldMaterial: distance field text rendering shaders
One QDistanceFieldMaterial will have to be created for each distinct
texture atlas that is used to store the glyphs of fonts.
The smaller the glyphs are in pixel space (i.e. the more texels per
pixel), the smoother we render the threshold between inside-glyph
and outside-glyph. We measure the size of the glyph by taking
the per-fragment delta of the texture coordinates. This is to reduce
aiasing as much as possible without resorting to screen-space AA
techniques.
The same method is used in the QtQuick renderer, but here in this case
we allow a much smoother threshold for small glyphs.
Change-Id: I6632e7e1da31126c84f5bd2b4a4498377bdad79e
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src/extras/shaders/gl3')
-rw-r--r-- | src/extras/shaders/gl3/distancefieldtext.frag | 38 | ||||
-rw-r--r-- | src/extras/shaders/gl3/distancefieldtext.vert | 19 |
2 files changed, 57 insertions, 0 deletions
diff --git a/src/extras/shaders/gl3/distancefieldtext.frag b/src/extras/shaders/gl3/distancefieldtext.frag new file mode 100644 index 000000000..4f0c9cac0 --- /dev/null +++ b/src/extras/shaders/gl3/distancefieldtext.frag @@ -0,0 +1,38 @@ +#version 150 core + +uniform sampler2D distanceFieldTexture; +uniform float minAlpha; +uniform float maxAlpha; +uniform float textureSize; +uniform vec4 color; + +in vec3 position; +in vec2 texCoord; + +out vec4 fragColor; + +void main() +{ + // determine the scale of the glyph texture within pixel-space coordinates + // (that is, how many pixels are drawn for each texel) + vec2 texelDeltaX = abs(dFdx(texCoord)); + vec2 texelDeltaY = abs(dFdy(texCoord)); + float avgTexelDelta = textureSize * 0.5 * (texelDeltaX.x + texelDeltaX.y + texelDeltaY.x + texelDeltaY.y); + float texScale = 1.0 / avgTexelDelta; + + // scaled to interval [0.0, 0.15] + float devScaleMin = 0.00; + float devScaleMax = 0.15; + float scaled = (clamp(texScale, devScaleMin, devScaleMax) - devScaleMin) / (devScaleMax - devScaleMin); + + // thickness of glyphs should increase a lot for very small glyphs to make them readable + float base = 0.5; + float threshold = base * scaled; + float range = 0.06 / texScale; + + float minAlpha = threshold - range; + float maxAlpha = threshold + range; + + float distVal = texture(distanceFieldTexture, texCoord).r; + fragColor = color * smoothstep(minAlpha, maxAlpha, distVal); +} diff --git a/src/extras/shaders/gl3/distancefieldtext.vert b/src/extras/shaders/gl3/distancefieldtext.vert new file mode 100644 index 000000000..9bd2a0a90 --- /dev/null +++ b/src/extras/shaders/gl3/distancefieldtext.vert @@ -0,0 +1,19 @@ +#version 150 core + +in vec3 vertexPosition; +in vec2 vertexTexCoord; + +out vec3 position; +out vec2 texCoord; + +uniform mat4 modelView; +uniform mat4 mvp; + +void main() +{ + texCoord = vertexTexCoord; + position = vec3(modelView * vec4(vertexPosition, 1.0)); + + gl_Position = mvp * vec4(vertexPosition, 1.0); +} + |