2012-05-06 32 views
1

所以我试图用自定义字体来绘制文本到屏幕上,我已经完成了。问题出现在同时使用纹理时。涉及真正的字体和纹理的问题LWJGL

我打开我的纹理是这样的:

INT纹理{

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

正在发生的事情是一些输入流如何变得“混淆起来”,我想要的文字就是绘图机智h我加载的纹理。

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

我搜索了这个问题,找不到任何东西。 如果你能理解我,请提前致谢。

回答

0

而不是InputStreams混合起来,它更可能是纹理绑定。 在您的纹理类中,以下行被注释掉:

// glBindTexture(GL_TEXTURE_2D,Texture);

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

您需要的纹理绑定,然后才能将图像加载到其与glTexImage2D

+0

我忘了我在取消注释代码企图掩饰这个问题了,对不起。 你知道我该如何解决我遇到的问题吗? –

+0

我们需要看到你在渲染时绑定纹理。你能证明这一点吗? – Aaron

+0

这是显示列表: 'int buttonDisplayList = glGenLists(1); \t \t glNewList(buttonDisplayList,GL_COMPILE); \t \t { \t \t \t glBegin(GL_QUADS); \t \t \t glTexCoord2f(0,0); \t \t \t glVertex2f(xlocation,ylocation); \t \t \t glTexCoord2f(1,0); \t \t \t glVertex2f((xlocation + width),ylocation); \t \t \t glTexCoord2f(1,1); glVertex2f((xlocation + width),(ylocation-height));其中,glVertex2f((xlocation + width),(ylocation-height)); glVertex2f \t \t \t glTexCoord2f(0,1); \t \t \t glVertex2f(xlocation,(ylocation-height)); \t \t \t glEnd(); \t \t} \t \t glEndList();' –