这个帖子:http://www.ogre3d.org/wiki/index.php/Creating_dynamic_textures
里面讲解了如何创建并使用动态创建的纹理。使用该方法,创建并使用双通道的DDS纹理。具体方法如下:
1. 创建动态纹理:
#include "OgreHardwarePixelBuffer.h" // Create the texture TexturePtr texture = TextureManager::getSingleton().createManual( "DynamicTexture", // name ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, TEX_TYPE_2D, // type 256, 256, // width & height 0, // number of mipmaps PF_FLOAT32_GR, // pixel format (双通道DDS浮点纹理) TU_DEFAULT); // usage; should be TU_DYNAMIC_WRITE_ONLY_DISCARDABLE for // textures updated very often (e.g. each frame) // Get the pixel buffer HardwarePixelBufferSharedPtr pixelBuffer = texture->getBuffer(); // Lock the pixel buffer and get a pixel box pixelBuffer->lock(HardwareBuffer::HBL_NORMAL); // for best performance use HBL_DISCARD! const PixelBox& pixelBox = pixelBuffer->getCurrentLock(); float* pDest = static_cast<float*>(pixelBox.data); // Fill in some pixel data. This will give a semi-transparent blue, // but this is of course dependent on the chosen pixel format. for (size_t j = 0; j < 256; j++) for(size_t i = 0; i < 256; i++) { *pDest++ = *(gChannel++); // gChannel:G通道数据数组指针 *pDest++ = *(rChannel++); // rChannel:R通道数据数组指针 } // Unlock the pixel buffer pixelBuffer->unlock();
2. 保存动态创建的DDS纹理(Failed!):
创建完纹理后想用:http://www.ogre3d.org/wiki/index.php/Saving_dynamic_textures
中的方法保存DDS纹理,以查看创建是否正确。代码如下:
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); // 此处抛出异常:DDS encoding not supported! readbuffer->unlock(); }但是,程序执行到img.save(filename)的时候出现异常,提示“不支持DDS编码”。Ogre Forum上面搜到这个贴:https://www.ogre3d.org/forums/viewtopic.php?f=5&t=38606&start=0 里面有人说:“Ogre's DDS codec does not support encoding DDS, only decoding.”查看ogreddscodec.cpp中的异常抛出函数确实是这样的:void DDSCodec::codeToFile(MemoryDataStreamPtr& input, const String& outFileName, Codec::CodecDataPtr& pData) const { OGRE_EXCEPT(Exception::ERR_NOT_IMPLEMENTED, "DDS encoding not supported", "DDSCodec::codeToFile" ) ; }
3. 使用动态纹理:
使用上面创建的动态纹理:
MaterialPtr material = MaterialManager::getSingleton().getByName("Test"); material.getPointer()->getTechnique(0)->getPass(0)->getTextureUnitState("DynamicTextureTU")->setTextureName("DynamicTextureTU");材质Test的定义:
material Test { technique { pass { lighting off scene_blend alpha_blend depth_write off texture_unit DynamicTextureTU { // blank } } } }
PS:真正意义的处女贴!