LibGDX texture for ByteBuffer

I am trying to convert a texture in Pixmap to LibGDX so that I can get all the pixel data in ByteBuffer for some experiments, and from what I can say, I would have to do this by doing the following:

Pixmap pixmap = textureName.getTexture().getTextureData().consumePixmap(); ByteBuffer data = pixmap.getPixels(); 

It seems that it returns a correctly matched Pixmap, and the ByteBuffer seems to be created just fine, but is filled entirely with zeros, which leads to an empty transparent image. This and a few more tests led me to think that Pixmap itself is simply created as a completely transparent image, but I can not find what could be the reason for this. Are there any restrictions that prevent this from working, or am I just missing something obvious?

+4
source share
2 answers

I believe the API ( consumePixmap() ) is for the Texture class to retrieve data from the Pixmap when the Pixmap is clicked on the GPU.
The Libgdx Texture object represents texture data on the GPU, so getting basic data from the CPU is usually not trivial (its "wrong" direction for the rendering API). See http://code.google.com/p/libgdx/wiki/GraphicsPixmap

In addition, the consumePixmap() documentation says it requires a << 25> call before it works.

To extract pixel information from a texture, you create a FrameBuffer object, render the texture, and extract pixels (see ScreenUtils ).

It is not clear what you are trying to accomplish, but going in the other direction (byte array → Pixmap → Texture ), then change the byte array and repeat the conversion.

+7
source

I had a similar problem when I wanted to create a PNG level created in my level editor. The level is made of tiles placed by the creator, then the entire level is saved as .png, which will be used in the game.

Decided it like P.T. explains. I hope this is helpful to others who are facing the same problem.

And in your application configuration: cfg.useGL20 = true; GL20 must be able to create FrameBuffer

  public boolean exportLevel(){ //width and height in pixels int width = (int)lba.getPrefWidth(); int height = (int)lba.getPrefHeight(); //Create a SpriteBatch to handle the drawing. SpriteBatch sb = new SpriteBatch(); //Set the projection matrix for the SpriteBatch. Matrix4 projectionMatrix = new Matrix4(); //because Pixmap has its origin on the topleft and everything else in LibGDX has the origin left bottom //we flip the projection matrix on y and move it -height. So it will end up side up in the .png projectionMatrix.setToOrtho2D(0, -height, width, height).scale(1,-1,1); //Set the projection matrix on the SpriteBatch sb.setProjectionMatrix(projectionMatrix); //Create a frame buffer. FrameBuffer fb = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false); //Call begin(). So all next drawing will go to the new FrameBuffer. fb.begin(); //Set up the SpriteBatch for drawing. sb.begin(); //Draw all the tiles. BuildTileActor[][] btada = lba.getTiles(); for(BuildTileActor[] btaa: btada){ for(BuildTileActor bta: btaa){ bta.drawTileOnly(sb); } } //End drawing on the SpriteBatch. This will flush() any sprites remaining to be drawn as well. sb.end(); //Then retrieve the Pixmap from the buffer. Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0, width, height); //Close the FrameBuffer. Rendering will resume to the normal buffer. fb.end(); //Save the pixmap as png to the disk. FileHandle levelTexture = Gdx.files.local("levelTexture.png"); PixmapIO.writePNG(levelTexture, pm); //Dispose of the resources. fb.dispose(); sb.dispose(); 

Note. I'm new to LibGDX, so this may not be the best way to do this.

+7
source

All Articles