javaopenglcanvasjogltext-rendering

Blurry text on GLCanvas


I have a simple custom Canvas

import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.util.awt.TextRenderer;

import java.awt.*;

public class MyCanvas extends GLCanvas implements GLEventListener {

private GLU glu;

public MyCanvas() {
    this.addGLEventListener(this);
}

TextRenderer renderer;
@Override
public void init(GLAutoDrawable drawable) {
    renderer = new TextRenderer(new Font("SansSerif", Font.BOLD, 16));

    GL2 gl = drawable.getGL().getGL2();
    glu = new GLU();
    gl.glClearColor(1f, 1f, 1f, 1f);
    gl.glClearDepth(1.0f);
    gl.glEnable(GL.GL_DEPTH_TEST);
    gl.glDepthFunc(GL.GL_LEQUAL);
    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
    gl.glShadeModel(GL2.GL_SMOOTH);
}

@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
    GL2 gl = drawable.getGL().getGL2();

    if (height == 0) height = 1;
    float aspect = (float)width / height;

    gl.glViewport(0, 0, width, height);

    gl.glMatrixMode(GL2.GL_PROJECTION);
    gl.glLoadIdentity();
    glu.gluPerspective(45.0, aspect, 0.1, 100.0);

    gl.glMatrixMode(GL2.GL_MODELVIEW);
    gl.glLoadIdentity();
}

@Override
public void display(GLAutoDrawable drawable) {
    GL2 gl = drawable.getGL().getGL2();
    gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();

    gl.glTranslatef(0.0f, 0.0f, -50f);
    gl.glColor3f(0.5f, 0.5f, 0.5f);
    gl.glBegin(GL2.GL_TRIANGLES);
    gl.glVertex3f(0.0f, 1.0f, 0.0f);
    gl.glVertex3f(-1.0f, -1.0f, 0.0f);
    gl.glVertex3f(1.0f, -1.0f, 0.0f);
    gl.glEnd();

    String text = "Text";
    renderer.setColor(Color.BLACK);
    renderer.begin3DRendering();
    gl.glPushMatrix();
    gl.glTranslated(0, 0, 0);
    renderer.draw(text, 0, 0);
    renderer.flush();
    gl.glPopMatrix();
    renderer.end3DRendering();
}

@Override
public void dispose(GLAutoDrawable drawable) { }
}

And I have a simple JFrame for displaying this canvas.

import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.FPSAnimator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class App extends JFrame {

private static final int CANVAS_WIDTH = 640;
private static final int CANVAS_HEIGHT = 480;
private static final int FPS = 60;

public App() {
    GLCanvas canvas = new MyCanvas();
    canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));

    final FPSAnimator animator = new FPSAnimator(canvas, FPS, true);

    this.getContentPane().add(canvas);
    this.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            new Thread() {
                @Override
                public void run() {
                    if (animator.isStarted()) animator.stop();
                    System.exit(0);
                }
            }.start();
        }
    });
    this.setTitle("JOGL (GLCanvas)");
    this.pack();
    this.setVisible(true);
    animator.start();
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(App::new);
}
}

When I run programm I see very blurry text. enter image description here

And when I reduce text size, then text became is more and more blurry. What should I do to get a clear text?


Solution

  • The text is blurry, because the font size doesn't match the rendered size on screen and bi-linear interpolation is used to magnify the font/font-texture.

    The problem might happen because you are mixing 2D and 3D text rendering functions. You should either use begin3DRendering, draw3D and end3DRendering, where you can control the actual size of the text with the scaling parameter, or you use beginRendering, draw and endRendering where a orthographic projection is used to map pixels from the font to the screen.