aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8.1/examples/02.Quake3Map/tutorial.html
blob: 1858b866ae4abeb6775daea173de989248a01ac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<html>
<head>
<title>Irrlicht Engine Tutorial</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<br>
<table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
  <tr> 
    <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>
    <td bgcolor="#666699" width="100%"> <div align="center"><b><font color="#FFFFFF"></font></b></div>
      <b><font color="#FFFFFF">Tutorial 2.Quake3Map</font></b></td>
  </tr>
  <tr bgcolor="#eeeeff"> 
    <td height="90" colspan="2"> <div align="left"> 
        <p>This Tutorial shows how to load a Quake 3 map into the engine, create 
          a SceneNode for optimizing the speed of rendering and how to create 
          a user controlled camera. Please note that you should know the basics 
          of the engine before starting this tutorial, just take a short look 
          at the first tutorial, 1.HelloWorld, if you haven't done this yet.<br>
          The result of this example will look like this:</p>
        <p align="center"><img src="../../media/002shot.jpg" width="259" height="202"><br>
        </p>
      </div></td>
  </tr>
</table>
<br>
<table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
  <tr> 
    <td bgcolor="#666699"> <div align="center"><b><font color="#000000"></font></b></div>
      <font color="#FFFFFF"><b>Lets start!</b></font></td>
  </tr>
  <tr> 
    <td height="90" bgcolor="#eeeeff" valign="top"> <div align="left"> 
        <p>Lets start like the HelloWorld example: We include the irrlicht header 
          files and an additional file to be able<br>
          to ask the user for a driver type using the console.</p>
        <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
          <tr> 
            <td> <pre>#include &lt;irrlicht.h&gt;<br>#include &lt;iostream&gt;<br></pre></td>
          </tr>
        </table>
        <p>As already written in the HelloWorld example, in the Irrlicht Engine, 
          everything can be found in the namespace 'irr'. To get rid of the irr:: 
          in front of the name of every class, we tell the compiler that we use 
          that namespace from now on, and we will not have to write that 'irr::'.<br>
          There are 5 other sub namespaces 'core', 'scene', 'video', 'io' and 
          'gui'. Unlike in the HelloWorld example, we do not a 'using namespace' 
          for these 5 other namespaces because in this way you will see what can 
          be found in which namespace. But if you like, you can also include the 
          namespaces like in the previous example. Code just like you want to.</p>
        <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
          <tr> 
            <td> <pre>using namespace irr;</pre> </td>
          </tr>
        </table>
        <p>Again, to be able to use the Irrlicht.DLL file, we need to link with 
          the Irrlicht.lib. We could set this option in the project settings, 
          but to make it easy, we use a pragma comment lib:</p>
        <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
          <tr> 
            <td> <pre>#pragma comment(lib, &quot;Irrlicht.lib&quot;)</pre> </td>
          </tr>
        </table>
        
      </div>
      <p>Ok, lets start. Again, we use the main() method as start, not the WinMain(), 
        because its shorter to write.</p>
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
        <tr> 
          <td> <pre>int main()<br>{</pre> </td>
        </tr>
      </table>
      <p> Like in the HelloWorld example, we create an IrrlichtDevice with createDevice(). 
        The difference now is that we ask the user to select which hardware accelerated 
        driver to use. The Software device would be too slow to draw a huge Quake 
        3 map, but just for the fun of it, we make this decision possible too.</p>
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
        <tr> 
          <td> <pre>// ask user for driver<br><br>video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;<br><br>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;);<br>
char i;<br>std::cin &gt;&gt; i;<br><br>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>}	<br><br>// create device and exit if creation failed<br><br>IrrlichtDevice *device =<br>    createDevice(driverType, core::dimension2d&lt;s32&gt;(640, 480));<br><br>if (device == 0)<br>   return 1;</pre></td>
        </tr>
      </table>
      <p>Get a pointer to the video driver and the SceneManager so that we do 
        not always have to write device-&gt;getVideoDriver() and device-&gt;getSceneManager().</p>
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
        <tr> 
          <td> <pre>video::IVideoDriver* driver = device-&gt;getVideoDriver();
scene::ISceneManager* smgr = device-&gt;getSceneManager();</pre> </td>
        </tr>
      </table>
      <p>To display the Quake 3 map, we first need to load it. Quake 3 maps are 
        packed into .pk3 files wich are nothing other than .zip files. So we add 
        the .pk3 file to our FileSystem. After it was added, we are able to read 
        from the files in that archive as they would directly be stored on disk.</p>
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
        <tr> 
          <td> <pre>device-&gt;getFileSystem()-&gt;addZipFileArchive(&quot;../../media/map-20kdm2.pk3&quot;);</pre> </td>
        </tr>
      </table>
      <p>Now we can load the mesh by calling getMesh(). We get a pointer returned 
        to a IAnimatedMesh. As you know, Quake 3 maps are not really animated, 
        they are only a huge chunk of static geometry with some materials attached. 
        Hence the IAnimated mesh consists of only one frame,<br>
        so we get the &quot;first frame&quot; of the &quot;animation&quot;, which 
        is our quake level and create an OctTree scene node with it, using addOctTreeSceneNode(). 
        The OctTree optimizes the scene a little bit, trying to draw only geometry 
        which is currently visible. An alternative to the OctTree would be a AnimatedMeshSceneNode, 
        which would draw always the complete geometry of the mesh, without optimization. 
        Try it out: Write addAnimatedMeshSceneNode instead of addOctTreeSceneNode 
        and compare the primitives drawed by the video driver. (There is a getPrimitiveCountDrawed() 
        method in the IVideoDriver class). Note that this optimization with the 
        Octree is only useful when drawing huge meshes consiting of lots of geometry.</p>
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
        <tr> 
          <td> <pre>scene::IAnimatedMesh* mesh = smgr-&gt;getMesh(&quot;20kdm2.bsp&quot;);<br>scene::ISceneNode* node = 0;

if (mesh)<br>    node = smgr-&gt;addOctTreeSceneNode(mesh-&gt;getMesh(0));</pre> </td>
        </tr>
      </table>
      <p>Because the level was modelled not around the origin (0,0,0), we translate 
        the whole level a little bit.</p>
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
        <tr> 
          <td> <pre>if (node)<br>    node-&gt;setPosition(core::vector3df(-1300,-144,-1249));</pre> </td>
        </tr>
      </table>
      <p>Now we only need a Camera to look at the Quake 3 map. And we want to 
        create a user controlled camera. There are some different cameras available 
        in the Irrlicht engine. For example the Maya Camera which can be controlled 
        compareable to the camera in Maya: Rotate with left mouse button pressed, 
        Zoom with both buttons pressed,<br>
        translate with right mouse button pressed. This could be created with 
        addCameraSceneNodeMaya(). But for this example, we want to create a camera 
        which behaves like the ones in first person shooter games (FPS):</p>
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
        <tr> 
          <td> <pre>smgr-&gt;addCameraSceneNodeFPS();</pre> </td>
        </tr>
      </table>
      <p>The mouse cursor needs not to be visible, so we make it invisible. </p>
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
        <tr> 
          <td> <pre>device-&gt;getCursorControl()-&gt;setVisible(false);</pre> </td>
        </tr>
      </table>
      <p>We have done everything, so lets draw it. We also write the current frames 
        per second and the drawn primitives to the caption of the window. The 
        'if (device-&gt;isWindowActive())' line is optional, but prevents the 
        engine render to set the position of the mouse cursor after task switching 
        when other program are active.</p>
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
        <tr> 
          <td> <pre>int lastFPS = -1;</pre> 
            <pre>while(device-&gt;run())
{
  driver-&gt;beginScene(true, true, video::SColor(0,200,200,200));
  smgr-&gt;drawAll();
  driver-&gt;endScene();</pre>
            <pre>  int fps = driver-&gt;getFPS();</pre>
            <pre>  if (lastFPS != fps)
  {
     core::stringw str = L&quot;Irrlicht Engine - Quake 3 Map example [&quot;;<br>     str += driver-&gt;getName();<br>     str += &quot;] FPS:&quot;;<br>     str += fps;<br>     device-&gt;setWindowCaption(str.c_str());<br>     lastFPS = fps;
  }
}</pre> </td>
        </tr>
      </table>
      <p>In the end, delete the Irrlicht device.</p>
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
        <tr> 
          <td> <pre>  device-&gt;drop();<br>  return 0;<br>}</pre> </td>
        </tr>
      </table>
      <p>That's it. Compile and play around with the program. </p></td>
  </tr>
</table>
<p>&nbsp;</p>
<p>&nbsp;</p>
</body>
</html>