如何翻转像素图以绘制到 libgdx 中的纹理?

2022-01-12 00:00:00 textures java libgdx flip

所以我要做的是通过将像素图绘制到纹理来为我的游戏生成背景图像.到目前为止,我可以做到这一点,但现在我需要将在 X 或 Y 轴上翻转的像素图绘制到纹理上.但是我找不到任何事情要做.pixmap 类不提供该功能.然后我想我可以将翻转的纹理区域绘制到纹理上,但到目前为止我还没有找到如何做到这一点.所以我想知道我该如何做这样的事情,是否可以使用其他 java 库翻转 png 图像,然后从翻转的图像创建像素图?

So what I'm trying to do is to generate a background image for my game by drawing pixmaps to a texture. So far I can do that, but now I need to draw the pixmaps flipped in the X or Y axis to the texture. However I can't find anything to do so. The pixmap class does not provide that functionality. Then I thought I could draw a flipped texture region to a texture, but so far I haven't found how to do so. So I was wondering how can I do such a thing, would it be possible to flip a png image with other java libraries and then create a pixmap from that flipped image?

推荐答案

除了迭代像素,我也没有看到其他选项:

I also don't see other option except to iterate the pixels:

public Pixmap flipPixmap(Pixmap src) {
    final int width = src.getWidth();
    final int height = src.getHeight();
    Pixmap flipped = new Pixmap(width, height, src.getFormat());

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            flipped.drawPixel(x, y, src.getPixel(width - x - 1, y));
        }
    }
    return flipped;
}

相关文章