1
/** Example 013 Render To Texture
3
This tutorial shows how to render to a texture using Irrlicht. Render to
4
texture is a feature with which it is possible to create nice special effects.
5
In addition, this tutorial shows how to enable specular highlights.
7
In the beginning, everything as usual. Include the needed headers, ask the user
8
for the rendering driver, create the Irrlicht Device:
12
#include "driverChoice.h"
17
#pragma comment(lib, "Irrlicht.lib")
22
// ask user for driver
23
video::E_DRIVER_TYPE driverType=driverChoiceConsole();
24
if (driverType==video::EDT_COUNT)
27
// create device and exit if creation failed
29
IrrlichtDevice *device =
30
createDevice(driverType, core::dimension2d<u32>(640, 480),
34
return 1; // could not create selected driver.
36
video::IVideoDriver* driver = device->getVideoDriver();
37
scene::ISceneManager* smgr = device->getSceneManager();
38
gui::IGUIEnvironment* env = device->getGUIEnvironment();
41
Now, we load an animated mesh to be displayed. As in most examples,
42
we'll take the fairy md2 model. The difference here: We set the
43
shininess of the model to a value other than 0 which is the default
44
value. This enables specular highlights on the model if dynamic
45
lighting is on. The value influences the size of the highlights.
48
// load and display animated fairy mesh
50
scene::IAnimatedMeshSceneNode* fairy = smgr->addAnimatedMeshSceneNode(
51
smgr->getMesh("../../media/faerie.md2"));
55
fairy->setMaterialTexture(0,
56
driver->getTexture("../../media/faerie2.bmp")); // set diffuse texture
57
fairy->setMaterialFlag(video::EMF_LIGHTING, true); // enable dynamic lighting
58
fairy->getMaterial(0).Shininess = 20.0f; // set size of specular highlights
59
fairy->setPosition(core::vector3df(-10,0,-100));
60
fairy->setMD2Animation ( scene::EMAT_STAND );
64
To make specular highlights appear on the model, we need a dynamic
65
light in the scene. We add one directly in vicinity of the model. In
66
addition, to make the model not that dark, we set the ambient light to
71
smgr->addLightSceneNode(0, core::vector3df(-15,5,-105),
72
video::SColorf(1.0f, 1.0f, 1.0f));
75
smgr->setAmbientLight(video::SColor(0,60,60,60));
78
The next is just some standard stuff: Add a test cube and let it rotate
79
to make the scene more interesting. The user defined camera and cursor
80
setup is made later on, right before the render loop.
84
scene::ISceneNode* test = smgr->addCubeSceneNode(60);
86
// let the cube rotate and set some light settings
87
scene::ISceneNodeAnimator* anim = smgr->createRotationAnimator(
88
core::vector3df(0.3f, 0.3f,0));
90
test->setPosition(core::vector3df(-100,0,-100));
91
test->setMaterialFlag(video::EMF_LIGHTING, false); // disable dynamic lighting
92
test->addAnimator(anim);
96
device->setWindowCaption(L"Irrlicht Engine - Render to Texture and Specular Highlights example");
99
To test out the render to texture feature, we need a render target
100
texture. These are not like standard textures, but need to be created
101
first. To create one, we call IVideoDriver::addRenderTargetTexture()
102
and specify the size of the texture. Please don't use sizes bigger than
103
the frame buffer for this, because the render target shares the zbuffer
104
with the frame buffer.
105
Because we want to render the scene not from the user camera into the
106
texture, we add another fixed camera to the scene. But before we do all
107
this, we check if the current running driver is able to render to
108
textures. If it is not, we simply display a warning text.
111
// create render target
112
video::ITexture* rt = 0;
113
scene::ICameraSceneNode* fixedCam = 0;
116
if (driver->queryFeature(video::EVDF_RENDER_TO_TARGET))
118
rt = driver->addRenderTargetTexture(core::dimension2d<u32>(256,256), "RTT1");
119
test->setMaterialTexture(0, rt); // set material of cube to render target
122
fixedCam = smgr->addCameraSceneNode(0, core::vector3df(10,10,-80),
123
core::vector3df(-10,10,-100));
127
// create problem text
128
gui::IGUISkin* skin = env->getSkin();
129
gui::IGUIFont* font = env->getFont("../../media/fonthaettenschweiler.bmp");
133
gui::IGUIStaticText* text = env->addStaticText(
134
L"Your hardware or this renderer is not able to use the "\
135
L"render to texture feature. RTT Disabled.",
136
core::rect<s32>(150,20,470,60));
138
text->setOverrideColor(video::SColor(100,255,255,255));
142
scene::ICameraSceneNode* fpsCamera = smgr->addCameraSceneNodeFPS();
143
fpsCamera->setPosition(core::vector3df(-50,50,-150));
145
// disable mouse cursor
146
device->getCursorControl()->setVisible(false);
149
Nearly finished. Now we need to draw everything. Every frame, we draw
150
the scene twice. Once from the fixed camera into the render target
151
texture and once as usual. When rendering into the render target, we
152
need to disable the visibilty of the test cube, because it has the
153
render target texture applied to it. That's it, wasn't too complicated
160
if (device->isWindowActive())
162
driver->beginScene(true, true, 0);
166
// draw scene into render target
168
// set render target texture
169
driver->setRenderTarget(rt, true, true, video::SColor(0,0,0,255));
171
// make cube invisible and set fixed camera as active camera
172
test->setVisible(false);
173
smgr->setActiveCamera(fixedCam);
175
// draw whole scene into render buffer
178
// set back old render target
179
// The buffer might have been distorted, so clear it
180
driver->setRenderTarget(0, true, true, 0);
182
// make the cube visible and set the user controlled camera as active one
183
test->setVisible(true);
184
smgr->setActiveCamera(fpsCamera);
187
// draw scene normally
193
// display frames per second in window title
194
int fps = driver->getFPS();
197
core::stringw str = L"Irrlicht Engine - Render to Texture and Specular Highlights example";
201
device->setWindowCaption(str.c_str());
206
device->drop(); // drop device