Saving dynamic textures

From Ogre Wiki

Jump to: navigation, search

To save an image add this function to your code:

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();
}

and then simply call SaveImage

e.g SaveImage(texture, "test.png"); where texture is the TexturePtr you have drawn your dynamic texture into.

Will Bricknell


Practical method with RenderTexture

The class RenderTarget contains a function "writeContentsToFile(string)" which do exactly what you want :

RenderTexture* mRenderTexture = mRoot->getRenderSystem()->createRenderTexture("depthMap", 512, 512, TEX_TYPE_2D, PF_FLOAT16_RGBA ); .... mRenderTexture->writeContentsToFile(filename);

Rockeye.

Personal tools
administration