Antialiased text rendering with GL3?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Antialiased text rendering with GL3?

farrellf
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
Reply | Threaded
Open this post in threaded view
|

Re: Antialiased text rendering with GL3?

gouessej
Administrator
Hello

I hope that someone else will know better than me how to solve your problem with the newer text renderer. However, there is a pull request about adding GL3 support into com.jogamp.opengl.util.awt.TextRenderer on github, feel free to have a look and to contribute.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Antialiased text rendering with GL3?

farrellf
It seems that adding "caps.setSampleBuffers(true);" before creating the GLWindow does the trick. Now the text is antialiased.

But more testing leads me to a few other problems:

1. When using the "C:/Windows/Fonts/verdana.ttf" font, the number 9 renders incorrectly. The "hole" in the 9 is filled in.
2. The font rendering looks slightly less accurate than the results of the old AWT-based API. It looks as if the curves are overly simplified or overly hinted. Is there some setting for this?
2. The method "com.jogamp.graph.font.Font.getMetricWidth(text, size)" does not seem to give an accurate width. The text drawn on screen uses roughly 8% fewer pixels, as least when using the verdana.ttf font. (If i multiply the return value by 0.92f, it seems more accurate, but it's still not perfect.) Is there some better way to figure out how wide a string of 2D text will be on screen? (I'm only doing 2D text rendering.)
3. Does the new graph font rendering API have an easier way to specify a font? The old AWT-based API let you specifiy a font name and the JVM would try to map it to whatever was available. Now I have to know the file path, or bundle my own font?

Thanks,
-Farrell
Reply | Threaded
Open this post in threaded view
|

Re: Antialiased text rendering with GL3?

farrellf
Never mind, I gave up and just wrote my own shader-based text rendering code.