Guest User

Untitled

a guest
Nov 21st, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. /* Copyright 2011 Gima
  2.  
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU Lesser General Public License for more details.
  12.  
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. package murprum.gl.text;
  17.  
  18. import java.awt.Font;
  19. import java.awt.FontFormatException;
  20. import java.awt.Shape;
  21. import java.awt.font.FontRenderContext;
  22. import java.awt.font.GlyphVector;
  23. import java.awt.geom.PathIterator;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileNotFoundException;
  27. import java.io.IOException;
  28. import java.io.InputStream;
  29.  
  30. import murprum.math.MathHelper;
  31. import murprum.sys.S;
  32.  
  33. import org.lwjgl.opengl.GL11;
  34. import org.lwjgl.util.glu.GLU;
  35. import org.lwjgl.util.glu.GLUtessellator;
  36. import org.lwjgl.util.glu.GLUtessellatorCallback;
  37. import org.lwjgl.util.glu.GLUtessellatorCallbackAdapter;
  38.  
  39. public class GLTextRenderer {
  40.    
  41.     private final Font font;
  42.     private final FontRenderContext fontRenderContext;
  43.     private final GLUtessellatorCallback callback;
  44.     private GlyphVector gv;
  45.     private Shape gs;
  46.     private PathIterator pathItr;
  47.     private GLUtessellator tess;
  48.    
  49.     public GLTextRenderer(InputStream ttfFont) throws FontFormatException, IOException {
  50.         font = Font.createFont(Font.TRUETYPE_FONT, ttfFont);
  51.         fontRenderContext = new FontRenderContext(MathHelper.identityMatrix, true, true);
  52.         callback = new GLTRCallback();
  53.     }
  54.    
  55.     public GLTextRenderer(String filename) throws FileNotFoundException, FontFormatException, IOException {
  56.         this(new FileInputStream(new File(filename)));
  57.     }
  58.  
  59.     public void prepare(String string) {
  60.         gv = font.createGlyphVector(fontRenderContext, string);
  61.         gs = gv.getOutline();
  62.         pathItr = gs.getPathIterator(MathHelper.identityMatrix, 0.5f);
  63.        
  64.         tess = GLU.gluNewTess();
  65.         tess.gluTessCallback(GLU.GLU_TESS_BEGIN, callback);
  66.         tess.gluTessCallback(GLU.GLU_TESS_VERTEX, callback);
  67.         tess.gluTessCallback(GLU.GLU_TESS_COMBINE, callback);
  68.         tess.gluTessCallback(GLU.GLU_TESS_END, callback);
  69.         tess.gluTessCallback(GLU.GLU_TESS_ERROR, callback);
  70.     }
  71.    
  72.     public void render() {
  73.         if (pathItr.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
  74.             tess.gluTessProperty(GLU.GLU_TESS_WINDING_RULE, GLU.GLU_TESS_WINDING_ODD);
  75.         }
  76.         else {
  77.             // PathIterator.WIND_NON_ZERO:
  78.             tess.gluTessProperty(GLU.GLU_TESS_WINDING_RULE, GLU.GLU_TESS_WINDING_NONZERO);
  79.         }
  80.        
  81.         tess.gluBeginPolygon();
  82.        
  83.         double coords[] = new double[3];
  84.         boolean first = true;
  85.         int segType;
  86.        
  87.         while (!pathItr.isDone()) {
  88.             segType = pathItr.currentSegment(coords);
  89.            
  90.             switch (segType) {
  91.             case PathIterator.SEG_MOVETO:
  92.                 System.out.println("move");
  93.                 // SEG_MOVETO and SEG_LINETO types returns one point,
  94.                 if (!first) tess.gluTessBeginContour();
  95.                 else first = false;
  96.                 tess.gluTessVertex(coords, 0, null);
  97.                 break;
  98.             case PathIterator.SEG_LINETO:
  99.                 System.out.println("line");
  100.                 // SEG_MOVETO and SEG_LINETO types returns one point,
  101.                 tess.gluTessVertex(coords, 0, null);
  102.                 break;
  103.             case PathIterator.SEG_CLOSE:
  104.                 System.out.println("close");
  105.                 // SEG_CLOSE does not return any points.
  106.                 tess.gluTessEndContour();
  107.                 break;
  108.             }
  109.            
  110.             pathItr.next();
  111.         }
  112.        
  113.         tess.gluEndPolygon();
  114.     }
  115.    
  116.     private static class GLTRCallback extends GLUtessellatorCallbackAdapter {
  117.  
  118.         @Override
  119.         public void begin(int type) {
  120.             GL11.glBegin(type);
  121.         }
  122.  
  123.         @Override
  124.         public void vertex(Object vertexData) {
  125.             System.out.println("derp:"+vertexData);
  126.             double coords[] = (double[]) vertexData;
  127.             GL11.glVertex3d(coords[0], coords[1], coords[2]);
  128.         }
  129.  
  130.         @Override
  131.         public void combine(double[] coords, Object[] data, float[] weight, Object[] outData) {
  132.             double[] vertex = new double[3];
  133.             for (int i=0; i<3; i++) vertex[i] = coords[i];
  134.             outData[0] = vertex;
  135.         }
  136.  
  137.         @Override
  138.         public void end() {
  139.             GL11.glEnd();
  140.         }
  141.  
  142.         @Override
  143.         public void error(int errnum) {
  144.             new Throwable(S.sprintf("GLU Tessellator error: %s", errnum)).printStackTrace();
  145.             S.printf("");
  146.         }
  147.  
  148.     }
  149.  
  150. }
  151.  
  152.  
Advertisement
Add Comment
Please, Sign In to add comment