Libgdx绘制和压缩精灵在Android上为黑色

时间:2018-12-29 21:21:20

标签: android opengl-es libgdx game-development

我正在Android上为游戏渲染精灵(带有一张图像和一张桌子的游戏卡),然后将它们与PixmapPacker打包在一起。当我使用Android模拟器运行代码时,子画面在Atlas的png文件上绘制得很好,但是文件的背景是完全黑色的,并且在子画面的侧面有一些毛刺的黄色像素。在我的手机上运行相同的代码会导致几乎所有的精灵都变成全黑的png(只绘制了我的“ back.png”)。

我是否正确清除了所有缓冲区,或者我对m_fbo.begin()m_fbo.end()的顺序做错了吗?另外,我不确定是否需要在循环内每次都调用spriteBatch.begin()spriteBatch.end()

任何帮助将不胜感激。

Sprite sprite;
BitmapFont font;

Texture texture;
OrthographicCamera cam;
FrameBuffer m_fbo = null;
SpriteBatch spriteBatch;
Pixmap pixmap;
Skin skin;

public AtlasGenerator() {
    cam = new OrthographicCamera(w, h);
    cam.position.set(new Vector2(w / 2, h / 2), 1);
    cam.update();

    PixmapPacker packer = new PixmapPacker(packer_width, packer_height, Pixmap.Format.RGB565, padding, true);
    //packer.setTransparentColor(Color.BLACK); // Is this necessary?
    font = new BitmapFont();
    spriteBatch = new SpriteBatch();
    cam.update();
    spriteBatch.setProjectionMatrix(cam.combined);

    Table table = new Table(skin);
    m_fbo = new FrameBuffer(Pixmap.Format.RGB565, w, h, false);
    m_fbo.begin();

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    Gdx.gl.glClearColor(1f, 1f, 1f, 1);
    Gdx.gl.glEnable(GL20.GL_BLEND);
    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    //Gdx.gl.glColorMask(false, false, false, true);

    int cardsCounter = game.getCounter();
    for (int cardID = 0; cardID < cardsCounter; cardID++) {
        System.out.println("Generating card " + (cardID + 1) + " of " + cardsCounter);

        texture = new Texture(Gdx.files.internal(game.get(cardID).getImages().get(0).getFilename()));
        sprite = new Sprite(texture);

        /* adding data to table here...*/

        /* Rendering */
        spriteBatch.begin();
        Gdx.gl.glClear(GL_COLOR_BUFFER_BIT);
        Gdx.gl.glClearColor(1f, 1, 1, 1f);

        spriteBatch.draw(sprite, 0, h - newh, neww, newh);
        table.draw(spriteBatch, 1f);
        spriteBatch.end();

        ByteBuffer buf;
        pixmap = new Pixmap(w, h, Pixmap.Format.RGB888);
        buf = pixmap.getPixels();
        Gdx.gl.glReadPixels(1, 1, w, h, GL20.GL_RGB, GL20.GL_UNSIGNED_BYTE, buf);
        pixmap = flipPixmap(pixmap);

        // convert pixmap to RGB565
        Pixmap.Format format = Pixmap.Format.RGB565; //desired format
        if (pixmap.getFormat()!=format) { //perform conversion if necessary
            Pixmap tmp = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), format);
            tmp.drawPixmap(pixmap, 0, 0); //copy pix to tmp
            pixmap.dispose(); //dispose old pix
            pixmap = tmp; //swap values
        }

        //packer.updatePageTextures();
        //packer.updateTextureAtlas();
        //packer.updateTextureRegions();

        packer.pack(String.valueOf(cardID), pixmap);
    }
    m_fbo.end();

    System.out.println("Finished generating cards");
    System.out.println("Start generating card pack...");

    PixmapPackerIO.SaveParameters saveParameters = new PixmapPackerIO.SaveParameters();
    saveParameters.magFilter = Texture.TextureFilter.Nearest;
    saveParameters.minFilter = Texture.TextureFilter.Nearest;
    packer.pack("back", new Pixmap(Gdx.files.internal("back.png")));
    PixmapPackerIO pixmapPackerIO = new PixmapPackerIO();
    try {
        pixmapPackerIO.save(Gdx.files.external("cards.atlas"), packer, saveParameters);
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Finished generating card pack");
    packer.dispose();
    dispose();
}

1 个答案:

答案 0 :(得分:0)

我通过删除OpenGL代码并使用TextureRegion对其进行了修复:

        m_fboRegion = new TextureRegion(m_fbo.getColorBufferTexture());
        m_fboRegion.flip(false, true);
        m_fbo.begin();
        Gdx.gl.glClearColor(1f, 1f, 1f, 1);
        Gdx.gl.glClear(GL_COLOR_BUFFER_BIT);

        /* adding some data to sprite and table */

        spriteBatch.begin();
        Gdx.gl.glClear(GL_COLOR_BUFFER_BIT);
        Gdx.gl.glClearColor(1f, 1, 1, 1f);
        spriteBatch.draw(sprite, 0, h - newh, neww, newh);
        table.draw(spriteBatch, 1f);
        spriteBatch.end();

        pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, w, h);
        pixmap = flipPixmap(pixmap);

        m_fbo.end();