Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Copyright 2011 Gima
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package murprum.gl.text;
- import java.awt.Font;
- import java.awt.FontFormatException;
- import java.awt.Shape;
- import java.awt.font.FontRenderContext;
- import java.awt.font.GlyphVector;
- import java.awt.geom.PathIterator;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import murprum.math.MathHelper;
- import murprum.sys.S;
- import org.lwjgl.opengl.GL11;
- import org.lwjgl.util.glu.GLU;
- import org.lwjgl.util.glu.GLUtessellator;
- import org.lwjgl.util.glu.GLUtessellatorCallback;
- import org.lwjgl.util.glu.GLUtessellatorCallbackAdapter;
- public class GLTextRenderer {
- private final Font font;
- private final FontRenderContext fontRenderContext;
- private final GLUtessellatorCallback callback;
- private GlyphVector gv;
- private Shape gs;
- private PathIterator pathItr;
- private GLUtessellator tess;
- public GLTextRenderer(InputStream ttfFont) throws FontFormatException, IOException {
- font = Font.createFont(Font.TRUETYPE_FONT, ttfFont);
- fontRenderContext = new FontRenderContext(MathHelper.identityMatrix, true, true);
- callback = new GLTRCallback();
- }
- public GLTextRenderer(String filename) throws FileNotFoundException, FontFormatException, IOException {
- this(new FileInputStream(new File(filename)));
- }
- public void prepare(String string) {
- gv = font.createGlyphVector(fontRenderContext, string);
- gs = gv.getOutline();
- pathItr = gs.getPathIterator(MathHelper.identityMatrix, 0.5f);
- tess = GLU.gluNewTess();
- tess.gluTessCallback(GLU.GLU_TESS_BEGIN, callback);
- tess.gluTessCallback(GLU.GLU_TESS_VERTEX, callback);
- tess.gluTessCallback(GLU.GLU_TESS_COMBINE, callback);
- tess.gluTessCallback(GLU.GLU_TESS_END, callback);
- tess.gluTessCallback(GLU.GLU_TESS_ERROR, callback);
- }
- public void render() {
- if (pathItr.getWindingRule() == PathIterator.WIND_EVEN_ODD) {
- tess.gluTessProperty(GLU.GLU_TESS_WINDING_RULE, GLU.GLU_TESS_WINDING_ODD);
- }
- else {
- // PathIterator.WIND_NON_ZERO:
- tess.gluTessProperty(GLU.GLU_TESS_WINDING_RULE, GLU.GLU_TESS_WINDING_NONZERO);
- }
- tess.gluBeginPolygon();
- double coords[] = new double[3];
- boolean first = true;
- int segType;
- while (!pathItr.isDone()) {
- segType = pathItr.currentSegment(coords);
- switch (segType) {
- case PathIterator.SEG_MOVETO:
- System.out.println("move");
- // SEG_MOVETO and SEG_LINETO types returns one point,
- if (!first) tess.gluTessBeginContour();
- else first = false;
- tess.gluTessVertex(coords, 0, null);
- break;
- case PathIterator.SEG_LINETO:
- System.out.println("line");
- // SEG_MOVETO and SEG_LINETO types returns one point,
- tess.gluTessVertex(coords, 0, null);
- break;
- case PathIterator.SEG_CLOSE:
- System.out.println("close");
- // SEG_CLOSE does not return any points.
- tess.gluTessEndContour();
- break;
- }
- pathItr.next();
- }
- tess.gluEndPolygon();
- }
- private static class GLTRCallback extends GLUtessellatorCallbackAdapter {
- @Override
- public void begin(int type) {
- GL11.glBegin(type);
- }
- @Override
- public void vertex(Object vertexData) {
- System.out.println("derp:"+vertexData);
- double coords[] = (double[]) vertexData;
- GL11.glVertex3d(coords[0], coords[1], coords[2]);
- }
- @Override
- public void combine(double[] coords, Object[] data, float[] weight, Object[] outData) {
- double[] vertex = new double[3];
- for (int i=0; i<3; i++) vertex[i] = coords[i];
- outData[0] = vertex;
- }
- @Override
- public void end() {
- GL11.glEnd();
- }
- @Override
- public void error(int errnum) {
- new Throwable(S.sprintf("GLU Tessellator error: %s", errnum)).printStackTrace();
- S.printf("");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment