gpt4 book ai didi

Java - LWJGL 加载纹理,只有我加载的最新纹理有效

转载 作者:行者123 更新时间:2023-12-01 14:39:31 24 4
gpt4 key购买 nike

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

看代码你就明白了:

我加载纹理的方法:

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();在此方法之后的数组列表中。

然后当我想要渲染时,这就是我绑定(bind)纹理的方式:

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

获取生成的所需纹理整数..(它获取正确的纹理整数,我已检查过。)

那么为什么会发生这种情况呢?

最佳答案

如果您的纹理加载器遇到问题,请在这里尝试我的: LWJGL Textures and Strings

如果你真的不想,请将你的代码与我的代码进行比较,看看是否有任何问题。

关于Java - LWJGL 加载纹理,只有我加载的最新纹理有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16120492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com