Advertisement
ClickerMonkey

OpenGL Text

Nov 15th, 2013
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.46 KB | None | 0 0
  1.  
  2. /**
  3.  * A class for drawing text in OpenGL with lines.
  4.  *
  5.  * GLYPH:
  6.  *
  7.  * <pre>
  8.  * 0---1---2
  9.  * |   |   |
  10.  * 3---4---5
  11.  * |   |   |
  12.  * 6---7---8
  13.  * |   |   |
  14.  * 9---A---B
  15.  * |   |   |
  16.  * C---D---E
  17.  * </pre>
  18.  *
  19.  * @author Philip Diffenderfer
  20.  *
  21.  */
  22. public class OpenGLText
  23. {
  24.  
  25.     private static int[][] GLYPHS = new int[256][];
  26.  
  27.     private static Vector[] GLYPH_POINTS = {
  28.         new Vector( 0.00f, 0.00f ), new Vector( 0.50f, 0.00f ), new Vector( 1.00f, 0.00f ),
  29.         new Vector( 0.00f, 0.25f ), new Vector( 0.50f, 0.25f ), new Vector( 1.00f, 0.25f ),
  30.         new Vector( 0.00f, 0.50f ), new Vector( 0.50f, 0.50f ), new Vector( 1.00f, 0.50f ),
  31.         new Vector( 0.00f, 0.72f ), new Vector( 0.50f, 0.75f ), new Vector( 1.00f, 0.75f ),
  32.         new Vector( 0.00f, 1.00f ), new Vector( 0.50f, 1.00f ), new Vector( 1.00f, 1.00f )
  33.     };
  34.  
  35.     static
  36.     {
  37.         GLYPHS['A'] = new int[] { 0x02, 0x0C, 0x2E, 0x68 };
  38.         GLYPHS['B'] = new int[] { 0x02, 0x25, 0x57, 0x7B, 0xBE, 0xCE, 0x0C, 0x67 };
  39.         GLYPHS['C'] = new int[] { 0x02, 0x0C, 0xCE };
  40.         GLYPHS['D'] = new int[] { 0x01, 0x15, 0x5B, 0xBD, 0xCD, 0x0C };
  41.         GLYPHS['E'] = new int[] { 0x02, 0x0C, 0xCE, 0x67 };
  42.         GLYPHS['F'] = new int[] { 0x02, 0x0C, 0x67 };
  43.         GLYPHS['G'] = new int[] { 0x02, 0x0C, 0xCE, 0xE8, 0x78 };
  44.         GLYPHS['H'] = new int[] { 0x0C, 0x2E, 0x68 };
  45.         GLYPHS['I'] = new int[] { 0x02, 0xCE, 0x1D };
  46.         GLYPHS['J'] = new int[] { 0x2E, 0xCE, 0x6C };
  47.         GLYPHS['K'] = new int[] { 0x0C, 0x26, 0x6E };
  48.         GLYPHS['L'] = new int[] { 0x0C, 0xCE };
  49.         GLYPHS['M'] = new int[] { 0x0C, 0x07, 0x27, 0x2E };
  50.         GLYPHS['N'] = new int[] { 0x0C, 0x0E, 0x2E };
  51.         GLYPHS['O'] = new int[] { 0x0C, 0x02, 0x2E, 0xCE };
  52.         GLYPHS['P'] = new int[] { 0x0C, 0x02, 0x28, 0x68 };
  53.         GLYPHS['Q'] = new int[] { 0x0C, 0x02, 0x2E, 0xCE, 0xAE };
  54.         GLYPHS['R'] = new int[] { 0x0C, 0x02, 0x28, 0x68, 0x6E };
  55.         GLYPHS['S'] = new int[] { 0x02, 0x06, 0x68, 0x8E, 0xCE };
  56.         GLYPHS['T'] = new int[] { 0x02, 0x1D };
  57.         GLYPHS['U'] = new int[] { 0x0C, 0xCE, 0x2E };
  58.         GLYPHS['V'] = new int[] { 0x0D, 0x2D };
  59.         GLYPHS['W'] = new int[] { 0x0C, 0xC7, 0x7E, 0x2E };
  60.         GLYPHS['X'] = new int[] { 0x0E, 0x2C };
  61.         GLYPHS['Y'] = new int[] { 0x07, 0x27, 0x7D };
  62.         GLYPHS['Z'] = new int[] { 0x02, 0x2C, 0xCE };
  63.  
  64.         GLYPHS['1'] = new int[] { 0x13, 0x1D, 0xCE };
  65.         GLYPHS['2'] = new int[] { 0x13, 0x15, 0x58, 0x8C, 0xCE };
  66.         GLYPHS['3'] = new int[] { 0x13, 0x15, 0x57, 0x7B, 0xBD, 0x9D };
  67.         GLYPHS['4'] = new int[] { 0x26, 0x68, 0x2E };
  68.         GLYPHS['5'] = new int[] { 0x02, 0x06, 0x67, 0x7B, 0xBD, 0xCD };
  69.         GLYPHS['6'] = new int[] { 0x02, 0x0C, 0xCE, 0x8E, 0x68 };
  70.         GLYPHS['7'] = new int[] { 0x02, 0x2C };
  71.         GLYPHS['8'] = new int[] { 0x02, 0x2E, 0xCE, 0x0C, 0x68 };
  72.         GLYPHS['9'] = new int[] { 0x02, 0x06, 0x68, 0x2E };
  73.         GLYPHS['0'] = new int[] { 0x02, 0x2E, 0x0C, 0xCE };
  74.  
  75.         GLYPHS['!'] = new int[] { 0x1A, 0xDD };
  76.         GLYPHS['@'] = new int[] { 0x78, 0x7A, 0xAB, 0x2B, 0x02, 0x0C, 0xCE };
  77.         GLYPHS['\'']= new int[] { 0x17 };
  78.         GLYPHS['"'] = new int[] { 0x17, 0x28 };
  79.         GLYPHS['$'] = new int[] { 0x15, 0x13, 0x3B, 0xBD, 0x9D, 0x1D };
  80.         GLYPHS['%'] = new int[] { 0x2C, 0x01, 0x14, 0x34, 0x03, 0xDE, 0xAB, 0xAD, 0xBE };
  81.         GLYPHS['#'] = new int[] { 0x1C, 0x2D, 0x35, 0x9B };
  82.         GLYPHS['^'] = new int[] { 0x16, 0x18 };
  83.         GLYPHS['&'] = new int[] { 0x13, 0x15, 0x59, 0x3E, 0x9D, 0xDB, 0x8B };
  84.         GLYPHS['*'] = new int[] { 0x4A, 0x68, 0x3B, 0x59 };
  85.         GLYPHS['('] = new int[] { 0x24, 0x4A, 0xAE };
  86.         GLYPHS[')'] = new int[] { 0x04, 0x4A, 0xAC };
  87.         GLYPHS['-'] = new int[] { 0x68 };
  88.         GLYPHS['_'] = new int[] { 0xCE };
  89.         GLYPHS['='] = new int[] { 0x35, 0x9B };
  90.         GLYPHS['+'] = new int[] { 0x68, 0x4A };
  91.         GLYPHS['{'] = new int[] { 0x12, 0x14, 0x46, 0x6A,0xAD, 0xDE };
  92.         GLYPHS['}'] = new int[] { 0x01, 0x14, 0x48, 0x8A, 0xAD, 0xCD };
  93.         GLYPHS['['] = new int[] { 0x12, 0x1D, 0xDE };
  94.         GLYPHS[']'] = new int[] { 0x01, 0x1D, 0xCD };
  95.             GLYPHS[':'] = new int[] { 0x44, 0xAA };
  96.             GLYPHS[';'] = new int[] { 0x44, 0xAC };
  97.             GLYPHS['<'] = new int[] { 0x56, 0x6B };
  98.             GLYPHS['>'] = new int[] { 0x38, 0x89 };
  99.             GLYPHS['.'] = new int[] { 0xDD };
  100.             GLYPHS[','] = new int[] { 0xAC };
  101.             GLYPHS['/'] = new int[] { 0x2C };
  102.             GLYPHS['\\']= new int[] { 0x0E };
  103.             GLYPHS['?'] = new int[] { 0x13, 0x15, 0x57, 0x7A, 0xDD };
  104.             GLYPHS['|'] = new int[] { 0x1D };
  105.     }
  106.  
  107.     public void drawString( float x, float y, float w, float h, float kerning, float lineWidth, String format, Object... args )
  108.     {
  109.         drawString( x, y, w, h, kerning, lineWidth, String.format( format, args ) );
  110.     }
  111.  
  112.     public void drawString( float x, float y, float w, float h, float kerning, float lineWidth, CharSequence chars )
  113.     {
  114.         glPushMatrix();
  115.         glLineWidth( lineWidth );
  116.         glBegin( GL_LINES );
  117.  
  118.         for (int i = 0; i < chars.length(); i++)
  119.         {
  120.             x += drawGlyph( chars.charAt( i ), x, y, w, h ) * kerning;
  121.         }
  122.  
  123.         glEnd();
  124.         glLineWidth( 1.0f );
  125.         glPopMatrix();
  126.     }
  127.  
  128.     public float drawGlyph( char c, float ox, float oy, float cw, float ch )
  129.     {
  130.         c = Character.toUpperCase( c );
  131.  
  132.         int[] lines = GLYPHS[c];
  133.  
  134.         if (lines != null)
  135.         {
  136.             for (int i = 0; i < lines.length; i++)
  137.             {
  138.                 Vector a = GLYPH_POINTS[(lines[i] >> 4) & 15];
  139.                 Vector b = GLYPH_POINTS[(lines[i] >> 0) & 15];
  140.                 float off = (a.isEqual( b ) ? 1 : 0);
  141.                
  142.                         glVertex2f( a.x * cw + ox, a.y * ch + oy );
  143.                         glVertex2f( b.x * cw + ox + off, b.y * ch + oy + off );  
  144.             }
  145.  
  146.             return cw;
  147.         }
  148.         else if (Character.isWhitespace( c ))
  149.         {
  150.             return cw;
  151.         }
  152.  
  153.         return 0.0f;
  154.     }
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement