LibGDX FrameBuffer

一世执手 提交于 2020-04-10 08:32:30

问题


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

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?

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

回答1:


The problem here lies in this line:

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

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.

Hope this helps.



来源:https://stackoverflow.com/questions/24434236/libgdx-framebuffer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!