1

所以我正在尝试使用我已经完成的自定义字体将文本绘制到屏幕上。同时使用纹理时会出现问题。

我像这样加载我的纹理:

诠释纹理{

        try {
            InputStream in = new FileInputStream(filelocation);
            PNGDecoder decoder = new PNGDecoder(in);
            ByteBuffer buffer = BufferUtils.createByteBuffer(4
                    * decoder.getWidth() * decoder.getHeight());
            decoder.decode(buffer, decoder.getWidth() * 4, Format.RGBA);
            buffer.flip();
            in.close();
            //glBindTexture(GL_TEXTURE_2D, Texture);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
                    GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                    GL_NEAREST);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, decoder.getWidth(),
                    decoder.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE,
                    buffer);
            glBindTexture(GL_TEXTURE_2D, 0);
        } catch (FileNotFoundException ex) {
            System.err
                    .println("Textures are not in their correct location.");
            Display.destroy();
            System.exit(1);
        } catch (IOException ex) {
            System.err
                    .println("Textures are not in their correct location.");
            Display.destroy();
            System.exit(1);
        }
    }

我的字体是这样的

公共静态无效负载(浮动大小){

    try {
        InputStream inputStream = ResourceLoader.getResourceAsStream(filelocation);

        Font awtFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);
        awtFont = awtFont.deriveFont(size);
        fontname = new TrueTypeFont(awtFont, true);

    } catch (Exception e) {
        e.printStackTrace();
    }   
}

正在发生的事情是输入流如何“混合”,而我想要的文本是用我加载的纹理绘制的。

我在游戏循环之前从 Font.class 加载字体,纹理从它们使用的类加载,在游戏循环期间调用。

我已经用谷歌搜索了这个问题,但找不到任何东西。如果你能理解我,在此先感谢。

4

1 回答 1

0

而不是 InputStreams 变得混淆它更有可能是纹理绑定。在您的 Texture 类中,以下行被注释掉:

//glBindTexture(GL_TEXTURE_2D, Texture);

这导致纹理图像被加载到之前绑定到 GL_TEXTURE2D 的任何内容中,替换之前的纹理。

您需要先绑定纹理,然后才能使用 glTexImage2D 将图像加载到其中

于 2012-05-06T19:58:34.243 回答