qtQGLV.cpp 14.4 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 24
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, 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: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

Sylvain Thery's avatar
Sylvain Thery committed
25
#define CGoGN_UTILS_DLL_EXPORT 1
Sylvain Thery's avatar
Sylvain Thery committed
26 27 28 29 30 31
#include "Utils/GLSLShader.h"
#include "Utils/Qt/qtQGLV.h"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_precision.hpp"
#include "glm/gtc/type_ptr.hpp"

Sylvain Thery's avatar
Sylvain Thery committed
32
#include <QTextEdit>
Sylvain Thery's avatar
Sylvain Thery committed
33
#include <QImage>
Sylvain Thery's avatar
Sylvain Thery committed
34 35 36 37 38 39
#include <QStatusBar>
#include <QFileDialog>
#include <QMessageBox>
#include <QKeyEvent>
#include <QMouseEvent>

Sylvain Thery's avatar
Sylvain Thery committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

namespace CGoGN
{

namespace Utils
{

namespace QT
{

SimpleQGLV::SimpleQGLV() :
	m_dock(NULL),
	m_projection_matrix(m_mat.m_matrices[0]),
	m_modelView_matrix(m_mat.m_matrices[1]),
	m_transfo_matrix(m_mat.m_matrices[2])
{
56 57 58
	QGLFormat format;
	if (GLSLShader::CURRENT_OGL_VERSION >= 3)
	{
Sylvain Thery's avatar
Sylvain Thery committed
59 60 61 62 63
		QGLFormat glFormat;
		glFormat.setVersion( Utils::GLSLShader::MAJOR_OGL_CORE, Utils::GLSLShader::MINOR_OGL_CORE);
		glFormat.setProfile( QGLFormat::CoreProfile ); // Requires >=Qt-4.8.0
		glFormat.setSampleBuffers( true );
		QGLFormat::setDefaultFormat(glFormat);
64 65
	}

Sylvain Thery's avatar
Sylvain Thery committed
66
	m_qglWidget = new QGLView(this);
67

Sylvain Thery's avatar
Sylvain Thery committed
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
	setCentralWidget(m_qglWidget);
	setWindowTitle(tr("CGoGN"));

	m_fileMenu = menuBar()->addMenu(tr("&File"));

	QAction* action = new QAction(tr("New"), this);
	connect(action, SIGNAL(triggered()), this, SLOT(cb_New()));
	m_fileMenu->addAction(action);

	action = new QAction(tr("Open"), this);
	connect(action, SIGNAL(triggered()), this, SLOT(cb_Open()));
	m_fileMenu->addAction(action);

	action = new QAction(tr("Save"), this);
	connect(action, SIGNAL(triggered()), this, SLOT(cb_Save()));
	m_fileMenu->addAction(action);

	m_fileMenu->addSeparator();

	action = new QAction(tr("Quit"), this);
	connect(action, SIGNAL(triggered()), this, SLOT(cb_Quit()));
	m_fileMenu->addAction(action);

	m_appMenu = menuBar()->addMenu(tr("&Application"));

	QMenu* m_helpMenu = menuBar()->addMenu(tr("&Help"));

	action = new QAction(tr("console on/off"), this);
	connect(action, SIGNAL(triggered()), this, SLOT(cb_consoleOnOff()));
	m_helpMenu->addAction(action);

	action = new QAction(tr("console clear"), this);
	connect(action, SIGNAL(triggered()), this, SLOT(cb_consoleClear()));
	m_helpMenu->addAction(action);

	action = new QAction(tr("About"), this);
	connect(action, SIGNAL(triggered()), this, SLOT(cb_about()));
	m_helpMenu->addAction(action);

	action = new QAction(tr("About CGoGN"), this);
	connect(action, SIGNAL(triggered()), this, SLOT(cb_about_cgogn()));
	m_helpMenu->addAction(action);

	m_dockConsole = new QDockWidget(tr("Console"), this);
	m_dockConsole->setAllowedAreas(Qt::BottomDockWidgetArea);
	m_dockConsole->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetClosable);
	addDockWidget(Qt::BottomDockWidgetArea, m_dockConsole);

	m_textConsole = new QTextEdit();
	m_textConsole->setLineWrapMode(QTextEdit::NoWrap);
	m_textConsole->setTabStopWidth(20);
//	m_textConsole->setReadOnly(true);

	m_dockConsole->setWidget(m_textConsole);

	m_dockConsole->hide();

	m_transfo_matrix = glm::mat4(1.0f);

	resize(1200,800);
	m_qglWidget->setFocus(Qt::MouseFocusReason);
129

130 131 132 133
//	QGLFormat format(/*QGL::SampleBuffers |*/ QGL::DoubleBuffer | QGL::AlphaChannel);
//	format.setSamples(2);
//	m_qglWidget->setFormat(format);
//	m_qglWidget->makeCurrent();
Sylvain Thery's avatar
Sylvain Thery committed
134 135 136
}

SimpleQGLV::SimpleQGLV(const SimpleQGLV& sqt):
137
	QMainWindow(),
Sylvain Thery's avatar
Sylvain Thery committed
138 139 140 141 142
	m_dock(NULL),
	m_projection_matrix(m_mat.m_matrices[0]),
	m_modelView_matrix(m_mat.m_matrices[1]),
	m_transfo_matrix(m_mat.m_matrices[2])
{
143
	QGLFormat format = sqt.m_qglWidget->format();
Sylvain Thery's avatar
Sylvain Thery committed
144
	m_qglWidget = new QGLView(this);
145

Sylvain Thery's avatar
Sylvain Thery committed
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
	setCentralWidget(m_qglWidget);

	m_dock = new QDockWidget(sqt.m_dock) ;
	m_dockConsole = new QDockWidget(sqt.m_dockConsole) ;
	m_textConsole = new QTextEdit(sqt.m_textConsole) ;
	m_dockOn = sqt.m_dockOn ;


	for (unsigned int i = 0; i < 4; ++i)
	{
		m_curquat[i] = sqt.m_curquat[i];
		m_lastquat[i] = sqt.m_lastquat[i];
	}
	m_trans_x = sqt.m_trans_x ;
	m_trans_y = sqt.m_trans_y ;
	m_trans_z = sqt.m_trans_z ;

	m_qglWidget->setFocus(Qt::MouseFocusReason);
}

SimpleQGLV::~SimpleQGLV()
{
	delete m_qglWidget; // ??
}

void SimpleQGLV::operator=(const SimpleQGLV& sqt)
{
Sylvain Thery's avatar
Sylvain Thery committed
173
	m_qglWidget = new QGLView(this);
174

Sylvain Thery's avatar
Sylvain Thery committed
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
	setCentralWidget(m_qglWidget) ;

	m_dock = new QDockWidget(sqt.m_dock) ;
	m_dockConsole = new QDockWidget(sqt.m_dockConsole) ;
	m_textConsole = new QTextEdit(sqt.m_textConsole) ;
	m_dockOn = sqt.m_dockOn ;

	m_projection_matrix = sqt.m_projection_matrix;
	m_modelView_matrix = sqt.m_modelView_matrix;
	for (unsigned int i = 0; i < 4; ++i)
	{
		m_curquat[i] = sqt.m_curquat[i];
		m_lastquat[i] = sqt.m_lastquat[i];
	}
	m_trans_x = sqt.m_trans_x ;
	m_trans_y = sqt.m_trans_y ;
	m_trans_z = sqt.m_trans_z ;
}

void SimpleQGLV::setDock(QDockWidget *dock)
{
	m_dock = dock;
	addDockWidget(Qt::RightDockWidgetArea, m_dock);
	m_dock->show();
}

QDockWidget* SimpleQGLV::dockWidget()
{
	return m_dock;
}

void SimpleQGLV::setCallBack( const QObject* sender, const char* signal, const char* method)
{
	connect(sender, signal, this, method);
}

void SimpleQGLV::windowTitle(const char* windowTitle)
{
	setWindowTitle(tr(windowTitle));
}

void SimpleQGLV::dockTitle(const char* dockTitle)
{
	if (m_dock)
		m_dock->setWindowTitle(tr(dockTitle));
}

void SimpleQGLV::statusMsg(const char* msg, int timeoutms)
{
	if (msg)
	{
		QString message = tr(msg);
		statusBar()->showMessage(message,timeoutms);
	}
	else
	{
		if (statusBar()->isHidden())
			statusBar()->show();
		else
			statusBar()->hide();
	}
}

QDockWidget* SimpleQGLV::addEmptyDock()
{
	m_dock = new QDockWidget(tr("Control"), this);
	m_dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
	m_dock->setFeatures(QDockWidget::DockWidgetMovable|QDockWidget::DockWidgetFloatable|QDockWidget::DockWidgetClosable);
	addDockWidget(Qt::RightDockWidgetArea, m_dock);

	m_dock->hide();
	return m_dock;
}

void SimpleQGLV::visibilityDock(bool visible)
{
	if (visible)
		m_dock->show();
	else
		m_dock->hide();
}

void SimpleQGLV::visibilityConsole(bool visible)
{
	if (visible)
		m_dockConsole->show();
	else
		m_dockConsole->hide();
}

void SimpleQGLV::toggleVisibilityDock()
{
	if (m_dock->isHidden())
		m_dock->show();
	else
		m_dock->hide();
}

void SimpleQGLV::toggleVisibilityConsole()
{
	if (m_dockConsole->isHidden())
		m_dockConsole->show();
	else
		m_dockConsole->hide();
}

void SimpleQGLV::add_menu_entry(const std::string label, const char* method)
{
	QAction * action = new QAction(tr(label.c_str()), this);
	connect(action, SIGNAL(triggered()), this, method);
	m_appMenu->addAction(action);
}

void SimpleQGLV::init_app_menu()
{
	m_appMenu->clear();
}

void SimpleQGLV::setHelpMsg(const std::string& msg)
{
	m_helpString = msg;
}

void SimpleQGLV::setGLWidgetMouseTracking(bool b)
{
	m_qglWidget->setMouseTracking(b);
}

void SimpleQGLV::closeEvent(QCloseEvent *event)
{
	QWidget::closeEvent(event) ;
	cb_exit();
}

void SimpleQGLV::keyPressEvent(QKeyEvent *e)
{
	if (e->modifiers() & Qt::ShiftModifier)
	{
		if ((e->key() == Qt::Key_Return))
			toggleVisibilityConsole();
	}
	else
	{
		if ((e->key() == Qt::Key_Return) && m_dock != NULL)
			toggleVisibilityDock();
	}

    if (e->key() == Qt::Key_Escape)
    	close();
    else
        QWidget::keyPressEvent(e);

327
//	m_qglWidget->keyPressEvent(e); // ?
Sylvain Thery's avatar
Sylvain Thery committed
328 329 330 331 332
}

void SimpleQGLV::keyReleaseEvent(QKeyEvent *e)
{
	QWidget::keyReleaseEvent(e);
333
//	m_qglWidget->keyReleaseEvent(e);
Sylvain Thery's avatar
Sylvain Thery committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
}

void SimpleQGLV::glMousePosition(int& x, int& y)
{
	QPoint xy = m_qglWidget->mapFromGlobal(QCursor::pos());
	x = xy.x();
	y = m_qglWidget->getHeight() - xy.y();
}


//GLfloat SimpleQT::getOrthoScreenRay(int x, int y, Geom::Vec3f& rayA, Geom::Vec3f& rayB, int radius)
//{
//	// get Z from depth buffer
//	int yy = y;
//	GLfloat depth_t[25];
//	glReadPixels(x-2, yy-2, 5, 5, GL_DEPTH_COMPONENT, GL_FLOAT, depth_t);
//
//	GLfloat depth=0.0f;
//	unsigned int nb=0;
//	for (unsigned int i=0; i< 25; ++i)
//	{
//		if (depth_t[i] != 1.0f)
//		{
//			depth += depth_t[i];
//			nb++;
//		}
//	}
//	if (nb>0)
//		depth /= float(nb);
//	else
//		depth = 0.5f;
//
//	glm::i32vec4 viewport;
//	glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
//
//	glm::vec3 win(x, yy, 0.0f);
//
//	glm::vec3 P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
//
//	rayA[0] = P[0];
//	rayA[1] = P[1];
//	rayA[2] = P[2];
//
//	win[2] = depth;
//
//	P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
//	rayB[0] = P[0];
//	rayB[1] = P[1];
//	rayB[2] = P[2];
//
//	if (depth == 1.0f)	// depth vary in [0-1]
//		win[2] = 0.5f;
//
//	win[0] += radius;
//	P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
//	Geom::Vec3f Q;
//	Q[0] = P[0];
//	Q[1] = P[1];
//	Q[2] = P[2];
//
//	// compute & return distance
//	Q -= rayB;
//	return float(Q.norm());
//}
//
//float SimpleQT::getWidthInWorld(unsigned int pixel_width, const Geom::Vec3f& center)
//{
//
//	glm::i32vec4 viewport;
//	glGetIntegerv(GL_VIEWPORT, &(viewport[0]));
//
//	glm::vec3 win = glm::project(glm::vec3(center[0],center[1],center[2]), m_modelView_matrix, m_projection_matrix, viewport);
//
//	win[0]-= pixel_width/2;
//
//	glm::vec3 P = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
//
//	win[0] += pixel_width;
//
//	glm::vec3 Q = glm::unProject(win, m_modelView_matrix, m_projection_matrix, viewport);
//
//	return glm::distance(P,Q);
//}
//

/*
void SimpleQGLV::synchronize(SimpleQT* sqt)
{
	m_glWidget->getObjPos() = sqt->m_glWidget->getObjPos() ;

	m_projection_matrix = sqt->m_projection_matrix;
	m_modelView_matrix = sqt->m_modelView_matrix;
	for (unsigned int i = 0; i < 4; ++i)
	{
		m_curquat[i] = sqt->m_curquat[i];
		m_lastquat[i] = sqt->m_lastquat[i];
	}
	m_trans_x = sqt->trans_x();
	m_trans_y = sqt->trans_y();
	m_trans_z = sqt->trans_z();

	SimpleQT::cb_updateMatrix();

	m_glWidget->modelModified();
	m_glWidget->updateGL();
}
*/

void SimpleQGLV::registerShader(GLSLShader* ptr)
{
	GLSLShader::registerShader(this, ptr) ;
}

void SimpleQGLV::unregisterShader(GLSLShader* ptr)
{
	GLSLShader::unregisterShader(this, ptr) ;
}

void SimpleQGLV::cb_updateMatrix()
{
	glm::mat4 model(m_modelView_matrix);
	model *= m_transfo_matrix;

457
	if (GLSLShader::CURRENT_OGL_VERSION == 1) // more more used
Sylvain Thery's avatar
Sylvain Thery committed
458 459 460 461 462 463 464 465 466
	{
		glMatrixMode(GL_PROJECTION);
		glLoadMatrixf(glm::value_ptr(m_projection_matrix));

		glMatrixMode(GL_MODELVIEW);
		glLoadMatrixf(glm::value_ptr(model));
	}
	else
	{
467 468
		for(std::set< std::pair<void*, GLSLShader*> >::iterator it = GLSLShader::m_registeredShaders->begin();
			it != GLSLShader::m_registeredShaders->end();
Sylvain Thery's avatar
Sylvain Thery committed
469 470 471 472 473 474 475 476 477 478
			++it)
		{
			if ((it->first == NULL) || (it->first == this))
			{
				it->second->updateMatrices(m_projection_matrix, model);
			}
		}
	}
}

479 480 481 482 483 484 485 486 487
void SimpleQGLV::paintEvent(QPaintEvent* )
{
	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing, true);
	painter.setRenderHint(QPainter::HighQualityAntialiasing, true);
	painter.setRenderHint(QPainter::TextAntialiasing, false);
}


Sylvain Thery's avatar
Sylvain Thery committed
488 489 490 491 492 493 494 495 496 497 498
void SimpleQGLV::updateGL()
{
	m_qglWidget->updateGL();
}

void SimpleQGLV::updateGLMatrices()
{
}

void SimpleQGLV::transfoRotate(float angle, float x, float y, float z)
{
499
	transfoMatrix() = glm::rotate(transfoMatrix(), glm::radians(angle), glm::vec3(x,y,z));
Sylvain Thery's avatar
Sylvain Thery committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
}

void SimpleQGLV::transfoTranslate(float tx, float ty, float tz)
{
	transfoMatrix() = glm::translate(transfoMatrix(), glm::vec3(tx,ty,tz));
}

void SimpleQGLV::transfoScale(float sx, float sy, float sz)
{
	transfoMatrix() = glm::scale(transfoMatrix(), glm::vec3(sx,sy,sz));
}

void SimpleQGLV::pushTransfoMatrix()
{
	m_stack_trf.push(transfoMatrix());
}

bool SimpleQGLV::popTransfoMatrix()
{
	if (m_stack_trf.empty())
		return false;
	transfoMatrix() = m_stack_trf.top();
	m_stack_trf.pop();
	return true;
}

std::string SimpleQGLV::selectFile(const std::string& title, const std::string& dir, const std::string& filters)
{
    QString fileName = QFileDialog::getOpenFileName(this, tr(title.c_str()), tr(dir.c_str()), tr(filters.c_str()), 0, 0);
    return fileName.toStdString();
}

std::string SimpleQGLV::selectFileSave(const std::string& title, const std::string& dir, const std::string& filters)
{
    QString fileName = QFileDialog::getSaveFileName(this, tr(title.c_str()), tr(dir.c_str()), tr(filters.c_str()), 0, 0);
    return fileName.toStdString();
}

void SimpleQGLV::cb_about_cgogn()
{
	QString str("CGoGN:\nCombinatorial and Geometric modeling\n"
				"with Generic N-dimensional Maps\n"
				"Web site: http://cgogn.unistra.fr \n"
				"Contact information: cgogn@unistra.fr");
	QMessageBox::about(this, tr("About CGoGN"), str);
}

void SimpleQGLV::cb_about()
{
   QMessageBox::about(this, tr("About App"), m_helpString.c_str());
}

void SimpleQGLV::snapshot(const QString& filename, const char* format, const int& quality)
{
	QImage im = m_qglWidget->grabFrameBuffer(false);
	im.save(filename, format, quality);
}

void SimpleQGLV::setGeometry(int x, int y, int w, int h)
{
	move(x,y);
	resize(w,h);
}

} // namespace QT

} // namespace Utils

} // namespace CGoGN