Render To Texture

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
docWild
Posts: 38
Joined: Wed Nov 30, 2011 4:29 pm

Render To Texture

Post by docWild »

I've reduced to the simplest form I can:

Code: Select all

#include <irrlicht.h>
#include "driverChoice.h"
 
using namespace irr;
 
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#endif
 
int main()
{
 
    video::E_DRIVER_TYPE driverType=driverChoiceConsole();
    if (driverType==video::EDT_COUNT)
        return 1;
    // create device and exit if creation failed
 
    IrrlichtDevice *device =
        createDevice(driverType, core::dimension2d<u32>(640, 480),
                     32, false, false);
 
    if (device == 0)
        return 1; // could not create selected driver.
 
    video::IVideoDriver* driver = device->getVideoDriver();
    scene::ISceneManager* smgr = device->getSceneManager();
 
 
    // create render target
    video::ITexture* rt = 0;
 
    rt = driver->addRenderTargetTexture(core::dimension2d<u32>(256,256), "RTT1");
 
    driver->beginScene(true, true, 0);
 
    if (rt)
    {
        driver->setRenderTarget(rt, true, true, video::SColor(0, 255, 255, 255));
        u32* ImgTemp = (u32*)rt->lock();
 
        driver->draw2DLine(core::position2d<s32>(0,0), core::position2d<s32>(256,256), video::SColor(255,0,0,0));
 
        video::IImage *image = driver->createImageFromData(rt->getColorFormat(), rt->getSize(), ImgTemp);
        driver->writeImageToFile(image, "result.bmp");
        image->drop();
        rt->unlock();
    }
    driver->endScene();
 
 
    device->drop(); // drop device
    return 0;
}
  
 
My expected result is to see a diagonal black line drawn on a white background which is saved in appdir/result.bmp.

With OpenGL or Burning's video driver I see only background. I can see the line with the basic software renderer. I have not tested with directx.

Is there mention of this in the docs? Is there a workaround. Searching the forum tends to show up long lists of old threads without mention of resolution. I believed this to be the texture flip problem, but if it were simply vertically flipped I might expect to see the line traveling in the opposite direction but I do not see it at all.

It seems like something which should be mentioned in the RTT tutorial,at least, as debugging this without knowing the cause is time consuming.

Thanks.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Render To Texture

Post by mongoose7 »

According to the OpenGL specs, you can't read and write a render target. I think you should driver->setRenderTarget(0) first, then driver->createImageFromData(rt).
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Re: Render To Texture

Post by Radikalizm »

Exactly, you can't read from a texture which is bound to the rendering pipeline as a render target
docWild
Posts: 38
Joined: Wed Nov 30, 2011 4:29 pm

Re: Render To Texture

Post by docWild »

It seems strange that the image is generated with the correct background colour, and not just a junk file. However, thanks for the replies. I suppose the workaround is to use a second device with the software renderer in setup to precalc the textures.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Render To Texture

Post by hybrid »

Well, two things. First of all, don't do such things in the render loop. So simply move the whole image creation stuff just after the endScene. Otherwise you will never know, if you have finished rendering. Moreover, you always have to set the render target 0 again, as this will copy over the framebuffer content to the texture, in case you don't have frame buffer objects support. You can do this easily with OpenGL or D3D, just do as the other suggested and I detailed here.
Post Reply