The setup:
Irrlicht Engine version 1.7.3
Microsoft Windows 7 Ultimate Edition (Build 7600)
Using renderer: OpenGL 4.2.11631
ATI Radeon HD 5770
GLSL version: 4.2
- Initialize Irrlicht with an OpenGL device in 32 bit color (I didn't compile D3D support into my DLL, so I didn't test that)
- Create a scene using the scene manager with at least one solid object visible in the camera's frustrum
- Create a render target texture with an alpha channel — I used ECF_A8R8G8B8
- Clear the render target texture to an alpha value < 255 and render your scene
Here you can see a render target texture of an unlit sphere over a red background:
And here is the render target texture saved with the code in the following file:
Here is the complete source:
Code: Select all
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
class MyEventReceiver : public IEventReceiver {
IrrlichtDevice* Device;
public:
MyEventReceiver(IrrlichtDevice* device) : Device(device) {
}
bool OnEvent(const SEvent& event) {
if (event.EventType == irr::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown) {
switch (event.KeyInput.Key) {
case KEY_KEY_T:
//Save the render target texture from the last frame to a file
Device->getVideoDriver()->writeImageToFile(Device->getVideoDriver()->createImageFromData(
Device->getVideoDriver()->getTexture("problem")->getColorFormat(),
Device->getVideoDriver()->getTexture("problem")->getSize(),
Device->getVideoDriver()->getTexture("problem")->lock(),
false //copy mem
), "problem.png", 9);
return true;
default:
break;
}
}
return false;
}
};
int main(int argc, char** argv) {
IrrlichtDevice* device = createDevice(EDT_OPENGL, dimension2d<u32>(1024, 768), 32, false, false, true);
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
device->setWindowCaption(L"Alpha Issue");
MyEventReceiver receiver(device);
device->setEventReceiver(&receiver);
//A basic sphere node, should have a solid white material
ISceneNode* sphere = smgr->addSphereSceneNode(100, 100);
sphere->setMaterialFlag(EMF_LIGHTING, false); //So we can see the diffuse color
//Create a simple static camera to look at the object
smgr->addCameraSceneNode(smgr->getRootSceneNode(), vector3df(0, 0, -200), vector3df(0), true);
//Create our render target
ITexture* rttTex = driver->addRenderTargetTexture(dimension2d<u32>(1024, 768), "problem", ECF_A8R8G8B8);
while(device->run()) {
driver->beginScene();
//Render the scene
driver->setRenderTarget(rttTex);
smgr->drawAll();
//Draw the image over a red background to visually illustrate the issue
driver->setRenderTarget(0, true, true, SColor(255, 128, 0, 0));
driver->draw2DImage(rttTex, position2di(0, 0), recti(0, 0, 1024, 768), &recti(0, 0, 1024, 768), SColor(255,255,255,255), true);
driver->endScene();
}
device->drop();
return 0;
}
http://www.mediafire.com/?y5519nplj5eq988
Press the T key at runtime to save the render target texture to file for examination.