天天看点

ogre笔记

1、判断模型资源是否存在

     MeshManager::getSingletonPtr()->resourceExists("sphere.mesh")

 2、手动加载纹理

     TexturePtr tex = TextureManager::getSingleton().load(fInfo.filename, "General")

 3、TexturePtr 转换 Image 方法

      TextureIntoImage( Ogre::TexturePtr pTexture, const char* pFile)

{

Ogre::HardwarePixelBufferSharedPtr tmpTexBuf = pTexture->getBuffer();

int width = pTexture->getWidth();

int height= pTexture->getHeight();

char* tmpBuf = new char[width*height*4];

Ogre::PixelBox tmpBox(width,

height,

pTexture->getDepth(),

pTexture->getFormat(),

tmpBuf);

tmpTexBuf->blitToMemory(tmpBox);

Ogre::Image image;

image.loadDynamicImage((Ogre::uchar*)tmpBox.data, tmpBox.getWidth(), tmpBox.getHeight(), tmpBox.format);

image.save(pFile);

} 4、将大图片切分成若干小图片        void ProcessCardsTexture(Ogre::String sPrefix,int nRow, int nCol, Ogre::TexturePtr ptexSrc, Ogre::String sSaveDir)

{

if (sSaveDir.empty())

return;

int nWidth = ptexSrc->getWidth() / nCol;

int nHeight = ptexSrc->getHeight() / nRow;

Ogre::PixelFormat pf = ptexSrc->getFormat();

Ogre::HardwarePixelBufferSharedPtr pixelBuffer = ptexSrc->getBuffer();

// Lock the pixel buffer and get a pixel box

pixelBuffer->lock(Ogre::HardwareBuffer::HBL_NORMAL); // for best performance use HBL_DISCARD!

const Ogre::PixelBox& pixelBox = pixelBuffer->getCurrentLock();

//

Ogre::TexturePtr ptexCard;

char szTexture[32] = "/0";

for (int i = 0; i < nRow; i++)

{

for (int j = 0; j < nCol; j++)

{

Ogre::uint8 *psrc = static_cast<Ogre::uint8*>(pixelBox.data);

// move psrc point to current data.

psrc += (nWidth *4 *nCol) *nHeight *i + j *nWidth *4;

sprintf(szTexture, "%s_%d_%d.png",sPrefix.data(), i + 1, j + 1);

Ogre::String file = sSaveDir + szTexture;

if (!QFile::exists(QString(file.data())))

{

ptexCard = Ogre::TextureManager::getSingleton().createManual(

szTexture, // name

Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,

Ogre::TEX_TYPE_2D, // type

nWidth, nHeight, // width & height

0, // number of mipmaps

pf, // pixel format

Ogre::TU_DEFAULT);

Ogre::HardwarePixelBufferSharedPtr pixelBuf = ptexCard->getBuffer();

pixelBuf->lock(Ogre::HardwareBuffer::HBL_NORMAL);

const Ogre::PixelBox& pb = pixelBuf->getCurrentLock();

Ogre::uint8 *pdesc = static_cast<Ogre::uint8*>(pb.data);

// Copy texture pixel data.

for (int x = 0; x < nHeight; x++)

{

memcpy(pdesc, psrc, nWidth *4);

pdesc += nWidth *4;

psrc += nWidth *4 *nCol;

}

pixelBuf->unlock();

Ogre::Image image;

image.loadDynamicImage((Ogre::uchar*)pb.data, pb.getWidth(), pb.getHeight(), pb.format);

image.save(file);

}

}

}

// Unlock the pixel buffer

pixelBuffer->unlock();