LWJGL Forum

Programming => OpenGL => Topic started by: Shinmera on June 28, 2011, 21:53:11

Title: Turn Texture into BufferedImage
Post by: Shinmera on June 28, 2011, 21:53:11
Greetings,

I've been trying to wrap my mind about this for some time but can't seem to find a working solution for it.
Is there any way to turn a texture into a BufferedImage?
I suppose copying the texture data to a byte buffer with glTexImage2D and then converting that could work but I just can't seem to get a working example.

Any help would be appreciated.
Title: Re: Turn Texture into BufferedImage
Post by: CodeBunny on June 29, 2011, 00:04:51
Are you using Slick Textures? I believe they have a method that allows you to access the RGBA bytes.
Title: Re: Turn Texture into BufferedImage
Post by: Shinmera on June 29, 2011, 13:11:59
I've converted my program to use Slick textures now so I can use the getTextureData(); function.
However, I still get an empty Image. Here's the code I've been trying so far:

        //render scene to texture
        render();
        texture.bind();
        glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGB,0,0,DISPLAY_WIDTH, DISPLAY_HEIGHT,0);
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        glBindTexture(GL_TEXTURE_2D,0);
       
        //turn texture into a BufferedImage
        BufferedImage image = new BufferedImage(DISPLAY_WIDTH, DISPLAY_HEIGHT,BufferedImage.TYPE_INT_RGB);
        InputStream in = new ByteArrayInputStream(texture.getTextureData());
        try{image = ImageIO.read(in);}catch(Exception ex){ex.printStackTrace();}
Title: Re: Turn Texture into BufferedImage
Post by: jediTofu on June 30, 2011, 03:26:48
I'm betting that the array returned doesn't function like BufferedImage and InputStream expect.

Just do this for a test:

byte[] b=texture.getTextureData();
for(int i = 0; i < b.length; ++i) {
  System.out.print(i + " ");
}

Then you can kind of look at that and do any tests to see what it's returning.  It may include the alpha when calling getTextureData or have any other number of different orders for RGB:
http://slick.cokeandcode.com/javadoc-util/org/newdawn/slick/opengl/Texture.html#getTextureData()
Title: Re: Turn Texture into BufferedImage
Post by: jediTofu on June 30, 2011, 03:28:16
Also Slick-Util uses BufferedImage underneath I believe (at least to load the data), so there's probably a way to get to that directly (probably have to hack the code).
Title: Re: Turn Texture into BufferedImage
Post by: monster860 on July 28, 2012, 18:54:01
You could use glGetTexImage.
see: http://www.opengl.org/sdk/docs/man/xhtml/glGetTexImage.xml