OpenGL:纹理未正确对齐

时间:2018-07-23 22:08:04

标签: opengl lwjgl texture-mapping

我对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);
}

不幸的是,我最终得到这样的东西:

this

但是实际上它应该是这样的(在搅拌机中):

this

可以找到纹理:Here is the texture

因此,所有内容都是弯曲的,并且在一定程度上遵循对角线。该模型是使用Blender制作的,因此具有适当的纹理坐标。我还设法将模型加纹理加载到另一个引擎中。但是不是我的。有谁知道如何解决这个问题?

0 个答案:

没有答案