2013-01-06 59 views
0

我在同时呈现纹理和真正的字体时遇到了一些问题。Java LWJGL光滑TTF模糊

下面的截图描绘我的问题:

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

这将显示更改为纹理后的字体。它一切都很模糊。

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

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

这里是我的代码:

GL initialation:

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

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

回答

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