Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm fairly new to OpenGL/JOGL, working through various tutorials and books and making steady progress. Text, however, is an area where I'm stuck. I figured out one way using BufferedImage and Graphics2D to draw strings and then swizzle the pixels and copy to an OpenGL texture, but the quality is low, it is resolution dependent, and it's not efficient.

I found this: http://forum.jogamp.org/GPU-based-Resolution-Independent-Curve-Rendering-td2764277.html. Unfortunately while there are some demos in the GitHub repo I can't quite get my head around them. The code I've tried to use is below:

In the init() method:

    InputStream fontFile = getClass().getResourceAsStream("media/futura.ttf");
    try {
        font = FontFactory.get(fontFile, true);
    } catch (IOException e) {
        System.err.println("Couldn't open font!");
        e.printStackTrace();
    }

    RenderState renderState = RenderState.createRenderState(SVertex.factory());
    renderState.setColorStatic(1, 1, 1, 1);
    renderState.setHintMask(RenderState.BITHINT_GLOBAL_DEPTH_TEST_ENABLED);

    renderer = RegionRenderer.create(renderState, RegionRenderer.defaultBlendEnable, RegionRenderer.defaultBlendDisable);
    renderer.init(gl, Region.VBAA_RENDERING_BIT);

    util = new TextRegionUtil(Region.VBAA_RENDERING_BIT);

In the display() method:

    PMVMatrix pmv = renderer.getMatrix();
    pmv.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
    pmv.glLoadIdentity();
    pmv.glTranslatef(0, 0, -300);
    float pixelSize = font.getPixelSize(32, 96);
    util.drawString3D(gl, renderer, font, pixelSize, "Test", null, samples);

I've searched and searched for a tutorial on this stuff or a simple, commented code example explaining how it works but to no avail. If anyone can help me I'd be extremely grateful!

share|improve this question
    
If you are new to OpenGL, you should stick with simpler approaches to text rendering, like Bitmap fonts. –  Alexandre Desbiens Jun 16 at 19:40

1 Answer 1

up vote 2 down vote accepted

Ok so to answer my own question, it turns out I'm an idiot and overlooked something really simple. The code in my question works perfectly, but I never set the text renderer's projection matrix. To make it work, in the resize method I added the following code:

renderer.enable(gl, true);
renderer.reshapeOrtho(width, height, 0.1f, 1000.0f);
renderer.enable(gl, false);

I also added an x/y coordinate offset in screen coordinates to the pmv.glTranslatef call to move the text in.

Now I have nice, cleanly rendered text from any font at any resolution, thanks Rami Santina and Sven Goethal of the JOGL team :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.