qtgl.h 6.13 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-2012, 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.unistra.fr/                                           *
Sylvain Thery's avatar
Sylvain Thery committed
21 22 23 24 25 26 27
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/

#ifndef __QT_GL2__
#define __QT_GL2__

Thery Sylvain's avatar
Thery Sylvain committed
28
#include "Utils/gl_def.h"
Sylvain Thery's avatar
Sylvain Thery committed
29 30 31 32 33 34 35 36 37
#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
merge..  
Pierre Kraemer committed
42

Sylvain Thery's avatar
Sylvain Thery committed
43 44
namespace Utils
{
Pierre Kraemer's avatar
merge..  
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
	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;
85 86
	glm::vec3 m_obj_pos_save;
	float m_obj_width;
Sylvain Thery's avatar
Sylvain Thery committed
87 88 89 90 91 92 93

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

	int m_state_modifier;

94 95 96
	/// stack for transformation matrix
	std::stack<glm::mat4> m_stack_trf;

Pierre Kraemer's avatar
Pierre Kraemer committed
97 98
	bool allow_rotation;

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

104 105 106 107 108 109
	/**
	 * 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
110
public:
111 112
	void resetCenterOfRotation(float width, float* pos);

Sylvain Thery's avatar
Sylvain Thery committed
113 114
	void setParamObject(float width, float* pos);

Pierre Kraemer's avatar
Pierre Kraemer committed
115 116
	void setRotation(bool b);

Sylvain Thery's avatar
Sylvain Thery committed
117 118 119 120 121 122
	void initializeGL();

	void paintGL();

	void resizeGL(int width, int height);

123 124 125 126
public:
	void mousePressEvent(QMouseEvent* event);

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

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

130 131
	void mouseDoubleClickEvent(QMouseEvent* event);

132
	void mouseMoveEvent(QMouseEvent* event);
Sylvain Thery's avatar
Sylvain Thery committed
133

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

136
	void keyReleaseEvent(QKeyEvent* event);
Sylvain Thery's avatar
Sylvain Thery committed
137

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

	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
merge..  
Pierre Kraemer committed
146
	int getHeight() const { return H; }
147
	int getWidth() const { return W; }
148

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

	/**
	 * get the focale distance
	 */
157 158 159 160 161 162 163
	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
164

165 166
	static float getFarPlane() { return FAR_PLANE ; }

167 168
	glm::vec3& getObjPos() ;

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
169
	void modelModified() { newModel = 1; }
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 219 220
	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
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
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
merge..  
Pierre Kraemer committed
240
	float getScale() { return scalefactor / foc; }
Sylvain Thery's avatar
Sylvain Thery committed
241 242
};

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
243 244 245 246 247
} // namespace QT

} // namespace Utils

} // namespace CGoGN
Sylvain Thery's avatar
Sylvain Thery committed
248 249

#endif