~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to examples/04.Movement/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="center"></div>
 
15
        <div align="left"><b><font color="#FFFFFF">Tutorial 4.Movement</font></b></div>
 
16
      </div>
 
17
      </td>
 
18
  </tr>
 
19
  <tr bgcolor="#eeeeff"> 
 
20
    <td height="90" colspan="2"> 
 
21
      <div align="left"> 
 
22
        <p>This Tutorial shows how to move and animate SceneNodes. The basic concept 
 
23
          of SceneNodeAnimators is shown as well as manual movement of nodes using 
 
24
          the keyboard.</p>
 
25
        <p>The program which is described here will look like this:</p>
 
26
        <p align="center"><img src="../../media/004shot.jpg" width="259" height="204"><br>
 
27
        </p>
 
28
      </div>
 
29
    </td>
 
30
  </tr>
 
31
</table>
 
32
<br>
 
33
<table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
 
34
  <tr> 
 
35
    <td bgcolor="#666699"> <div align="center"><b><font color="#FFFFFF"></font></b></div>
 
36
      <b><font color="#FFFFFF">Lets start!</font></b></td>
 
37
  </tr>
 
38
  <tr> 
 
39
    <td height="90" bgcolor="#eeeeff" valign="top"> <div align="left"> 
 
40
        <p>As always, I include the header files, use the irr namespace, and tell 
 
41
          the linker to link with the .lib file. </p>
 
42
        <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
 
43
          <tr> 
 
44
            <td> <pre>#include &lt;stdio.h&gt;<br>#include &lt;wchar.h&gt;<br>#include &lt;irrlicht.h&gt;</pre> 
 
45
              <pre>using namespace irr;</pre>
 
46
              <pre>#pragma comment(lib, &quot;Irrlicht.lib&quot;)</pre></td>
 
47
          </tr>
 
48
        </table>
 
49
        <p>In this tutorial, one of our goals is to move a scene node using some 
 
50
          keys on the keyboard. We store a pointer to the scene node we want to 
 
51
          move with the keys here.<br>
 
52
          The other pointer is a pointer to the Irrlicht Device, which we need 
 
53
          int the EventReceiver to manipulate the scene node and to get the active 
 
54
          camera.</p>
 
55
        <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
 
56
          <tr> 
 
57
            <td> <pre>scene::ISceneNode* node = 0;<br>IrrlichtDevice* device = 0; </pre></td>
 
58
          </tr>
 
59
        </table>
 
60
        <p>To get events like mouse and keyboard input, or GUI events like &quot;the 
 
61
          OK button has been clicked&quot;, we need an object wich is derived 
 
62
          from the IEventReceiver object. There is only one method to override: 
 
63
          OnEvent. This method will be called by the engine when an event happened. 
 
64
          We will use this input to move the scene node with the keys W and S.</p>
 
65
        <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
 
66
          <tr> 
 
67
            <td> <pre>class MyEventReceiver : public IEventReceiver<br>{<br>public:<br> virtual bool OnEvent(const SEvent&amp; event)<br>       { </pre></td>
 
68
          </tr>
 
69
        </table>
 
70
        <p>If the key 'W' or 'S' was left up, we get the position of the scene 
 
71
          node, and modify the Y coordinate a little bit. So if you press 'W', 
 
72
          the node moves up, and if you press 'S' it moves down.</p>
 
73
        <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
 
74
          <tr> 
 
75
            <td> <pre>if (node != 0 &amp;&amp; event.EventType == irr::EET_KEY_INPUT_EVENT&amp;&amp;<br>                        !event.KeyInput.PressedDown)<br>{<br>           switch(event.KeyInput.Key)<br>          {<br>           case KEY_KEY_W:<br>             case KEY_KEY_S:<br>                     {<br>                           core::vector3df v = node-&gt;getPosition();<br>                         v.Y += event.KeyInput.Key == KEY_KEY_W ? 2.0f : -2.0f;<br>                              node-&gt;setPosition(v);<br>                    }<br>                   return true;<br>                }<br>} return false; <br> } <br> };</pre></td>
 
76
          </tr>
 
77
        </table>
 
78
        
 
79
      </div>
 
80
      <p>The event receiver for moving a scene node is ready. So lets just create 
 
81
        an Irrlicht Device and the scene node we want to move. We also create 
 
82
        some other additional scene nodes, to show that there are also some different 
 
83
        possibilities to move and animate scene nodes.</p>
 
84
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
 
85
        <tr> 
 
86
          <td><pre>int main()<br>{<br>   MyEventReceiver receiver;
 
87
   
 
88
  device = createDevice(video::EDT_OPENGL, core::dimension2d&lt;s32&gt;(640, 480),
 
89
       16, false, false, false, &amp;receiver);</pre>
 
90
            <pre>   video::IVideoDriver* driver = device-&gt;getVideoDriver();
 
91
   scene::ISceneManager* smgr = device-&gt;getSceneManager();</pre>
 
92
            </td>
 
93
        </tr>
 
94
      </table>
 
95
      <p> Create the node for moving it with the 'W' and 'S' key. We create a 
 
96
        sphere node, which is a built in geometric primitive scene node.
 
97
        We place the node at (0,0,30) and assign a texture to it to let it look 
 
98
        a little bit more interesting.</p>
 
99
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
 
100
        <tr> 
 
101
          <td><pre>node = smgr-&gt;addSphereSceneNode();
 
102
node-&gt;setPosition(core::vector3df(0,0,30));
 
103
node-&gt;setMaterialFlag(video::EMF_LIGHTING, false);
 
104
node-&gt;setMaterialTexture(0, driver-&gt;getTexture(&quot;../../media/wall.bmp&quot;));</pre></td>
 
105
        </tr>
 
106
      </table>
 
107
      <p>Now we create another node, moving using a scene node animator. Scene 
 
108
        node animators modify scene nodes and can be attached to any scene node 
 
109
        like<br>
 
110
        mesh scene nodes, billboards, lights and even camera scene nodes. Scene 
 
111
        node animators are not only able to modify the position of a scene node, 
 
112
        they can<br>
 
113
        also animate the textures of an object for example. We create a test scene 
 
114
        node again an attach a 'fly circle' scene node to it, letting this node 
 
115
        fly around our first test scene node.</p>
 
116
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
 
117
        <tr> 
 
118
          <td><pre>scene::ISceneNode* n = smgr-&gt;addCubeSceneNode();
 
119
n-&gt;setMaterialTexture(0, driver-&gt;getTexture(&quot;../../media/t351sml.jpg&quot;));
 
120
n-&gt;setMaterialFlag(video::EMF_LIGHTING, false);
 
121
scene::ISceneNodeAnimator* anim = 
 
122
        smgr-&gt;createFlyCircleAnimator(core::vector3df(0,0,30), 20.0f);
 
123
n-&gt;addAnimator(anim);
 
124
anim-&gt;drop();</pre></td>
 
125
        </tr>
 
126
      </table>
 
127
      <p>The last scene node we add to show possibilities of scene node animators 
 
128
        is a md2 model, which uses a 'fly straight' animator to run between to 
 
129
        points.</p>
 
130
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
 
131
        <tr> 
 
132
          <td> <pre>scene::IAnimatedMeshSceneNode* anms = smgr-&gt;addAnimatedMeshSceneNode(<br>                smgr-&gt;getMesh(&quot;../../media/sydney.md2&quot;));
 
133
 
 
134
if (n)<br>      {<br>           anim = smgr-&gt;createFlyStraightAnimator(core::vector3df(100,0,60), <br>                       core::vector3df(-100,0,60), 10000, true);<br>           anms-&gt;addAnimator(anim);<br>         anim-&gt;drop();</pre>
 
135
            </td>
 
136
        </tr>
 
137
      </table>
 
138
      <p>To make to model look right we set the frames between which 
 
139
        the animation should loop, rotate the model around 180 degrees, and adjust 
 
140
        the animation speed and the texture.<br>
 
141
        To set the right animation (frames and speed), we would also be able to 
 
142
        just call &quot;anms-&gt;setMD2Animation(scene::EMAT_RUN)&quot; for the 
 
143
        'run' animation instead of &quot;setFrameLoop&quot; and &quot;setAnimationSpeed&quot;, 
 
144
        but this only works with MD2 animations, and so you know how to start 
 
145
        other animations.</p>
 
146
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
 
147
        <tr> 
 
148
          <td> <pre>   anms-&gt;setMaterialFlag(video::EMF_LIGHTING, false);<br>   anms-&gt;setFrameLoop(320, 360);
 
149
   anms-&gt;setAnimationSpeed(30);<br>   anms-&gt;setRotation(core::vector3df(0,180.0f,0));<br>   anms-&gt;setMaterialTexture(0, driver-&gt;getTexture(&quot;../../media/sydney.bmp&quot;));<br>}<br></pre></td>
 
150
        </tr>
 
151
      </table>
 
152
      <p>To be able to look at and move around in this scene, we create a first 
 
153
        person shooter style camera and make the mouse cursor invisible.</p>
 
154
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
 
155
        <tr> 
 
156
          <td> <pre>smgr-&gt;addCameraSceneNodeFPS(0, 100.0f, 100.0f);<br>device-&gt;getCursorControl()-&gt;setVisible(false); </pre></td>
 
157
        </tr>
 
158
      </table>
 
159
      <p>We have done everything, so lets draw it. We also write the current frames 
 
160
        per second and the name of the driver to the caption of the window.</p>
 
161
      <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
 
162
        <tr> 
 
163
          <td> <pre>int lastFPS = -1;</pre>
 
164
            <pre>while(device-&gt;run())
 
165
{
 
166
     driver-&gt;beginScene(true, true, video::SColor(255,90,90,156));
 
167
     smgr-&gt;drawAll();
 
168
     driver-&gt;endScene();</pre>
 
169
            <pre>     int fps = driver-&gt;getFPS();</pre>
 
170
            <pre>     if (lastFPS != fps)
 
171
     {
 
172
        wchar_t tmp[1024];
 
173
        swprintf(tmp, 1024, L&quot;Movement Example - Irrlicht Engine (%ls)(fps:%d)&quot;,<br>        driver-&gt;getName(), fps);</pre>
 
174
            <pre>       device-&gt;setWindowCaption(tmp);
 
175
       lastFPS = fps;
 
176
     }
 
177
}
 
178
 
 
179
device-&gt;drop();<br>return 0;<br>}</pre></td>
 
180
        </tr>
 
181
      </table>
 
182
      <p>That's it. Compile and play around with the program. </p>
 
183
      <p>&nbsp;</p></td>
 
184
  </tr>
 
185
</table>
 
186
<p>&nbsp;</p>
 
187
      </body>
 
188
</html>