qtgl.cpp 7.32 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg                *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
24

Sylvain Thery's avatar
Sylvain Thery committed
25 26 27 28 29 30 31 32 33
#include <GL/glew.h>
#include <iostream>
#include "Utils/qtgl.h"
#include "Utils/trackball.h"

#include "Utils/qtSimple.h"

namespace CGoGN
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
34

Sylvain Thery's avatar
Sylvain Thery committed
35 36
namespace Utils
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
37

Sylvain Thery's avatar
Sylvain Thery committed
38 39 40
namespace QT
{

41

thery's avatar
thery committed
42
float GLWidget::FAR_PLANE=500.0f;
Sylvain Thery's avatar
Sylvain Thery committed
43

44

Sylvain Thery's avatar
Sylvain Thery committed
45
GLWidget::GLWidget(SimpleQT* cbs, QWidget *parent) :
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
46 47 48
	QGLWidget(QGLFormat(QGL::Rgba | QGL::DoubleBuffer| QGL::DepthBuffer), parent),
	m_cbs(cbs),
	m_state_modifier(0)
Sylvain Thery's avatar
Sylvain Thery committed
49 50 51 52 53
{
	makeCurrent();
	glewInit();

	newModel = 1;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
54 55
	m_cbs->trans_x() = 0.;
	m_cbs->trans_y() = 0.;
Sylvain Thery's avatar
Sylvain Thery committed
56
	float f = FAR_PLANE;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
57 58
	m_cbs->trans_z() = -f / 5.0f;
	foc = 2.0f;
Sylvain Thery's avatar
Sylvain Thery committed
59 60

	// init trackball
61
	trackball(m_cbs->curquat(), 0.0f, 0.0f, 0.0f, 0.0f);
Sylvain Thery's avatar
Sylvain Thery committed
62 63 64 65 66 67 68 69
}

GLWidget::~GLWidget()
{
}

void GLWidget::setParamObject(float width, float* pos)
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
70 71
	m_obj_sc = ((FAR_PLANE / 5.0f) / foc) / width;
	m_obj_pos = glm::vec3(-pos[0], -pos[1], -pos[2]);
Sylvain Thery's avatar
Sylvain Thery committed
72 73 74 75
}

void  GLWidget::setFocal(float df)
{
76 77
	if (df > 5.0f)
		df = 5.0f;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
78 79
	if (df < 0.2f)
		df = 0.2f;
Sylvain Thery's avatar
Sylvain Thery committed
80

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
81
	m_obj_sc *= foc / df;
Sylvain Thery's avatar
Sylvain Thery committed
82 83

	foc = df;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
84
	resizeGL(W, H);
Sylvain Thery's avatar
Sylvain Thery committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
}

QSize GLWidget::minimumSizeHint() const
{
    return QSize(200, 200);
}

QSize GLWidget::sizeHint() const
{
    return QSize(500, 500);
}

void GLWidget::recalcModelView()
{
	glm::mat4 m;

	oglPushModelViewMatrix();
102

Sylvain Thery's avatar
Sylvain Thery committed
103
	// positionne l'objet / mvt souris
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
104
	oglTranslate(m_cbs->trans_x(), m_cbs->trans_y(), m_cbs->trans_z());
Sylvain Thery's avatar
Sylvain Thery committed
105 106

	// tourne l'objet / mvt souris
107
	build_rotmatrixgl3(m, m_cbs->curquat());
Sylvain Thery's avatar
Sylvain Thery committed
108 109 110 111
	// update matrice
	m_cbs->modelViewMatrix() *= m;

	// transfo pour que l'objet soit centre et a la bonne taille
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
112
	oglScale(m_obj_sc, m_obj_sc, m_obj_sc);
Sylvain Thery's avatar
Sylvain Thery committed
113 114
	oglTranslate(m_obj_pos[0], m_obj_pos[1], m_obj_pos[2]);

115 116 117
	// ajout transformation in screen
	m_cbs->modelViewMatrix()*= m_cbs->transfoMatrix();

Sylvain Thery's avatar
Sylvain Thery committed
118 119 120 121
	newModel = 0;

	if (m_cbs)
		m_cbs->cb_updateMatrix();
122 123

	oglPopModelViewMatrix();
Sylvain Thery's avatar
Sylvain Thery committed
124 125 126 127 128 129 130 131 132 133 134 135
}

void GLWidget::initializeGL()
{
	glEnable(GL_DEPTH_TEST);

	if (m_cbs)
		m_cbs->cb_initGL();
}

void GLWidget::resizeGL(int w, int h)
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
136 137
	if (w > 0) W = w;
	if (h > 0) H = h;
Sylvain Thery's avatar
Sylvain Thery committed
138

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
139
	glViewport(0, 0, W, H);
Sylvain Thery's avatar
Sylvain Thery committed
140
	float f = FAR_PLANE;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
141
	m_cbs->projectionMatrix() = glm::frustum(-1.0f, 1.0f, -1.0f * H / W, 1.0f * H / W, foc, f);
Sylvain Thery's avatar
Sylvain Thery committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

	recalcModelView();
}

void GLWidget::paintGL()
{
	if (newModel)
	    recalcModelView();

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	if (m_cbs)
		m_cbs->cb_redraw();
}

void GLWidget::mousePressEvent(QMouseEvent* event)
{
	beginx = event->x();
	beginy = event->y();
160
	clickPoint = event->pos();
Sylvain Thery's avatar
Sylvain Thery committed
161 162 163
	m_current_button = event->button();

	if (m_cbs)
164
		m_cbs->cb_mousePress(event->button(), event->x(), getHeight() - event->y());
Sylvain Thery's avatar
Sylvain Thery committed
165 166 167 168 169
}

void GLWidget::mouseReleaseEvent(QMouseEvent* event)
{
	if (m_cbs)
170 171 172 173
		m_cbs->cb_mouseRelease(event->button(), event->x(), getHeight() - event->y());

	if(event->pos() == clickPoint)
		mouseClickEvent(event) ;
Sylvain Thery's avatar
Sylvain Thery committed
174 175
}

176
void GLWidget::mouseClickEvent(QMouseEvent* event)
Sylvain Thery's avatar
Sylvain Thery committed
177
{
178 179 180
	if (m_cbs)
		m_cbs->cb_mouseClick(event->button(), event->x(), getHeight() - event->y());
}
Sylvain Thery's avatar
Sylvain Thery committed
181

182 183
void GLWidget::mouseMoveEvent(QMouseEvent* event)
{
184 185
	// move object only if no special keys pressed
	if (!(m_state_modifier & ( Qt::ShiftModifier|Qt::ControlModifier|Qt::AltModifier| Qt::MetaModifier)))
Sylvain Thery's avatar
Sylvain Thery committed
186
	{
187 188 189 190
		int x = event->x();
		int y = event->y();

		switch (m_current_button)
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
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
			case Qt::RightButton:
			{
				float wl;
				if (m_cbs->trans_z() > -20.0f)
					wl = 20.0f / foc;
				else
					wl = -2.0f * m_cbs->trans_z() / foc;
				m_cbs->trans_x() += wl / W * (x - beginx);
				m_cbs->trans_y() += wl / H * (beginy - y);
			}
				break;
			case Qt::MidButton:
			{
				float wl = -0.5f * FAR_PLANE / foc;
				m_cbs->trans_z() -= wl / W * (x - beginx);
				m_cbs->trans_z() -= wl / H * (y - beginy);
			}
				break;
			case Qt::LeftButton:
			{
				trackball(
					m_cbs->lastquat(),
					(2.0f * beginx - W) / W,
					(H - 2.0f * beginy) / H,
					(2.0f * x - W) / W,(H - 2.0f * y) / H
				);
				add_quats(m_cbs->lastquat(), m_cbs->curquat(), m_cbs->curquat());
			}
				break;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
221 222
		}

223 224 225 226 227
		beginx = x;
		beginy = y;
		newModel = 1;
		updateGL();
	}
Sylvain Thery's avatar
Sylvain Thery committed
228 229

	if (m_cbs)
230
		m_cbs->cb_mouseMove(event->button(), event->x(), getHeight() - event->y());
Sylvain Thery's avatar
Sylvain Thery committed
231 232 233 234
}

void GLWidget::wheelEvent ( QWheelEvent * event )
{
235
	float wl = -0.05f * FAR_PLANE / foc;
Sylvain Thery's avatar
Sylvain Thery committed
236 237

	if (event->delta() > 0)
238
		m_cbs->trans_z() += wl;
Sylvain Thery's avatar
Sylvain Thery committed
239
	else
240
		m_cbs->trans_z() -= wl;
Sylvain Thery's avatar
Sylvain Thery committed
241 242 243 244 245 246 247 248 249 250

	newModel = 1;
	updateGL();
}

void GLWidget:: keyPressEvent(QKeyEvent* event)
{
    if (event->key() == Qt::Key_Escape)
        close();
    else
Sylvain Thery's avatar
Sylvain Thery committed
251
    	QWidget::keyPressEvent(event);
Sylvain Thery's avatar
Sylvain Thery committed
252 253 254 255

    m_state_modifier = event->modifiers();

    int k = event->key();
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
256 257
    if ( (k >= 65) && (k <= 91) && !(event->modifiers() & Qt::ShiftModifier) )
    	k += 32;
Sylvain Thery's avatar
Sylvain Thery committed
258

259
    if (m_cbs)
Sylvain Thery's avatar
Sylvain Thery committed
260 261 262 263 264
		m_cbs->cb_keyPress(k);
}

void GLWidget::keyReleaseEvent(QKeyEvent *event)
{
Sylvain Thery's avatar
Sylvain Thery committed
265 266
	QWidget::keyReleaseEvent(event);

Sylvain Thery's avatar
Sylvain Thery committed
267 268 269
	m_state_modifier = event->modifiers();

    int k = event->key();
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
270
    if ( (k >= 65) && (k <= 91) && (event->modifiers() != Qt::ShiftModifier) )
271
    	k += 32;
Sylvain Thery's avatar
Sylvain Thery committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

	if (m_cbs)
		m_cbs->cb_keyRelease(k);
}

void GLWidget::oglRotate(float angle, float x, float y, float z)
{
	m_cbs->modelViewMatrix() = glm::rotate(m_cbs->modelViewMatrix(), angle, glm::vec3(x,y,z));
}

void GLWidget::oglTranslate(float tx, float ty, float tz)
{
	m_cbs->modelViewMatrix() = glm::translate(m_cbs->modelViewMatrix(), glm::vec3(tx,ty,tz));
}

void GLWidget::oglScale(float sx, float sy, float sz)
{
	m_cbs->modelViewMatrix() = glm::scale(m_cbs->modelViewMatrix(), glm::vec3(sx,sy,sz));
}

void GLWidget::oglPushModelViewMatrix()
{
	m_stack_mv.push(m_cbs->modelViewMatrix());
}

bool GLWidget::oglPopModelViewMatrix()
{
	if (m_stack_mv.empty())
		return false;
	m_cbs->modelViewMatrix() = m_stack_mv.top();
	m_stack_mv.pop();
	return true;
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
306
} // namespace QT
Sylvain Thery's avatar
Sylvain Thery committed
307

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
308 309 310
} // namespace Utils

} // namespace CGoGN