2013-04-20 84 views
0

我在LWJGL中创建了一个游戏,并且当前正在实现纹理。 (3D) 当我只使用我的方法加载一个纹理时,它工作正常,但如果我加载2纹理或更多只有最新的纹理,我加载是积极的,我只能使用该...Java - LWJGL加载纹理,仅加载我加载的最新纹理

看到代码和你就会明白:

我的方法加载纹理:

private int[] loadRTexture(String filename) { 
    int[] twh = new int[3]; 
    ByteBuffer buf = null; 
    int tWidth = 0; 
    int tHeight = 0; 

    try { 
     // Open the PNG file as an InputStream 
     InputStream in = new FileInputStream(filename); 
     // Link the PNG decoder to this stream 
     PNGDecoder decoder = new PNGDecoder(in); 

     // Get the width and height of the texture 
     tWidth = decoder.getWidth(); 
     tHeight = decoder.getHeight(); 
     twh[1] = tWidth; 
     twh[2] = tHeight; 

     // Decode the PNG file in a ByteBuffer 
     buf = ByteBuffer.allocateDirect(
       4 * decoder.getWidth() * decoder.getHeight()); 
     decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA); 
     buf.flip(); 

     in.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     System.exit(-1); 
    } 

    // Create a new texture object in memory and bind it 
    int texId = GL11.glGenTextures(); 
    GL13.glActiveTexture(GL13.GL_TEXTURE0); 
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId); 

    // All RGB bytes are aligned to each other and each component is 1 byte 
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); 

    // Create a new texture object in memory and bind it  
    // Upload the texture data and generate mip maps (for scaling) 
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0, 
      GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf); 
    GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D); 

    // Setup the ST coordinate system 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT); 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT); 

    // Setup what to do when the texture has to be scaled 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, 
      GL11.GL_NEAREST); 
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, 
      GL11.GL_LINEAR_MIPMAP_LINEAR); 
    twh[0] = texId; 
    return twh; 
} 

我存储所生成的INT texId = GL11.glGenTextures();在这个方法之后的一个数组列表中。

然后当我想渲染,这是我怎么绑定质地:

GL11.glBegin(GL11.GL_QUADS); // Start Drawing The Cube 
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, game.getResourceManager().getTextures().get(game.getResourceManager().getSpriteSheets().get(0).getTextureID())); 
    GL11.glTexCoord2f(x, y); 
    GL11.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top) 
    GL11.glTexCoord2f(x, y+celly); 
    GL11.glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top) 
    GL11.glTexCoord2f(x+cellx, y+celly); 
    GL11.glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top) 
    GL11.glTexCoord2f(x+cellx, y); 
    GL11.glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top) 

这一行:

game.getResourceManager().getTextures().get(game.getResourceManager().getSpriteSheets().get(0).getTextureID()) 

获取已生成所需的纹理INT。(它得到正确的一,我已经查过了。)

那么为什么会这样呢?

+1

通过添加glBindTexture(GL_TEXTURE_2D,0)解决;在加载纹理方法 – Gopgop 2013-04-20 16:23:23

回答

1

如果您有您的纹理加载器的麻烦,在这里尝试矿: LWJGL Textures and Strings

如果你真的不想要,你的代码比较雷,看看是否有什么不妥。

+0

中通过添加glBindTexture(GL_TEXTURE_2D,0)来解决;在加载纹理方法 – Gopgop 2013-04-24 18:07:24

+0

哦,哎呀,我不知道那是你在主线程中的注释。无论如何,如果你仍然有麻烦,你总是可以尝试我的纹理加载器。 – Flafla2 2013-04-25 15:48:57

0

在调用glBegin后无法绑定纹理。这会搞砸一切,