source: flair-src/trunk/lib/FlairSimulator/src/Gui_impl.cpp@ 182

Last change on this file since 182 was 166, checked in by Sanahuja Guillaume, 8 years ago

m

File size: 11.6 KB
Line 
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
32using namespace irr;
33using namespace irr::video;
34using namespace irr::core;
35using namespace irr::scene;
36using namespace irr::gui;
37using namespace flair::core;
38using namespace flair::simulator;
39
40class MyEventReceiver : public IEventReceiver {
41public:
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
71private:
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
77Gui_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 /*
103env = device->getGUIEnvironment();
104IGUISkin* 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 =
124env->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
131Gui_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
139void Gui_impl::AddVisualizationCamera(VisualizationCamera* camera) {
140 cameras.push_back(camera);
141}
142
143void 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
177void 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 takeScreenshot(); // on enregistre l'image precedente
213 road_time_t time;
214 road_timerange_t tr = 0;
215 if (read_hdfile(dbtFile_r, (void *)dbtbuf, &time, &tr) != 0) {
216 vector3df vect;
217 char *buf = dbtbuf;
218 for (size_t i = 0; i < models.size(); i++) {
219 models.at(i)->ReaddbtBuf(buf);
220 buf += models.at(i)->dbtSize();
221 }
222 } else {
223 // Printf("fin play\n");
224 close_hdfile(dbtFile_r);
225 dbtFile_r = NULL;
226 free(dbtbuf);
227 for (size_t i = 0; i < models.size(); i++) {
228 models.at(i)->pimpl_->Resume();
229 }
230 }
231 } else { // mode normal
232 for (size_t i = 0; i < models.size(); i++) {
233 models.at(i)->pimpl_->UpdatePos();
234 }
235 }
236
237 driver->beginScene(true, true, video::SColor(255, 200, 200, 200));
238
239 // vue poursuite
240 //smgr->setActiveCamera(models.at(cam_id)->getFollowMeCamera()->getCameraSceneNode());
241 smgr->setActiveCamera(cameras.at(cam_id)->getCameraSceneNode());
242 driver->setViewPort(core::rect<s32>(0, 0, scene_width, scene_height));
243 smgr->drawAll(); // commente voir plus bas
244 /*
245 env->drawAll();
246 if (font)
247 {
248 font->draw(L"This demo shows that Irrlicht is also capable
249 of drawing 2D graphics.",
250 core::rect<s32>(130,10,300,50),
251 video::SColor(255,255,255,255));
252 }
253 else
254 {
255 printf("err\n");
256 }
257 device->setWindowCaption(L"toto");*/
258
259 if (dbtFile_r == NULL) {// mode normal
260 for (size_t i = 0; i < models.size(); i++) {
261 models.at(i)->pimpl_->CheckCollision();
262 }
263 }
264
265 for (size_t i = 0; i < models.size(); i++) {
266 models.at(i)->ProcessUpdate(NULL);
267 }
268
269 // on fait ca ici, devrait etre un peu plus haut
270 // mais a priori souci avec models.at(i)->pimpl_->CheckCollision();
271 // (setelipsoid?)
272 //smgr->setActiveCamera(models.at(cam_id)->getFollowMeCamera()->getCameraSceneNode());
273 smgr->setActiveCamera(cameras.at(cam_id)->getCameraSceneNode());
274 driver->setViewPort(core::rect<s32>(0, 0, scene_width, scene_height));
275 smgr->drawAll();
276
277 driver->endScene();
278
279 int fps = driver->getFPS();
280
281 if (lastFPS != fps) {
282 setWindowCaption(cam_id, fps);
283 lastFPS = fps;
284 }
285
286 if (receiver->IsKeyDown(KEY_PRIOR)) {
287 cam_id++;
288 if (cam_id >= (int)cameras.size()) cam_id = 0;
289 receiver->SetModel(getModelFromVisualizationCamera(models,cameras.at(cam_id)));
290 setWindowCaption(cam_id, fps);
291 }
292 if (receiver->IsKeyDown(KEY_NEXT)) {
293 cam_id--;
294 if (cam_id < 0) cam_id = cameras.size() - 1;
295 receiver->SetModel(getModelFromVisualizationCamera(models,cameras.at(cam_id)));
296 setWindowCaption(cam_id, fps);
297 }
298
299 // enregistrement DBT
300 if (receiver->IsKeyDown(KEY_KEY_R) && dbtFile_w == NULL) {
301 dbtFile_w = inithdFile((char *)"./record.dbt", UAV, dbtSize(models));
302 dbtbuf = (char *)malloc(dbtSize(models));
303 }
304 if (receiver->IsKeyDown(KEY_KEY_S) && dbtFile_w != NULL) {
305 close_hdfile(dbtFile_w);
306 dbtFile_w = NULL;
307 free(dbtbuf);
308 // rt_printf("stop rec\n");
309 }
310 if (dbtFile_w != NULL) {
311 Time time = GetTime();
312 vector3df vect;
313 char *buf = dbtbuf;
314
315 for (size_t i = 0; i < models.size(); i++) {
316 models.at(i)->WritedbtBuf(buf);
317 buf += models.at(i)->dbtSize();
318 }
319
320 write_hdfile(dbtFile_w, dbtbuf, (road_time_t)(time / 1000),
321 (road_timerange_t)(time % 1000), dbtSize(models));
322 }
323
324 // lecture dbt
325 if (receiver->IsKeyDown(KEY_KEY_P) && dbtFile_r == NULL) {
326 dbtFile_r = open_hdfile((char *)"./record.dbt", READ_MODE);
327 dbtbuf = (char *)malloc(dbtSize(models));
328 // on suspend les models pour ne pas interferer
329 for (size_t i = 0; i < models.size(); i++) {
330 models.at(i)->pimpl_->Suspend();
331 }
332 }
333 if (receiver->IsKeyDown(KEY_KEY_S) && dbtFile_r != NULL) {
334 // rt_printf("stop play\n");
335 close_hdfile(dbtFile_r);
336 dbtFile_r = NULL;
337 free(dbtbuf);
338 // on resume les models
339 for (size_t i = 0; i < models.size(); i++) {
340 models.at(i)->pimpl_->Resume();
341 }
342 }
343 }
344
345 receiver->SetModel(NULL);
346}
347
348Model *Gui_impl::getModelFromVisualizationCamera(std::vector<Model *> models,VisualizationCamera *camera) {
349 for (size_t i = 0; i < models.size(); i++) {
350 if(models.at(i)->getFollowMeCamera()==camera) return models.at(i);
351 }
352 return NULL;
353}
354
355void Gui_impl::setWindowCaption(int cam_id, int fps) {
356 std::ostringstream text;
357
358 text << "Cam: " << cameras.at(cam_id)->getName().c_str()
359 << ", FPS: " << fps;
360
361 device->setWindowCaption(stringw(text.str().c_str()).c_str());
362}
363
364void Gui_impl::takeScreenshot(void) {
365 static int cpt = 0;
366 // get image from the last rendered frame
367 IImage *const image = driver->createScreenShot();
368 if (image) // should always be true, but you never know. ;)
369 {
370 // construct a filename, consisting of local time and file extension
371 c8 filename[64];
372 // snprintf(filename, 64, "screenshot_%u.png",
373 // device->getTimer()->getRealTime());
374 snprintf(filename, 64, "screenshot_%u.png", cpt);
375 cpt++;
376 // write screenshot to file
377 if (!driver->writeImageToFile(image, filename))
378 device->getLogger()->log(L"Failed to take screenshot.", ELL_WARNING);
379
380 // Don't forget to drop image since we don't need it anymore.
381 image->drop();
382 }
383}
384
385size_t Gui_impl::dbtSize(std::vector<Model *> models) {
386 size_t size = 0;
387 for (size_t i = 0; i < models.size(); i++) {
388 size += models.at(i)->dbtSize();
389 }
390
391 return size;
392}
393#endif // GL
Note: See TracBrowser for help on using the repository browser.