Skip to main content

History: Creating dynamic textures

Preview of version: 4

This is a minimal example of how you can create dynamic textures (and programmatically create materials using them). It doesn't show how to use ManualResourceLoaders, though.

Copy to clipboard
#include "OgreHardwarePixelBuffer.h"
Copy to clipboard
// 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_BYTE_BGRA, // pixel format 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(); uint8* pDest = static_cast<uint8*>(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++ = 255; // B *pDest++ = 0; // G *pDest++ = 0; // R *pDest++ = 127; // A } // Unlock the pixel buffer pixelBuffer->unlock(); // Create a material using the texture MaterialPtr material = MaterialManager::getSingleton().create( "DynamicTextureMaterial", // name ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); material->getTechnique(0)->getPass(0)->createTextureUnitState("DynamicTexture"); material->getTechnique(0)->getPass(0)->setSceneBlending(SBT_TRANSPARENT_ALPHA);

See also


--DWORD 14:58, 29 Apr 2005 (CDT)

History

Information Version
Thu 31 of Oct, 2013 16:21 GMT-0000 RabbitB Added line that corrects for non-contiguous texture data; detects the number of bytes to skip (if any) between rows of pixels, and jumps over them. 5
Wed 19 of Jan, 2011 00:43 GMT-0000 Beauty fixed broken links 4
Sat 02 of Jan, 2010 05:33 GMT-0000 jacmoe 3
Thu 24 of Dec, 2009 17:55 GMT-0000 jacmoe 2
Sat 14 of Mar, 2009 17:18 GMT-0000 OgreWikiBot Robot: Cosmetic changes 1