00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 #ifndef BASEVIEWER_HH
00053 #define BASEVIEWER_HH
00054
00055
00056
00057 #include <OpenFlipper/common/Types.hh>
00058 #include <OpenFlipper/common/ViewerProperties.hh>
00059
00060 #include <ACG/GL/GLState.hh>
00061 #include <ACG/Scenegraph/SceneGraph.hh>
00062 #include <ACG/QtWidgets/QtSceneGraphWidget.hh>
00063
00064 #include <ACG/ShaderUtils/GLSLShader.hh>
00065 #include <ACG/GL/globjects.hh>
00066
00067 #include <QtOpenGL/QGLFormat>
00068 #include <QBoxLayout>
00069 #include <QtNetwork/QUdpSocket>
00070 #include <QWheelEvent>
00071 #include <QDropEvent>
00072 #include <QContextMenuEvent>
00073 #include <QDragEnterEvent>
00074 #include <QMouseEvent>
00075 #include <QAction>
00076 #include <QKeyEvent>
00077 #include <QSize>
00078 #include <QMap>
00079 #include <QString>
00080 #include <QMenu>
00081 #include <QToolBar>
00082 #include <QTime>
00083 #include <QTimer>
00084 #include <QGraphicsWidget>
00085 #include <QGraphicsSceneDragDropEvent>
00086
00087 #include <vector>
00088 #include <string>
00089
00090
00091
00092
00093
00094 class QPushButton;
00095 class QToolButton;
00096 class QSplitter;
00097 class QTimer;
00098 class QImage;
00099 class QSocketNotifier;
00100 class QGLFramebufferObject;
00101
00102
00103
00104
00105
00106
00107 namespace ACG {
00108 namespace QtWidgets {
00109 class QtWheel;
00110 }
00111 }
00112 class QGraphicsScene;
00113 class QtGLViewerLayout;
00114 class QtShiftPopupMenu;
00115 class CursorPainter;
00116
00117
00118
00119
00120
00127 class DLLEXPORT glViewer : public QGraphicsWidget
00128 {
00129 Q_OBJECT
00130
00131
00132 public:
00133
00134
00135
00144 glViewer( QGraphicsScene* _scene,
00145 QGLWidget* _glWidget,
00146 Viewer::ViewerProperties& _properties,
00147 QGraphicsWidget* _parent=0);
00148
00150 virtual ~glViewer();
00151
00152
00153
00154
00155
00156
00157
00158 virtual QSize sizeHint () const;
00159
00161 virtual void makeCurrent();
00163 virtual void swapBuffers();
00164
00166 void lockProjectionUpdate( void ) { projectionUpdateLocked_ = true; }
00167
00169 void unlockProjectionUpdate( void ) { projectionUpdateLocked_ = false; }
00170
00171 signals :
00172 void functionMenuUpdate();
00173
00174 void statusMessage (const QString & message, int timeout = 0);
00175
00176 public:
00177
00179 void trackMouse(bool _track);
00180
00187 void sceneGraph(ACG::SceneGraph::BaseNode* _root, const bool _resetTrackBall = false);
00188
00189
00191 enum ProjectionMode {
00192 ORTHOGRAPHIC_PROJECTION,
00193 PERSPECTIVE_PROJECTION
00194 };
00195
00197 enum NavigationMode {
00198 NORMAL_NAVIGATION,
00199 FIRSTPERSON_NAVIGATION
00200 };
00201
00203 void projectionMode(ProjectionMode _p);
00205 ProjectionMode projectionMode() const { return projectionMode_; }
00206
00208 void navigationMode(NavigationMode _n);
00210 NavigationMode navigationMode() const { return navigationMode_; }
00211
00216 void setScenePos( const ACG::Vec3d& _center, double _radius, const bool _resetTrackBall = false );
00217
00221 void setSceneCenter( const ACG::Vec3d& _center );
00222
00227 void setTrackBallCenter( const ACG::Vec3d& _center );
00228
00233 const ACG::Vec3d trackBallCenter( ) { return trackball_center_; };
00234
00238 const ACG::Vec3d& scene_center() const { return scene_center_; }
00242 double scene_radius() const { return scene_radius_; }
00243
00245 void viewingDirection( const ACG::Vec3d& _dir, const ACG::Vec3d& _up );
00246
00248 enum NormalsMode {
00250 DONT_TOUCH_NORMALS,
00255 NORMALIZE_NORMALS
00256 };
00257
00259 void normalsMode(NormalsMode _mode);
00261 NormalsMode normalsMode() const { return normalsMode_; }
00262
00263
00265 ACG::GLMatrixd& light_matrix() { return light_matrix_; }
00267 void rotate_lights(ACG::Vec3d& _axis, double _angle);
00269 void update_lights();
00270
00271 void draw_lights();
00272
00273
00275 void copyToImage( QImage& _image, GLenum _buffer=GL_BACK) {
00276 copyToImage(_image, 0, 0, glWidth(), glHeight(), _buffer);
00277 }
00278
00280 void copyToImage( QImage& _image,
00281 unsigned int _left, unsigned int _top,
00282 unsigned int _width, unsigned int _height,
00283 GLenum _buffer );
00284
00285
00287 unsigned int glWidth() const;
00289 unsigned int glHeight() const;
00291 QSize glSize() const;
00293 QPoint glMapFromGlobal( const QPoint& _pos ) const;
00295 QPoint glMapToGlobal( const QPoint& _pos ) const;
00296
00298 void encodeView(QString& _view);
00303 bool decodeView(const QString& _view);
00304
00306 void initModelviewMatrix();
00307
00308
00309 void grabGLArea();
00310
00311
00312 void releaseGLArea();
00313
00315 void drawBlendedObjects(bool _status) { blending_ = _status; }
00316
00318 void translate(const ACG::Vec3d& trans);
00319
00321 void rotate(const ACG::Vec3d& axis, double angle)
00322 { rotate(axis, angle, trackball_center_); }
00323
00325 void rotate(const ACG::Vec3d& axis, double angle, const ACG::Vec3d& _center);
00326
00328 void setCursorPainter (CursorPainter *_cursorPainter);
00329
00331 void updateCursorPosition (QPointF _scenePos);
00332
00334 void moveForward();
00335
00337 void moveBack();
00338
00340 void strafeLeft();
00341
00343 void strafeRight();
00344
00345
00346 public slots:
00347
00349 virtual void updateGL();
00350
00352 virtual void setHome();
00354 virtual void home();
00356 virtual void viewAll();
00358 virtual void flyTo(const QPoint& _pos, bool _move_back);
00360 virtual void flyTo(const QPoint& _pos) { flyTo(_pos,false); }
00362 virtual void flyFrom(const QPoint& _pos) { flyTo(_pos,true); }
00363
00369 virtual void flyTo(const ACG::Vec3d& _position,
00370 const ACG::Vec3d& _center,
00371 double _time = 1000.0);
00372
00374 virtual void perspectiveProjection();
00376 virtual void orthographicProjection();
00378 virtual void toggleProjectionMode();
00380 virtual void toggleNavigationMode();
00381
00382 signals:
00383
00384 void projectionModeChanged( bool _ortho );
00385 void navigationModeChanged( bool _normal );
00386
00387 public slots:
00388
00390 virtual void setView( const ACG::GLMatrixd& _modelview,
00391 const ACG::GLMatrixd& _inverse_modelview );
00392
00393 void actionPasteView();
00394 void actionCopyView();
00395
00396
00397 signals:
00398
00409 void signalMouseEvent(QMouseEvent*, const std::string&);
00410
00412 void signalMouseEvent(QMouseEvent*);
00413
00415 void signalWheelEvent(QWheelEvent*, const std::string&);
00416
00420 void signalMouseEventIdentify( QMouseEvent* );
00421
00423 void signalSceneGraphChanged(ACG::SceneGraph::BaseNode* _root);
00424
00427 void signalMouseEventClick ( QMouseEvent*, bool _double );
00428
00429
00430 signals:
00431
00436 void signalCustomContextMenuRequested ( const QPoint & );
00437
00438 private slots:
00439
00441 void slotClickTimeout ();
00442
00443
00444
00445 protected:
00446
00447 friend class QtGLGraphicsScene;
00448 friend class SimpleGLGraphicsScene;
00449 friend class QtGLGraphicsView;
00450
00452 virtual void initializeGL();
00454 virtual void paintGL();
00456 virtual void resizeEvent(QGraphicsSceneResizeEvent * _e);
00458 virtual void moveEvent (QGraphicsSceneMoveEvent * _e);
00459
00461 virtual void mousePressEvent(QGraphicsSceneMouseEvent* _event);
00463 virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* _event);
00465 virtual void mouseMoveEvent(QGraphicsSceneMouseEvent* _event);
00467 virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent* _event);
00469 virtual void wheelEvent(QGraphicsSceneWheelEvent* _event);
00471 virtual void contextMenuEvent(QGraphicsSceneContextMenuEvent* _e);
00472
00473
00475 void viewMouseEvent( QMouseEvent* _event);
00477 void viewWheelEvent(QWheelEvent* _event);
00479 void viewKeyEvent( QKeyEvent* _event);
00480
00482 void lightMouseEvent( QMouseEvent* );
00483
00484
00485 protected:
00486
00487
00489 void updateProjectionMatrix();
00490
00491
00492
00493 protected slots:
00494
00496 virtual void slotWheelX(double _dAngle);
00498 virtual void slotWheelY(double _dAngle);
00500 virtual void slotWheelZ(double _dist);
00501
00503 virtual void cleanupEventFilter()
00504 { removeEventFilter( sender());}
00505
00506
00507 private:
00508
00510 glViewer(const glViewer&);
00512 glViewer& operator=(const glViewer&);
00513
00514
00515 void createWidgets();
00516
00517
00518
00519 void drawScene();
00520
00521 void drawScene_mono();
00522
00523 void drawScene_stereo();
00524
00525
00526 void drawScene_glStereo();
00527
00528
00529 void drawScene_anaglyphStereo();
00530
00531
00532 void drawScene_customAnaglyphStereo();
00533
00534
00535 void updateCustomAnaglyphStereo();
00536
00537
00538 void finiCustomAnaglyphStereo();
00539
00540
00541 void drawScenePhilipsStereo();
00542
00543
00544 void updateScenePhilipsStereo();
00545
00546
00547 protected:
00548
00549
00550
00551 ACG::Vec3d scene_center_, trackball_center_;
00552 double scene_radius_, trackball_radius_;
00553
00554
00555
00556 GLdouble orthoWidth_;
00557 GLdouble near_, far_, fovy_;
00558
00559
00560 bool isRotating_;
00561 bool lookAround_;
00562
00563
00564
00565 private:
00566
00567
00568
00569 ACG::Vec3d home_center_;
00570 double home_radius_;
00571 ACG::GLMatrixd home_modelview_,
00572 home_inverse_modelview_;
00573 double homeOrthoWidth_;
00574
00575
00576
00577 ACG::GLMatrixd light_matrix_;
00578
00579
00580
00581 NormalsMode normalsMode_;
00582 ProjectionMode projectionMode_;
00583 NavigationMode navigationMode_;
00584
00585
00586
00587 bool trackMouse_;
00588 bool glareaGrabbed_;
00589 double frame_time_;
00590
00591
00592
00593 ACG::SceneGraph::BaseNode* sceneGraphRoot_;
00594
00595 bool projectionUpdateLocked_;
00596 bool blending_;
00597
00598
00599 QGraphicsScene* glScene_;
00600
00601
00602 QGLWidget* glWidget_;
00603
00604
00605 QtGLViewerLayout* glBaseLayout_;
00606
00607
00608 std::vector< QAction * > drawMenuActions_;
00609
00610
00611 QTime redrawTime_;
00612
00613
00614 CursorPainter *cursorPainter_;
00615
00616
00617 ACG::Vec3d cursorPoint3D_;
00618
00619
00620 bool cursorPositionValid_;
00621
00622
00625
00626
00627 public:
00628 bool wheelsVisible();
00629
00630 public slots:
00631 void slotShowWheels();
00632 void slotHideWheels();
00633
00634 private:
00635
00636 ACG::QtWidgets::QtWheel* wheelX_;
00637
00638 ACG::QtWidgets::QtWheel* wheelY_;
00639
00640 ACG::QtWidgets::QtWheel* wheelZ_;
00641
00642
00645
00648
00649 signals:
00651 void viewUpdated();
00652
00654 void viewChanged();
00655
00657 void signalMakeActive ();
00658
00661
00664
00665
00666 public slots:
00667
00669 virtual void setStereoMode(bool _b);
00670
00671 private:
00672
00673 bool stereo_;
00674
00675
00676 int agTexWidth_;
00677 int agTexHeight_;
00678 GLuint agTexture_[2];
00679 GLuint agProgram_;
00680 bool customAnaglyphSupported_;
00681
00682
00683 int pTexWidth_;
00684 int pTexHeight_;
00685 ACG::Texture2D pColorTexture_;
00686 ACG::Texture2D pDepthStencilTexture_;
00687 GLSL::PtrProgram pProgram_;
00688 bool philipsStereoInitialized_;
00689
00693
00696
00697
00698 signals:
00705 void signalKeyPressEvent(QKeyEvent*);
00706
00707 protected:
00708
00713 virtual void keyPressEvent(QKeyEvent* _event) { _event->ignore(); };
00714
00721 virtual void keyReleaseEvent(QKeyEvent* _event) { _event->ignore(); };
00722
00730 virtual bool viewKeyPressEvent(QKeyEvent* ) { return false; }
00731
00734
00737
00738
00739 public:
00740
00742 virtual void dragEnterEvent(QGraphicsSceneDragDropEvent* _e);
00743
00745 virtual void dropEvent(QGraphicsSceneDragDropEvent* _e);
00746
00747 signals:
00751 void startDragEvent( QMouseEvent* _event );
00752
00756 void dragEnterEvent(QDragEnterEvent* _event);
00757
00761 void dropEvent( QDropEvent* _event );
00762
00763
00766
00769
00770
00771 public slots:
00772
00773
00778 virtual void snapshot();
00779
00781 virtual void snapshot(QImage& _image, bool _offScreenRendering = false);
00782
00785
00788
00789 public:
00803 bool pick( ACG::SceneGraph::PickTarget _pickTarget,
00804 const QPoint& _mousePos,
00805 unsigned int& _nodeIdx,
00806 unsigned int& _targetIdx,
00807 ACG::Vec3d* _hitPointPtr=0 );
00808
00820 bool pick_region( ACG::SceneGraph::PickTarget _pickTarget,
00821 const QRegion& _region,
00822 QList<QPair<unsigned int, unsigned int> >& _list);
00823
00829 bool fast_pick( const QPoint& _mousePos,
00830 ACG::Vec3d& _hitPoint );
00831
00832 private:
00833
00835 int pickColor( ACG::SceneGraph::PickTarget _pickTarget,
00836 const QPoint& _mousePos,
00837 unsigned int& _nodeIdx,
00838 unsigned int& _targetIdx,
00839 ACG::Vec3d* _hitPointPtr=0 );
00840
00842 bool pickGL( ACG::SceneGraph::PickTarget _pickTarget,
00843 const QPoint& _mousePos,
00844 unsigned int& _nodeIdx,
00845 unsigned int& _targetIdx,
00846 ACG::Vec3d* _hitPointPtr=0 );
00847
00849 int pickFromCache( ACG::SceneGraph::PickTarget _pickTarget,
00850 const QPoint& _mousePos,
00851 unsigned int& _nodeIdx,
00852 unsigned int& _targetIdx,
00853 ACG::Vec3d* _hitPointPtr=0 );
00854
00855 private:
00856
00858 QGLFramebufferObject *pickCache_;
00859
00861 bool updatePickCache_;
00862
00864 ACG::SceneGraph::PickTarget pickCacheTarget_;
00865
00867 bool pickCacheSupported_;
00868
00871
00874
00875
00876 public:
00877
00879 void allowRotation( bool _mode ) { allowRotation_ = _mode; };
00880 bool allowRotation() { return allowRotation_; };
00881
00882 private slots:
00883
00884 void slotAnimation();
00885
00886 protected:
00887
00888
00889 QPoint lastPoint2D_;
00890
00891 private:
00892
00894 bool mapToSphere(const QPoint& _p, ACG::Vec3d& _result) const;
00895
00897 void handleFirstPersonNavigation( QMouseEvent* _event);
00898
00900 void handleNormalNavigation( QMouseEvent* _event);
00901
00902
00903
00904 ACG::Vec3d lastPoint3D_;
00905 bool lastPoint_hitSphere_;
00906 bool allowRotation_;
00907
00908
00909 ACG::Vec3d lastRotationAxis_;
00910 double lastRotationAngle_;
00911 QTime lastMoveTime_;
00912 QTimer* timer_;
00913
00914 QTimer clickTimer_;
00915 QTime clickTime_;
00916 QMouseEvent clickEvent_;
00917
00920
00923
00924
00925 public:
00927 Viewer::ViewerProperties* properties(){ return &properties_; };
00928
00929 private:
00931 Viewer::ViewerProperties& properties_;
00932
00934 ACG::GLState *glstate_;
00935
00937 bool initialized_;
00938
00939 private slots:
00943 void slotPropertiesUpdated();
00944
00945 private:
00949 void applyProperties();
00950
00952 };
00953
00954
00955
00956
00957 #endif // BASEVIEWER_HH defined
00958