Q3DWidget.h 4.53 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

/**
 * @file
 *   @brief Definition of the class Q3DWidget.
 *
 *   @author Lionel Heng <hengli@student.ethz.ch>
 *
 */

32 33
#ifndef Q3DWIDGET_H
#define Q3DWIDGET_H
lm's avatar
lm committed
34 35 36

#include <QtOpenGL>

37 38 39
#include <osg/LineSegment>
#include <osg/PositionAttitudeTransform>
#include <osgViewer/Viewer>
lm's avatar
lm committed
40 41 42

enum MouseState
{
43 44
    MOUSE_STATE_UP = 0,
    MOUSE_STATE_DOWN = 1
lm's avatar
lm committed
45 46 47 48
};

typedef void (*DisplayFunc)(void *);
typedef void (*KeyboardFunc)(char, void *);
49 50
typedef void (*MouseFunc)(Qt::MouseButton, MouseState, int, int, void *);
typedef void (*MotionFunc)(int, int, void *);
lm's avatar
lm committed
51

52
class Q3DWidget : public QGLWidget, public osgViewer::Viewer
lm's avatar
lm committed
53
{
54
    Q_OBJECT
lm's avatar
lm committed
55 56

public:
57 58
    Q3DWidget(QWidget* parent = 0);
    virtual ~Q3DWidget();
lm's avatar
lm committed
59

60 61 62
    void init(float fps);
    void setCameraParams(float minZoomRange, float cameraFov,
                         float minClipRange,
63
                         float maxClipRange);
lm's avatar
lm committed
64

65 66 67
    void forceRedraw(void);
    void recenter(void);
    void setDisplayMode3D(void);
lm's avatar
lm committed
68

69 70 71 72
    void setDisplayFunc(DisplayFunc func, void* clientData);
    void setKeyboardFunc(KeyboardFunc func, void* clientData);
    void setMouseFunc(MouseFunc func, void* clientData);
    void setMotionFunc(MotionFunc func, void* clientData);
73
    void addTimerFunc(uint msecs, void(*func)(void *),
74
                      void* clientData);
lm's avatar
lm committed
75

76 77 78
    std::pair<double,double> getGlobalCursorPosition(int32_t mouseX,
                                                     int32_t mouseY,
                                                     double z);
lm's avatar
lm committed
79

80 81
protected slots:
    void redraw(void);
82 83 84
    void userTimer(void);

protected:
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    osg::ref_ptr<osg::Geode> createRobot(void);
    osg::ref_ptr<osg::Node> createHUD(void);

    int getMouseX(void);
    int getMouseY(void);
    int getLastMouseX(void);
    int getLastMouseY(void);

    osgViewer::GraphicsWindow* getGraphicsWindow(void);
    const osgViewer::GraphicsWindow* getGraphicsWindow(void) const;

    virtual void resizeGL(int width, int height);
    virtual void paintGL(void);
    virtual void keyPressEvent(QKeyEvent* event);
    virtual void keyReleaseEvent(QKeyEvent* event);
    virtual void mousePressEvent(QMouseEvent* event);
    virtual void mouseReleaseEvent(QMouseEvent* event);
    virtual void mouseMoveEvent(QMouseEvent* event);
    virtual void wheelEvent(QWheelEvent* event);

    float r2d(float angle);
    float d2r(float angle);
    osgGA::GUIEventAdapter::KeySymbol convertKey(int key) const;
    bool getPlaneLineIntersection(const osg::Vec4d& plane,
                                  const osg::LineSegment& line,
                                  osg::Vec3d& isect);

    osg::ref_ptr<osg::Group> root;
    osg::ref_ptr<osg::Switch> allocentricMap;
    osg::ref_ptr<osg::Switch> rollingMap;
    osg::ref_ptr<osg::Switch> egocentricMap;
    osg::ref_ptr<osg::PositionAttitudeTransform> robotPosition;
    osg::ref_ptr<osg::PositionAttitudeTransform> robotAttitude;

    osg::ref_ptr<osg::Geode> hudGeode;
    osg::ref_ptr<osg::Projection> hudProjectionMatrix;
121 122 123 124 125

    DisplayFunc userDisplayFunc;
    KeyboardFunc userKeyboardFunc;
    MouseFunc userMouseFunc;
    MotionFunc userMotionFunc;
126
    void (*userTimerFunc)(void *);
127 128 129 130 131

    void* userDisplayFuncData;
    void* userKeyboardFuncData;
    void* userMouseFuncData;
    void* userMotionFuncData;
132
    void* userTimerFuncData;
133

134
    osg::ref_ptr<osgViewer::GraphicsWindowEmbedded> osgGW;
135

136
    QTimer timer;
137

138 139 140 141 142 143 144
    struct CameraParams
    {
        float minZoomRange;
        float cameraFov;
        float minClipRange;
        float maxClipRange;
    };
145 146 147

    CameraParams cameraParams;

148 149
    int lastMouseX;
    int lastMouseY;
150

151
    bool _forceRedraw;
lm's avatar
lm committed
152 153
};

154
#endif // Q3DWIDGET_H