I want to save a texture (render to texture) to a png, but the image data is always null although I can use the texture.getImage to set it to a niftygui image element, and it displays correctly
// texture.getImage().getData(0) is null at this point, but it still works
NiftyImage img = new NiftyImage(nifty.getRenderEngine(),new RenderImageJme(texture));
I generate the texture like this:
Camera offCamera = new Camera(width,height);
offView = renderManager.createPreView("Offscreen View", offCamera);
offView.setClearFlags(true, true, true);
offView.setBackgroundColor(ColorRGBA.DarkGray);
// create offscreen framebuffer
FrameBuffer offBuffer = new FrameBuffer(width,height, 1);
//setup framebuffer's cam
offCamera.setFrustumPerspective(45f, 1f, 1f, 1000f);
offCamera.setLocation(new Vector3f(5f, 5f, 5f));
offCamera.lookAt(new Vector3f(0f, 0.5f, 0f), Vector3f.UNIT_Y);
//setup framebuffer's texture
offTex = new Texture2D(width,height, Format.RGBA8);
offTex.setMinFilter(Texture.MinFilter.Trilinear);
offTex.setMagFilter(Texture.MagFilter.Bilinear);
//setup framebuffer to use texture
offBuffer.setDepthBuffer(Format.Depth);
offBuffer.setColorTexture(offTex);
//set viewport to render to offscreen framebuffer
offView.setOutputFrameBuffer(offBuffer);
offScreenNode= new Node("OffscreenNode");
offView.attachScene(offScreenNode);
offScreenNode.updateLogicalState(0.1f);
offScreenNode.updateGeometricState();
//offTex.getImage().setUpdateNeeded();
offTex.getImage().getData(0);<<------------------------- returns null
if your texture is from a frame buffer the data is on the GPU, and null on the CPU.
You need to call the renderer.readFrameBuffer method to copy the data in your byteBuffer.
seems like preparing the offscreen renderer and saving the texture straight after does not work
but if I save the texture before releasing the offscreenrenderer(later on) then the framebuffer contains the actual data and the texture is correctly save
Now you mention it I had a similar issue that I didn’t really sort out.
I worked around it by reading my frame buffer after the app did a few iterations on the render cycle (I waited like 10 frames). that’s a nasty workaround but I didn’t want to be sidetracked.
Maybe you hit the same kind of issue. @Momoko_Fan any idea on this?