ulfben

GLFont.java (pixel font geometry)

Mar 27th, 2017
114
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.ulfben.asteroids.GL;
  2. import java.util.HashMap;
  3. /**
  4.  * Created by Ulf Benjaminsson on 2017-03-27, www.ulfben.com
  5.  * GLFont is a "pixel font" for OpenGL. Each glyph is made up from 1-9 quads,
  6.  * ready to be put in a vertex-buffer and rendered as geometry.
  7.  *
  8.  * Example: https://media.giphy.com/media/11lKO38jQGliuY/giphy.gif
  9.  *
  10.  * getString(String) returns an array of GLModels. GLModel is a just glorified vertex buffer
  11.  * so replace it with whatever you use to pass vertices into OpenGL.
  12.  */
  13. public class GLFont {
  14.     public static float height = 25f;
  15.     public static float width = 25f;
  16.     public static float lineWidth = 5.0f;
  17.     private static HashMap<String, GLModel> glyphMap = new HashMap<String, GLModel>();
  18.     private GLFont(){}
  19.  
  20.     public static void init() {
  21.         final float LW2 = lineWidth*2f; //prepare some common values
  22.         final float LW3 = lineWidth*3f;
  23.         final float left = 0f;
  24.         final float top = height;
  25.         final float bottom = 0f;
  26.         final float right = left+width;
  27.         final float centerY = (top/2f)+(lineWidth/2f);
  28.         final float centerX = (right/2f)-(lineWidth/2f);
  29.         final float[] fullLeft = quad(left, top, lineWidth, height); //prepare some common quads
  30.         final float[] fullRight = quad(right-lineWidth, top, lineWidth, height);
  31.         final float[] fullTop = quad(left, top, width, lineWidth);
  32.         final float[] fullBottom = quad(left, bottom+lineWidth, width, lineWidth);
  33.         final float[] boxOpenLeft = concat(fullLeft, fullTop, fullBottom); //C, E, G, o, 0, 8, Q, 6
  34.         final float[] box = concat(boxOpenLeft, fullRight); //o, 0 and 8, Q
  35.         final float[] centerLineLeft = quad(left+lineWidth, centerY, width-(LW3), lineWidth); //E, F
  36.         final float[] centerLineRight = quad(left+LW2, centerY, width-LW2, lineWidth); //lip of G, 3
  37.         final float[] centerLineCentered = quad(left+lineWidth, centerY, width-LW2, lineWidth); //H, B, A etc
  38.         final float[] verticalCenter = quad(centerX, top, lineWidth, height);// I, T
  39.         final float[] shortTop = quad(left, top, width-lineWidth, lineWidth); //B, D, P, R
  40.         final float[] shortBottom = quad(left, bottom+lineWidth, width-lineWidth, lineWidth); //B, D
  41.  
  42.         final float[] a = concat(fullLeft, fullTop, fullRight, centerLineCentered);
  43.         final float[] b = concat(fullLeft, shortTop, shortBottom, centerLineCentered, quad(right-lineWidth, top-lineWidth, lineWidth, height-LW2));
  44.         final float[] c = boxOpenLeft;
  45.         final float[] d = concat(fullLeft, shortTop, shortBottom, quad(right-lineWidth, top-lineWidth, lineWidth, height-LW2));
  46.         final float[] e = concat(boxOpenLeft, centerLineLeft);
  47.         final float[] f = concat(fullLeft, fullTop, centerLineLeft);
  48.         final float[] g = concat(boxOpenLeft, quad(right-lineWidth, centerY, lineWidth, bottom+centerY), centerLineRight);
  49.         final float[] h = concat(fullLeft, fullRight, centerLineCentered);
  50.         final float[] i = concat(fullTop, fullBottom, verticalCenter);
  51.         final float[] j = concat(fullRight, fullBottom, quad(left, bottom+LW2, lineWidth, lineWidth));
  52.         final float[] k = concat(fullLeft, centerLineLeft,
  53.                 quad(right-LW2, centerY+lineWidth, lineWidth, lineWidth),
  54.                 quad(right-lineWidth, top, lineWidth, lineWidth),
  55.                 quad(right-LW2, centerY-lineWidth, lineWidth, lineWidth),
  56.                 quad(right-lineWidth, bottom+lineWidth, lineWidth, lineWidth)
  57.         );
  58.         final float[] l = concat(fullLeft, fullBottom);
  59.         final float[] m = concat(fullLeft, verticalCenter, fullRight, fullTop);
  60.         final float[] n = concat(fullLeft, fullTop, fullRight);
  61.         final float[] o = box;
  62.         final float[] p = concat(fullLeft, shortTop, centerLineCentered, quad(right-lineWidth, top-lineWidth, lineWidth, LW2));
  63.         final float[] q = concat(fullLeft, fullRight, fullTop, fullBottom, quad(centerX, bottom+LW2, lineWidth, LW3));
  64.         final float[] r = concat(fullLeft, shortTop, centerLineCentered,
  65.                 quad(right-lineWidth, top-lineWidth, lineWidth, centerY-lineWidth),
  66.                 quad(right-LW2, centerY-lineWidth, lineWidth, lineWidth),
  67.                 quad(right-lineWidth, bottom+lineWidth, lineWidth, lineWidth)
  68.         );
  69.         final float[] s = concat(fullTop, fullBottom, centerLineCentered,
  70.                 quad(left, top, lineWidth, centerY),
  71.                 quad(right-lineWidth, centerY, lineWidth, centerY));
  72.         final float[] t = concat(fullTop, verticalCenter);
  73.         final float[] u = concat(fullLeft, fullRight, fullBottom);
  74.         final float[] v = concat(quad(left, top, lineWidth, centerY),
  75.                 quad(right-lineWidth, top, lineWidth, centerY),
  76.                 quad(right-LW2, centerY-lineWidth, lineWidth, lineWidth),
  77.                 quad(left+lineWidth, centerY-lineWidth, lineWidth, lineWidth),
  78.                 quad(centerX, bottom+lineWidth, lineWidth, lineWidth)
  79.         );
  80.         final float[] w = concat(fullLeft, verticalCenter, fullRight, fullBottom);
  81.         final float[] x = concat(
  82.                 quad(left, top, lineWidth, lineWidth), quad(right-lineWidth, top, lineWidth, lineWidth),
  83.                 quad(left+lineWidth, top-lineWidth, lineWidth, lineWidth), quad(right-LW2, top-lineWidth, lineWidth, lineWidth),
  84.                 quad(left+LW2, top-LW2, lineWidth, lineWidth), //center of x
  85.                 quad(left+lineWidth, bottom+LW2, lineWidth, lineWidth), quad(right-LW2, bottom+LW2, lineWidth, lineWidth),
  86.                 quad(left, bottom+lineWidth, lineWidth, lineWidth), quad(right-lineWidth, bottom+lineWidth, lineWidth, lineWidth)
  87.         );
  88.         final float[] y = concat(
  89.                 quad(left, top, lineWidth, lineWidth), quad(right-lineWidth, top, lineWidth, lineWidth),
  90.                 quad(left+lineWidth, top-lineWidth, lineWidth, lineWidth), quad(right-LW2, top-lineWidth, lineWidth, lineWidth),
  91.                 quad(left+LW2, top-LW2, lineWidth, (LW3))
  92.         );
  93.         final float[] z = concat(fullTop, fullBottom, quad(right-LW2, top-lineWidth, lineWidth, lineWidth),
  94.                 quad(left+LW2, top-LW2, lineWidth, lineWidth),
  95.                 quad(left+lineWidth, bottom+LW2, lineWidth, lineWidth)
  96.         );
  97.         final  float[] zero = box;
  98.         final float[] one = concat(quad(centerX-lineWidth, top, lineWidth, lineWidth), verticalCenter);
  99.         final float[] two = concat(fullTop, fullBottom, centerLineCentered, quad(left, centerY, lineWidth, centerY),
  100.                 quad(right-lineWidth, top, lineWidth, centerY));
  101.         final  float[] three = concat(fullRight, fullTop, centerLineRight, fullBottom);
  102.         final float[] four = concat(fullRight, centerLineCentered, quad(left, top, lineWidth, centerY));
  103.         final float[] five = concat(fullTop, fullBottom, centerLineCentered, quad(left, top, lineWidth, centerY),
  104.                 quad(right-lineWidth, centerY, lineWidth, centerY));
  105.         final float[] six = concat(boxOpenLeft, centerLineCentered, quad(right-lineWidth, centerY, lineWidth, centerY));
  106.         final float[] seven = concat(fullTop, fullRight);
  107.         final float[] eight = concat(box, centerLineCentered);
  108.         final float[] nine = concat(fullRight, fullTop, centerLineCentered,  quad(left, top, lineWidth, centerY));
  109.         final float[] dot = quad(left, bottom+lineWidth, lineWidth, lineWidth);
  110.         final float[] dash = quad(left+lineWidth, centerY, width-LW2, lineWidth);
  111.         final float[] slash = concat(quad(right-lineWidth, top, lineWidth, lineWidth),
  112.                 quad(right-LW2, top-lineWidth, lineWidth, lineWidth), quad(left+LW2, top-LW2, lineWidth, lineWidth),
  113.                 quad(left+lineWidth, bottom+LW2, lineWidth, lineWidth), quad(left, bottom+lineWidth, lineWidth, lineWidth)
  114.         );
  115.         addGlyph("a", a);
  116.         addGlyph("b", b);
  117.         addGlyph("c", c);
  118.         addGlyph("d", d);
  119.         addGlyph("e", e);
  120.         addGlyph("f", f);
  121.         addGlyph("g", g);
  122.         addGlyph("h", h);
  123.         addGlyph("i", i);
  124.         addGlyph("j", j);
  125.         addGlyph("k", k);
  126.         addGlyph("l", l);
  127.         addGlyph("m", m);
  128.         addGlyph("n", n);
  129.         addGlyph("o", o);
  130.         addGlyph("p", p);
  131.         addGlyph("q", q);
  132.         addGlyph("r", r);
  133.         addGlyph("s", s);
  134.         addGlyph("t", t);
  135.         addGlyph("u", u);
  136.         addGlyph("v", v);
  137.         addGlyph("x", x);
  138.         addGlyph("y", y);
  139.         addGlyph("z", z);
  140.         addGlyph("0", zero);
  141.         addGlyph("1", one);
  142.         addGlyph("2", two);
  143.         addGlyph("3", three);
  144.         addGlyph("4", four);
  145.         addGlyph("5", five);
  146.         addGlyph("6", six);
  147.         addGlyph("7", seven);
  148.         addGlyph("8", eight);
  149.         addGlyph("9", nine);
  150.         addGlyph(".", dot);
  151.         addGlyph("-", dash);
  152.         addGlyph("/", slash);
  153.     }
  154.  
  155.     public static GLModel[] getString(final String s){
  156.         int length = s.length();
  157.         GLModel[] result = new GLModel[length];
  158.         for(int i = 0; i < length; i++){
  159.             result[i] = GLFont.getChar(s.substring(i, i+1));
  160.         }
  161.         return result;
  162.     }
  163.  
  164.     public static final GLModel getChar(final String s){
  165.         GLModel result = glyphMap.get(s);
  166.         if(result == null){
  167.             return glyphMap.get(".");
  168.         }
  169.         return result;
  170.     }
  171.  
  172.     private static float[] quad(float left, float top, float width, float height){
  173.         final float z = 0.0f;
  174.         final float right = left+width;
  175.         final float bottom = top-height;
  176.         return new float[]{
  177.                 left, top, z,
  178.                 left, bottom, z,
  179.                 right, bottom, z,
  180.                 right, top, z,
  181.                 left, top, z,
  182.                 right, bottom, z
  183.         };
  184.     }
  185.  
  186.     private static void addGlyph(final String index, final float[] vertices){
  187.         glyphMap.put(index, new GLModel(vertices));
  188.     }
  189.  
  190.     private static float[] concat(final float[]... arrays){
  191.         if(arrays == null || arrays.length < 1){ throw new IllegalArgumentException(""); }
  192.         int length = 0, pos = 0, count = arrays.length;
  193.         for (float[] a : arrays) {
  194.             length += a.length;
  195.         }
  196.         float[] result = new float[length];
  197.         for(int i = 0; i < count; i++){
  198.             System.arraycopy(arrays[i], 0, result, pos, arrays[i].length);
  199.             pos += arrays[i].length;
  200.         }
  201.         return result;
  202.     }
  203. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×