首页 > 解决方案 > OpenGL:纹理未正确对齐

问题描述

我对 OpenGL(在 LWJGL 中)和纹理映射有疑问。我正在使用加载 ARGB 图像

public static ByteBuffer toByteArray(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() * 4);
    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 >> 0) & 0xFF)); // Blue component
            buffer.put((byte) ((pixel >> 24) & 0xFF)); // Alpha component.
        }
    }
    buffer.flip();
    return buffer;
}

我正在使用上传纹理

int[] textureIds = new int[textures.size()];
GL11.glGenTextures(textureIds);
int i = 0;
for (Texture texture : textures.values()) {
    int textureId = textureIds[i++];
    GL13.glActiveTexture(GL13.GL_TEXTURE0);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
    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);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 4);

    BufferedImage data = texture.load();
    ByteBuffer bytes = Texture.toByteArray(data);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, data.getWidth(), data.getHeight(), 0, GL11.GL_RGBA,
        GL11.GL_UNSIGNED_BYTE, bytes);
    // GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
    texture.setTextureId(textureId);

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
}

不幸的是,我最终得到了这样的结果:

这个

但实际上它应该看起来像这样(在搅拌机中):

这个

可以找到纹理:这是纹理

所以一切都是弯曲的,并且有点遵循对角线。该模型是用搅拌机制作的,因此具有适当的纹理坐标。我还设法将模型和纹理加载到另一个引擎中。但不在我的。有谁知道如何解决这个问题?

标签: opengllwjgltexture-mapping

解决方案


推荐阅读