GLCanvas 上的文本模糊

问题描述 投票:0回答:2

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

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

当我减小文本大小时,文本变得越来越模糊。我应该怎样做才能获得清晰的文本?

java opengl canvas jogl text-rendering
2个回答
2
投票

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

出现此问题的原因可能是您混合了 2D 和 3D 文本渲染函数。您应该使用

begin3DRendering
draw3D
end3DRendering
,您可以使用缩放参数控制文本的实际大小,或者使用
beginRendering
draw
endRendering
,其中正交投影是用于将像素从字体映射到屏幕。


0
投票

在渲染到绘图坐标中绘图区域较小(源像素数量较少)的表面时,我也遇到了很多模糊文本的麻烦。最后通过增大字体大小和减小小字体的比例来解决它。 也许它对某人有帮助。

    /**
 * Draws a string using TextRenderer.draw using native GL coordinates,
 * usually setup to represent pixels on AEChip. Embedded newlines are not
 * rendered as additional lines.
 * <p>
 * If the TextRenderer does not exist for DrawGL, it is created. This can
 * cause problems if it is out of context, so it might be necessary to
 * create it.
 *
 * @param fontSize typically 12 to 36
 * @param x x position (0 at left)
 * @param y y position (0 at bottom)
 * @param alignmentX 0 for left aligned, .5 for centered, 1 for right
 * @param color, e.g. Color.red
 * @param s the string to draw
 * @return the bounds of the text
 */
public static Rectangle2D drawString(int fontSize, float x, float y, float alignmentX, Color color, String s) { // TODO gl is not actually used
    float scale = 1;
    if (fontSize < 12) {
        fontSize *= 4;
        scale = .25f;
    }
    TextRenderer textRenderer=new TextRenderer(new Font("SansSerif", Font.PLAIN, fontSize), true, true);
    textRenderer.begin3DRendering();
    textRenderer.setColor(color);
    Rectangle2D r = textRenderer.getBounds(s);
    r.setRect(r.getX(), r.getY(), r.getWidth() * scale, r.getHeight() * scale); // adjust bounds for actual drawing scale of text
    textRenderer.draw3D(s, (int) (x - alignmentX * r.getWidth()), (int) (y), 0, scale);
    textRenderer.end3DRendering();
    return r;
}
© www.soinside.com 2019 - 2024. All rights reserved.