Page 1 of 1

Dynamically changing texture content

Posted: Tue Jul 31, 2012 12:36 pm
by Roland
Hi forum,

I have a problem which I dont know how to solve.

I have some 2d-data that is created dynamically by some process. From that data, I create an image and I want to draw the Image. The data is changing over time and with the data change, the presented image should change as well.

What I did so far is:
*1 create an IImage using the video driver:
this->myImage = this->device->getVideoDriver()->createImage(irr::video::ECF_A8R8G8B8, irr::core::dimension2d<irr::u32>(width, height));
*2 fill the image with values:
this->myImage->lock();
loop_over_all_pixels
this->myImage->setPixel(x, y, irr::video::SColor(255, value, value, value));
this->myImage->unlock();
*3 put the image into a texture:
this->myTexture = this->device->getVideoDriver()->addTexture("Name", this->myImage);
*4 use draw2DImage (in a different class) to draw the image:
if(this->myTexture) driver->draw2DImage(this->myTexture, targetRectangle, rect<s32>(dimension2d<s32>(0,0), this->myTexture->getSize()), 0, 0, false);

If I change the IImage pixel values using step 2 later, the changed values dont show in the texture. So there is the problem. Is there an easy way of telling the texture: "hey, the image has changed. Get the new values now!"? Also, is there a more efficient way of changing the pixel values other than by creating so many SColor objects?


If thats not possible, I would have to create a new texture for each image change. But what happens to the old texture objects? I dont want my memory being polluted with old texture objects. Since I have to use "addTexture", I cant use drop() to destroy the old texture. Also on a related note: is it possible to give each new texture the same name as the last one?

Thanks!
Roland

Re: Dynamically changing texture content

Posted: Tue Jul 31, 2012 1:03 pm
by hybrid
You can drop your image once you created the texture. After that, every change requires a lock to the texture (which downloads the texture from GPU), change the values in the memory area that you get from the lock (cast void* properly) and unlock the texture again (which uploads the data back to GPU). This mechanism is in general faster than creating a new texture each time.

Re: Dynamically changing texture content

Posted: Tue Jul 31, 2012 1:41 pm
by Roland
Thx, that works very well! failing to access the pixel values via a "get" function in ITexture made me not look any further there. I did not imagined that a lock function would provide that functionality. Thanks for the help :)