Q3DWidget.h 6.29 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>
 *
 */

lm's avatar
lm committed
32 33 34 35 36 37 38 39
#ifndef Q3DWIDGET_H_
#define Q3DWIDGET_H_

#include <inttypes.h>
#include <string>
#include <QtOpenGL>
#include <QtGui>

pixhawk's avatar
pixhawk committed
40 41
//class GLUquadricObj;

lm's avatar
lm committed
42 43
enum CameraState
{
44 45 46 47
    IDLE = 0,
    ROTATING = 1,
    MOVING = 2,
    ZOOMING = 3
lm's avatar
lm committed
48 49 50 51
};

struct CameraPose
{
52 53 54 55
    CameraState state;
    float pan, tilt, distance;
    float xOffset, yOffset, zOffset;
    float xOffset2D, yOffset2D, rotation2D, zoom, warpX, warpY;
lm's avatar
lm committed
56 57 58 59
};

struct CameraParams
{
60 61 62 63 64 65 66 67 68 69 70 71
    float zoomSensitivity;
    float rotateSensitivity;
    float moveSensitivity;

    float minZoomRange;
    float cameraFov;
    float minClipRange;
    float maxClipRange;

    float zoomSensitivity2D;
    float rotateSensitivity2D;
    float moveSensitivity2D;
lm's avatar
lm committed
72 73 74 75
};

enum MouseState
{
76 77
    MOUSE_STATE_UP = 0,
    MOUSE_STATE_DOWN = 1
lm's avatar
lm committed
78 79 80 81 82 83 84
};

typedef void (*DisplayFunc)(void *);
typedef void (*KeyboardFunc)(char, void *);
typedef void (*MouseFunc)(Qt::MouseButton, MouseState, int32_t, int32_t, void *);
typedef void (*MotionFunc)(int32_t, int32_t, void *);

85 86 87
/**
 * @brief A base 3D widget which executes OpenGL commands.
 **/
lm's avatar
lm committed
88 89
class Q3DWidget: public QGLWidget
{
90
    Q_OBJECT
lm's avatar
lm committed
91 92

public:
93 94
    explicit Q3DWidget(QWidget* parent);
    ~Q3DWidget();
lm's avatar
lm committed
95

96 97
    void initialize(int32_t windowX, int32_t windowY,
                    int32_t windowWidth, int32_t windowHeight, float fps);
lm's avatar
lm committed
98

99 100 101 102
    void setCameraParams(float zoomSensitivity, float rotateSensitivity,
                         float moveSensitivity, float minZoomRange,
                         float cameraFov, float minClipRange,
                         float maxClipRange);
lm's avatar
lm committed
103

104
    void setCameraLimit(bool onoff);
lm's avatar
lm committed
105

106 107 108
    void set2DCameraParams(float zoomSensitivity,
                           float rotateSensitivity,
                           float moveSensitivity);
lm's avatar
lm committed
109

110 111
    void set3D(bool onoff);
    bool is3D(void) const;
lm's avatar
lm committed
112

113 114 115 116 117 118
    void setInitialCameraPos(float pan, float tilt, float range,
                             float xOffset, float yOffset, float zOffset);
    void setInitial2DCameraPos(float xOffset, float yOffset,
                               float rotation, float zoom);
    void setCameraPose(const CameraPose& cameraPose);
    CameraPose getCameraPose(void) const;
lm's avatar
lm committed
119

120 121 122 123 124 125
    void setDisplayFunc(DisplayFunc func, void* clientData);
    void setKeyboardFunc(KeyboardFunc func, void* clientData);
    void setMouseFunc(MouseFunc func, void* clientData);
    void setMotionFunc(MotionFunc func, void* clientData);
    void addTimerFunc(uint32_t msecs, void(*func)(void *),
                      void* clientData);
lm's avatar
lm committed
126

127
    void forceRedraw(void);
lm's avatar
lm committed
128

129
    void set2DWarping(float warpX, float warpY);
lm's avatar
lm committed
130

131 132
    void recenter(void);
    void recenter2D(void);
lm's avatar
lm committed
133

134
    void set2DRotation(bool onoff);
lm's avatar
lm committed
135

136
    void setDisplayMode2D(void);
lm's avatar
lm committed
137

138 139
    std::pair<float,float> getPositionIn3DMode(int32_t mouseX,
                                               int32_t mouseY);
140

141 142
    std::pair<float,float> getPositionIn2DMode(int32_t mouseX,
                                               int32_t mouseY);
lm's avatar
lm committed
143

144 145 146 147 148 149
    int32_t getWindowWidth(void);
    int32_t getWindowHeight(void);
    int32_t getLastMouseX(void);
    int32_t getLastMouseY(void);
    int32_t getMouseX(void);
    int32_t getMouseY(void);
lm's avatar
lm committed
150 151

private Q_SLOTS:
152 153 154 155 156 157 158 159 160 161 162 163 164
    void userTimer(void);

protected:
    void rotateCamera(float dx, float dy);
    void zoomCamera(float dy);
    void moveCamera(float dx, float dy);
    void rotateCamera2D(float dx);
    void zoomCamera2D(float dx);
    void moveCamera2D(float dx, float dy);

    void switchTo3DMode(void);
    void setDisplayMode3D(void);

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    float r2d(float angle) const;
    float d2r(float angle) const;

    void wireSphere(double radius, int slices, int stacks) const;
    void solidSphere(double radius, int slices, int stacks) const;
    void wireCone(double base, double height, int slices, int stacks) const;
    void solidCone(double base, double height, int slices, int stacks) const;
    void drawBox(float size, GLenum type) const;
    void wireCube(double size) const;
    void solidCube(double size) const;
    void doughnut(float r, float R, int nsides, int rings) const;
    void wireTorus(double innerRadius, double outerRadius,
                   int nsides, int rings) const;
    void solidTorus(double innerRadius, double outerRadius,
                    int nsides, int rings) const;
pixhawk's avatar
pixhawk committed
180 181 182

    GLUquadricObj* quadObj;

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    // QGLWidget events
    void initializeGL(void);
    void paintGL(void);
    void resizeGL(int32_t width, int32_t height);

    // Qt events
    void keyPressEvent(QKeyEvent* event);
    void mousePressEvent(QMouseEvent* event);
    void mouseReleaseEvent(QMouseEvent* event);
    void mouseMoveEvent(QMouseEvent* event);
    void wheelEvent(QWheelEvent *wheel);
    void timerEvent(QTimerEvent* event);
    void closeEvent(QCloseEvent* event);

    DisplayFunc userDisplayFunc;
    KeyboardFunc userKeyboardFunc;
    MouseFunc userMouseFunc;
    MotionFunc userMotionFunc;

    void* userDisplayFuncData;
    void* userKeyboardFuncData;
    void* userMouseFuncData;
    void* userMotionFuncData;

    int32_t windowWidth, windowHeight;
    float requestedFps;
    CameraPose cameraPose;
    int32_t lastMouseX, lastMouseY;

    bool _is3D;

    bool _forceRedraw;
    bool allow2DRotation;
    bool limitCamera;

    CameraParams cameraParams;

    QBasicTimer timer;

    void (*timerFunc)(void *);
    void* timerFuncData;
lm's avatar
lm committed
224 225 226
};

#endif