1

我一直在尝试使用 LWJGL 构建一个小程序,只是为了自学一些图形,当我尝试将文本叠加到我正在绘制的 2D 场景上时(这是一个不断反弹的框窗口)文本变得模糊,框反复闪烁 - http://imgur.com/UDelj1d仍然很奇怪,如果我复制 font.drawString 行但让它显示一个单词而不是 co-ords 它不会闪烁,但仍然模糊 - http://imgur.com/3VkTSXH

任何人都可以帮助我了解为什么会发生这种情况以及我将如何解决它?我非常精通Java,但这是我第一次尝试任何面向图形的东西。

import java.nio.FloatBuffer;
import java.text.DecimalFormat;

import org.lwjgl.LWJGLException;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;

import org.lwjgl.BufferUtils;

import static org.lwjgl.opengl.GL11.*;


public class TimerDemo
{
//time in ms since the last frame was rendered
private static long lastFrame;

//current time in ms
private static long getTime()
{
    return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}

//gets the difference between the current time and lastFrame, then updates lastFrame
private static double getDelta()
{
    long currentTime = getTime();
    double delta = (double) (currentTime - lastFrame);
    lastFrame = getTime();
    return delta;
}


private static UnicodeFont font;
private static void setUpFonts()
{
    java.awt.Font awtFont = new java.awt.Font("Arial", java.awt.Font.PLAIN, 12);
    font = new UnicodeFont(awtFont);
    font.getEffects().add(new ColorEffect(java.awt.Color.white));
    font.addAsciiGlyphs();

    try
    {
      font.loadGlyphs();
    } 
    catch (SlickException e)
    {
        e.printStackTrace();
    }
}

public static void main(String[] args)
{
    try
    {
        Display.setDisplayMode(new DisplayMode(680, 480));
        Display.setTitle("Timer Demo");
        Display.create();
    }
    catch (LWJGLException e)
    {
        e.printStackTrace();
        Display.destroy();
        System.exit(1);
    }

    setUpFonts();

    //position
    int x = 100;
    int y = 100;

    //velocity
    int dx = 1;
    int dy = 1;

    //ensure lastframe is updated
    lastFrame = getTime();

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 680, 480, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);


    while (!Display.isCloseRequested())
    {
        // Render

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();

        //change in position
        double delta = getDelta();

        if(x + 30 >= 680)
            dx = -1;
        else if (x <= 0)
            dx = 1;

        if(y + 30 >= 480)
            dy = -1;
        else if (y <= 0)
            dy = 1;

        x += delta * dx * 0.1;
        y += delta * dy * 0.1;

        glRecti(x, y, x + 30, y + 30);

        font.drawString(100, 10, "x is " + x + " y is " + y);


        Display.update();
        Display.sync(60);
        }

    Display.destroy();
    System.exit(0);
}
}
4

1 回答 1

0

打电话glEnable(GL_TEXTURE_2D);font.drawString(100, 10, "x is " + x + " y is " + y); glDisable(GL_TEXTURE_2D);而不是仅仅打电话font.drawString(100, 10, "x is " + x + " y is " + y);

于 2013-11-19T09:54:30.020 回答