实验七 记两个Ogre中的小工具
这几天整一个地形编辑器,越想越美,想了太多功能,结果自然是把自己弄的焦头烂额。。。还好目前似乎拔出几根线头,一切正在朝好的方向前进!yeah!
静下来,好好整理一下收获,顺手先记两个Ogre中的小工具,热手:
1,
将数字转换为String:
StringConverter::toString( StaticEntityIndex++ ), sValue );
(很sb地干了件事,自己去写这个类,到头来才晕晕乎乎地想起那里见过,Orz。。。)
2,
将任意Texture保存为图片:
盗来的,哼哼~~~
稍微完善下吧,注意加上
Lib:
RenderSystem_Direct3D9_d.lib d3dx9.lib d3d9.lib
Include:(如果编译器说找不到,自己手动到Ogre目录下拷过去)
#include <OgreD3D9Texture.h> #include <OgreD3D9Mappings.h>详细代码如下:
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; }
保存文件名的时候注意,要加上后缀,支持相当广泛
比如
_saveTextureToLocal(layer0,"First.dds");
感谢万能的开源组织吧!