BALL  1.4.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
scene.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_VIEW_WIDGETS_SCENE_H
6 #define BALL_VIEW_WIDGETS_SCENE_H
7 
8 #ifndef BALL_VIEW_KERNEL_MODULARWIDGET_H
10 #endif
11 
12 #ifndef BALL_VIEW_RENDERING_GLRENDERER_H
14 #endif
15 
16 #ifndef BALL_VIEW_KERNEL_COMMON_H
17 # include <BALL/VIEW/KERNEL/common.h>
18 #endif
19 
20 #ifndef BALL_VIEW_RENDERING_RENDERWINDOW_H
22 #endif
23 
24 #ifndef BALL_VIEW_RENDERING_GLRENDERWINDOW_H
26 #endif
27 
28 #ifndef BALL_VIEW_RENDERING_RENDERSETUP_H
30 #endif
31 
32 #include <QtCore/QThread>
33 #include <QtCore/QTimer>
34 #include <QtGui/QDragEnterEvent>
35 #include <QtGui/QWheelEvent>
36 #include <QtGui/QKeyEvent>
37 #include <QtGui/QDropEvent>
38 #include <QtGui/QToolBar>
39 #include <QtGui/QActionGroup>
40 
41 // This allows us to switch raytracing on and off. Later, we might add this flag
42 // to config.h or remove it completely and always raytracing always.
43 #undef ENABLE_RAYTRACING
44 //#define ENABLE_RAYTRACING
45 
46 #ifdef ENABLE_RAYTRACING
47 
48 #ifndef BALL_VIEW_RENDERING_RAYTRACINGRENDERER_H
49 # include <BALL/VIEW/RENDERING/raytracingRenderer.h>
50 #endif
51 
52 # include <boost/shared_ptr.hpp>
53 #endif // ENABLE_RAYTRACING
54 
55 class QMouseEvent;
56 class QRubberBand;
57 class QMenu;
58 
59 namespace BALL
60 {
61  namespace VIEW
62  {
63  class Preferences;
64  class LightSettings;
65  class StageSettings;
66  class MaterialSettings;
67  class AnimationThread;
68  class ClippingPlane;
69 
70  class TransformationEvent6D;
71  class MotionTrackingEvent;
72  class ButtonEvent;
73 
116  : public QWidget,
117  public ModularWidget
118  {
119  friend class AnimationThread;
120  friend class RenderSetup;
121 
122 #ifdef ENABLE_RAYTRACING
123  typedef boost::shared_ptr<RaytracingRenderer> RaytracingRendererPtr;
124  typedef boost::shared_ptr<t_RenderWindow> RaytracingWindowPtr;
125 #endif
126 
127  Q_OBJECT
128 
129  public:
130 
132 
133 
136 
137 
138 
143  enum ModeType
144  {
146  ROTATE__MODE = 0,
147 
150 
151  // add new modi here!!!
152 
154  PICKING__MODE
155  };
156 
159  {
162 
165 
168 
170  ROTATE_CLOCKWISE_ACTION
171  };
172 
174 
177 
184  {
186  CONTROL_WINDOW = 0,
187 
190 
192  RIGHT_EYE_WINDOW
193  };
194 
196 
199 
200  Scene();
201 
212  Scene(QWidget* parent_widget, const char* name = NULL, Qt::WFlags w_flags = 0);
213 
224  Scene (const Scene& scene, QWidget* parent_widget = NULL, const char* name = NULL, Qt::WFlags wflags = 0);
225 
228  virtual ~Scene();
229 
238  virtual void clear();
239 
241 
244 
249  void set(const Scene& scene);
250 
254  const Scene& operator = (const Scene& scene);
255 
257 
260 
267  virtual void onNotify(Message *message);
268 
271  virtual bool exportScene(Renderer &er) const;
272 
274 
277 
287  virtual void initializeWidget(MainControl& main_control);
288 
290  virtual void fetchPreferences(INIFile& inifile);
291 
293  virtual void writePreferences(INIFile& inifile);
294 
305  virtual void checkMenu(MainControl& main_control);
306 
308 
311 
315  virtual bool isValid() const;
316 
323  virtual void dump(std::ostream& s = std::cout, Size depth = 0) const;
324 
326  Stage* getStage()
327  { return stage_;}
328 
330  const Stage* getStage() const
331  { return stage_;}
332 
334  void setCamera(const Camera& camera);
335 
337  std::list<Camera>& getAnimationPoints()
338  { return animation_points_;}
339 
341  static void setAnimationSmoothness(float value)
342  { animation_smoothness_ = value;}
343 
345  static float getAnimationSmoothness()
346  { return animation_smoothness_;}
347 
349  void setDefaultLighting(bool update_GL = true);
350 
352  void initializePreferencesTab(Preferences &preferences);
353 
355  void finalizePreferencesTab(Preferences &preferences);
356 
358  virtual void applyPreferences();
359 
361  static void setMouseSensitivity(float sensitivity)
362  { mouse_sensitivity_ = sensitivity; }
363 
365  static float getMouseSensitivity()
366  { return mouse_sensitivity_;}
367 
369  static void setMouseWheelSensitivity(float sensitivity)
370  { mouse_wheel_sensitivity_ = sensitivity; }
371 
373  static float getMouseWheelSensitivity()
374  { return mouse_wheel_sensitivity_;}
375 
377  static void setShowLightSources(bool state)
378  { show_light_sources_ = state;}
379 
381  static bool showLightSourcesEnabled()
382  { return show_light_sources_;}
383 
385  GLRenderer& getGLRenderer()
386  { return *gl_renderer_;}
387 
388 #ifdef ENABLE_RAYTRACING
389 
390  RaytracingRenderer& getRaytracingRenderer()
391  { return *rt_renderer_;}
392 #endif
393 
398  void setGLRenderer(GLRenderer& renderer);
399 
401  static bool stereoBufferSupportTest();
402 
404  float getMousePositionX() { return x_window_pos_new_;}
405 
407  float getMousePositionY() { return y_window_pos_new_;}
408 
410  bool exportPNG(const String& filename);
411 
413  virtual void setWidgetVisible(bool state);
414 
416  void setOffScreenRendering(bool enabled, Size factor);
417 
419  virtual void paintEvent(QPaintEvent* e);
420 
422  virtual void keyPressEvent(QKeyEvent* e);
423 
425  virtual void mouseDoubleClickEvent(QMouseEvent* e);
426 
428  virtual bool eventFilter(QObject* object, QEvent* event);
429 
431  ModeType getMode() const
432  { return current_mode_;}
433 
435  virtual void setMode(ModeType mode);
436 
438  virtual void projectionModeChanged();
439 
441  static void setScreenShotNumber(Position pos) { screenshot_nr_ = pos;}
442 
444  static void setPOVNumber(Position pos) { pov_nr_ = pos;}
445 
447  static void setVRMLNumber(Position pos) {vrml_nr_ = pos;}
448 
450  void rotate(float degree_right, float degree_up);
451 
453  void rotateClockwise(float degree);
454 
460  void move(Vector3 v);
461 
467  void moveComposites(const std::list<Composite*>& composites, Vector3 v);
468 
474  void rotateComposites(const std::list<Composite*>& composites, float degree_right, float degree_up, float degree_clockwise = 0);
475 
477  bool isAnimationRunning() const;
478 
480  void setTurnPoint(const Vector3& v) { system_origin_ = v;}
481 
483  const Vector3& getTurnPoint() const { return system_origin_;}
484 
486  void setFullScreen(bool state);
487 
489  void setFPSEnabled(bool state) { show_fps_ = state; }
490 
494  void showText(const String& text, Size font_size = 20);
495 
497  virtual void addToolBarEntries(QToolBar* tb);
498 
500  bool isUpdateRunning() const { return update_running_;}
501 
502  void resetTracking() {tracking_initialized_ = false;}
503 
505  bool inMoveMode() const { return (mouse_button_is_pressed_ && (getMode() == MOVE__MODE)); }
506 
507 // TODO: this pretty hacky!
508 #ifdef ENABLE_RAYTRACING
509  void updateAllRTMaterials();
510 
511  void updateRTMaterialForRepresentation(Representation const* rep, const Stage::RaytracingMaterial& new_material);
512 #endif
513 
514  Position prepareGridTextures(const RegularData3D& grid, const ColorMap& map);
515 
516  void updateGL();
517 
518  public slots:
519 
521  void createCoordinateSystem();
522 
524  void createCoordinateSystemAtOrigin();
525 
527  String exportPNG();
528 
530  void exportPOVRay();
531 
533  void exportNextPOVRay();
534 
536  void printScene();
537 
539  void showExportPNGDialog();
540 
542  void showExportVRMLDialog();
543 
545  void setPreview(bool state) { use_preview_ = state; }
546 
548  bool usePreview() const { return use_preview_; }
549 
553  virtual void switchShowWidget();
554 
556  void addGlWindow();
557 
559  void exitStereo();
560 
562  void enterActiveStereo();
563 
565  void enterDualStereo();
566 
568  void enterDualStereoDifferentDisplays();
569 
571  void clearRecordedAnimation();
572 
574  void startAnimation();
575 
577  void stopAnimation();
578 
580  void switchToLastMode();
581 
583  void switchShowGrid();
584 
586  void showInfos();
587 
589  void setupViewVolume();
590 
592  void storeViewPoint();
593 
595  void restoreViewPoint();
596 
597 #ifdef ENABLE_RAYTRACING
598  RaytracingWindowPtr getWindow(WindowType aWindowType);
599 #endif
600 
601  protected slots:
602 
604 
607 
615  virtual void rotateMode_();
616 
624  virtual void pickingMode_();
625 
628  virtual void moveMode_();
629 
631  virtual void showViewPoint_();
632 
634  virtual void setViewPoint_();
635 
637  virtual void resetCamera_();
638 
642  virtual void resetRepresentationsForRenderer_(RenderSetup& rs);
643 
645  virtual void dropEvent(QDropEvent* e);
646 
648  virtual void dragEnterEvent(QDragEnterEvent* e);
649 
650  // dummy slot for menu entries without immediate action (saves many lines code this way)
651  void dummySlot(){}
652 
654  protected:
655 
657 
660 
663  virtual void init();
664 
671  virtual void paintGL();
672 
679  virtual void resizeEvent(QResizeEvent* event);
680 
686  virtual void customEvent(QEvent* evt);
687 
691  virtual void transformationEvent6D(TransformationEvent6D* evt);
692 
696  virtual void motionTrackingEvent(MotionTrackingEvent* evt);
697 
701  virtual void buttonPressEvent(ButtonEvent* evt);
702 
706  virtual void buttonReleaseEvent(ButtonEvent* evt);
707 
712  virtual void mouseMoveEvent(QMouseEvent* qmouse_event);
713 
718  virtual void mousePressEvent(QMouseEvent* qmouse_event);
719 
724  virtual void mouseReleaseEvent(QMouseEvent* qmouse_event);
725 
729  virtual void wheelEvent(QWheelEvent* qmouse_event);
730 
731  //_
732  void animate_();
733 
734  void processRotateModeMouseEvents_(QMouseEvent* e);
735  void processMoveModeMouseEvents_(QMouseEvent* e);
736 
737  void rotateSystem_();
738  void rotateSystemClockwise_();
739  void translateSystem_();
740  void zoomSystem_();
741  Index getMoveModeAction_(const QMouseEvent& e);
742 
743  void selectionPressed_();
744  void selectionPressedMoved_();
745 
746  void selectObjects_();
747  void pickParent_(QPoint p);
748 
749  void writeLights_(INIFile& inifile) const;
750 
751  void readLights_(const INIFile& inifile);
752 
753  inline float getXDiff_();
754  inline float getYDiff_();
755  inline Vector3 getTranslationVector_(const Vector3& v);
756 
757  void createCoordinateSystem_(bool at_origin);
758 
760  String createFPSInfo_();
761 
762  //_ state of the scene: picking or rotate mode?
764 
765  //_ last state of the scene: picking or rotate mode?
767 
768  // Menu entry IDs
769  QAction *rotate_action_, *picking_action_, *move_action_;
770  QAction *no_stereo_action_, *active_stereo_action_, *dual_stereo_action_, *dual_stereo_different_display_action_;
771  QAction *record_animation_action_, *start_animation_action_, *clear_animation_action_, *cancel_animation_action_;
772  QAction *animation_export_POV_action_, *animation_export_VRML_action_, *animation_export_PNG_action_, *animation_repeat_action_;
773  QAction *switch_grid_;
775 
780 
783 
788 
794  QRubberBand* rb_;
795 
799 
800  std::vector<RenderSetup> renderers_;
802 
803 #ifdef ENABLE_RAYTRACING
804  RaytracingRendererPtr rt_renderer_;
805  RaytracingWindowPtr rt_window_;
806 #endif
807 
808  static float mouse_sensitivity_;
810  static bool show_light_sources_;
811  static float animation_smoothness_;
812 
816 
817  // nr of last png file export
819  // nr of last pov file export
821  //nr of last vrml or stl export
823 
824  std::list<Camera> animation_points_;
829  bool preview_;
831 
834  QPoint info_point_;
835  QByteArray last_state_;
836  list<float> fps_;
837  bool show_fps_;
838  static bool offscreen_rendering_;
845  QActionGroup* mode_group_;
846 
848 
850 
853  };
854 
855 
858  : public QThread
859  {
860  public:
861 
864 
866  virtual void run() {scene_->animate_();}
867 
869  void mySleep(Size msec);
870 
872  void setScene(Scene* scene) { scene_ = scene;}
873 
875  Scene* getScene() { return scene_;}
876 
877  protected:
878 
880  };
881 
882 
883 } } // namespaces
884 
885 #endif // BALL_VIEW_WIDGETS_SCENE_H