LibGDX帧缓冲区 [英] LibGDX FrameBuffer

查看:119
本文介绍了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应该为红色(8位),然后为绿色(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天全站免登陆