简体   繁体   English

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

[英]how to flip a pixmap to draw to a texture in libgdx?

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. 到目前为止,我可以做到,但是现在我需要将在X轴或Y轴上翻转的像素图绘制到纹理。 However I can't find anything to do so. 但是我找不到任何办法。 The pixmap class does not provide that functionality. pixmap类不提供该功能。 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? 所以我想知道我该怎么做,是否有可能用其他Java库翻转png图像,然后从翻转的图像创建pixmap?

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;
}

Here's a solution that doesn't require the creation of a new Pixmap. 这是不需要创建新的Pixmap的解决方案。 This code can also be modified to flip a Pixmap horizontally and vertically by swapping corners of a pixmap image instead of swapping pixels on opposite sides of the image. 还可以修改此代码,以通过交换像素图图像的角而不是交换图像相对侧上的像素来水平和垂直翻转像素图。

public static void flipPixmap( Pixmap p ){
    int w = p.getWidth();
    int h = p.getHeight();
    int hold;

    //change blending to 'none' so that alpha areas will not show
      //previous orientation of image
    p.setBlending(Pixmap.Blending.None);
    for (int y = 0; y < h / 2; y++) {
        for (int x = 0; x < w / 2; x++) {
            //get color of current pixel
            hold = p.getPixel(x,y);
            //draw color of pixel from opposite side of pixmap to current position
            p.drawPixel(x,y, p.getPixel(w-x-1, y));
            //draw saved color to other side of pixmap
            p.drawPixel(w-x-1,y, hold);
            //repeat for height/width inverted pixels
            hold = p.getPixel(x, h-y-1);
            p.drawPixel(x,h-y-1, p.getPixel(w-x-1,h-y-1));
            p.drawPixel(w-x-1,h-y-1, hold);
        }
    }
    //set blending back to default
    p.setBlending(Pixmap.Blending.SourceOver);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM