GLCanvas上的模糊文字

时间:2017-02-11 12:17:49

标签: java opengl canvas jogl text-rendering

我有一个简单的自定义画布

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) { }
}

我有一个简单的JFrame来显示这个画布。

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

当我运行程序时,我看到非常模糊的文字。 enter image description here

当我缩小文字大小时,文字变得越来越模糊。我该怎么做才能得到明确的文字?

1 个答案:

答案 0 :(得分:2)

文字模糊,因为字体大小与屏幕上的渲染大小不匹配,双线性插值用于放大字体/字体纹理。

问题可能发生,因为您正在混合2D和3D文本渲染功能。您应该使用begin3DRenderingdraw3Dend3DRendering,您可以使用缩放参数控制文本的实际大小,或者使用beginRendering,{{1} }和draw,其中使用正交投影将像素从字体映射到屏幕。