Page 1 of 1

Rendering to a array

Posted: Tue Jun 09, 2009 12:04 pm
by bit-pusher
Greetings friends.

I'm experienced with C++ and Irrlicht, but I've hit a stumbling block I'm hoping someone can direct me out of.

I want to render a scene with Irrlicht, but I don't want to display it directly; I just want to render the frame into eg a char array.

I don't expect the OpenGL or DirectX renderers will particularly want to do this. However, I only need low resolution - 320 x 240 max (don't even ask what the project is :D) - so I figure all I need do is tweak one of the software renderers to render into an array.

I've been having a look through the source, and I think once I find the right method to amend it should be fairly straight forward, but my eyes are glazing... I could really use a suggestion on the particular method I should look into editing/replacing.

Many thanks for any help you can offer.

Posted: Tue Jun 09, 2009 12:16 pm
by Brainsaw
Maybe the "Render to Texture" tutorial on the Irrlicht sourceforge page might help you. I think it just renders to a buffer within a texture.

Posted: Tue Jun 09, 2009 12:29 pm
by bit-pusher
Brainsaw - (name from a Therapy? lyric??) - thanks! I was thinking ITexture didn't support pixel access but I see ITexture::lock() returns a void pointer to the pixel data.

I'll have a play and see if I can get that working.

Posted: Tue Jun 09, 2009 8:15 pm
by bit-pusher
Awesome -- works great. Thanks for the pointer Brainsaw, wheel reinvention avoided.

Trivial use case example rendering to a bitmap file:

Code: Select all

    // Create render target
	if (!driver->queryFeature(video::EVDF_RENDER_TO_TARGET)) exit(1);
    video::ITexture* rt = driver->addRenderTargetTexture(core::dimension2d<s32>(256,256), "RTT1");

    driver->setRenderTarget(rt, true, true, video::SColor(0,0,0,255));

    // Render once, testing
    device->run();
	driver->beginScene(true, true, 0);
	driver->setRenderTarget(rt, true, true, video::SColor(0,0,0,255));  // Set + clear RTT
	smgr->drawAll();
	driver->endScene();

	// Create image from RTT
	video::IImage* image = driver->createImageFromData (
		rt->getColorFormat(),
		rt->getSize(),
		rt->lock(),
		true
	);
	rt->unlock();

	// Save it
	driver->writeImageToFile(image,"look_it_works.bmp");
	image->drop();