【问题标题】:Drawing text to display绘制要显示的文本
【发布时间】:2015-05-30 17:59:28
【问题描述】:

我正在尝试在this tutorial 之后在 LWJGL 中的屏幕上绘制文本。问题是当我在屏幕上绘制文本时,它会完美地绘制但是当我用文本绘制一个四边形时,四边形不显示。

游戏类

package moa.bermudez;

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

import java.awt.Font;
import java.io.InputStream;

import moa.bermudez.utils.Artist;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.TrueTypeFont;

public class Game 
{
public static final int WIDTH = 320;
public static final int HEIGHT = WIDTH * 9 / 16;
public static final int SCALE = 3;
public static final String NAME = "----";
public static final float VERSION = 0.1f;
public static final int MAX_FPS = 60;

private TrueTypeFont font;
private boolean fontAS = true;

public Game() 
{
    try 
    {
        Display.setDisplayMode(new DisplayMode(WIDTH * SCALE, HEIGHT * SCALE));
        Display.setTitle(NAME + " " + VERSION);
        Display.create();
    } catch(LWJGLException e)
    {
        e.printStackTrace();
        Display.destroy();
    }

    init();
    while(!Display.isCloseRequested())
    {
        setCamera();
        update();
        Display.sync(MAX_FPS);
    }

    Display.destroy();
}

public void update()
{       
    glClear(GL_COLOR_BUFFER_BIT);

    font.drawString(0, 0, "This text is showing");
    Artist.drawQuad(0, 0, 100, 100);    // This is not showing.

    Display.update();
}

public void setCamera()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glViewport(0, 0, WIDTH * SCALE, HEIGHT * SCALE);
    glOrtho(0, WIDTH * SCALE, HEIGHT * SCALE, 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

public void init()
{
    try {
        InputStream inputStream = moa.bermudez.fonts.Font.DEFAULT_FONT;

        Font awtFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);
        awtFont = awtFont.deriveFont(24f); 
        font = new TrueTypeFont(awtFont, fontAS);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args)
{
    new Game();
}
}

艺术家的 DrawQuad 方法

public static void drawQuad(float x, float y, float width, float height)
{
    glBegin(GL_QUADS);

        glColor3d(0, 0, 1);
        glVertex2f(x, y);
        glVertex2f(x + width, y);
        glVertex2f(x + width, y + height);
        glVertex2f(x, y + height);

    glEnd();
}

【问题讨论】:

    标签: java lwjgl slick


    【解决方案1】:

    经过搜索,我从this stackoverflow post找到了解决这个问题的方法

    不在 initGL 中调用 glEnable(GL_BLEND) 而是在绘制文本时调用 glEnable(GL_BLEND) 应该可以正常工作。

    glEnable(GL_BLEND);
    font.drawString(0, 0, "This is a text that should appear");
    glDisable(GL_BLEND);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 2015-03-14
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多