-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cc
307 lines (267 loc) · 11 KB
/
main.cc
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/** Simulator main file. */
#include <irrlicht/irrlicht.h>
#include <sys/time.h>
#include <unistd.h>
#include <iostream>
#include "heli.h"
#include "flight_controller.h"
#include "controls.h"
#include "dashboard.h"
#include "input_event_reciever.h"
/*
In the Irrlicht Engine, everything can be found in the namespace 'irr'. So if
you want to use a class of the engine, you have to write irr:: before the name
of the class. For example to use the IrrlichtDevice write: irr::IrrlichtDevice.
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
irr:: anymore.
*/
using namespace irr;
/*
There are 5 sub namespaces in the Irrlicht Engine. Take a look at them, you can
read a detailed description of them in the documentation by clicking on the top
menu item 'Namespace List' or by using this link:
http://irrlicht.sourceforge.net/docu/namespaces.html
Like the irr namespace, we do not want these 5 sub namespaces now, to keep this
example simple. Hence, we tell the compiler again that we do not want always to
write their names.
*/
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
void add_banana(irr::scene::ISceneManager *smgr,
irr::video::IVideoDriver *driver,
const core::vector3df &position,
const core::vector3df &rotation)
{
IMesh* banana_mesh = smgr->getMesh("media/banana/source/banana.obj");
IMeshSceneNode* banana_node = smgr->addMeshSceneNode(banana_mesh);
banana_node->setPosition(position);
banana_node->setRotation(rotation);
banana_node->setScale(core::vector3df(1, 1, 1)/3);
banana_node->setMaterialFlag(EMF_LIGHTING, true);
banana_node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
banana_node->setDebugDataVisible(scene::EDS_OFF);
banana_node->setMaterialTexture(0, driver->getTexture("media/banana/textures/rgb.jpeg"));
for (unsigned int i=0; i < banana_node->getMaterialCount(); i++) {
banana_node->getMaterial(i).AmbientColor.set(255, 255, 255, 255);
}
banana_node->addShadowVolumeSceneNode();
}
/*
This is the main method. We can now use main() on every platform.
*/
int main()
{
/*
The most important function of the engine is the createDevice()
function. The IrrlichtDevice is created by it, which is the root
object for doing anything with the engine. createDevice() has 7
parameters:
- deviceType: Type of the device. This can currently be the Null-device,
one of the two software renderers, D3D8, D3D9, or OpenGL. In this
example we use EDT_SOFTWARE, but to try out, you might want to
change it to EDT_BURNINGSVIDEO, EDT_NULL, EDT_DIRECT3D8,
EDT_DIRECT3D9, or EDT_OPENGL.
- windowSize: Size of the Window or screen in FullScreenMode to be
created. In this example we use 640x480.
- bits: Amount of color bits per pixel. This should be 16 or 32. The
parameter is often ignored when running in windowed mode.
- fullscreen: Specifies if we want the device to run in fullscreen mode
or not.
- stencilbuffer: Specifies if we want to use the stencil buffer (for
drawing shadows).
- vsync: Specifies if we want to have vsync enabled, this is only useful
in fullscreen mode.
- eventReceiver: An object to receive events. We do not want to use this
parameter here, and set it to 0.
Always check the return value to cope with unsupported drivers,
dimensions, etc.
*/
EventReceiver receiver;
IrrlichtDevice *device =
createDevice( video::EDT_OPENGL, dimension2d<u32>(1600, 1200), 32,
false, true, false, &receiver);
if (!device)
return 1;
/*
Set the caption of the window to some nice text. Note that there is an
'L' in front of the string. The Irrlicht Engine uses wide character
strings when displaying text.
*/
device->setWindowCaption(L"My RC Helicopter Simulator");
/*
Get a pointer to the VideoDriver, the SceneManager and the graphical
user interface environment, so that we do not always have to write
device->getVideoDriver(), device->getSceneManager(), or
device->getGUIEnvironment().
*/
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();
/*
We add a hello world label to the window, using the GUI environment.
The text is placed at the position (10,10) as top left corner and
(260,22) as lower right corner.
*/
guienv->addStaticText(L"Hello World! This is the Irrlicht Software renderer!",
rect<s32>(10,10,260,22), true);
// Light configuration.
smgr->setAmbientLight(video::SColorf(0.5, 0.5, 0.5));
smgr->addLightSceneNode(0, core::vector3df(-200, 100, -200),
video::SColorf(0.3, 0.3, 0.3),2000);
smgr->setShadowColor(video::SColor(80,0,0,0));
/*
To show something interesting, we load a Quake 2 model and display it.
We only have to get the Mesh from the Scene Manager with getMesh() and add
a SceneNode to display the mesh with addAnimatedMeshSceneNode(). We
check the return value of getMesh() to become aware of loading problems
and other errors.
Instead of writing the filename sydney.md2, it would also be possible
to load a Maya object file (.obj), a complete Quake3 map (.bsp) or any
other supported file format. By the way, that cool Quake 2 model
called sydney was modelled by Brian Collins.
*/
IMesh* mesh = smgr->getMesh("media/BasketballStadium/source/63BasketBallZemin.obj");
if (!mesh)
{
device->drop();
return 1;
}
IMeshSceneNode* node = smgr->addMeshSceneNode(mesh);
node->setPosition(core::vector3df(0, 0, 0));
node->setScale(core::vector3df(4, 4, 4));
node->setMaterialFlag(EMF_LIGHTING, true);
node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
node->setDebugDataVisible(scene::EDS_OFF);
node->setMaterialTexture(0, driver->getTexture("media/BasketballStadium/textures/BasketZemin_Color.png"));
for (unsigned int i=0; i < node->getMaterialCount(); i++) {
node->getMaterial(i).AmbientColor.set(255, 255, 255, 255);
}
add_banana(smgr, driver, core::vector3df(-0.5, 0.05, 0.2), core::vector3df(90, 73, 0));
add_banana(smgr, driver, core::vector3df(0.9, 0.05, 0.3), core::vector3df(90, 40, 0));
add_banana(smgr, driver, core::vector3df(0.4, 0.05, 0.0), core::vector3df(90, 0, 0));
// The helicopter setup.
BellHeli heli(smgr, driver);
GyroFlightController flight_controller(&heli);
Controls controls(
&flight_controller,
// Throttle curves:
{
// Normal mode:
ControllerCurve({
ControllerCurve::Point(-1, -1),
ControllerCurve::Point(-0.5, 0.2),
ControllerCurve::Point(1, 0.2),
}),
// Idle-up mode:
ControllerCurve({
ControllerCurve::Point(-1, 0.75),
ControllerCurve::Point( 1, 0.75),
}),
},
// Blades pitch curves:
{
// Normal mode:
ControllerCurve({
ControllerCurve::Point(-1, -0.1),
ControllerCurve::Point(-0.5, -0.0),
ControllerCurve::Point( 1, 1.),
}),
// Idle-up mode:
ControllerCurve({
ControllerCurve::Point(-1, -1),
ControllerCurve::Point( 1, 1),
}),
}
);
Dashboard dashboard(driver,
controls.get_throttle_curves(),
controls.get_lift_curves(),
heli.get_params().main_rotor_max_vel
);
// Add skybox
smgr->addSkyBoxSceneNode(
driver->getTexture("media/skybox/irrlicht2_up.jpg"),
driver->getTexture("media/skybox/irrlicht2_dn.jpg"),
driver->getTexture("media/skybox/irrlicht2_lf.jpg"),
driver->getTexture("media/skybox/irrlicht2_rt.jpg"),
driver->getTexture("media/skybox/irrlicht2_ft.jpg"),
driver->getTexture("media/skybox/irrlicht2_bk.jpg"));
irr::scene::ICameraSceneNode* camera_node = device->getSceneManager()->addCameraSceneNode();
irrvec3 camera_pos(0, 1.5, -3);
camera_node->setPosition(camera_pos);
/*
Ok, now we have set up the scene, lets draw everything: We run the
device in a while() loop, until the device does not want to run any
more. This would be when the user closes the window or presses ALT+F4
(or whatever keycode closes a window).
*/
int lastFPS = -1;
struct timeval tv;
gettimeofday(&tv, NULL);
double then = tv.tv_sec*1000. + tv.tv_usec/1000.;
double time_delta;
irr::core::array<SJoystickInfo> joystickInfo;
device->activateJoysticks(joystickInfo);
while(device->run())
{
// calculate the delta time, and make sure it does not exceed 100 fps
double now;
gettimeofday(&tv, NULL);
now = tv.tv_sec*1000. + tv.tv_usec/1000.;
time_delta = (now - then) / 1000.;
then = now;
time_delta = time_delta > 0.03 ? 0.03 : time_delta;
// Show FPS.
int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw str = L"RC Heli Simulator";
str += driver->getName();
str += "] FPS:";
str += fps;
device->setWindowCaption(str.c_str());
lastFPS = fps;
}
// Update the helicopter.
UserInput user_input = receiver.update_input(time_delta);
heli.update(
time_delta,
irrvec3(0, 0, 0),
controls.get_servo_data(user_input.controls_input, time_delta)
);
// Apply external force on the helicopter touch points.
heli.reset_force();
std::vector<BaseHeli::TouchPoint> touchpoints = heli.get_touchpoints_in_world();
for (unsigned int i=0; i<touchpoints.size(); i++) {
BaseHeli::TouchPoint tp = touchpoints[i];
if (tp.pos_in_world.Y < 0) {
irrvec3 tp_force = irrvec3(0, -500*tp.pos_in_world.Y, 0);
tp_force += - tp.vel_in_world * irrvec3(15, 10, 15) * (-tp.pos_in_world.Y / 0.02);
heli.add_force(i, tp_force);
}
}
// Draw.
camera_node->setTarget(heli.get_position());
driver->beginScene(true, true, video::SColor(255,200,200,200));
smgr->drawAll();
dashboard.update_ui(controls.get_telemetry(), heli.get_telemetry());
driver->endScene();
}
/*
After we are done with the render loop, we have to delete the Irrlicht
Device created before with createDevice(). In the Irrlicht Engine, you
have to delete all objects you created with a method or function which
starts with 'create'. The object is simply deleted by calling ->drop().
See the documentation at irr::IReferenceCounted::drop() for more
information.
*/
device->drop();
return 0;
}
/*
That's it. Compile and run.
**/