Login  Register

Antialiased text rendering with GL3?

Posted by farrellf on May 05, 2020; 4:35am
URL: https://forum.jogamp.org/Antialiased-text-rendering-with-GL3-tp4040593.html

When using GL2, I get nice antialiased text when using the old "com.jogamp.opengl.util.awt.TextRenderer" classes, etc. I'm trying to port all of my code over to OpenGL 3.2 Core Profile, so now it seems like I should use the newer "com.jogamp.graph.curve.opengl.TextRegionUtil" classes, etc. I found this demo code by Xerxes:

https://github.com/xranby/jogamp-forum-examples/blob/master/src/main/java/xranby/com/gudinna/JogAmpGraphAPITextDemo.java

It works nicely, but there does not appear to be any antialiasing (in his screenshot, or on my PC when I run it.) There is a "sampleCount" variable set to 4, but it seems to have no affect. Changing it to 16 has no visible affect.

Is it possible to render antialiased text with JOGL when using modern (core profile) OpenGL?

Here is my code, which is a simplified version of Xerxes' code:

public class JogAmpGraphAPITextDemo {

	public static void main(String[] args) {

		GLCapabilities caps = new GLCapabilities(GLProfile.get(GLProfile.GL3));
		caps.setAlphaBits(4);
		GLWindow glWindow = GLWindow.create(caps);
		glWindow.setSize(800, 400);
		glWindow.setVisible(true);

		glWindow.addGLEventListener(new GLEventListener() {

			TextRegionUtil textUtil;
			RenderState renderState;
			RegionRenderer textShader;
			Font font;
			int[] sampleCount = new int[] { 4 }; // for antialiasing? seems to have no affect.

			@Override public void init(GLAutoDrawable drawable) {

				GL3 gl = drawable.getGL().getGL3();
				gl.setSwapInterval(1);
				gl.glEnable(GL.GL_BLEND);

				try {
//					font = FontFactory.get(new File("C:/Windows/Fonts/georgia.ttf"));
//					font = FontFactory.get(new File("C:/Windows/Fonts/consola.ttf"));
					font = FontFactory.get(new File("C:/Windows/Fonts/verdana.ttf"));
				} catch (IOException e) {
					System.exit(1);
				}

				textUtil = new TextRegionUtil(0);

				renderState = RenderState.createRenderState(SVertex.factory());
				renderState.setColorStatic(1.0f, 0.0f, 0.0f, 1.0f); // red

				textShader = RegionRenderer.create(renderState, RegionRenderer.defaultBlendEnable,
						RegionRenderer.defaultBlendDisable);
				textShader.init(gl, 0);
				textShader.enable(gl, false);

			}

			@Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {

				textShader.reshapeOrtho(width, height, -1, 1);

			}

			@Override public void display(GLAutoDrawable drawable) {

				GL3 gl = drawable.getGL().getGL3();
				gl.glClearColor(0, 0, 0, 1);
				gl.glClear(GL.GL_COLOR_BUFFER_BIT);

				// animate a 10 second rotation
				PMVMatrix matrix = textShader.getMatrix();
				matrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
				matrix.glLoadIdentity();
				matrix.glTranslatef(400, 200, 0);
				matrix.glRotatef(System.currentTimeMillis() % (360 * 10) / 10, 0, 0, 1);

				// draw some text
				textShader.enable(gl, true);
				textUtil.drawString3D(gl, textShader, font, 30, "Hello world!", null, sampleCount);
				textShader.enable(gl, false);

			}

			@Override public void dispose(GLAutoDrawable drawable) {

				// free GPU resources, the JVM can't do this automatically
				GL3 gl = drawable.getGL().getGL3();
				renderState.destroy(gl);

			}

		});

		Animator animator = new Animator();
		animator.add(glWindow);
		animator.start();

		glWindow.addWindowListener(new WindowAdapter() {
			@Override public void windowDestroyed(WindowEvent e) {
				animator.stop();
				System.exit(0);
			}
		});

	}

}
Thanks, -Farrell