Search code examples
textureslibgdxbytebufferpixmap

LibGDX Texture to ByteBuffer


I am trying to convert a Texture to a Pixmap in LibGDX so I can get all of the pixel data into a ByteBuffer for some experiments, and from what I can tell, I should be able to do this by doing the following :

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

This does seem to return a properly sized Pixmap, and the ByteBuffer does seem to be created just fine, but it is filled entirely with zeros, resulting in a blank, transparent image. This, and a couple other tests, lead me to believe that the Pixmap itself is simply being created as a fully transparent image like that, but I cannot find what may be causing that to happen. Is there some sort of limitation preventing this from working, or am I simply missing something obvious?


Solution

  • I believe that API (consumePixmap()) is meant for the Texture class to extract data out of a Pixmap when pushing the Pixmap up to the GPU.
    A Libgdx Texture object represents texture data on the GPU, so getting the underlying data to the CPU isn't generally trivial (its the "wrong" direction for a rendering API). See http://code.google.com/p/libgdx/wiki/GraphicsPixmap

    Additionally, the documentation on consumePixmap() says it requires a prepare() call before it will work.

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

    Its not clear what you're trying to accomplish, but going the other direction (byte array -> Pixmap -> Texture) and then modifying the byte array and re-doing the transformation might work.