3
<title>Irrlicht Engine Tutorial</title>
4
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
7
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
9
<table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
11
<td bgcolor="#666699" width="10"><b><a href="http://irrlicht.sourceforge.net" target="_blank"><img src="../../media/irrlichtlogo.jpg" width="88" height="31" border="0"></a></b></td>
12
<td bgcolor="#666699" width="100%">
14
<div align="left"><b><font color="#FFFFFF">Tutorial 13. Render to Texture</font></b></div>
18
<tr bgcolor="#eeeeff">
19
<td height="90" colspan="2">
21
<p> This tutorial shows how to render to a texture using Irrlicht. Render
22
to texture is a feature with which it is possible to create nice special
23
effects. In addition, this tutorial shows how to enable specular highlights.</p>
24
<p>The program which is described here will look like this:</p>
25
<p align="center"><img src="../../media/013shot.jpg" width="256" height="200"><br>
32
<table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
34
<td bgcolor="#666699"> <b><font color="#FFFFFF">Lets start!</font></b></td>
37
<td height="90" bgcolor="#eeeeff" valign="top"> <div align="left">
39
<p>In the beginning, everything as usual. Include the needed headers,
40
ask the user for the rendering driver, create the Irrlicht Device:</p>
41
<table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
43
<td><pre>#include <irrlicht.h>
44
#include <iostream>
48
#pragma comment(lib, "Irrlicht.lib")
52
// let user select driver type
54
video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;
56
printf("Please select the driver you want for this example:\n"\<br> " (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n"\<br> " (d) Software Renderer\n (e) Apfelbaum Software Renderer\n"\<br> " (f) NullDevice\n (otherKey) exit\n\n");
61
switch(i)<br> {<br> case 'a': driverType = video::EDT_DIRECT3D9;break;<br> case 'b': driverType = video::EDT_DIRECT3D8;break;<br> case 'c': driverType = video::EDT_OPENGL; break;<br> case 'd': driverType = video::EDT_SOFTWARE; break;<br> case 'e': driverType = video::EDT_BURNINGSVIDEO;break;<br> case 'f': driverType = video::EDT_NULL; break;<br> default: return 1;<br> }
63
// create device and exit if creation failed
65
IrrlichtDevice *device =
66
createDevice(driverType, core::dimension2d<s32>(640, 480),
70
return 1; // could not create selected driver.
72
video::IVideoDriver* driver = device->getVideoDriver();
73
scene::ISceneManager* smgr = device->getSceneManager();
74
gui::IGUIEnvironment* env = device->getGUIEnvironment();</pre></td>
77
<p>Now, we load an animated mesh to be displayed. As in most examples,
78
we'll take the fairy md2 model. The difference here: We set the shininess<br>
79
of the model to a value other than 0 which is the default value. This
80
enables specular highlights on the model if dynamic lighting is on.
81
The value influences the size of the highlights.</p>
82
<table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
84
<td><pre>// load and display animated fairy mesh
86
scene::IAnimatedMeshSceneNode* fairy = smgr->addAnimatedMeshSceneNode(
87
smgr->getMesh("../../media/faerie.md2"));
91
fairy->setMaterialTexture(0, driver->getTexture("../../media/faerie2.bmp")); // set diffuse texture
92
fairy->setMaterialFlag(video::EMF_LIGHTING, true); // enable dynamic lighting
93
fairy->getMaterial(0).Shininess = 20.0f; // set size of specular highlights
94
fairy->setPosition(core::vector3df(-10,0,-100));
98
<p> To make specular highlights appear on the model, we need a dynamic
99
light in the scene. We add one directly in vicinity of the model.
100
In addition, to make the model not that dark, we set the ambient light
102
<table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
106
scene::ILightSceneNode* light = smgr->addLightSceneNode(0,
107
core::vector3df(-15,5,-105), video::SColorf(1.0f, 1.0f, 1.0f));
110
driver->setAmbientLight(video::SColor(0,60,60,60));</pre></td>
113
<p>The next is just some standard stuff: Add a user controlled camera
114
to the scene, disable mouse cursor, and add a test cube and let it
115
rotate to make the scene more interesting.</p>
116
<table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
120
scene::ICameraSceneNode* fpsCamera = smgr->addCameraSceneNodeFPS();
121
fpsCamera->setPosition(core::vector3df(-50,50,-150));
123
// disable mouse cursor
124
device->getCursorControl()->setVisible(false);
127
scene::ISceneNode* test = smgr->addCubeSceneNode(60);
129
// let the cube rotate and set some light settings
130
scene::ISceneNodeAnimator* anim = smgr->createRotationAnimator(
131
core::vector3df(0.3f, 0.3f,0));
133
test->setPosition(core::vector3df(-100,0,-100));
134
test->setMaterialFlag(video::EMF_LIGHTING, false); // disable dynamic lighting
135
test->addAnimator(anim);
138
// set window caption
139
device->setWindowCaption(L"Irrlicht Engine - Render to Texture and Specular Highlights example");</pre></td>
142
<p> To test out the render to texture feature, we need a render target
143
texture. These are not like standard textures, but need to be created
144
first. To create one, we call IVideoDriver::createRenderTargetTexture()
145
and specify the size of the texture. Please don't use sizes bigger
146
than the frame buffer for this, because the render target shares the
147
zbuffer with the frame buffer. And because we want to render the scene
148
not from the user camera into the texture, we add another, fixed camera
149
to the scene. But before we do all this, we check if the current running
150
driver is able to render to textures. If it is not, we simply display
152
<table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
154
<td><pre>// create render target
155
video::ITexture* rt = 0;
156
scene::ICameraSceneNode* fixedCam = 0;
159
if (driver->queryFeature(video::EVDF_RENDER_TO_TARGET))
161
rt = driver->createRenderTargetTexture(core::dimension2d<s32>(256,256));
162
test->setMaterialTexture(0, rt); // set material of cube to render target
165
fixedCam = smgr->addCameraSceneNode(0, core::vector3df(10,10,-80),
166
core::vector3df(-10,10,-100));
170
// create problem text
171
gui::IGUISkin* skin = env->getSkin();
172
gui::IGUIFont* font = env->getFont("../../media/fonthaettenschweiler.bmp");
176
gui::IGUIStaticText* text = env->addStaticText(
177
L"Your hardware or this renderer is not able to use the "\
178
L"render to texture feature. RTT Disabled.",
179
core::rect<s32>(150,20,470,60));
181
text->setOverrideColor(video::SColor(100,255,255,255));
185
<p> Nearly finished. Now we need to draw everything. Every frame, we
186
draw the scene twice. Once from the fixed camera into the render target
187
texture and once as usual. When rendering into the render target,
188
we need to disable the visibilty of the test cube, because it has
189
the render target texture applied to it.<br>
190
That's, wasn't quite complicated I hope. :)</p>
191
<table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
193
<td><pre>while(device->run())
194
if (device->isWindowActive())
196
driver->beginScene(true, true, 0);
200
// draw scene into render target
202
// set render target texture
203
driver->setRenderTarget(rt, true, true, video::SColor(0,0,0,255));
205
// make cube invisible and set fixed camera as active camera
206
test->setVisible(false);
207
smgr->setActiveCamera(fixedCam);
209
// draw whole scene into render buffer
212
// set back old render target
213
driver->setRenderTarget(0);
215
// make the cube visible and set the user controlled camera as active one
216
test->setVisible(true);
217
smgr->setActiveCamera(fpsCamera);
220
// draw scene normally
228
rt->drop(); // drop render target because we created if with a create() method
230
device->drop(); // drop device