scene.h

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 
00005 #ifndef BALL_VIEW_WIDGETS_SCENE_H
00006 #define BALL_VIEW_WIDGETS_SCENE_H
00007 
00008 #ifndef BALL_VIEW_KERNEL_MODULARWIDGET_H
00009 # include <BALL/VIEW/KERNEL/modularWidget.h>
00010 #endif
00011 
00012 #ifndef BALL_VIEW_RENDERING_GLRENDERER_H
00013 # include <BALL/VIEW/RENDERING/glRenderer.h>
00014 #endif
00015 
00016 #ifndef BALL_VIEW_KERNEL_COMMON_H
00017 # include <BALL/VIEW/KERNEL/common.h>
00018 #endif 
00019 
00020 #ifndef BALL_VIEW_RENDERING_RENDERWINDOW_H
00021 # include <BALL/VIEW/RENDERING/renderWindow.h>
00022 #endif
00023 
00024 #ifndef BALL_VIEW_RENDERING_GLRENDERWINDOW_H
00025 # include <BALL/VIEW/RENDERING/glRenderWindow.h>
00026 #endif
00027 
00028 #ifndef BALL_VIEW_RENDERING_RENDERSETUP_H
00029 # include <BALL/VIEW/RENDERING/renderSetup.h>
00030 #endif
00031 
00032 #include <QtCore/qthread.h>
00033 #include <QtCore/qtimer.h>
00034 #include <QtGui/QDragEnterEvent>
00035 #include <QtGui/QWheelEvent>
00036 #include <QtGui/QKeyEvent>
00037 #include <QtGui/QDropEvent>
00038 #include <QtGui/QToolBar>
00039 #include <QtGui/QActionGroup>
00040 
00041 // This allows us to switch raytracing on and off. Later, we might add this flag
00042 // to config.h or remove it completely and always raytracing always.
00043 #undef ENABLE_RAYTRACING
00044 //#define ENABLE_RAYTRACING
00045        
00046 #ifdef ENABLE_RAYTRACING 
00047 
00048 #ifndef BALL_VIEW_RENDERING_RAYTRACINGRENDERER_H
00049 # include <BALL/VIEW/RENDERING/raytracingRenderer.h>
00050 #endif
00051 
00052 # include <boost/shared_ptr.hpp>
00053 #endif // ENABLE_RAYTRACING 
00054 
00055 class QMouseEvent;
00056 class QRubberBand;
00057 class QMenu;
00058 
00059 namespace BALL
00060 {
00061   namespace VIEW
00062   {
00063     class Preferences;
00064     class LightSettings;
00065     class StageSettings;
00066     class MaterialSettings;
00067     class AnimationThread;
00068     class ClippingPlane;
00069 
00070     class TransformationEvent6D;
00071     class MotionTrackingEvent;
00072     class ButtonEvent;
00073 
00115     class BALL_VIEW_EXPORT Scene
00116       : public QWidget,
00117         public ModularWidget
00118     {
00119       friend class AnimationThread;
00120       friend class RenderSetup;
00121 
00122 #ifdef ENABLE_RAYTRACING
00123       typedef boost::shared_ptr<RaytracingRenderer> RaytracingRendererPtr;
00124       typedef boost::shared_ptr<t_RenderWindow> RaytracingWindowPtr;
00125 #endif
00126 
00127       Q_OBJECT
00128 
00129       public:
00130 
00131       BALL_EMBEDDABLE(Scene, ModularWidget)
00132 
00133       
00136 
00137 
00138       
00143       enum ModeType
00144       {
00146         ROTATE__MODE         = 0,
00147 
00149         MOVE__MODE,
00150 
00151         // add new modi here!!!
00152         
00154         PICKING__MODE
00155       };
00156 
00158       enum ModeAction
00159       {
00161         TRANSLATE_ACTION,
00162 
00164         ZOOM_ACTION,
00165 
00167         ROTATE_ACTION,
00168 
00170         ROTATE_CLOCKWISE_ACTION
00171       };
00172 
00174 
00177       
00183       enum WindowType
00184       {
00186         CONTROL_WINDOW = 0,
00187         
00189         LEFT_EYE_WINDOW,
00190 
00192         RIGHT_EYE_WINDOW
00193       };
00194         
00196 
00199 
00200       Scene();
00201 
00212       Scene(QWidget* parent_widget, const char* name = NULL, Qt::WFlags w_flags = 0);
00213 
00224       Scene (const Scene& scene, QWidget* parent_widget = NULL, const char* name = NULL, Qt::WFlags wflags = 0);
00225 
00228       virtual ~Scene();
00229 
00238       virtual void clear();
00239 
00241 
00244 
00249       void set(const Scene& scene);
00250 
00254       const Scene& operator = (const Scene& scene);
00255 
00257 
00260 
00267       virtual void onNotify(Message *message);
00268 
00271       virtual bool exportScene(Renderer &er) const;
00272 
00274 
00277       
00287       virtual void initializeWidget(MainControl& main_control);
00288     
00290       virtual void fetchPreferences(INIFile& inifile);
00291       
00293       virtual void writePreferences(INIFile& inifile);
00294 
00305       virtual void checkMenu(MainControl& main_control);
00306       
00308 
00311 
00315       virtual bool isValid() const;
00316 
00323       virtual void dump(std::ostream& s = std::cout, Size depth = 0) const;
00324       
00326       Stage* getStage()
00327         { return stage_;}
00328 
00330       const Stage* getStage() const
00331         { return stage_;}
00332 
00334       void setCamera(const Camera& camera);
00335 
00337       List<Camera>& getAnimationPoints()
00338         { return animation_points_;}
00339 
00341       static void setAnimationSmoothness(float value)
00342         { animation_smoothness_ = value;}
00343 
00345       static float getAnimationSmoothness()
00346         { return animation_smoothness_;}
00347 
00349       void setDefaultLighting(bool update_GL = true);
00350 
00352       void initializePreferencesTab(Preferences &preferences);
00353       
00355       void finalizePreferencesTab(Preferences &preferences);
00356 
00358       virtual void applyPreferences();
00359         
00361       static void setMouseSensitivity(float sensitivity)
00362         { mouse_sensitivity_ = sensitivity; }
00363 
00365       static float getMouseSensitivity()
00366         { return mouse_sensitivity_;}
00367       
00369       static void setMouseWheelSensitivity(float sensitivity)
00370         { mouse_wheel_sensitivity_ = sensitivity; }
00371 
00373       static float getMouseWheelSensitivity()
00374         { return mouse_wheel_sensitivity_;}
00375 
00377       static void setShowLightSources(bool state)
00378         { show_light_sources_ = state;}
00379       
00381       static bool showLightSourcesEnabled() 
00382         { return show_light_sources_;}
00383 
00385       GLRenderer& getGLRenderer()
00386         { return *gl_renderer_;}
00387 
00388 #ifdef ENABLE_RAYTRACING
00390       RaytracingRenderer& getRaytracingRenderer()
00391         { return *rt_renderer_;}
00392 #endif
00393 
00398       void setGLRenderer(GLRenderer& renderer);
00399 
00401       static bool stereoBufferSupportTest();
00402       
00404       float getMousePositionX() { return x_window_pos_new_;}
00405 
00407       float getMousePositionY() { return y_window_pos_new_;}
00408 
00410       bool exportPNG(const String& filename);
00411 
00413       virtual void setWidgetVisible(bool state);
00414 
00416       void setOffScreenRendering(bool enabled, Size factor);
00417 
00419       virtual void paintEvent(QPaintEvent* e);
00420 
00422       virtual void keyPressEvent(QKeyEvent* e);
00423 
00425       virtual void mouseDoubleClickEvent(QMouseEvent* e);
00426 
00428       virtual bool eventFilter(QObject* object, QEvent* event);
00429 
00431       ModeType getMode() const
00432         { return current_mode_;}
00433 
00435       virtual void setMode(ModeType mode);
00436 
00438       static void setScreenShotNumber(Position pos) { screenshot_nr_ = pos;}
00439 
00441       static void setPOVNumber(Position pos) { pov_nr_ = pos;}
00442 
00444       static void setVRMLNumber(Position pos) {vrml_nr_ = pos;}
00445 
00447       void rotate(float degree_right, float degree_up);
00448 
00450       void rotateClockwise(float degree);
00451 
00457       void move(Vector3 v);
00458 
00464       void moveComposites(const List<Composite*>& composites, Vector3 v);
00465 
00471       void rotateComposites(const List<Composite*>& composites, float degree_right, float degree_up, float degree_clockwise = 0);
00472 
00474       bool isAnimationRunning() const;
00475 
00477       void setTurnPoint(const Vector3& v) { system_origin_ = v;}
00478 
00480       const Vector3& getTurnPoint() const { return system_origin_;}
00481 
00483       void setFullScreen(bool state);
00484 
00486       void setFPSEnabled(bool state) { show_fps_ = state; }
00487 
00491       void showText(const String& text, Size font_size = 20);
00492 
00494       virtual void addToolBarEntries(QToolBar* tb);
00495 
00497       bool isUpdateRunning() const { return update_running_;}
00498 
00499       void resetTracking() {tracking_initialized_ = false;}
00500       
00502       bool inMoveMode() const { return (mouse_button_is_pressed_ && (getMode() == MOVE__MODE)); }
00503 
00504 // TODO: this pretty hacky!
00505 #ifdef ENABLE_RAYTRACING
00506       void updateAllRTMaterials();
00507 
00508       void updateRTMaterialForRepresentation(Representation const* rep, const Stage::RaytracingMaterial& new_material);
00509 #endif
00510 
00511       Position prepareGridTextures(const RegularData3D& grid, const ColorMap& map);
00512 
00513       void updateGL();
00514 
00515     public slots:
00516 
00518       void createCoordinateSystem();
00519 
00521       void createCoordinateSystemAtOrigin();
00522 
00524       String exportPNG();
00525       
00527       void exportPOVRay();
00528 
00530       void exportNextPOVRay();
00531 
00533       void printScene();
00534 
00536       void showExportPNGDialog();
00537 
00539       void showExportVRMLDialog();
00540 
00542       void setPreview(bool state) { use_preview_ = state; }
00543 
00545       bool usePreview() const { return use_preview_; }
00546       
00550       virtual void switchShowWidget();
00551 
00553       void addGlWindow();
00554 
00556       void exitStereo();
00557 
00559       void enterActiveStereo();
00560 
00562       void enterDualStereo();
00563 
00565       void enterDualStereoDifferentDisplays();
00566 
00568       void clearRecordedAnimation();
00569       
00571       void startAnimation();
00572 
00574       void stopAnimation();
00575 
00577       void switchToLastMode();
00578 
00580       void switchShowGrid();
00581 
00583       void showInfos();
00584 
00586       void setupViewVolume();
00587 
00589       void storeViewPoint();
00590 
00592       void restoreViewPoint();
00593 
00594 #ifdef ENABLE_RAYTRACING
00595       RaytracingWindowPtr getWindow(WindowType aWindowType);
00596 #endif
00597   
00598       protected slots:
00599 
00601 
00604 
00612       virtual void rotateMode_();
00613 
00621       virtual void pickingMode_();
00622 
00625       virtual void moveMode_();
00626       
00628       virtual void showViewPoint_();
00629 
00631       virtual void setViewPoint_();
00632 
00634       virtual void resetCamera_();
00635 
00639       virtual void resetRepresentationsForRenderer_(RenderSetup& rs);
00640 
00642       virtual void dropEvent(QDropEvent* e);
00643 
00645       virtual void dragEnterEvent(QDragEnterEvent* e);
00646 
00647       // dummy slot for menu entries without immediate action (saves many lines code this way)
00648       void dummySlot(){}
00649 
00651       protected:
00652 
00654 
00657 
00660       virtual void init();
00661 
00668       virtual void paintGL();
00669 
00676       virtual void resizeEvent(QResizeEvent* event);
00677 
00683       virtual void customEvent(QEvent* evt);
00684 
00688       virtual void transformationEvent6D(TransformationEvent6D* evt);
00689 
00693       virtual void motionTrackingEvent(MotionTrackingEvent* evt);
00694 
00698       virtual void buttonPressEvent(ButtonEvent* evt);
00699 
00703       virtual void buttonReleaseEvent(ButtonEvent* evt);
00704 
00709       virtual void mouseMoveEvent(QMouseEvent* qmouse_event);
00710 
00715       virtual void mousePressEvent(QMouseEvent* qmouse_event);
00716 
00721       virtual void mouseReleaseEvent(QMouseEvent* qmouse_event);
00722 
00726       virtual void wheelEvent(QWheelEvent* qmouse_event);
00727 
00728       //_
00729       void animate_();
00730 
00731       void processRotateModeMouseEvents_(QMouseEvent* e);
00732       void processMoveModeMouseEvents_(QMouseEvent* e);
00733 
00734       void rotateSystem_();
00735       void rotateSystemClockwise_();
00736       void translateSystem_();
00737       void zoomSystem_();
00738       Index getMoveModeAction_(const QMouseEvent& e);
00739 
00740       void selectionPressed_();
00741       void selectionPressedMoved_();
00742 
00743       void selectObjects_();
00744       void pickParent_(QPoint p);
00745 
00746       void writeLights_(INIFile& inifile) const;
00747       
00748       void readLights_(const INIFile& inifile);
00749 
00750       inline float getXDiff_();
00751       inline float getYDiff_();
00752       inline Vector3 getTranslationVector_(const Vector3& v);
00753       
00754       void createCoordinateSystem_(bool at_origin);
00755   
00757       String createFPSInfo_();
00758 
00759       //_ state of the scene: picking or rotate mode?
00760       ModeType current_mode_;
00761 
00762       //_ last state of the scene: picking or rotate mode?
00763       ModeType last_mode_;
00764   
00765       // Menu entry IDs
00766       QAction *rotate_action_, *picking_action_, *move_action_;
00767       QAction *no_stereo_action_, *active_stereo_action_, *dual_stereo_action_, *dual_stereo_different_display_action_, *fullscreen_action_;
00768       QAction *record_animation_action_, *start_animation_action_, *clear_animation_action_, *cancel_animation_action_;
00769       QAction *animation_export_POV_action_, *animation_export_VRML_action_,  *animation_export_PNG_action_, *animation_repeat_action_;
00770       QAction *switch_grid_;
00771       QMenu* create_coordinate_system_;
00772       
00773       Vector3 system_origin_;
00774       Vector3 old_trackorigin_;
00775       bool tracking_initialized_;
00776       Quaternion old_trackrotation_;
00777 
00778       bool need_update_;
00779       bool update_running_;
00780 
00781       Index x_window_pos_old_;
00782       Index y_window_pos_old_;
00783       Index x_window_pos_new_;
00784       Index y_window_pos_new_;
00785 
00786       Index x_window_pick_pos_first_;
00787       Index y_window_pick_pos_first_;
00788       Index x_window_pick_pos_second_;
00789       Index y_window_pick_pos_second_;
00790       bool pick_select_;
00791       QRubberBand* rb_;
00792 
00793       Stage* stage_;
00794       Camera stereo_camera_;
00795       Camera stored_camera_;
00796 
00797       std::vector<RenderSetup> renderers_;
00798       GLRenderer* gl_renderer_;
00799 
00800 #ifdef ENABLE_RAYTRACING
00801       RaytracingRendererPtr rt_renderer_;
00802       RaytracingWindowPtr rt_window_;
00803 #endif
00804 
00805       static float mouse_sensitivity_;
00806       static float mouse_wheel_sensitivity_;
00807       static bool show_light_sources_;
00808       static float animation_smoothness_;
00809 
00810       LightSettings* light_settings_;
00811       StageSettings* stage_settings_;
00812       MaterialSettings* material_settings_;
00813 
00814       // nr of last png file export
00815       static Position screenshot_nr_;
00816       // nr of last pov file export
00817       static Position pov_nr_;
00818       //nr of last vrml or stl export
00819       static Position vrml_nr_;
00820 
00821       List<Camera> animation_points_;
00822       AnimationThread* animation_thread_;
00823       bool stop_animation_;
00824       bool want_to_use_vertex_buffer_;
00825       bool mouse_button_is_pressed_;
00826       bool preview_;
00827       bool use_preview_;
00828 
00829       PreciseTime time_;
00830       float zoom_factor_;
00831       QPoint info_point_;
00832       QByteArray last_state_;
00833       list<float> fps_;
00834       bool show_fps_;
00835       static bool offscreen_rendering_;
00836       Size offscreen_factor_;
00837       String text_;
00838       Size   font_size_;
00839       QToolBar* toolbar_view_controls_;
00840       QList<QAction*> toolbar_actions_view_controls_;
00841       bool ignore_pick_;
00842       QActionGroup* mode_group_;
00843 
00844       String info_string_;
00845 
00846       GLRenderWindow* main_display_;
00847 
00848       Index stereo_left_eye_;
00849       Index stereo_right_eye_;
00850     };
00851 
00852 
00854     class BALL_VIEW_EXPORT AnimationThread
00855       : public QThread
00856     {
00857       public:
00858         
00860         AnimationThread(){};
00861 
00863         virtual void run() {scene_->animate_();}
00864 
00866         void mySleep(Size msec);
00867 
00869         void setScene(Scene* scene) { scene_ = scene;}
00870 
00872         Scene* getScene() { return scene_;}
00873 
00874       protected:
00875 
00876         Scene* scene_;
00877     };
00878 
00879 
00880 } } // namespaces
00881 
00882 #endif // BALL_VIEW_WIDGETS_SCENE_H