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

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

simu cameras

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