/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- capture_location
- get_network
- split_horizontally
- split_vertically
- add_view
- paintEvent
- paintGL
- resizeGL
- _get_view_index_with_focus
- keyPressEvent
- home
- forward
- backward
- left
- right
- up
- down
- zoom
- _get_transformation
- _set_transformation
- roll
- pitch
- yaw
- keyReleaseEvent
- mouseMoveEvent
- mousePressEvent
- mouseReleaseEvent
- wheelEvent
- event
- onHome
- onResize
- getEventQueue
1 #include "view/NetworkViewer.hpp"
2
3 NetworkViewer::NetworkViewer( Network * network
4 , QWidget * parent
5 , const QGLWidget* share_widget
6 , Qt::WindowFlags f
7 ) : network(network)
8 , QGLWidget( parent
9 , share_widget
10 , f
11 )
12 , _graphics_window( new osgViewer::GraphicsWindowEmbedded( this->x()
13 , this->y()
14 , this->width()
15 , this->height()
16 )
17 )
18 , _viewer( new osgViewer::CompositeViewer )
19 , capture_format("jpeg")
20 , capture_location("/home/aviral/moogli_test")
21 {
22
23 // Light * light;
24 // LightSource * light_source;
25
26 // light = new osg::Light;
27 // light->setLightNum( 0 );
28 // light->setDiffuse( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
29 // light->setAmbient( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
30 // light->setDirection( osg::Vec3(1.0f, 0.0f, 0.0f) );
31 // light->setPosition( osg::Vec4(0,0,0,0) ); // last param w = 0.0 directional light (direction)
32 // // w = 1.0 point light (position)
33 // // light source
34 // light_source = new osg::LightSource;
35 // light_source->setLight( light );
36 // network-> node ->addChild( light_source );
37
38
39 // light = new osg::Light;
40 // light->setLightNum( 1 );
41 // light->setDiffuse( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
42 // light->setAmbient( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
43 // light->setDirection( osg::Vec3(-1.0f, 0.0f, 0.0f) );
44 // light->setPosition( osg::Vec4(0,0,0,0) ); // last param w = 0.0 directional light (direction)
45 // // w = 1.0 point light (position)
46 // // light source
47 // light_source = new osg::LightSource;
48 // light_source->setLight( light );
49 // network-> node ->addChild( light_source );
50
51 // light = new osg::Light;
52 // light->setLightNum( 2 );
53 // light->setDiffuse( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
54 // light->setAmbient( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
55 // light->setDirection( osg::Vec3(0.0f, 1.0f, 0.0f) );
56 // light->setPosition( osg::Vec4(0,0,0,0) ); // last param w = 0.0 directional light (direction)
57 // // w = 1.0 point light (position)
58 // // light source
59 // light_source = new osg::LightSource;
60 // light_source->setLight( light );
61 // network-> node ->addChild( light_source );
62
63 // light = new osg::Light;
64 // light->setLightNum( 3 );
65 // light->setDiffuse( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
66 // light->setAmbient( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
67 // light->setDirection( osg::Vec3(0.0f, -1.0f, 0.0f) );
68 // light->setPosition( osg::Vec4(0,0,0,0) ); // last param w = 0.0 directional light (direction)
69 // // w = 1.0 point light (position)
70 // // light source
71 // light_source = new osg::LightSource;
72 // light_source->setLight( light );
73 // network-> node ->addChild( light_source );
74
75 // light = new osg::Light;
76 // light->setLightNum( 4 );
77 // light->setDiffuse( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
78 // light->setAmbient( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
79 // light->setDirection( osg::Vec3(0.0f, 0.0f, 1.0f) );
80 // light->setPosition( osg::Vec4(0,0,0,0) ); // last param w = 0.0 directional light (direction)
81 // // w = 1.0 point light (position)
82 // // light source
83 // light_source = new osg::LightSource;
84 // light_source->setLight( light );
85 // network-> node ->addChild( light_source );
86
87 // light = new osg::Light;
88 // light->setLightNum( 5 );
89 // light->setDiffuse( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
90 // light->setAmbient( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
91 // light->setDirection( osg::Vec3(0.0f, 0.0f, -1.0f) );
92 // light->setPosition( osg::Vec4(0,0,0,0) ); // last param w = 0.0 directional light (direction)
93 // // w = 1.0 point light (position)
94 // // light source
95 // light_source = new osg::LightSource;
96 // light_source->setLight( light );
97 // network-> node ->addChild( light_source );
98
99 // network -> node ->getOrCreateStateSet()->setMode( GL_LIGHT0, osg::StateAttribute::OFF );
100 // network -> node ->getOrCreateStateSet()->setMode( GL_LIGHT1, osg::StateAttribute::ON );
101 // network -> node ->getOrCreateStateSet()->setMode( GL_LIGHT2, osg::StateAttribute::ON );
102 // network -> node ->getOrCreateStateSet()->setMode( GL_LIGHT3, osg::StateAttribute::ON );
103 // network -> node ->getOrCreateStateSet()->setMode( GL_LIGHT4, osg::StateAttribute::ON );
104 // network -> node ->getOrCreateStateSet()->setMode( GL_LIGHT5, osg::StateAttribute::ON );
105
106 // network -> node ->getOrCreateStateSet()->setMode( GL_LIGHT0, osg::StateAttribute::OFF );
107
108
109
110 osg::StateSet* stateSet = network -> node -> getOrCreateStateSet();
111 osg::Material* material = new osg::Material;
112
113 material->setColorMode( osg::Material::AMBIENT_AND_DIFFUSE );
114 material->setAlpha( osg::Material::FRONT_AND_BACK, 1.0);
115 material->setShininess(osg::Material::FRONT_AND_BACK, 128.0);
116 material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 1.0));
117 material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 1.0));
118 // material->setColorMode( osg::Material::EMISSION);
119 // material->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 1.0));
120 // material->setShininess(osg::Material::FRONT_AND_BACK, 128.0);
121 // material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 1.0));
122 // material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0, 1.0, 1.0, 1.0));
123
124
125 stateSet -> setAttributeAndModes( material, osg::StateAttribute::ON );
126 stateSet -> setMode( GL_DEPTH_TEST, osg::StateAttribute::ON );
127 stateSet -> setMode( GL_BLEND, osg::StateAttribute::ON );
128 stateSet -> setMode( GL_ALPHA_TEST, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);// just added this now as a test but still no luck
129 osg::ShadeModel * shade_model = new osg::ShadeModel(osg::ShadeModel::SMOOTH);
130 stateSet -> setAttributeAndModes(shade_model,osg::StateAttribute::ON);
131 stateSet -> setRenderingHint( osg::StateSet::TRANSPARENT_BIN );
132 _previous_width = this -> width();
133 _previous_height = this -> height();
134 add_view(0,0,this -> width(), this -> height());
135 // add_view(this -> width()/2, 0, this -> width()/2, this -> height());
136 _viewer->setThreadingModel( osgViewer::CompositeViewer::SingleThreaded );
137
138 // This ensures that the widget will receive keyboard events. This focus
139 // policy is not set by default. The default, Qt::NoFocus, will result in
140 // keyboard events that are ignored.
141 this->setFocusPolicy( Qt::StrongFocus );
142 this->setMinimumSize( 200
143 , 200
144 );
145
146 // Ensures that the widget receives mouse move events even though no
147 // mouse button has been pressed. We require this in order to let the
148 // graphics window switch viewports properly.
149 this->setMouseTracking( true );
150 const osg::BoundingSphere& bs = network -> node -> getBound();
151 up_distance = bs.radius() * 0.01;
152 down_distance = bs.radius() * 0.01;
153 left_distance = bs.radius() * 0.01;
154 right_distance = bs.radius() * 0.01;
155 forward_distance = bs.radius() * 0.01;
156 backward_distance = bs.radius() * 0.01;
157 zoom_factor = 0.10;
158 roll_angle = M_PI / 36.0;
159 pitch_angle = M_PI / 36.0;
160 yaw_angle = M_PI / 36.0;
161 }
162
163 Network *
164 NetworkViewer::get_network()
165 {
166 return network;
167 }
168
169 void
170 NetworkViewer::split_horizontally( unsigned int view_index
171 , unsigned int width_factor
172 )
173 {
174 float aspect_ratio = static_cast<float>( this->width()) / static_cast<float>( this->height() );
175 osgViewer::View * parent_view = _viewer -> getView (view_index);
176 osg::Camera * parent_camera = parent_view -> getCamera();
177 osg::Viewport * parent_viewport = parent_camera -> getViewport();
178 int new_parent_width = parent_viewport -> width() * (width_factor - 1) / width_factor;
179 add_view( parent_viewport -> x() + new_parent_width
180 , parent_viewport -> y()
181 , parent_viewport -> width() - new_parent_width
182 , parent_viewport -> height()
183 );
184 parent_camera->setViewport( parent_viewport -> x()
185 , parent_viewport -> y()
186 , new_parent_width
187 , parent_viewport -> height()
188 );
189 parent_camera->setProjectionMatrixAsPerspective( 30.f
190 , static_cast<float>(new_parent_width)
191 / static_cast<float>(parent_viewport -> height())
192 , 1.0f
193 , 10000.0f
194 );
195 }
196
197 void
198 NetworkViewer::split_vertically( unsigned int view_index
199 , unsigned int height_factor
200 )
201 {
202 float aspect_ratio = static_cast<float>( this->width()) / static_cast<float>( this->height() );
203 osgViewer::View * parent_view = _viewer -> getView (view_index);
204 osg::Camera * parent_camera = parent_view -> getCamera();
205 osg::Viewport * parent_viewport = parent_camera -> getViewport();
206
207 // std::cout << "parent x => " << parent_viewport -> x() << std::endl;
208 // std::cout << "parent y => " << parent_viewport -> y() << std::endl;
209 // std::cout << "parent width => " << parent_viewport -> width() << std::endl;
210 // std::cout << "parent height => " << parent_viewport -> height() << std::endl;
211
212 int new_parent_height = parent_viewport -> height() * (height_factor - 1) / height_factor;
213 add_view( parent_viewport -> x()
214 , parent_viewport -> y() + new_parent_height
215 , parent_viewport -> width()
216 , parent_viewport -> height() - new_parent_height
217 );
218
219 // std::cout << "child x => " << parent_viewport -> x() << std::endl;
220 // std::cout << "child y => " << parent_viewport -> y() + new_parent_height << std::endl;
221 // std::cout << "child width => " << parent_viewport -> width() << std::endl;
222 // std::cout << "child height => " << parent_viewport -> height() - new_parent_height << std::endl;
223
224 parent_camera->setViewport( parent_viewport -> x()
225 , parent_viewport -> y()
226 , parent_viewport -> width()
227 , new_parent_height
228 );
229 // std::cout << "parent2 x => " << parent_viewport -> x() << std::endl;
230 // std::cout << "parent2 y => " << parent_viewport -> y() << std::endl;
231 // std::cout << "parent2 width => " << parent_viewport -> width() << std::endl;
232 // std::cout << "parent2 height => " << new_parent_height << std::endl;
233
234 parent_camera->setProjectionMatrixAsPerspective( 30.f
235 , static_cast<float>(parent_viewport -> width())
236 / static_cast<float>(new_parent_height)
237 , 1.0f
238 , 10000.0f
239 );
240 }
241
242 void
243 NetworkViewer::add_view( int x
244 , int y
245 , int width
246 , int height
247 )
248 {
249 float aspect_ratio = static_cast<float>(width)
250 / static_cast<float>(height);
251
252 osg::Camera* camera = new osg::Camera;
253 camera->setViewport(x, y, width, height );
254 camera->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f, 1.f ));
255 camera->setProjectionMatrixAsPerspective( 30.f
256 , aspect_ratio
257 , 1.0f
258 , 10000.0f
259 );
260 camera->setGraphicsContext( _graphics_window );
261 osg::StateSet* stateset = camera -> getOrCreateStateSet();
262 stateset->setGlobalDefaults();
263 osgViewer::View* view = new osgViewer::View();
264 view->setCamera( camera );
265 view->setSceneData( network -> node.get() );
266 // view->addEventHandler( new osgViewer::StatsHandler );
267 view->setCameraManipulator( new osgGA::TrackballManipulator() );
268 _viewer->addView( view );
269 view -> setLightingMode(osg::View::HEADLIGHT);
270 // osg::Light * light = view -> getLight();
271 // light->setDiffuse( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
272 // light->setAmbient( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
273
274 unsigned int index = _viewer -> getNumViews() - 1;
275 QDir().mkdir((capture_location + "/" + to_string(index)).c_str());
276 auto* capture_operation =
277 new osgViewer::ScreenCaptureHandler::WriteToFile( capture_location + "/" + to_string(index)
278 , capture_format
279 );
280
281 // auto* capture_handler = new MorphologyCaptureHandler(
282 // dynamic_cast<osgViewer::ScreenCaptureHandler::CaptureOperation *>(capture_operation)
283 // );
284 // view -> addEventHandler(capture_handler);
285 }
286
287 NetworkViewer::~NetworkViewer()
288 {
289 }
290
291 void
292 NetworkViewer::paintEvent( QPaintEvent* /* paintEvent */ )
293 {
294 // this->makeCurrent();
295 QPainter painter( this );
296 painter.setRenderHint( QPainter::Antialiasing );
297 this->paintGL();
298 painter.end();
299 this->swapBuffers();
300 this->doneCurrent();
301 }
302
303 void
304 NetworkViewer::paintGL()
305 {
306 _viewer -> frame();
307 }
308
309 void
310 NetworkViewer::resizeGL( int width, int height )
311 {
312 this->getEventQueue() -> windowResize( this->x(), this->y(), width, height );
313 _graphics_window -> resized( this->x(), this->y(), width, height );
314 this->onResize( width, height );
315 }
316
317 unsigned int
318 NetworkViewer::_get_view_index_with_focus()
319 {
320 osgViewer::ViewerBase::Views views;
321 osgViewer::View * view = _viewer -> getViewWithFocus();
322 _viewer -> getViews(views);
323 auto iter = std::find(views.begin(),views.end(),view);
324 if(iter == views.end()) { return 0; }
325 return (iter - views.begin());
326 }
327
328 void
329 NetworkViewer::keyPressEvent( QKeyEvent* event )
330 {
331 QString keyString = event->text();
332 const char* keyData = keyString.toLocal8Bit().data();
333 unsigned int view_index = _get_view_index_with_focus();
334 // std::cout << "Index => " << view_index << std::endl;
335 switch(event->key())
336 {
337 case Qt::Key_Q : if(event->modifiers() & Qt::ControlModifier)
338 {
339 QApplication::quit();
340 }
341 // case Qt::Key_P : if(event->modifiers() & Qt::ControlModifier)
342 // {
343 // QApplication::quit();
344 // }
345 // case Qt::Key_S : if(event->modifiers() & Qt::ShiftModifier)
346 // {
347 // toggle_horizontal_split();
348 // }
349 // case Qt::Key_Z : if(event->modifiers() & Qt::ShiftModifier)
350 // {
351 // toggle_vertical_split();
352 // }
353 // case Qt::Key_S : if(event->modifiers() & Qt::ShiftModifier)
354 // {
355 // capture_continuous_toggle(view_index);
356 // }
357 // else
358 // {
359 // capture_once(view_index);
360 // }
361 break;
362 case Qt::Key_Space : home(view_index);
363 break;
364 case Qt::Key_Up : up(up_distance, view_index);
365 break;
366 case Qt::Key_Down : down(down_distance, view_index);
367 break;
368 case Qt::Key_Left : left(left_distance, view_index);
369 break;
370 case Qt::Key_Right : right(right_distance, view_index);
371 break;
372 case Qt::Key_F : forward(forward_distance, view_index);
373 break;
374 case Qt::Key_B : backward(backward_distance, view_index);
375 break;
376 case Qt::Key_Plus : zoom(zoom_factor, view_index);
377 break;
378 case Qt::Key_Equal : zoom(zoom_factor, view_index);
379 break;
380 case Qt::Key_Period : zoom(zoom_factor, view_index);
381 break;
382 case Qt::Key_Greater : zoom(zoom_factor, view_index);
383 break;
384 case Qt::Key_Minus : zoom(-zoom_factor, view_index);
385 break;
386 case Qt::Key_Underscore : zoom(-zoom_factor, view_index);
387 break;
388 case Qt::Key_Comma : zoom(-zoom_factor, view_index);
389 break;
390 case Qt::Key_Less : zoom(-zoom_factor, view_index);
391 break;
392 case Qt::Key_R : if(event->modifiers() & Qt::ShiftModifier)
393 {
394 roll(-roll_angle, view_index);
395 }
396 else
397 {
398 roll(roll_angle, view_index);
399 }
400 break;
401 case Qt::Key_P : if(event->modifiers() & Qt::ShiftModifier)
402 {
403 pitch(-pitch_angle, view_index);
404 }
405 else
406 {
407 pitch(pitch_angle, view_index);
408 }
409 break;
410 case Qt::Key_Y : if(event->modifiers() & Qt::ShiftModifier)
411 {
412 yaw(-yaw_angle, view_index);
413 }
414 else
415 {
416 yaw(yaw_angle, view_index);
417 }
418 break;
419 default : break;
420 }
421 }
422
423 // void
424 // NetworkViewer::capture_continuous_toggle(unsigned int index)
425 // {
426 // osgViewer::View * view = _viewer -> getView(index);
427 // auto * handler = dynamic_cast<MorphologyCaptureHandler *>((view -> getEventHandlers()).front().get());
428 // handler -> toggle_capture();
429 // }
430
431 // void
432 // NetworkViewer::capture_once(unsigned int index)
433 // {
434 // osgViewer::View * view = _viewer -> getView(index);
435 // auto * handler = dynamic_cast<MorphologyCaptureHandler *>((view -> getEventHandlers()).front().get());
436 // handler->setFramesToCapture(1);
437 // }
438
439 void
440 NetworkViewer::home(unsigned int index)
441 {
442 osgViewer::View * view = _viewer -> getView(index);
443 osgGA::TrackballManipulator * manipulator = dynamic_cast<osgGA::TrackballManipulator *>(view -> getCameraManipulator());
444 manipulator -> home(0.0);
445 }
446
447 void
448 NetworkViewer::forward(double distance_delta, unsigned int index)
449 {
450 osg::Vec3d eye;
451 osg::Vec3d center;
452 osg::Vec3d up;
453 osg::Vec3d look;
454 osg::Vec3d side;
455 double distance;
456 _get_transformation(index, eye, center, distance, up, look, side);
457 _set_transformation(index, eye + look * distance_delta, center + look * distance_delta, up);
458 }
459
460 void
461 NetworkViewer::backward(double distance_delta, unsigned int index)
462 {
463 osg::Vec3d eye;
464 osg::Vec3d center;
465 osg::Vec3d up;
466 osg::Vec3d look;
467 osg::Vec3d side;
468 double distance;
469 _get_transformation(index, eye, center, distance, up, look, side);
470 _set_transformation(index, eye - look * distance_delta, center - look * distance_delta, up);
471 }
472
473 void
474 NetworkViewer::left(double distance_delta, unsigned int index)
475 {
476 osg::Vec3d eye;
477 osg::Vec3d center;
478 osg::Vec3d up;
479 osg::Vec3d look;
480 osg::Vec3d side;
481 double distance;
482 _get_transformation(index, eye, center, distance, up, look, side);
483 _set_transformation(index, eye + side * distance_delta, center + side * distance_delta, up);
484 }
485
486 void
487 NetworkViewer::right(double distance_delta, unsigned int index)
488 {
489 osg::Vec3d eye;
490 osg::Vec3d center;
491 osg::Vec3d up;
492 osg::Vec3d look;
493 osg::Vec3d side;
494 double distance;
495 _get_transformation(index, eye, center, distance, up, look, side);
496 _set_transformation(index, eye - side * distance_delta, center - side * distance_delta, up);
497 }
498
499 void
500 NetworkViewer::up(double distance_delta, unsigned int index)
501 {
502 osg::Vec3d eye;
503 osg::Vec3d center;
504 osg::Vec3d up;
505 osg::Vec3d look;
506 osg::Vec3d side;
507 double distance;
508 _get_transformation(index, eye, center, distance, up, look, side);
509 _set_transformation(index, eye - up * distance_delta, center - up * distance_delta, up);
510 }
511
512 void
513 NetworkViewer::down(double distance_delta, unsigned int index)
514 {
515 osg::Vec3d eye;
516 osg::Vec3d center;
517 osg::Vec3d up;
518 osg::Vec3d look;
519 osg::Vec3d side;
520 double distance;
521 _get_transformation(index, eye, center, distance, up, look, side);
522 _set_transformation(index, eye + up * distance_delta, center + up * distance_delta, up);
523 }
524
525 void
526 NetworkViewer::zoom(double factor, unsigned int index)
527 {
528 osg::Vec3d eye;
529 osg::Vec3d center;
530 osg::Vec3d up;
531 osg::Vec3d look;
532 osg::Vec3d side;
533 double distance;
534 _get_transformation(index, eye, center, distance, up, look, side);
535 _set_transformation(index, eye + look * factor * distance, center, up);
536 }
537
538 void
539 NetworkViewer::_get_transformation( unsigned int index
540 , osg::Vec3d & eye
541 , osg::Vec3d & center
542 , double & distance
543 , osg::Vec3d & up
544 , osg::Vec3d & look
545 , osg::Vec3d & side
546 )
547 {
548 osgViewer::View * view = _viewer -> getView(index);
549 osgGA::TrackballManipulator * manipulator = dynamic_cast<osgGA::TrackballManipulator *>(view -> getCameraManipulator());
550 manipulator -> getTransformation(eye, center, up);
551 up.normalize();
552 look = center - eye;
553 distance = look.normalize();
554 side = look ^ up;
555 side.normalize();
556 }
557
558 void
559 NetworkViewer::_set_transformation( unsigned int index
560 , const osg::Vec3d & eye
561 , const osg::Vec3d & center
562 , const osg::Vec3d & up
563 )
564 {
565 osgViewer::View * view = _viewer -> getView(index);
566 osgGA::TrackballManipulator * manipulator = dynamic_cast<osgGA::TrackballManipulator *>(view -> getCameraManipulator());
567 manipulator -> setTransformation(eye, center, up);
568 }
569
570 void
571 NetworkViewer::roll(double angle, unsigned int index)
572 {
573 osg::Vec3d eye;
574 osg::Vec3d center;
575 osg::Vec3d up;
576 osg::Vec3d look;
577 osg::Vec3d side;
578 double distance;
579 _get_transformation(index, eye, center, distance, up, look, side);
580 Quat rotation = Quat(-angle, look);
581 Vec3f rotated_up = rotation * up;
582 rotated_up.normalize();
583 _set_transformation(index, eye, center, rotated_up);
584
585 }
586
587 void
588 NetworkViewer::pitch(double angle, unsigned int index)
589 {
590 osg::Vec3d eye;
591 osg::Vec3d center;
592 osg::Vec3d up;
593 osg::Vec3d look;
594 osg::Vec3d side;
595 double distance;
596 _get_transformation(index, eye, center, distance, up, look, side);
597 Quat rotation = Quat(-angle, side);
598 Vec3f rotated_up = rotation * up;
599 rotated_up.normalize();
600 Vec3f rotated_look = rotation * look;
601 rotated_look.normalize();
602 _set_transformation(index, center - rotated_look * distance ,center, rotated_up);
603 }
604
605 void
606 NetworkViewer::yaw(double angle, unsigned int index)
607 {
608 osg::Vec3d eye;
609 osg::Vec3d center;
610 osg::Vec3d up;
611 osg::Vec3d look;
612 osg::Vec3d side;
613 double distance;
614 _get_transformation(index, eye, center, distance, up, look, side);
615 Quat rotation = Quat(-angle, up);
616 Vec3f rotated_look = rotation * look;
617 rotated_look.normalize();
618 _set_transformation(index, center - rotated_look * distance ,center, up);
619 }
620
621
622 void
623 NetworkViewer::keyReleaseEvent( QKeyEvent* event )
624 {
625 QString keyString = event->text();
626 const char* keyData = keyString.toLocal8Bit().data();
627
628 this->getEventQueue()->keyRelease( osgGA::GUIEventAdapter::KeySymbol( *keyData ) );
629 }
630
631 void
632 NetworkViewer::mouseMoveEvent( QMouseEvent* event )
633 {
634 this->getEventQueue()->mouseMotion( static_cast<float>( event->x() )
635 , static_cast<float>( event->y() )
636 );
637 }
638
639 void
640 NetworkViewer::mousePressEvent( QMouseEvent* event )
641 {
642 // 1 = left mouse button
643 // 2 = middle mouse button
644 // 3 = right mouse button
645
646 unsigned int button = 0;
647
648 switch( event->button() )
649 {
650 case Qt::LeftButton: button = 1;
651 break;
652
653 case Qt::MiddleButton: button = 2;
654 break;
655
656 case Qt::RightButton: button = 3;
657 break;
658
659 default: break;
660 }
661
662 this->getEventQueue()->mouseButtonPress( static_cast<float>( event->x() )
663 , static_cast<float>( event->y() )
664 , button
665 );
666 }
667
668 void
669 NetworkViewer::mouseReleaseEvent(QMouseEvent* event)
670 {
671
672 // 1 = left mouse button
673 // 2 = middle mouse button
674 // 3 = right mouse button
675
676 unsigned int button = 0;
677
678 switch( event->button() )
679 {
680 case Qt::LeftButton: button = 1;
681 break;
682
683 case Qt::MiddleButton: button = 2;
684 break;
685
686 case Qt::RightButton: button = 3;
687 break;
688
689 default: break;
690 }
691
692 this->getEventQueue()->mouseButtonRelease( static_cast<float>( event->x() )
693 , static_cast<float>( event->y() )
694 , button
695 );
696
697 }
698
699 void
700 NetworkViewer::wheelEvent( QWheelEvent* event )
701 {
702
703 event->accept();
704 int delta = event->delta();
705
706 osgGA::GUIEventAdapter::ScrollingMotion motion = delta > 0 ? osgGA::GUIEventAdapter::SCROLL_UP
707 : osgGA::GUIEventAdapter::SCROLL_DOWN;
708
709 this->getEventQueue()->mouseScroll( motion );
710 }
711
712 bool
713 NetworkViewer::event( QEvent* event )
714 {
715 bool handled = QGLWidget::event( event );
716
717 // This ensures that the OSG widget is always going to be repainted after the
718 // user performed some interaction. Doing this in the event handler ensures
719 // that we don't forget about some event and prevents duplicate code.
720 switch( event->type() )
721 {
722 case QEvent::KeyPress:
723 case QEvent::KeyRelease:
724 case QEvent::MouseButtonDblClick:
725 case QEvent::MouseButtonPress:
726 case QEvent::MouseButtonRelease:
727 case QEvent::MouseMove:
728 case QEvent::Wheel: this->update();
729 break;
730 default: break;
731 }
732
733 return( handled );
734 }
735
736 void
737 NetworkViewer::onHome()
738 {
739 osgViewer::ViewerBase::Views views;
740 _viewer->getViews( views );
741
742 for( std::size_t i = 0; i < views.size(); i++ )
743 {
744 osgViewer::View* view = views.at(i);
745 view->home();
746 }
747 }
748
749 void
750 NetworkViewer::onResize( int width, int height )
751 {
752 // std::vector<osg::Camera*> cameras;
753 // _viewer->getCameras( cameras );
754 // float width_factor = static_cast<float>(width)
755 // / static_cast<float>(_previous_width);
756 // float height_factor = static_cast<float>(height)
757 // / static_cast<float>(_previous_height);
758 // for(osg::Camera * camera : cameras)
759 // {
760 // osg::Viewport * viewport = camera -> getViewport();
761 // cout << "x => " << viewport -> x() * width_factor << endl;
762 // cout << "y => " << viewport -> y() * height_factor << endl;
763 // camera -> setViewport( viewport -> x() * width_factor
764 // , viewport -> y() * height_factor
765 // , viewport -> width() * width_factor
766 // , viewport -> height() * height_factor
767 // );
768 // camera -> setProjectionMatrixAsPerspective( 30.f
769 // , (viewport -> width() * width_factor)
770 // / (viewport -> height() * height_factor)
771 // , 1.0f
772 // , 10000.0f
773 // );
774 // }
775 _previous_height = height;
776 _previous_width = width;
777 }
778
779 osgGA::EventQueue*
780 NetworkViewer::getEventQueue() const
781 {
782 osgGA::EventQueue* event_queue = _graphics_window -> getEventQueue();
783
784 if( event_queue ) return( event_queue );
785 else throw( std::runtime_error( "Unable to obtain valid event queue") );
786 }