有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用帧缓冲区和SpriteBatch时的java纹理拉伸

我已经为此工作了两天,仍然不知道发生了什么。我正在开发一个游戏框架,我已经从Slick2D迁移到了LibGDX,我想渲染一次纹理,以节省CPU/GPU周期并提高性能。我创建了一个名为DrawSurface的类,它的主要目标是允许我绘制屏幕外的纹理,然后使用内置的LibGDXs图像类来绘制

public final class DrawSurface {

    public static interface DrawCall{
        void draw(SpriteBatch b, int width, int height);
    }

    public static Texture offscreenDraw(DrawCall c, int canvasWidth, int canvasHeight){
        FrameBuffer fbo = new FrameBuffer(Pixmap.Format.RGBA8888,(canvasWidth),(canvasHeight),true);
        SpriteBatch batch = new SpriteBatch();
                batch.setProjectionMatrix(new Matrix4().setToOrtho2D(0,0,fbo.getWidth(),fbo.getHeight()));
        fbo.begin();
        batch.begin();
        c.draw(batch,fbo.getWidth(),fbo.getHeight());
        batch.end();
        Pixmap map = ScreenUtils.getFrameBufferPixmap(0,0,fbo.getWidth(),fbo.getHeight());
        fbo.end();
        Texture t = new Texture(map);
        map.dispose();
        return t;
    }

}

这让我在绘制图像时得到了这样的结果: Glitchy Rendering

用于获取此图像的“DrawCall”是:

 Texture t = DrawSurface.offscreenDraw(new DrawSurface.DrawCall() {
            @Override
            public void draw(SpriteBatch b, int w, int h) {
                Image img = new Image(0x0000FF,w,h);
                img.setLocation(0,0);
                img.draw(b);
                for(int i = 0; i < w; i += 64){
                    for(int j = 0; j < h; j += 64){
                        Blocks.GRASS_BLOCK.setLocation(i,j);
                        Blocks.GRASS_BLOCK.draw(b);
                    }
                }
            }
        },512,512);

该图像应渲染为512x512像素的蓝色正方形,小“草”块图像也应为正方形。尺寸为16x16。不幸的是,我得到了一个扭曲的结果,我不明白为什么。因为白色的大东西(我创建的一个软件操纵杆)没有被拉伸,而“DrawsSurface”被拉伸。如果你想看看我的相机代码:

        // Constructor Above.
        this.gameWidth = gameWidth;
        this.gameHeight = gameHeight;
        this.camera = new OrthographicCamera();
        FlatPixelGame.gameCamera = this.camera;
        this.viewport = new StretchViewport(gameWidth,gameHeight,camera);
        this.viewport.apply();
        LogBot.log("Game Instance Created Size [%s,%s]",gameWidth,gameHeight);
        InputManager.getInstance().addListener(this);

        this.camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);

任何帮助都将不胜感激,我已经搜索了论坛、StackOverflow和LibGDX文档,但似乎仍然无法解决这个问题

再次提前感谢:)


共 (1) 个答案

  1. # 1 楼答案

    在使用BadLogic的LibGDX源代码进行了一些幕后工作,并读取了bug线程之后,似乎在将PixMap转换为纹理时出现了问题。渲染从文件派生的图像效果很好,但由于屏幕外视图端口与当前渲染视图端口不同,从Pixmap生成的图像可能会显示扭曲

    解决这个问题的一个快速方法是使用TextureRegion和精灵

    public static Image offscreenDraw(DrawCall c, int canvasWidth, int canvasHeight){
            FrameBuffer fbo = new FrameBuffer(Pixmap.Format.RGBA8888,canvasWidth,canvasHeight,true);
            SpriteBatch batch = new SpriteBatch();
            Matrix4 matrix = new Matrix4();
            matrix.setToOrtho2D(0, 0, canvasWidth,canvasHeight);
            batch.setProjectionMatrix(matrix);
            fbo.begin();
            batch.begin();
            c.draw(batch,canvasWidth,canvasHeight);
            batch.end();
    
            Texture t = fbo.getColorBufferTexture();
            t.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
            if(!t.getTextureData().isPrepared()){
                t.getTextureData().prepare();
            }
            fbo.end();
    
            TextureRegion r = new TextureRegion(t,0,0,canvasWidth,canvasHeight);
            return new Image(new com.badlogic.gdx.scenes.scene2d.ui.Image(new Sprite(r)));
        }