Libgdx 在 Android 上绘制和打包精灵是黑色的
Libgdx draw and pack sprites are black on Android
我正在 Android 上为游戏渲染 sprite(带有一张图像和 table 的游戏卡),然后用 PixmapPacker 将它们打包。当我 运行 使用 Android 模拟器的代码时,精灵在图集的 png 文件上绘制得很好,但是文件的背景是完全黑色的,侧面有一些毛刺的黄色像素精灵。 运行 我的 phone 上的相同代码导致几乎完全黑色的 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();
}
我通过删除 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();
我正在 Android 上为游戏渲染 sprite(带有一张图像和 table 的游戏卡),然后用 PixmapPacker 将它们打包。当我 运行 使用 Android 模拟器的代码时,精灵在图集的 png 文件上绘制得很好,但是文件的背景是完全黑色的,侧面有一些毛刺的黄色像素精灵。 运行 我的 phone 上的相同代码导致几乎完全黑色的 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();
}
我通过删除 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();