Q3DWidget.h 4.06 KB
Newer Older
lm's avatar
lm committed
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
#ifndef Q3DWIDGET_H_
#define Q3DWIDGET_H_

#include <boost/scoped_ptr.hpp>
#include <inttypes.h>
#include <string>
#include <QtOpenGL>
#include <QtGui>

enum CameraState
{
	IDLE = 0,
	ROTATING = 1,
	MOVING = 2,
	ZOOMING = 3
};

struct CameraPose
{
        CameraState state;
	float pan, tilt, distance;
	float xOffset, yOffset, zOffset;
	float xOffset2D, yOffset2D, rotation2D, zoom, warpX, warpY;
};

struct CameraParams
{
	float zoomSensitivity;
	float rotateSensitivity;
	float moveSensitivity;

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

	float zoomSensitivity2D;
	float rotateSensitivity2D;
	float moveSensitivity2D;
};

enum MouseState
{
	MOUSE_STATE_UP = 0,
	MOUSE_STATE_DOWN = 1
};

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 *);

class Q3DWidget: public QGLWidget
{
	Q_OBJECT

public:
        explicit Q3DWidget(QWidget* parent);
        ~Q3DWidget();

        void initialize(int32_t windowX, int32_t windowY,
                        int32_t windowWidth, int32_t windowHeight, float fps);

	void setCameraParams(float zoomSensitivity, float rotateSensitivity,
						 float moveSensitivity, float minZoomRange,
						 float camera_fov, float minClipRange,
						 float maxClipRange);

	void setCameraLimit(bool onoff);

	void set2DCameraParams(float zoomSensitivity,
						   float rotateSensitivity,
						   float moveSensitivity);

	void set3D(bool onoff);
	bool is3D(void) const;

	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;

        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);

	void forceRedraw(void);

	void set2DWarping(float warpX, float warpY);

	void recenter(void);
	void recenter2D(void);

	void set2DRotation(bool onoff);

	std::pair<float,float> pickPoint(int32_t mouseX, int32_t mouseY);

	std::pair<float,float> get2DPosition(int32_t x, int32_t y);

	int32_t getWindowWidth(void);
	int32_t getWindowHeight(void);
	int32_t getLastMouseX(void);
	int32_t getLastMouseY(void);
	int32_t getMouseX(void);
	int32_t getMouseY(void);

private Q_SLOTS:
	void userTimer(void);

private:
	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 setDisplayMode2D(int32_t width, int32_t height);
	void setDisplayMode3D(int32_t width, int32_t height);

	float r2d(float angle);
	float d2r(float angle);

	// 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;
};

#endif