qtgl.h 6.07 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1 2 3
/*******************************************************************************
 * CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
 * version 0.1                                                                  *
4
 * Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Sylvain Thery's avatar
Sylvain Thery committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *                                                                              *
 * This library is free software; you can redistribute it and/or modify it      *
 * under the terms of the GNU Lesser General Public License as published by the *
 * Free Software Foundation; either version 2.1 of the License, or (at your     *
 * option) any later version.                                                   *
 *                                                                              *
 * This library 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 Lesser General Public License  *
 * for more details.                                                            *
 *                                                                              *
 * You should have received a copy of the GNU Lesser General Public License     *
 * along with this library; if not, write to the Free Software Foundation,      *
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
 *                                                                              *
20
 * Web site: http://cgogn.u-strasbg.fr/                                         *
Sylvain Thery's avatar
Sylvain Thery committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/

#ifndef __QT_GL2__
#define __QT_GL2__

#include <GL/glew.h>
#include <QGLWidget>
#include <QMouseEvent>
#include <QKeyEvent>
#include <iostream>
#include <stack>

#include "glm/glm.hpp"
#include "glm/gtc/matrix_projection.hpp"
#include "glm/gtc/matrix_transform.hpp"
38
#include "Geometry/vector_gen.h"
Sylvain Thery's avatar
Sylvain Thery committed
39 40 41

namespace CGoGN
{
Pierre Kraemer's avatar
Pierre Kraemer committed
42

Sylvain Thery's avatar
Sylvain Thery committed
43 44
namespace Utils
{
Pierre Kraemer's avatar
Pierre Kraemer committed
45

Sylvain Thery's avatar
Sylvain Thery committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
namespace QT
{

class SimpleQT;

class GLWidget : public QGLWidget
{
	Q_OBJECT

public:
	GLWidget(SimpleQT* cbs, QWidget *parent = 0);

	~GLWidget();

    QSize minimumSizeHint() const;

    QSize sizeHint() const;

protected:
65

thery's avatar
thery committed
66
	static float FAR_PLANE;
Sylvain Thery's avatar
Sylvain Thery committed
67 68 69 70

	SimpleQT* m_cbs;

	int m_current_button;
71
	QPoint clickPoint;
Sylvain Thery's avatar
Sylvain Thery committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	int beginx;
	int beginy;
	int newModel;

	int moving;
	int scaling;
	int translating;

	float scalefactor;
	float foc;

	float m_obj_sc;
	glm::vec3 m_obj_pos;

	// width and height of windows
	int W;
	int H;

	int m_state_modifier;

92 93 94
	/// stack for transformation matrix
	std::stack<glm::mat4> m_stack_trf;

Pierre Kraemer's avatar
Pierre Kraemer committed
95 96
	bool allow_rotation;

97 98 99
	/**
	 * met a jour la matrice modelview
	 */
Sylvain Thery's avatar
Sylvain Thery committed
100 101
	void recalcModelView();

102 103 104 105 106 107
	/**
	 * recalcul le quaternion ainsi que le deplacement courant
	 * pour un nouveau centre de rotation
	 */
	void changeCenterOfRotation(const glm::vec3& newCenter);

Sylvain Thery's avatar
Sylvain Thery committed
108 109 110
public:
	void setParamObject(float width, float* pos);

Pierre Kraemer's avatar
Pierre Kraemer committed
111 112
	void setRotation(bool b);

Sylvain Thery's avatar
Sylvain Thery committed
113 114 115 116 117 118
	void initializeGL();

	void paintGL();

	void resizeGL(int width, int height);

119 120 121 122
public:
	void mousePressEvent(QMouseEvent* event);

	void mouseReleaseEvent(QMouseEvent* event);
Sylvain Thery's avatar
Sylvain Thery committed
123

124
	void mouseClickEvent(QMouseEvent* event);
Sylvain Thery's avatar
Sylvain Thery committed
125

126 127
	void mouseDoubleClickEvent(QMouseEvent* event);

128
	void mouseMoveEvent(QMouseEvent* event);
Sylvain Thery's avatar
Sylvain Thery committed
129

130 131
	void closeEvent(QCloseEvent *event);

132
	void keyPressEvent(QKeyEvent* event);
Sylvain Thery's avatar
Sylvain Thery committed
133

134
	void keyReleaseEvent(QKeyEvent* event);
Sylvain Thery's avatar
Sylvain Thery committed
135

136
	void wheelEvent(QWheelEvent* event);
Sylvain Thery's avatar
Sylvain Thery committed
137 138 139 140 141 142 143

	bool Shift() { return m_state_modifier & Qt::ShiftModifier; }

	bool Control() { return m_state_modifier & Qt::ControlModifier; }

	bool Alt() { return m_state_modifier & Qt::AltModifier; }

Pierre Kraemer's avatar
Pierre Kraemer committed
144
	int getHeight() const { return H; }
145
	int getWidth() const { return W; }
146

Sylvain Thery's avatar
Sylvain Thery committed
147 148 149 150 151 152 153 154
	/**
	 * set the focale distance (for a screen width of 2), default value is 1
	 */
	void setFocal(float df);

	/**
	 * get the focale distance
	 */
155 156 157 158 159 160 161
	float getFocal() const { return foc; }

	/**
	 * get current state
	 */
	int getStateModifier() const { return m_state_modifier ; }
	int getCurrentButton() const { return m_current_button ; }
Sylvain Thery's avatar
Sylvain Thery committed
162

163 164
	static float getFarPlane() { return FAR_PLANE ; }

165 166
	glm::vec3& getObjPos() ;

Pierre Kraemer's avatar
Pierre Kraemer committed
167
	void modelModified() { newModel = 1; }
168

169 170 171 172 173 174 175 176 177 178 179 180 181 182 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
	void glMousePosition(int& x, int& y);

	/**
	 * get a ray (2 points) from a pick point in GL area
	 * @param x mouse position
	 * @param y mouse position
	 * @param rayA first computed point of ray
	 * @param rayA second computed point of ray
	 * @param radius radius on pixel for clicking precision
	 * @return the distance in modelview world corresponding to radius pixel in screen
	 */
	GLfloat getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius=4);

	/**
	 * transform a pixel distance on screen in distance in world
	 * @param pixel_width width on pixel on screen
	 * @param center reference point on world to use (defaut 0,0,0)
	 */
	float getWidthInWorld(unsigned int pixel_width, const Geom::Vec3f& center=Geom::Vec3f(0.0f,0.0f,0.0f));

	/**
	 * current transfo matrix
	 */
	const glm::mat4& transfoMatrix() const;
	glm::mat4& transfoMatrix();

	/**
	 * current modelview matrix
	 */
	const glm::mat4& modelViewMatrix() const;
	glm::mat4& modelViewMatrix();

	/**
	 * current projection matrix
	 */
	const glm::mat4& projectionMatrix() const;
	glm::mat4& projectionMatrix();


	void transfoRotate(float angle, float x, float y, float z);

	void transfoTranslate(float tx, float ty, float tz);

	void transfoScale(float sx, float sy, float sz);

	void pushTransfoMatrix();

	bool popTransfoMatrix();


Sylvain Thery's avatar
Sylvain Thery committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
protected:
	/**
	 * equivalent to old school glRotate
	 */
	void oglRotate(float angle, float x, float y, float z);

	/**
	 * equivalent to old school glTranslate
	 */
	void oglTranslate(float tx, float ty, float tz);

	/**
	 * equivalent to old school glScale
	 */
	void oglScale(float sx, float sy, float sz);

	/**
	 * get the focale distance
	 */
Pierre Kraemer's avatar
Pierre Kraemer committed
238
	float getScale() { return scalefactor / foc; }
Sylvain Thery's avatar
Sylvain Thery committed
239 240
};

Pierre Kraemer's avatar
Pierre Kraemer committed
241 242 243 244 245
} // namespace QT

} // namespace Utils

} // namespace CGoGN
Sylvain Thery's avatar
Sylvain Thery committed
246 247

#endif