Login  Register

Re: Create a texture from a BufferedImage

Posted by Chen norris on Apr 19, 2018; 6:01pm
URL: https://forum.jogamp.org/Create-a-texture-from-a-BufferedImage-tp2661201p4038825.html

Hello,

Maybe the way I did it can help you, I found it very nice, by using the AWTGLReadBufferUtil class :
// Here, you render what you want for the texture

// Once the scene is rendered, saving it in a texture that will be used as the texture
AWTGLReadBufferUtil awtGlReadBufferUtil = new AWTGLReadBufferUtil(gl.getGLProfile(), false);
BufferedImage bufferedImage = awtGlReadBufferUtil.readPixelsToBufferedImage(gl, false);
TextureData textureData = AWTTextureIO.newTextureData(gl.getGLProfile(), bufferedImage, true);
The false final parameter above is for rendering upside-down or not (have a try to see what fits better your needs).
And then, you may want to buffer your image in memory. This is how I did it :
gl.glBindTexture(GL.GL_TEXTURE_2D, idTexture);

// Texture parameterization
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);

// Buffering the texture
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, texture.getInternalFormat(), texture.getWidth(),
texture.getHeight(), 0, texture.getPixelFormat(), texture.getPixelType(), texture.getBuffer()); gl.glGenerateMipmap(GL.GL_TEXTURE_2D);

// Unbinding the texture
gl.glBindTexture(GL.GL_TEXTURE_2D, 0);


Just pay attention to the version of JOGL you use : I found a bug with the 2.3.1 with an unrecognized pixelFormat. This problem was solved by the 2.3.2 version of JOGL

Hope this may help you ;)