# How can I render text using the new(ish) JOGL GPU curve rendering classes?

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

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.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!

• If you are new to OpenGL, you should stick with simpler approaches to text rendering, like Bitmap fonts. Jun 16, 2015 at 19:40
• @AlexandreDesbiens why?
– Andy
Apr 8, 2017 at 17:21

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

• 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...
– Andy
Apr 8, 2017 at 17:19