更新像素缓存
这里提供了两种不同的更新像素缓存的方法;其中一种很简单,另外一种稍微有一些难度,不过在某些场合会表现的高效率。不过不管是选择了哪一种方法,都需要借助PixelBox对象来帽徽缓存中的图像数据(可以参照像素盒)。
blitFromMemory
从像素缓存中得到图像的简单方法是通过调用HardwarePixelBuffer::blitFromMemory方法。这里获得一个PixelBox对象并帮助你进行所有必要的定点格式转换与缩放。例如,建立一个手动纹理并把一个图片放入里面,你将要采用以下代码:
// Manually loads an image and puts the contents in a manually created texture
Image img;
img.load("elephant.png", "General");
// Create RGB texture with 5 mipmaps
TexturePtr tex = TextureManager::getSingleton().createManual( "elephant", "General", TEX_TYPE_2D, img.getWidth(), img.getHeight(), 5, PF_X8R8G8B8);
// Copy face 0 mipmap 0 of the image to face 0 mipmap 0 of the texture.
tex->getBuffer(0,0)->blitFromMemory(img.getPixelBox(0,0));
直接对缓存加锁
另外一个高级的方法来从PixelBuffer(像素缓存)中得到图像数据的方法是对缓存加锁。通过对PixelBuffer加锁,你可以不管缓存的具体格式而直接的在GPU中存取其内容。
/// Lock the buffer so we can write to it
buffer->lock(HardwareBuffer::HBL_DISCARD);
const PixelBox &pb = buffer->getCurrentLock();
/// Update the contents of pb here
/// Image data starts at pb.data and has format pb.format
/// Here we assume data.format is PF_X8R8G8B8 so we can address pixels as uint32.
uint32 *data = static_cast<uint32*>(pb.data);
size_t height = pb.getHeight();
size_t width = pb.getWidth();
size_t pitch = pb.rowPitch;
// Skip between rows of image
for(size_t y=0; y<height; ++y)
{for(size_t x=0; x<width; ++x)
{
// 0xRRGGBB -> fill the buffer with yellow pixels
data[pitch*y + x] = 0x00FFFF00;
}
}
/// Unlock the buffer again (frees it for use by the GPU)
buffer->unlock();