1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
5 | // created: 2013/03/27
|
---|
6 | // filename: Gui.cpp
|
---|
7 | //
|
---|
8 | // author: Guillaume Sanahuja
|
---|
9 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
10 | //
|
---|
11 | // version: $Id: $
|
---|
12 | //
|
---|
13 | // purpose: classe definissant une Gui
|
---|
14 | //
|
---|
15 | /*********************************************************************/
|
---|
16 | #ifdef GL
|
---|
17 |
|
---|
18 | #include "Gui_impl.h"
|
---|
19 | #include "Gui.h"
|
---|
20 | #include "Simulator.h"
|
---|
21 | #include "GenericObject.h"
|
---|
22 | #include "Model.h"
|
---|
23 | #include "Model_impl.h"
|
---|
24 | #include "VisualizationCamera.h"
|
---|
25 | #include "FollowMeCamera.h"
|
---|
26 | #include <Object.h>
|
---|
27 | #include <Euler.h>
|
---|
28 | #include <irrlicht.h>
|
---|
29 | #include <unistd.h>
|
---|
30 | #include <sstream>
|
---|
31 |
|
---|
32 | using namespace irr;
|
---|
33 | using namespace irr::video;
|
---|
34 | using namespace irr::core;
|
---|
35 | using namespace irr::scene;
|
---|
36 | using namespace irr::gui;
|
---|
37 | using namespace flair::core;
|
---|
38 | using namespace flair::simulator;
|
---|
39 |
|
---|
40 | class MyEventReceiver : public IEventReceiver {
|
---|
41 | public:
|
---|
42 | // This is the one method that we have to implement
|
---|
43 | bool OnEvent(const SEvent &event) {
|
---|
44 | // Remember whether each key is down or up
|
---|
45 | if (event.EventType == EET_KEY_INPUT_EVENT)
|
---|
46 | KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
|
---|
47 |
|
---|
48 | if (model)
|
---|
49 | return model->OnEvent(event);
|
---|
50 |
|
---|
51 | return false;
|
---|
52 | }
|
---|
53 |
|
---|
54 | // This is used to check whether a key is being held down
|
---|
55 | bool IsKeyDown(EKEY_CODE keyCode) {
|
---|
56 | if (KeyIsDown[keyCode] == true) {
|
---|
57 | KeyIsDown[keyCode] = false;
|
---|
58 | return true;
|
---|
59 | } else {
|
---|
60 | return false;
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | MyEventReceiver(void) {
|
---|
65 | model = NULL;
|
---|
66 | for (u32 i = 0; i < KEY_KEY_CODES_COUNT; ++i)
|
---|
67 | KeyIsDown[i] = false;
|
---|
68 | }
|
---|
69 | void SetModel(Model *model) { this->model = model; }
|
---|
70 |
|
---|
71 | private:
|
---|
72 | // We use this array to store the current state of each key
|
---|
73 | bool KeyIsDown[KEY_KEY_CODES_COUNT];
|
---|
74 | Model *model;
|
---|
75 | };
|
---|
76 |
|
---|
77 | Gui_impl::Gui_impl(Gui *self, int app_width, int app_height, int scene_width,
|
---|
78 | int scene_height, std::string media_path,
|
---|
79 | E_DRIVER_TYPE driver_type) {
|
---|
80 | this->self = self;
|
---|
81 | dbtFile_w = NULL;
|
---|
82 | dbtFile_r = NULL;
|
---|
83 | this->media_path = media_path;
|
---|
84 | this->scene_width = scene_width;
|
---|
85 | this->scene_height = scene_height;
|
---|
86 |
|
---|
87 | device = createDevice(driver_type, dimension2d<u32>(app_width, app_height),
|
---|
88 | 16, false, false, false);
|
---|
89 | receiver = new MyEventReceiver();
|
---|
90 | device->setEventReceiver(receiver);
|
---|
91 | device->getLogger()->setLogLevel(ELL_NONE);
|
---|
92 |
|
---|
93 | device->getCursorControl()->setVisible(false);
|
---|
94 | device->setResizable(false);
|
---|
95 |
|
---|
96 | // font = device->getGUIEnvironment()->getBuiltInFont();
|
---|
97 | driver = device->getVideoDriver();
|
---|
98 | smgr = device->getSceneManager();
|
---|
99 |
|
---|
100 | smgr->setAmbientLight(video::SColorf(1, 1, 1));
|
---|
101 |
|
---|
102 | /*
|
---|
103 | env = device->getGUIEnvironment();
|
---|
104 | IGUISkin* skin = env->getSkin();
|
---|
105 | font = env->getFont("./fonthaettenschweiler.bmp");
|
---|
106 |
|
---|
107 | if (font)
|
---|
108 | skin->setFont(font);
|
---|
109 |
|
---|
110 | // create menu
|
---|
111 |
|
---|
112 | IGUIContextMenu* menu = env->addMenu();
|
---|
113 | menu->setMinSize(core::dimension2du(640,20));
|
---|
114 | menu->addItem(L"File", -1, true, true);
|
---|
115 | menu->addItem(L"View", -1, true, true);
|
---|
116 | menu->addItem(L"Camera", -1, true, true);
|
---|
117 | menu->addItem(L"Help", -1, true, true);
|
---|
118 |
|
---|
119 | // disable alpha
|
---|
120 |
|
---|
121 | for (s32 i=0; i<gui::EGDC_COUNT ; ++i)
|
---|
122 | {
|
---|
123 | video::SColor col =
|
---|
124 | env->getSkin()->getColor((gui::EGUI_DEFAULT_COLOR)i);
|
---|
125 | col.setAlpha(255);
|
---|
126 | env->getSkin()->setColor((gui::EGUI_DEFAULT_COLOR)i, col);
|
---|
127 | }
|
---|
128 | */
|
---|
129 | }
|
---|
130 |
|
---|
131 | Gui_impl::~Gui_impl() {
|
---|
132 | // printf("del Gui_impl\n");
|
---|
133 | device->drop();
|
---|
134 |
|
---|
135 | delete receiver;
|
---|
136 | // printf("del Gui_impl ok\n");
|
---|
137 | }
|
---|
138 |
|
---|
139 | void Gui_impl::AddVisualizationCamera(VisualizationCamera* camera) {
|
---|
140 | cameras.push_back(camera);
|
---|
141 | }
|
---|
142 |
|
---|
143 | void Gui_impl::setMesh(std::string file, vector3df position, vector3df rotation,
|
---|
144 | vector3df scale) {
|
---|
145 | IAnimatedMesh *mesh = smgr->getMesh(file.c_str());
|
---|
146 |
|
---|
147 | if (!mesh) {
|
---|
148 | // model could not be loaded
|
---|
149 | self->Err("Model %s could not be loaded\n", file.c_str());
|
---|
150 | return;
|
---|
151 | }
|
---|
152 |
|
---|
153 | node = smgr->addOctreeSceneNode(mesh->getMesh(0), 0, -1, 1024);
|
---|
154 | node->setPosition(position);
|
---|
155 | rotation +=
|
---|
156 | irr::core::vector3df(90, 0, Euler::ToDegree(getSimulator()->Yaw()));
|
---|
157 | node->setRotation(rotation);
|
---|
158 | for (int i = 0; i < node->getMaterialCount(); i++) {
|
---|
159 | node->getMaterial(i).TextureLayer->TextureWrapU = video::ETC_REPEAT;
|
---|
160 | node->getMaterial(i).TextureLayer->TextureWrapV = video::ETC_REPEAT;
|
---|
161 | }
|
---|
162 | // Ceillig
|
---|
163 | // node->getMaterial(0).getTextureMatrix(0).setTextureScale(scale.X/2.0,scale.Z/2.0);
|
---|
164 | // Walls
|
---|
165 | node->getMaterial(1).getTextureMatrix(0).setTextureScale(1 / (scale.Y / 2.5),
|
---|
166 | 1 / (scale.Z / 2.5));
|
---|
167 | // Floor
|
---|
168 | node->getMaterial(2).getTextureMatrix(0).setTextureScale(
|
---|
169 | 1 / (scale.X / 20.0), 1 / (scale.Z / 20.0));
|
---|
170 |
|
---|
171 | node->setScale(scale);
|
---|
172 | // selector
|
---|
173 | selector = smgr->createOctreeTriangleSelector(node->getMesh(), node, 128);
|
---|
174 | node->setTriangleSelector(selector);
|
---|
175 | }
|
---|
176 |
|
---|
177 | void Gui_impl::RunGui(std::vector<Model *> models,
|
---|
178 | std::vector<GenericObject *> objects) {
|
---|
179 | int lastFPS = -1;
|
---|
180 | int cam_id = 0;
|
---|
181 |
|
---|
182 | receiver->SetModel(models.at(0));
|
---|
183 |
|
---|
184 | for (size_t i = 0; i < models.size(); i++) {
|
---|
185 | models.at(i)->Draw();
|
---|
186 | }
|
---|
187 |
|
---|
188 | for (size_t i = 0; i < models.size(); i++) {
|
---|
189 | models.at(i)->pimpl_->MetaTriangleSelector()->addTriangleSelector(selector);
|
---|
190 | for (size_t j = 0; j < objects.size(); j++) {
|
---|
191 | models.at(i)->pimpl_->MetaTriangleSelector()->addTriangleSelector(
|
---|
192 | objects.at(j)->TriangleSelector());
|
---|
193 | }
|
---|
194 | for (size_t j = 0; j < models.size(); j++) {
|
---|
195 | if (i == j) continue;
|
---|
196 | models.at(i)->pimpl_->MetaTriangleSelector()->addTriangleSelector(
|
---|
197 | models.at(j)->pimpl_->TriangleSelector());
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | selector->drop(); // As soon as we're done with the selector, drop it.*/
|
---|
202 |
|
---|
203 | // wait all models to be started
|
---|
204 | for (size_t i = 0; i < models.size(); i++) {
|
---|
205 | models.at(i)->pimpl_->SynchronizationPoint();
|
---|
206 | }
|
---|
207 |
|
---|
208 | setWindowCaption(0, 0);
|
---|
209 |
|
---|
210 | while (device->run()) {
|
---|
211 | if (dbtFile_r != NULL) // rejeu
|
---|
212 | {
|
---|
213 | takeScreenshot(); // on enregistre l'image precedente
|
---|
214 | road_time_t time;
|
---|
215 | road_timerange_t tr = 0;
|
---|
216 | if (read_hdfile(dbtFile_r, (void *)dbtbuf, &time, &tr) != 0) {
|
---|
217 | vector3df vect;
|
---|
218 | char *buf = dbtbuf;
|
---|
219 | for (size_t i = 0; i < models.size(); i++) {
|
---|
220 | models.at(i)->ReaddbtBuf(buf);
|
---|
221 | buf += models.at(i)->dbtSize();
|
---|
222 | }
|
---|
223 | } else {
|
---|
224 | // Printf("fin play\n");
|
---|
225 | close_hdfile(dbtFile_r);
|
---|
226 | dbtFile_r = NULL;
|
---|
227 | free(dbtbuf);
|
---|
228 | for (size_t i = 0; i < models.size(); i++) {
|
---|
229 | models.at(i)->pimpl_->Resume();
|
---|
230 | }
|
---|
231 | }
|
---|
232 | } else // mode normal
|
---|
233 | {
|
---|
234 | for (size_t i = 0; i < models.size(); i++) {
|
---|
235 | models.at(i)->pimpl_->UpdatePos();
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | driver->beginScene(true, true, video::SColor(255, 200, 200, 200));
|
---|
240 |
|
---|
241 | // vue poursuite
|
---|
242 | //smgr->setActiveCamera(models.at(cam_id)->getFollowMeCamera()->getCameraSceneNode());
|
---|
243 | smgr->setActiveCamera(cameras.at(cam_id)->getCameraSceneNode());
|
---|
244 | driver->setViewPort(core::rect<s32>(0, 0, scene_width, scene_height));
|
---|
245 | smgr->drawAll(); // commente voir plus bas
|
---|
246 | /*
|
---|
247 | env->drawAll();
|
---|
248 | if (font)
|
---|
249 | {
|
---|
250 | font->draw(L"This demo shows that Irrlicht is also capable
|
---|
251 | of drawing 2D graphics.",
|
---|
252 | core::rect<s32>(130,10,300,50),
|
---|
253 | video::SColor(255,255,255,255));
|
---|
254 | }
|
---|
255 | else
|
---|
256 | {
|
---|
257 | printf("err\n");
|
---|
258 | }
|
---|
259 | device->setWindowCaption(L"toto");*/
|
---|
260 |
|
---|
261 | if (dbtFile_r == NULL) // mode normal
|
---|
262 | {
|
---|
263 | for (size_t i = 0; i < models.size(); i++) {
|
---|
264 | models.at(i)->pimpl_->CheckCollision();
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | for (size_t i = 0; i < models.size(); i++) {
|
---|
269 | models.at(i)->ProcessUpdate(NULL);
|
---|
270 | }
|
---|
271 |
|
---|
272 | // on fait ca ici, devrait etre un peu plus haut
|
---|
273 | // mais a priori souci avec models.at(i)->pimpl_->CheckCollision();
|
---|
274 | // (setelipsoid?)
|
---|
275 | //smgr->setActiveCamera(models.at(cam_id)->getFollowMeCamera()->getCameraSceneNode());
|
---|
276 | smgr->setActiveCamera(cameras.at(cam_id)->getCameraSceneNode());
|
---|
277 | driver->setViewPort(core::rect<s32>(0, 0, scene_width, scene_height));
|
---|
278 | smgr->drawAll();
|
---|
279 |
|
---|
280 | driver->endScene();
|
---|
281 |
|
---|
282 | int fps = driver->getFPS();
|
---|
283 |
|
---|
284 | if (lastFPS != fps) {
|
---|
285 | setWindowCaption(cam_id, fps);
|
---|
286 | lastFPS = fps;
|
---|
287 | }
|
---|
288 |
|
---|
289 | if (receiver->IsKeyDown(KEY_PRIOR)) {
|
---|
290 | cam_id++;
|
---|
291 | if (cam_id >= (int)cameras.size()) cam_id = 0;
|
---|
292 | receiver->SetModel(getModelFromVisualizationCamera(models,cameras.at(cam_id)));
|
---|
293 | setWindowCaption(cam_id, fps);
|
---|
294 | }
|
---|
295 | if (receiver->IsKeyDown(KEY_NEXT)) {
|
---|
296 | cam_id--;
|
---|
297 | if (cam_id < 0) cam_id = cameras.size() - 1;
|
---|
298 | receiver->SetModel(getModelFromVisualizationCamera(models,cameras.at(cam_id)));
|
---|
299 | setWindowCaption(cam_id, fps);
|
---|
300 | }
|
---|
301 |
|
---|
302 | // enregistrement DBT
|
---|
303 | if (receiver->IsKeyDown(KEY_KEY_R) && dbtFile_w == NULL) {
|
---|
304 | dbtFile_w = inithdFile((char *)"./record.dbt", UAV, dbtSize(models));
|
---|
305 | dbtbuf = (char *)malloc(dbtSize(models));
|
---|
306 | }
|
---|
307 | if (receiver->IsKeyDown(KEY_KEY_S) && dbtFile_w != NULL) {
|
---|
308 | close_hdfile(dbtFile_w);
|
---|
309 | dbtFile_w = NULL;
|
---|
310 | free(dbtbuf);
|
---|
311 | // rt_printf("stop rec\n");
|
---|
312 | }
|
---|
313 | if (dbtFile_w != NULL) {
|
---|
314 | Time time = GetTime();
|
---|
315 | vector3df vect;
|
---|
316 | char *buf = dbtbuf;
|
---|
317 |
|
---|
318 | for (size_t i = 0; i < models.size(); i++) {
|
---|
319 | models.at(i)->WritedbtBuf(buf);
|
---|
320 | buf += models.at(i)->dbtSize();
|
---|
321 | }
|
---|
322 |
|
---|
323 | write_hdfile(dbtFile_w, dbtbuf, (road_time_t)(time / 1000),
|
---|
324 | (road_timerange_t)(time % 1000), dbtSize(models));
|
---|
325 | }
|
---|
326 |
|
---|
327 | // lecture dbt
|
---|
328 | if (receiver->IsKeyDown(KEY_KEY_P) && dbtFile_r == NULL) {
|
---|
329 | dbtFile_r = open_hdfile((char *)"./record.dbt", READ_MODE);
|
---|
330 | dbtbuf = (char *)malloc(dbtSize(models));
|
---|
331 | // on suspend les models pour ne pas interferer
|
---|
332 | for (size_t i = 0; i < models.size(); i++) {
|
---|
333 | models.at(i)->pimpl_->Suspend();
|
---|
334 | }
|
---|
335 | }
|
---|
336 | if (receiver->IsKeyDown(KEY_KEY_S) && dbtFile_r != NULL) {
|
---|
337 | // rt_printf("stop play\n");
|
---|
338 | close_hdfile(dbtFile_r);
|
---|
339 | dbtFile_r = NULL;
|
---|
340 | free(dbtbuf);
|
---|
341 | // on resume les models
|
---|
342 | for (size_t i = 0; i < models.size(); i++) {
|
---|
343 | models.at(i)->pimpl_->Resume();
|
---|
344 | }
|
---|
345 | }
|
---|
346 | }
|
---|
347 |
|
---|
348 | receiver->SetModel(NULL);
|
---|
349 | }
|
---|
350 |
|
---|
351 | Model *Gui_impl::getModelFromVisualizationCamera(std::vector<Model *> models,VisualizationCamera *camera) {
|
---|
352 | for (size_t i = 0; i < models.size(); i++) {
|
---|
353 | if(models.at(i)->getFollowMeCamera()==camera) return models.at(i);
|
---|
354 | }
|
---|
355 | return NULL;
|
---|
356 | }
|
---|
357 |
|
---|
358 | void Gui_impl::setWindowCaption(int cam_id, int fps) {
|
---|
359 | std::ostringstream text;
|
---|
360 |
|
---|
361 | text << "Cam: " << cameras.at(cam_id)->getName().c_str()
|
---|
362 | << ", FPS: " << fps;
|
---|
363 |
|
---|
364 | device->setWindowCaption(stringw(text.str().c_str()).c_str());
|
---|
365 | }
|
---|
366 |
|
---|
367 | void Gui_impl::takeScreenshot(void) {
|
---|
368 | static int cpt = 0;
|
---|
369 | // get image from the last rendered frame
|
---|
370 | IImage *const image = driver->createScreenShot();
|
---|
371 | if (image) // should always be true, but you never know. ;)
|
---|
372 | {
|
---|
373 | // construct a filename, consisting of local time and file extension
|
---|
374 | c8 filename[64];
|
---|
375 | // snprintf(filename, 64, "screenshot_%u.png",
|
---|
376 | // device->getTimer()->getRealTime());
|
---|
377 | snprintf(filename, 64, "screenshot_%u.png", cpt);
|
---|
378 | cpt++;
|
---|
379 | // write screenshot to file
|
---|
380 | if (!driver->writeImageToFile(image, filename))
|
---|
381 | device->getLogger()->log(L"Failed to take screenshot.", ELL_WARNING);
|
---|
382 |
|
---|
383 | // Don't forget to drop image since we don't need it anymore.
|
---|
384 | image->drop();
|
---|
385 | }
|
---|
386 | }
|
---|
387 |
|
---|
388 | size_t Gui_impl::dbtSize(std::vector<Model *> models) {
|
---|
389 | size_t size = 0;
|
---|
390 | for (size_t i = 0; i < models.size(); i++) {
|
---|
391 | size += models.at(i)->dbtSize();
|
---|
392 | }
|
---|
393 |
|
---|
394 | return size;
|
---|
395 | }
|
---|
396 | #endif // GL
|
---|