To save an image, add this function to your code:
Copy to clipboard
void SaveImage(TexturePtr TextureToSave, String filename) { HardwarePixelBufferSharedPtr readbuffer; readbuffer = TextureToSave->getBuffer(0, 0); readbuffer->lock(HardwareBuffer::HBL_NORMAL ); const PixelBox &readrefpb = readbuffer->getCurrentLock(); uchar *readrefdata = static_cast<uchar*>(readrefpb.data); Image img; img = img.loadDynamicImage (readrefdata, TextureToSave->getWidth(), TextureToSave->getHeight(), TextureToSave->getFormat()); img.save(filename); readbuffer->unlock(); }
Then simply call SaveImage()
e.g SaveImage(texture, "test.png");
where texture is the TexturePtr you have drawn your dynamic texture into.
Practical method with RenderTexture
The class RenderTarget contains a function "writeContentsToFile(string)" which does exactly what you want:
Copy to clipboard
RenderTexture* mRenderTexture = mRoot->getRenderSystem()->createRenderTexture("depthMap", 512, 512, TEX_TYPE_2D, PF_FLOAT16_RGBA ); .... mRenderTexture->writeContentsToFile(filename);
Shortest method
Copy to clipboard
void SaveImage(TexturePtr TextureToSave, String filename) { Image img; TextureToSave->convertToImage(img); img.save(filename); }