Dynamic Texture + UIImage

Discussion of issues specific to mobile platforms such as iOS, Android, Symbian and Meego.
DenisY
Gnoblar
Posts: 1
Joined: Thu Jul 31, 2014 12:59 pm

Dynamic Texture + UIImage

Post by DenisY »

I want to create simple dynamic texture (main goal to do it from UIImage, but now I want to understand more simple examples). I started with simple code suggested by Ogre team:

Code: Select all

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
    }
 
    pDest += pixelBox.getRowSkip() * Ogre::PixelUtil::getNumElemBytes(pixelBox.format);
}
 
// 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);

entity->getSubEntity(0)->setMaterial(material);
where entity is just a plane.Nothing happens on the screen as result.
Then I tried to create texture with one pixel:

Code: Select all

TexturePtr texture = TextureManager::getSingleton().createManual(
      "myTexture", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
      TEX_TYPE_2D, 1, 1,
      0, PF_A8R8G8B8, TU_DYNAMIC_WRITE_ONLY);

Ogre::HardwarePixelBufferSharedPtr buffer = texture->getBuffer();
unsigned char color[4] = {255, 255, 255, 255};
PixelBox pb(1, 1, 1, PF_A8R8G8B8, &color);
buffer->blitFromMemory(pb);

// Create a material using the texture
MaterialPtr material = MaterialManager::getSingleton().create(
    "DynamicTextureMaterial", // name
    ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
 
material->getTechnique(0)->getPass(0)->createTextureUnitState("myTexture");
material->getTechnique(0)->getPass(0)->setSceneBlending(SBT_TRANSPARENT_ALPHA);

entity->getSubEntity(0)->setMaterial(material);
It works for me.

What is the problem with first code listening and how to create complex dynamic textures? Is it possible to apply UIImage as texture using Ogre?