0
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ If you are new to OpenGL, you should stick with simpler approaches to text rendering, like Bitmap fonts. \$\endgroup\$ – Alexandre Desbiens Jun 16 '15 at 19:40
  • \$\begingroup\$ @AlexandreDesbiens why? \$\endgroup\$ – Andy Apr 8 '17 at 17:21
3
\$\begingroup\$

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 :)

\$\endgroup\$
  • \$\begingroup\$ reshapeOrtho torpedos all of the prexisting stuff I was drawing, leaving me with a blank screen...yet I can't find where in the code it's actually calling glViewport or anything that would affect my other stuff. Any advice on how to get it to work with preexisting code? These guys are too smart to make an easy to use text API... \$\endgroup\$ – Andy Apr 8 '17 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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