LibGDX 帧缓冲区 [英] LibGDX FrameBuffer

查看:31
本文介绍了LibGDX 帧缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一款游戏,您可以在其中使用零件建造宇宙飞船,然后将其飞来飞去等等.

I'm trying to make a game where you build a spaceship from parts, and fly it around and such.

我想用一系列组件(例如来自 TextureAtlas)创建飞船.我想从它的组件纹理中绘制船,然后将绘制的船保存为一个大纹理.(所以我不必绘制 50 个组件纹理,只需绘制一个船舶纹理.最好的方法是什么?

I would like to create the ship from a series of components (from a TextureAtlas for instance). I'd like to draw the ship from it's component textures, and then save the drawn ship as one large Texture. (So i don't have to draw 50 component textures, just one ship texture. What would be the best way to go about this?

我一直在尝试使用 FrameBuffer.我已经能够将组件绘制到纹理上,并将纹理绘制到屏幕上,但无论我尝试什么,纹理最终都会以帧缓冲区大小的纯色背景结束.就像 clear 命令无法透明地清除一样.这是我目前的绘图代码.这艘船只是一个 Sprite,我将 FrameBuffer 纹理保存到其中.

I've been trying to do so using a FrameBuffer. I've been able to draw the components to a Texture, and draw the texture to the screen, but no matter what I try the Texture ends up with a solid background the size of the frame buffer. It's like the clear command can't clear with transparency. Here's the drawing code I have at the moment. The ship is just a Sprite which i save the FrameBuffer texture to.

public void render(){

    if (ship == null){
        int screenwidth = Gdx.graphics.getWidth();
        int screenheight = Gdx.graphics.getHeight();

        SpriteBatch fb = new SpriteBatch();

        FrameBuffer fbo = new FrameBuffer(Format.RGB888, screenwidth, screenheight, false);

        fbo.begin();

        fb.enableBlending();
        Gdx.gl.glBlendFuncSeparate(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA, GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA);

        Gdx.gl.glClearColor(1, 0, 1, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        fb.begin();

        atlas.createSprite("0").draw(fb);

        fb.end();

        fbo.end();

        ship = new Sprite(fbo.getColorBufferTexture());
        ship.setPosition(0, -screenheight);
    }

    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();

    batch.enableBlending();
    batch.setBlendFunction(GL20.GL_ONE, GL20.GL_ZERO);
    ship.draw(batch);
    batch.end();
}

推荐答案

这里的问题出在这行:

 FrameBuffer fbo = new FrameBuffer(Format.RGB888, screenwidth, screenheight, false);

特别是 Format.RGB888.这行是说您的 FrameBuffer 应该是 Red(8 位),然后是 Green(8 位),然后是 Blue(8 位).但是请注意,这种格式没有任何 Alpha 位(透明度).为了从帧缓冲区中获得透明度,您可能希望使用 Format.RGBA8888,其中包含额外的 8 位用于 Alpha.

specifically with Format.RGB888. This line is saying that your FrameBuffer should be Red (8 bits) followed by Green (8 bits) followed by Blue (8 bits). Notice however, that this format doesn't have any bits for Alpha (transparency). In order to get transparency out of your frame buffer, you probably instead want to use the Format.RGBA8888, which includes an additional 8 bits for Alpha.

希望这会有所帮助.

这篇关于LibGDX 帧缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆