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

Last change on this file since 69 was 69, checked in by Sanahuja Guillaume, 6 years ago

refonte camera simu

File size: 11.5 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/*
101 // camera
102 camera = smgr->addCameraSceneNode();
103
104 camera->setAspectRatio(
105 (float)scene_width / (float)scene_height); // on force a cause du view port
106
107 camera->setUpVector(vector3df(0, 0, 1));
108
109 FixedCamera* animator = new FixedCamera(vector3df(1,1,1));
110 camera->addAnimator(animator);
111
112 camera->setFarValue(8000);*/
113 /*
114env = device->getGUIEnvironment();
115IGUISkin* skin = env->getSkin();
116 font = env->getFont("./fonthaettenschweiler.bmp");
117
118 if (font)
119 skin->setFont(font);
120
121 // create menu
122
123 IGUIContextMenu* menu = env->addMenu();
124 menu->setMinSize(core::dimension2du(640,20));
125 menu->addItem(L"File", -1, true, true);
126 menu->addItem(L"View", -1, true, true);
127 menu->addItem(L"Camera", -1, true, true);
128 menu->addItem(L"Help", -1, true, true);
129
130 // disable alpha
131
132 for (s32 i=0; i<gui::EGDC_COUNT ; ++i)
133 {
134 video::SColor col =
135env->getSkin()->getColor((gui::EGUI_DEFAULT_COLOR)i);
136 col.setAlpha(255);
137 env->getSkin()->setColor((gui::EGUI_DEFAULT_COLOR)i, col);
138 }
139 */
140}
141
142Gui_impl::~Gui_impl() {
143 // printf("del Gui_impl\n");
144 device->drop();
145
146 delete receiver;
147 // printf("del Gui_impl ok\n");
148}
149
150void Gui_impl::setMesh(std::string file, vector3df position, vector3df rotation,
151 vector3df scale) {
152 IAnimatedMesh *mesh = smgr->getMesh(file.c_str());
153
154 if (!mesh) {
155 // model could not be loaded
156 self->Err("Model %s could not be loaded\n", file.c_str());
157 return;
158 }
159
160 node = smgr->addOctreeSceneNode(mesh->getMesh(0), 0, -1, 1024);
161 node->setPosition(position);
162 rotation +=
163 irr::core::vector3df(90, 0, Euler::ToDegree(getSimulator()->Yaw()));
164 node->setRotation(rotation);
165 for (int i = 0; i < node->getMaterialCount(); i++) {
166 node->getMaterial(i).TextureLayer->TextureWrapU = video::ETC_REPEAT;
167 node->getMaterial(i).TextureLayer->TextureWrapV = video::ETC_REPEAT;
168 }
169 // Ceillig
170 // node->getMaterial(0).getTextureMatrix(0).setTextureScale(scale.X/2.0,scale.Z/2.0);
171 // Walls
172 node->getMaterial(1).getTextureMatrix(0).setTextureScale(1 / (scale.Y / 2.5),
173 1 / (scale.Z / 2.5));
174 // Floor
175 node->getMaterial(2).getTextureMatrix(0).setTextureScale(
176 1 / (scale.X / 20.0), 1 / (scale.Z / 20.0));
177
178 node->setScale(scale);
179 // selector
180 selector = smgr->createOctreeTriangleSelector(node->getMesh(), node, 128);
181 node->setTriangleSelector(selector);
182}
183
184void Gui_impl::RunGui(std::vector<Model *> models,
185 std::vector<GenericObject *> objects) {
186 int lastFPS = -1;
187 int cam_id = 0;
188
189 receiver->SetModel(models.at(cam_id));
190
191 for (size_t i = 0; i < models.size(); i++) {
192 models.at(i)->Draw();
193 }
194
195 for (size_t i = 0; i < models.size(); i++) {
196 models.at(i)->pimpl_->MetaTriangleSelector()->addTriangleSelector(selector);
197 for (size_t j = 0; j < objects.size(); j++) {
198 models.at(i)->pimpl_->MetaTriangleSelector()->addTriangleSelector(
199 objects.at(j)->TriangleSelector());
200 }
201 for (size_t j = 0; j < models.size(); j++) {
202 if (i == j) continue;
203 models.at(i)->pimpl_->MetaTriangleSelector()->addTriangleSelector(
204 models.at(j)->pimpl_->TriangleSelector());
205 }
206 }
207
208 selector->drop(); // As soon as we're done with the selector, drop it.*/
209
210 // wait all models to be started
211 for (size_t i = 0; i < models.size(); i++) {
212 models.at(i)->pimpl_->SynchronizationPoint();
213 }
214
215 setWindowCaption(models.at(0), 0);
216
217 while (device->run()) {
218 if (dbtFile_r != NULL) // rejeu
219 {
220 takeScreenshot(); // on enregistre l'image precedente
221 road_time_t time;
222 road_timerange_t tr = 0;
223 if (read_hdfile(dbtFile_r, (void *)dbtbuf, &time, &tr) != 0) {
224 vector3df vect;
225 char *buf = dbtbuf;
226 for (size_t i = 0; i < models.size(); i++) {
227 models.at(i)->ReaddbtBuf(buf);
228 buf += models.at(i)->dbtSize();
229 }
230 } else {
231 // Printf("fin play\n");
232 close_hdfile(dbtFile_r);
233 dbtFile_r = NULL;
234 free(dbtbuf);
235 for (size_t i = 0; i < models.size(); i++) {
236 models.at(i)->pimpl_->Resume();
237 }
238 }
239 } else // mode normal
240 {
241 for (size_t i = 0; i < models.size(); i++) {
242 models.at(i)->pimpl_->UpdatePos();
243 }
244 }
245
246 driver->beginScene(true, true, video::SColor(255, 200, 200, 200));
247
248 // vue poursuite
249 smgr->setActiveCamera(models.at(cam_id)->getFollowMeCamera()->getCameraSceneNode());
250 driver->setViewPort(core::rect<s32>(0, 0, scene_width, scene_height));
251 smgr->drawAll(); // commente voir plus bas
252 /*
253 env->drawAll();
254 if (font)
255 {
256 font->draw(L"This demo shows that Irrlicht is also capable
257 of drawing 2D graphics.",
258 core::rect<s32>(130,10,300,50),
259 video::SColor(255,255,255,255));
260 }
261 else
262 {
263 printf("err\n");
264 }
265 device->setWindowCaption(L"toto");*/
266
267 if (dbtFile_r == NULL) // mode normal
268 {
269 for (size_t i = 0; i < models.size(); i++) {
270 models.at(i)->pimpl_->CheckCollision();
271 }
272 }
273
274 for (size_t i = 0; i < models.size(); i++) {
275 models.at(i)->ProcessUpdate(NULL);
276 }
277
278 // on fait ca ici, devrait etre un peu plus haut
279 // mais a priori souci avec models.at(i)->pimpl_->CheckCollision();
280 // (setelipsoid?)
281 smgr->setActiveCamera(models.at(cam_id)->getFollowMeCamera()->getCameraSceneNode());
282 driver->setViewPort(core::rect<s32>(0, 0, scene_width, scene_height));
283 smgr->drawAll();
284
285 driver->endScene();
286
287 int fps = driver->getFPS();
288 // printf("fps %i\n",fps);
289 if (lastFPS != fps) {
290 setWindowCaption(models.at(cam_id), fps);
291 lastFPS = fps;
292 }
293
294 if (receiver->IsKeyDown(KEY_PRIOR)) {
295 cam_id++;
296 if (cam_id >= (int)models.size()) cam_id = 0;
297 receiver->SetModel(models.at(cam_id));
298 setWindowCaption(models.at(cam_id), fps);
299 }
300 if (receiver->IsKeyDown(KEY_NEXT)) {
301 cam_id--;
302 if (cam_id < 0) cam_id = models.size() - 1;
303 receiver->SetModel(models.at(cam_id));
304 setWindowCaption(models.at(cam_id), fps);
305 }
306
307 // enregistrement DBT
308 if (receiver->IsKeyDown(KEY_KEY_R) && dbtFile_w == NULL) {
309 dbtFile_w = inithdFile((char *)"./record.dbt", UAV, dbtSize(models));
310 dbtbuf = (char *)malloc(dbtSize(models));
311 }
312 if (receiver->IsKeyDown(KEY_KEY_S) && dbtFile_w != NULL) {
313 close_hdfile(dbtFile_w);
314 dbtFile_w = NULL;
315 free(dbtbuf);
316 // rt_printf("stop rec\n");
317 }
318 if (dbtFile_w != NULL) {
319 Time time = GetTime();
320 vector3df vect;
321 char *buf = dbtbuf;
322
323 for (size_t i = 0; i < models.size(); i++) {
324 models.at(i)->WritedbtBuf(buf);
325 buf += models.at(i)->dbtSize();
326 }
327
328 write_hdfile(dbtFile_w, dbtbuf, (road_time_t)(time / 1000),
329 (road_timerange_t)(time % 1000), dbtSize(models));
330 }
331
332 // lecture dbt
333 if (receiver->IsKeyDown(KEY_KEY_P) && dbtFile_r == NULL) {
334 dbtFile_r = open_hdfile((char *)"./record.dbt", READ_MODE);
335 dbtbuf = (char *)malloc(dbtSize(models));
336 // on suspend les models pour ne pas interferer
337 for (size_t i = 0; i < models.size(); i++) {
338 models.at(i)->pimpl_->Suspend();
339 }
340 }
341 if (receiver->IsKeyDown(KEY_KEY_S) && dbtFile_r != NULL) {
342 // rt_printf("stop play\n");
343 close_hdfile(dbtFile_r);
344 dbtFile_r = NULL;
345 free(dbtbuf);
346 // on resume les models
347 for (size_t i = 0; i < models.size(); i++) {
348 models.at(i)->pimpl_->Resume();
349 }
350 }
351 }
352
353 receiver->SetModel(NULL);
354}
355
356void Gui_impl::setWindowCaption(Object *object, int fps) {
357 std::ostringstream text;
358 text << "Cam: " << object->ObjectName().c_str()
359 << ", Kbd: " << object->ObjectName().c_str() << ", 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.