~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to examples/13.RenderToTexture/tutorial.html

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html>
 
2
<head>
 
3
<title>Irrlicht Engine Tutorial</title>
 
4
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 
5
</head>
 
6
 
 
7
<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
 
8
<br>
 
9
<table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
 
10
  <tr> 
 
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%">
 
13
<div align="center">
 
14
        <div align="left"><b><font color="#FFFFFF">Tutorial 13. Render to Texture</font></b></div>
 
15
      </div>
 
16
      </td>
 
17
  </tr>
 
18
  <tr bgcolor="#eeeeff"> 
 
19
    <td height="90" colspan="2"> 
 
20
      <div align="left"> 
 
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>
 
26
        </p>
 
27
      </div>
 
28
    </td>
 
29
  </tr>
 
30
</table>
 
31
<br>
 
32
<table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
 
33
  <tr> 
 
34
    <td bgcolor="#666699"> <b><font color="#FFFFFF">Lets start!</font></b></td>
 
35
  </tr>
 
36
  <tr> 
 
37
    <td height="90" bgcolor="#eeeeff" valign="top"> <div align="left"> 
 
38
        <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">
 
42
            <tr> 
 
43
              <td><pre>#include &lt;irrlicht.h>
 
44
#include &lt;iostream>
 
45
 
 
46
using namespace irr;
 
47
 
 
48
#pragma comment(lib, "Irrlicht.lib")
 
49
 
 
50
int main()
 
51
{
 
52
        // let user select driver type
 
53
 
 
54
        video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;
 
55
 
 
56
        printf(&quot;Please select the driver you want for this example:\n&quot;\<br>           &quot; (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n&quot;\<br>               &quot; (d) Software Renderer\n (e) Apfelbaum Software Renderer\n&quot;\<br>             &quot; (f) NullDevice\n (otherKey) exit\n\n&quot;);
 
57
 
 
58
        char i;
 
59
        std::cin >> i;
 
60
 
 
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>  }       
 
62
 
 
63
        // create device and exit if creation failed
 
64
 
 
65
        IrrlichtDevice *device =
 
66
                createDevice(driverType, core::dimension2d<s32>(640, 480),
 
67
                16, false, false);
 
68
 
 
69
        if (device == 0)
 
70
                return 1; // could not create selected driver.
 
71
 
 
72
        video::IVideoDriver* driver = device->getVideoDriver();
 
73
        scene::ISceneManager* smgr = device->getSceneManager();
 
74
        gui::IGUIEnvironment* env = device->getGUIEnvironment();</pre></td>
 
75
            </tr>
 
76
          </table>
 
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">
 
83
            <tr> 
 
84
              <td><pre>// load and display animated fairy mesh
 
85
 
 
86
        scene::IAnimatedMeshSceneNode* fairy = smgr->addAnimatedMeshSceneNode(
 
87
                smgr->getMesh("../../media/faerie.md2"));
 
88
 
 
89
        if (fairy)
 
90
        {
 
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));
 
95
        }</pre></td>
 
96
            </tr>
 
97
          </table>
 
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 
 
101
            to gray. </p>
 
102
          <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
 
103
            <tr> 
 
104
              <td><pre>
 
105
        // add white light
 
106
        scene::ILightSceneNode* light = smgr->addLightSceneNode(0,
 
107
                core::vector3df(-15,5,-105), video::SColorf(1.0f, 1.0f, 1.0f));
 
108
 
 
109
        // set ambient light
 
110
        driver->setAmbientLight(video::SColor(0,60,60,60));</pre></td>
 
111
            </tr>
 
112
          </table>
 
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">
 
117
            <tr> 
 
118
              <td><pre>
 
119
        // add fps camera
 
120
        scene::ICameraSceneNode* fpsCamera = smgr->addCameraSceneNodeFPS();
 
121
        fpsCamera->setPosition(core::vector3df(-50,50,-150));
 
122
 
 
123
        // disable mouse cursor
 
124
        device->getCursorControl()->setVisible(false);
 
125
 
 
126
        // create test cube
 
127
        scene::ISceneNode* test = smgr->addCubeSceneNode(60);
 
128
 
 
129
        // let the cube rotate and set some light settings
 
130
        scene::ISceneNodeAnimator* anim = smgr->createRotationAnimator(
 
131
                core::vector3df(0.3f, 0.3f,0));
 
132
 
 
133
        test->setPosition(core::vector3df(-100,0,-100));
 
134
        test->setMaterialFlag(video::EMF_LIGHTING, false); // disable dynamic lighting
 
135
        test->addAnimator(anim);
 
136
        anim->drop();
 
137
 
 
138
        // set window caption
 
139
        device->setWindowCaption(L"Irrlicht Engine - Render to Texture and Specular Highlights example");</pre></td>
 
140
            </tr>
 
141
          </table>
 
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 
 
151
            a warning text.</p>
 
152
          <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
 
153
            <tr> 
 
154
              <td><pre>// create render target
 
155
        video::ITexture* rt = 0;
 
156
        scene::ICameraSceneNode* fixedCam = 0;
 
157
        
 
158
 
 
159
        if (driver->queryFeature(video::EVDF_RENDER_TO_TARGET))
 
160
        {
 
161
                rt = driver->createRenderTargetTexture(core::dimension2d<s32>(256,256));
 
162
                test->setMaterialTexture(0, rt); // set material of cube to render target
 
163
 
 
164
                // add fixed camera
 
165
                fixedCam = smgr->addCameraSceneNode(0, core::vector3df(10,10,-80),
 
166
                        core::vector3df(-10,10,-100));
 
167
        }
 
168
        else
 
169
        {
 
170
                // create problem text
 
171
                gui::IGUISkin* skin = env->getSkin();
 
172
                gui::IGUIFont* font = env->getFont("../../media/fonthaettenschweiler.bmp");
 
173
                if (font)
 
174
                        skin->setFont(font);
 
175
 
 
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));
 
180
 
 
181
                text->setOverrideColor(video::SColor(100,255,255,255));
 
182
        }</pre></td>
 
183
            </tr>
 
184
          </table>
 
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">
 
192
            <tr> 
 
193
              <td><pre>while(device->run())
 
194
        if (device->isWindowActive())
 
195
        {
 
196
                driver->beginScene(true, true, 0);
 
197
 
 
198
                if (rt)
 
199
                {
 
200
                        // draw scene into render target
 
201
                        
 
202
                        // set render target texture
 
203
                        driver->setRenderTarget(rt, true, true, video::SColor(0,0,0,255));     
 
204
 
 
205
                        // make cube invisible and set fixed camera as active camera
 
206
                        test->setVisible(false);
 
207
                        smgr->setActiveCamera(fixedCam);
 
208
 
 
209
                        // draw whole scene into render buffer
 
210
                        smgr->drawAll();                 
 
211
 
 
212
                        // set back old render target
 
213
                        driver->setRenderTarget(0);      
 
214
 
 
215
                        // make the cube visible and set the user controlled camera as active one
 
216
                        test->setVisible(true);
 
217
                        smgr->setActiveCamera(fpsCamera);
 
218
                }
 
219
                
 
220
                // draw scene normally
 
221
                smgr->drawAll(); 
 
222
                env->drawAll();
 
223
 
 
224
                driver->endScene();
 
225
        }
 
226
 
 
227
        if (rt)
 
228
                rt->drop(); // drop render target because we created if with a create() method
 
229
 
 
230
        device->drop(); // drop device
 
231
        return 0;
 
232
}
 
233
</pre></td>
 
234
            </tr>
 
235
          </table>
 
236
          
 
237
        <p>&nbsp;</p></div>
 
238
      </div>
 
239
      </td>
 
240
  </tr>
 
241
</table>
 
242
<p>&nbsp;</p>
 
243
      </body>
 
244
</html>