rtt
rtt
could some one post code on how to lock/write to individual pixels when doing render to texture
-
- Admin
- Posts: 14143
- Joined: Wed Apr 19, 2006 9:20 pm
- Location: Oldenburg(Oldb), Germany
- Contact:
If you just want to write a very few pixels, you can use drawPixel. Otherwise simply lock() the texture and cast the returned pointer to e.g. u8*. then you can write each byte of the texture. Depending on the color format, each pixel has 2-4 bytes. The elements of each pixel are given in the color format. Once you unlock the texture it is uploaded to the GPU.
thx hybrid
code I came up with off the bat
u8 * Pixel = 0;
while(device->run())
if (device->isWindowActive())
{
driver->beginScene(true, true, 0);
if (rt)
{
// draw scene into render target
// set render target texture
// 128x128 texture
driver->setRenderTarget(rt, true, true, video::SColor(0,0,0,255));
Pixel = rt->Lock();
for(int z = 0; z< 128; z++)
{
for(int x = 0; x< 128; x++)
{
Pixel[z * 128 + x] = a * r * g * b;
}
}
// a, r, g, b ints ranging from 0 to 256
code I came up with off the bat
u8 * Pixel = 0;
while(device->run())
if (device->isWindowActive())
{
driver->beginScene(true, true, 0);
if (rt)
{
// draw scene into render target
// set render target texture
// 128x128 texture
driver->setRenderTarget(rt, true, true, video::SColor(0,0,0,255));
Pixel = rt->Lock();
for(int z = 0; z< 128; z++)
{
for(int x = 0; x< 128; x++)
{
Pixel[z * 128 + x] = a * r * g * b;
}
}
// a, r, g, b ints ranging from 0 to 256
-
- Admin
- Posts: 14143
- Joined: Wed Apr 19, 2006 9:20 pm
- Location: Oldenburg(Oldb), Germany
- Contact:
You should lock the rtt outside the beginScene/endScene, just to be sure that the rtt is not in use. Moreover, the color value needs to be different. If you have an A8R8G8B8 texture you can write SColor.color to the pixel, otherwise use a conversion method from SColor.h to convert to the 16bit values.
maybe you could cast the void pointer?
Dustbin::Games on the web: https://www.dustbin-online.de/
Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
Here is an example. It comes from my CGUITTFont class. In this code fragment, I am converting a FreeType font bitmap into an Irrlicht IImage. FreeType stores the font as an alpha channel. That is, each "pixel" is actually just an 8-bit alpha value.
IImage and ITexture both have a lock() function for grabbing the raw pixel data, so you can learn from this example.
IImage and ITexture both have a lock() function for grabbing the raw pixel data, so you can learn from this example.
Code: Select all
// Create our blank image.
// texture_size is a dimension2du that is storing the size of our texture.
image = driver->createImage(video::ECF_A8R8G8B8, texture_size);
// Fill the image with black pixels with an alpha of 0.
// FreeType returns just an alpha channel, so we will overwrite the alpha values
// in the code below.
image->fill(video::SColor(0, 255, 255, 255));
// In Irrlicht, pitch is the number of bits for 1 row of pixels.
// We divide by u32 to return how many pixels are in one row.
// Sometimes, an image may have some unused border pixels, so the width will not
// be the same as the pitch, so always use pitch when calculating our
// location manually!
const u32 image_pitch = image->getPitch() / sizeof(u32);
// Here is where we lock the texture. Our image is A8R8G8B8,
// so one full pixel is 32-bits.
// The first 8 are alpha, next 8 are red, then green, and the last 8 are blue.
// I cast the pointer to u32 so when I increment the pointer,
// I move forward one full pixel.
u32* data = (u32*)image->lock();
// This is a pointer to the FreeType bitmap.
u8* row = glyph->bitmap.buffer;
// Now, I iterate through the FreeType bitmap.
for (s32 y = 0; y < bits.rows; ++y)
{
// I create a new pointer to point to the current row in the FreeType bitmap.
u8* bitsdata = row;
// Now, I iterate through each pixel in the FreeType bitmap row.
// y * image_pitch + x will return the current pixel in our
// image to manipulate.
// *bitsdata++ will return the current alpha value I am looking at
// in the FreeType bitmap,
// then increment to the next value.
// Since *bitsdata is returning an alpha value, we shift it 24 bits to the left.
// That places at the location of alpha in a 32-bit A8R8G8B8 pixel.
for (s32 x = 0; x < bits.width; ++x)
data[y * image_pitch + x] |= ((u32)(*bitsdata++) << 24);
// We want to go to the next row now,
// so we increment our row pointer by the pitch.
row += bits.pitch;
}
image->unlock();
Last edited by Nalin on Wed Mar 10, 2010 7:48 pm, edited 1 time in total.
Ok so I got
u32* data = (u32*)image->lock();
the question is what do I use u32, u16 or u8? Is it dependant on the image format?
ISceneNode* n = M->addCubeSceneNode(60);
ITexture* t = 0;
if (R->queryFeature(video::EVDF_RENDER_TO_TARGET))
{
t = R->addRenderTargetTexture(core::dimension2d<u32>(256,256), "RTT1");
n->setMaterialTexture(0, t);
}
u32* P = (u32*)t->lock();
u32* data = (u32*)image->lock();
the question is what do I use u32, u16 or u8? Is it dependant on the image format?
ISceneNode* n = M->addCubeSceneNode(60);
ITexture* t = 0;
if (R->queryFeature(video::EVDF_RENDER_TO_TARGET))
{
t = R->addRenderTargetTexture(core::dimension2d<u32>(256,256), "RTT1");
n->setMaterialTexture(0, t);
}
u32* P = (u32*)t->lock();