Ogre 中将Texture转成本地任意格式图片

来源:互联网 发布:淘宝客双十一怎么赚钱 编辑:程序博客网 时间:2024/06/05 17:26

bool _saveTextureToLocal(Ogre::TexturePtr texPtr, const Ogre::String& fileName)
    {
        Ogre::HardwarePixelBufferSharedPtr tmpTexBuf = texPtr->getBuffer();
        int width = texPtr->getWidth();
        int height= texPtr->getHeight();

        char* tmpBuf = new char[width*height*4];
        Ogre::PixelBox tmpBox(width,
            height,
            texPtr->getDepth(),
            texPtr->getFormat(),
            tmpBuf);
        tmpTexBuf->blitToMemory(tmpBox);

        TexturePtr tmpTexPtr = TextureManager::getSingleton().createManual ( fileName, "General",
            TEX_TYPE_2D, texPtr->getWidth(), texPtr->getHeight(), 1, 0, PF_A8B8G8R8 );
        tmpTexPtr->load();

        HRESULT hr;
        IDirect3DSurface9 *pDstSurface = 0;
        Ogre::D3D9Texture *d3dTex = reinterpret_cast< D3D9Texture * >(tmpTexPtr.get());
        RECT dstRC = {0, 0, d3dTex->getWidth(), d3dTex->getHeight()};

        if( FAILED( hr = d3dTex->getNormTexture()->GetSurfaceLevel(0, &pDstSurface) ) )
        {
            return false;
        }

        size_t rowWidth;
        if (PixelUtil::isCompressed(tmpBox.format))
        {
            // D3D wants the width of one row of cells in bytes
            if (tmpBox.format == PF_DXT1)
            {
                // 64 bits (8 bytes) per 4x4 block
                rowWidth = (tmpBox.rowPitch / 4) * 8;
            }
            else
            {
                // 128 bits (16 bytes) per 4x4 block
                rowWidth = (tmpBox.rowPitch / 4) * 16;
            }
        }
        else
        {
            rowWidth = tmpBox.rowPitch * PixelUtil::getNumElemBytes(tmpBox.format);
        }

        if( FAILED( hr = D3DXLoadSurfaceFromMemory(
            pDstSurface, NULL, &dstRC, tmpBox.data,
            Ogre::D3D9Mappings::_getPF( tmpBox.format),
            rowWidth, 0,
            &dstRC, D3DX_FILTER_TRIANGLE | D3DX_FILTER_DITHER, 0)) )
        {
            SAFE_RELEASE(pDstSurface);
            return false;
        }

        Ogre::HardwarePixelBufferSharedPtr tmpDesTexBuf = tmpTexPtr->getBuffer();
        char* tmpDesBuf = new char[width*height*4];
        Ogre::PixelBox tmpDesBox(width,
            height,
            tmpTexPtr->getDepth(),
            tmpTexPtr->getFormat(),
            tmpDesBuf);
        tmpDesTexBuf->blitToMemory(tmpDesBox);
        Ogre::Image image;
        image.loadDynamicImage((Ogre::uchar*)tmpDesBox.data, tmpDesBox.getWidth(), tmpDesBox.getHeight(), tmpDesBox.format);
        image.save(fileName);

        delete [] tmpDesBuf;
        delete [] tmpBuf;

 
        tmpTexPtr.setNull();

        return true;
    }

原创粉丝点击