This is a very simple demonstration of how to render the signed distance field texture generated by makeglfont.
Among other uses, signed distance fields allow a simple GLSL shader to recover a vector-like representation of a shape from a bitmap of values. This makes them very useful for rendering fonts in OpenGL video games. A small (e.g., 512 by 512 pixel) bitmap holding an SDF representation of a glyph can be smoothly scaled with no pixelation and with very little perceptible loss in quality. A full discussion of the why and what of signed distance fields can be found on krytharn's YouTube presentation or in Valve's original paper.
Make sure your web browser is WebGL-capable.
Start the server with:
node server.js
Connect your web browser to http://127.0.0.1:8888/ .
Use 'w' to move the text as close to you as you like. Notice that the
bitmap from which the text is rendered (visible at mensch.png
in the
source tree) is quite pixelated when zoomed up to an equivalent size.
SDFs allow you to do fun things like adding glowing borders to your text. Use the 'g' and 'h' keys to enable and disable a blue glowing border for the text. (There are other ways to do this, of course, but this is a nice side effect of using SDFs for text.)
This software © 2014 Raphael Martelles and is released under the following license:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
The font mensch.ttf
is from http://robey.lag.net/2010/06/21/mensch-font.html.
The server.js code is from https://gist.github.com/rpflorence/701407.