Java LWJGL Slick TTF模糊

时间:2013-01-06 22:50:56

标签: java lwjgl blur true-type-fonts slick2d

我在同时渲染纹理和真实字体方面遇到了一些麻烦。

以下屏幕截图描述了我的问题:

这显示了字体渲染的完美。这是在我使用纹理之前。 http://i.imgur.com/sUnoz.png

显示更改为纹理后的字体。它变得模糊不清。

http://i.imgur.com/gyhrZ.png

我不知道如何解决这个问题。我尝试过启用和禁用各种功能,但我无法理解。

这是我的代码:

GL初始化:

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
    glMatrixMode(GL_MODELVIEW);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glDisable(GL_DEPTH_TEST);
    glClearColor(0, 0, 0, 0);
    glEnable(GL_TEXTURE_2D);

精灵渲染:

        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        glPushMatrix();
        glBindTexture(GL_TEXTURE_2D, this.textureID);
        glBegin(GL_QUADS);
        {
            glTexCoord2f(0, 0);
            glVertex2f(0, 0);

            glTexCoord2f(1, 0);
            glVertex2f(this.width, 0);

            glTexCoord2f(1, 1);
            glVertex2f(this.width, this.height);

            glTexCoord2f(0, 1);
            glVertex2f(0, this.height);
        }
        glEnd();
        glPopMatrix();

纹理加载器:

    public static int loadTexture(BufferedImage image) {

    int[] pixels = new int[image.getWidth() * image.getHeight()];
    image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0,
            image.getWidth());

    ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth()
            * image.getHeight() * BYTES_PER_PIXEL); // 4 for RGBA, 3 for RGB

    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            int pixel = pixels[y * image.getWidth() + x];
            buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
            buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
            buffer.put((byte) (pixel & 0xFF)); // Blue component
            buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component.
                                                        // Only for RGBA
        }
    }

    buffer.flip(); // FOR THE LOVE OF GOD DO NOT FORGET THIS

    // You now have a ByteBuffer filled with the color data of each pixel.
    // Now just create a texture ID and bind it. Then you can load it using
    // whatever OpenGL method you want, for example:

    int textureID = glGenTextures(); // Generate texture ID
    glBindTexture(GL_TEXTURE_2D, textureID); // Bind texture ID

    // Setup wrap mode
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

    // Setup texture scaling filtering
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    // Send texel data to OpenGL
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(),
            image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    // Return the texture ID so we can bind it later again
    return textureID;
}

如果有必要,我愿意提供更多信息。 任何帮助表示感谢,提前谢谢。

1 个答案:

答案 0 :(得分:0)

我知道这个问题是在6个月前的答案时提出来的,但我只是想让其他人知道,以防他们遇到同样的问题。

我在尝试渲染图像时遇到了同样的问题,对我的修复就是在渲染完成后解开纹理。

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glPushMatrix();
glBindTexture(GL_TEXTURE_2D, this.textureID);
glBegin(GL_QUADS);
{
    glTexCoord2f(0, 0);
    glVertex2f(0, 0);

    glTexCoord2f(1, 0);
    glVertex2f(this.width, 0);

    glTexCoord2f(1, 1);
    glVertex2f(this.width, this.height);

    glTexCoord2f(0, 1);
    glVertex2f(0, this.height);
}
glEnd();
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, 0);