BALL  1.4.1
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>
00033 #include <QtCore/QTimer>
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       std::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
00389 
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       virtual void projectionModeChanged();
00439 
00441       static void setScreenShotNumber(Position pos) { screenshot_nr_ = pos;}
00442 
00444       static void setPOVNumber(Position pos) { pov_nr_ = pos;}
00445 
00447       static void setVRMLNumber(Position pos) {vrml_nr_ = pos;}
00448 
00450       void rotate(float degree_right, float degree_up);
00451 
00453       void rotateClockwise(float degree);
00454 
00460       void move(Vector3 v);
00461 
00467       void moveComposites(const std::list<Composite*>& composites, Vector3 v);
00468 
00474       void rotateComposites(const std::list<Composite*>& composites, float degree_right, float degree_up, float degree_clockwise = 0);
00475 
00477       bool isAnimationRunning() const;
00478 
00480       void setTurnPoint(const Vector3& v) { system_origin_ = v;}
00481 
00483       const Vector3& getTurnPoint() const { return system_origin_;}
00484 
00486       void setFullScreen(bool state);
00487 
00489       void setFPSEnabled(bool state) { show_fps_ = state; }
00490 
00494       void showText(const String& text, Size font_size = 20);
00495 
00497       virtual void addToolBarEntries(QToolBar* tb);
00498 
00500       bool isUpdateRunning() const { return update_running_;}
00501 
00502       void resetTracking() {tracking_initialized_ = false;}
00503       
00505       bool inMoveMode() const { return (mouse_button_is_pressed_ && (getMode() == MOVE__MODE)); }
00506 
00507 // TODO: this pretty hacky!
00508 #ifdef ENABLE_RAYTRACING
00509       void updateAllRTMaterials();
00510 
00511       void updateRTMaterialForRepresentation(Representation const* rep, const Stage::RaytracingMaterial& new_material);
00512 #endif
00513 
00514       Position prepareGridTextures(const RegularData3D& grid, const ColorMap& map);
00515 
00516       void updateGL();
00517 
00518     public slots:
00519 
00521       void createCoordinateSystem();
00522 
00524       void createCoordinateSystemAtOrigin();
00525 
00527       String exportPNG();
00528       
00530       void exportPOVRay();
00531 
00533       void exportNextPOVRay();
00534 
00536       void printScene();
00537 
00539       void showExportPNGDialog();
00540 
00542       void showExportVRMLDialog();
00543 
00545       void setPreview(bool state) { use_preview_ = state; }
00546 
00548       bool usePreview() const { return use_preview_; }
00549       
00553       virtual void switchShowWidget();
00554 
00556       void addGlWindow();
00557 
00559       void exitStereo();
00560 
00562       void enterActiveStereo();
00563 
00565       void enterDualStereo();
00566 
00568       void enterDualStereoDifferentDisplays();
00569 
00571       void clearRecordedAnimation();
00572       
00574       void startAnimation();
00575 
00577       void stopAnimation();
00578 
00580       void switchToLastMode();
00581 
00583       void switchShowGrid();
00584 
00586       void showInfos();
00587 
00589       void setupViewVolume();
00590 
00592       void storeViewPoint();
00593 
00595       void restoreViewPoint();
00596 
00597 #ifdef ENABLE_RAYTRACING
00598       RaytracingWindowPtr getWindow(WindowType aWindowType);
00599 #endif
00600   
00601       protected slots:
00602 
00604 
00607 
00615       virtual void rotateMode_();
00616 
00624       virtual void pickingMode_();
00625 
00628       virtual void moveMode_();
00629       
00631       virtual void showViewPoint_();
00632 
00634       virtual void setViewPoint_();
00635 
00637       virtual void resetCamera_();
00638 
00642       virtual void resetRepresentationsForRenderer_(RenderSetup& rs);
00643 
00645       virtual void dropEvent(QDropEvent* e);
00646 
00648       virtual void dragEnterEvent(QDragEnterEvent* e);
00649 
00650       // dummy slot for menu entries without immediate action (saves many lines code this way)
00651       void dummySlot(){}
00652 
00654       protected:
00655 
00657 
00660 
00663       virtual void init();
00664 
00671       virtual void paintGL();
00672 
00679       virtual void resizeEvent(QResizeEvent* event);
00680 
00686       virtual void customEvent(QEvent* evt);
00687 
00691       virtual void transformationEvent6D(TransformationEvent6D* evt);
00692 
00696       virtual void motionTrackingEvent(MotionTrackingEvent* evt);
00697 
00701       virtual void buttonPressEvent(ButtonEvent* evt);
00702 
00706       virtual void buttonReleaseEvent(ButtonEvent* evt);
00707 
00712       virtual void mouseMoveEvent(QMouseEvent* qmouse_event);
00713 
00718       virtual void mousePressEvent(QMouseEvent* qmouse_event);
00719 
00724       virtual void mouseReleaseEvent(QMouseEvent* qmouse_event);
00725 
00729       virtual void wheelEvent(QWheelEvent* qmouse_event);
00730 
00731       //_
00732       void animate_();
00733 
00734       void processRotateModeMouseEvents_(QMouseEvent* e);
00735       void processMoveModeMouseEvents_(QMouseEvent* e);
00736 
00737       void rotateSystem_();
00738       void rotateSystemClockwise_();
00739       void translateSystem_();
00740       void zoomSystem_();
00741       Index getMoveModeAction_(const QMouseEvent& e);
00742 
00743       void selectionPressed_();
00744       void selectionPressedMoved_();
00745 
00746       void selectObjects_();
00747       void pickParent_(QPoint p);
00748 
00749       void writeLights_(INIFile& inifile) const;
00750       
00751       void readLights_(const INIFile& inifile);
00752 
00753       inline float getXDiff_();
00754       inline float getYDiff_();
00755       inline Vector3 getTranslationVector_(const Vector3& v);
00756       
00757       void createCoordinateSystem_(bool at_origin);
00758   
00760       String createFPSInfo_();
00761 
00762       //_ state of the scene: picking or rotate mode?
00763       ModeType current_mode_;
00764 
00765       //_ last state of the scene: picking or rotate mode?
00766       ModeType last_mode_;
00767   
00768       // Menu entry IDs
00769       QAction *rotate_action_, *picking_action_, *move_action_;
00770       QAction *no_stereo_action_, *active_stereo_action_, *dual_stereo_action_, *dual_stereo_different_display_action_;
00771       QAction *record_animation_action_, *start_animation_action_, *clear_animation_action_, *cancel_animation_action_;
00772       QAction *animation_export_POV_action_, *animation_export_VRML_action_,  *animation_export_PNG_action_, *animation_repeat_action_;
00773       QAction *switch_grid_;
00774       QMenu* create_coordinate_system_;
00775       
00776       Vector3 system_origin_;
00777       Vector3 old_trackorigin_;
00778       bool tracking_initialized_;
00779       Quaternion old_trackrotation_;
00780 
00781       bool need_update_;
00782       bool update_running_;
00783 
00784       Index x_window_pos_old_;
00785       Index y_window_pos_old_;
00786       Index x_window_pos_new_;
00787       Index y_window_pos_new_;
00788 
00789       Index x_window_pick_pos_first_;
00790       Index y_window_pick_pos_first_;
00791       Index x_window_pick_pos_second_;
00792       Index y_window_pick_pos_second_;
00793       bool pick_select_;
00794       QRubberBand* rb_;
00795 
00796       Stage* stage_;
00797       Camera stereo_camera_;
00798       Camera stored_camera_;
00799 
00800       std::vector<RenderSetup> renderers_;
00801       GLRenderer* gl_renderer_;
00802 
00803 #ifdef ENABLE_RAYTRACING
00804       RaytracingRendererPtr rt_renderer_;
00805       RaytracingWindowPtr rt_window_;
00806 #endif
00807 
00808       static float mouse_sensitivity_;
00809       static float mouse_wheel_sensitivity_;
00810       static bool show_light_sources_;
00811       static float animation_smoothness_;
00812 
00813       LightSettings* light_settings_;
00814       StageSettings* stage_settings_;
00815       MaterialSettings* material_settings_;
00816 
00817       // nr of last png file export
00818       static Position screenshot_nr_;
00819       // nr of last pov file export
00820       static Position pov_nr_;
00821       //nr of last vrml or stl export
00822       static Position vrml_nr_;
00823 
00824       std::list<Camera> animation_points_;
00825       AnimationThread* animation_thread_;
00826       bool stop_animation_;
00827       bool want_to_use_vertex_buffer_;
00828       bool mouse_button_is_pressed_;
00829       bool preview_;
00830       bool use_preview_;
00831 
00832       PreciseTime time_;
00833       float zoom_factor_;
00834       QPoint info_point_;
00835       QByteArray last_state_;
00836       list<float> fps_;
00837       bool show_fps_;
00838       static bool offscreen_rendering_;
00839       Size offscreen_factor_;
00840       String text_;
00841       Size   font_size_;
00842       QToolBar* toolbar_view_controls_;
00843       QList<QAction*> toolbar_actions_view_controls_;
00844       bool ignore_pick_;
00845       QActionGroup* mode_group_;
00846 
00847       String info_string_;
00848 
00849       GLRenderWindow* main_display_;
00850 
00851       Index stereo_left_eye_;
00852       Index stereo_right_eye_;
00853     };
00854 
00855 
00857     class BALL_VIEW_EXPORT AnimationThread
00858       : public QThread
00859     {
00860       public:
00861         
00863         AnimationThread(){};
00864 
00866         virtual void run() {scene_->animate_();}
00867 
00869         void mySleep(Size msec);
00870 
00872         void setScene(Scene* scene) { scene_ = scene;}
00873 
00875         Scene* getScene() { return scene_;}
00876 
00877       protected:
00878 
00879         Scene* scene_;
00880     };
00881 
00882 
00883 } } // namespaces
00884 
00885 #endif // BALL_VIEW_WIDGETS_SCENE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines