gpt4 book ai didi

java - GLCanvas 上的模糊文字

转载 作者:行者123 更新时间:2023-12-05 01:08:40 26 4
gpt4 key购买 nike

我有一个简单的自定义 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) { }
}

我有一个简单的 JFrame 来显示这个 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);
}
}

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

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

最佳答案

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

出现此问题的原因可能是您混合了 2D 和 3D 文本渲染功能。您应该使用 begin3DRenderingdraw3Dend3DRendering,您可以在其中使用缩放参数控制文本的实际大小,或者您使用 beginRenderingdrawendRendering,其中正交投影用于将像素从字体映射到屏幕。

关于java - GLCanvas 上的模糊文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42175761/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com