0

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

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

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

这显示了更改为纹理后的字体。一切都变得模糊。

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

我不知道如何解决这个问题。我试过启用和禁用各种东西,但我无法弄清楚。

这是我的代码:

总帐初始化:

    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;
}

如有必要,我愿意提供更多信息。任何帮助表示赞赏,在此先感谢。

4

1 回答 1

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);
于 2013-07-14T05:54:55.177 回答