view.cpp 9.23 KB
Newer Older
1 2
#include "view.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
3
#include "plugin.h"
4
#include "camera.h"
5 6
#include "viewButtonArea.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
7 8 9
#include "dialogs/cameraViewDialog.h"
#include "dialogs/pluginsViewDialog.h"
#include "dialogs/mapsViewDialog.h"
10 11

#include "Utils/GLSLShader.h"
12
#include "Algo/Geometry/boundingbox.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
13 14 15 16

#include <QKeyEvent>
#include <QMouseEvent>
#include <QWheelEvent>
17

Pierre Kraemer's avatar
Pierre Kraemer committed
18 19 20 21 22 23
namespace CGoGN
{

namespace SCHNApps
{

24 25
unsigned int View::viewCount = 0;

Pierre Kraemer's avatar
Pierre Kraemer committed
26 27
View::View(const QString& name, Window* w, const QGLWidget* shareWidget) :
	QGLViewer(NULL, shareWidget),
28
	m_name(name),
Pierre Kraemer's avatar
Pierre Kraemer committed
29
	m_window(w),
Pierre Kraemer's avatar
Pierre Kraemer committed
30 31 32 33
	m_cameraButton(NULL),
	m_pluginsButton(NULL),
	m_mapsButton(NULL),
	m_closeButton(NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
34
{
35 36
	++viewCount;

37
	m_currentCamera = m_window->addCamera();
38

39 40 41
	m_cameraViewDialog = new CameraViewDialog(m_window, this);
	m_pluginsViewDialog = new PluginsViewDialog(m_window, this);
	m_mapsViewDialog = new MapsViewDialog(m_window, this);
42 43 44 45
}

View::~View()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
46 47
	m_currentCamera->unlinkView(this);

48 49 50
	qglviewer::Camera* c = new qglviewer::Camera();
	this->setCamera(c);

Pierre Kraemer's avatar
Pierre Kraemer committed
51
	foreach(Plugin* plugin, l_plugins)
52 53
	{
		plugin->unlinkView(this);
54
//		unlinkPlugin(plugin);
55
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
56

57
	foreach(MapHandlerGen* map, l_maps)
58 59
	{
		map->unlinkView(this);
60
//		unlinkMap(map);
61
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
62 63

	delete m_buttonArea;
Pierre Kraemer's avatar
Pierre Kraemer committed
64 65
}

66
void View::init()
Pierre Kraemer's avatar
Pierre Kraemer committed
67
{
68 69 70
	m_buttonArea = new ViewButtonArea(this);
	m_buttonArea->setTopRightPosition(this->width(), 0);

Pierre Kraemer's avatar
Pierre Kraemer committed
71
	m_cameraButton = new ViewButton(":icons/icons/cameras.png", this);
72
	m_buttonArea->addButton(m_cameraButton);
73
	connect(m_cameraButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(cb_cameraView(int, int, int, int)));
Pierre Kraemer's avatar
Pierre Kraemer committed
74

Pierre Kraemer's avatar
Pierre Kraemer committed
75
	m_pluginsButton = new ViewButton(":icons/icons/plugins.png", this);
76
	m_buttonArea->addButton(m_pluginsButton);
77
	connect(m_pluginsButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(cb_pluginsView(int, int, int, int)));
Pierre Kraemer's avatar
Pierre Kraemer committed
78

Pierre Kraemer's avatar
Pierre Kraemer committed
79
	m_mapsButton = new ViewButton(":icons/icons/maps.png", this);
80
	m_buttonArea->addButton(m_mapsButton);
81
	connect(m_mapsButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(cb_mapsView(int, int, int, int)));
Pierre Kraemer's avatar
Pierre Kraemer committed
82

Pierre Kraemer's avatar
Pierre Kraemer committed
83 84 85 86 87 88 89 90 91
	m_VsplitButton = new ViewButton(":icons/icons/Vsplit.png", this);
	m_buttonArea->addButton(m_VsplitButton);
	connect(m_VsplitButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(cb_VsplitView(int, int, int, int)));

	m_HsplitButton = new ViewButton(":icons/icons/Hsplit.png", this);
	m_buttonArea->addButton(m_HsplitButton);
	connect(m_HsplitButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(cb_HsplitView(int, int, int, int)));

	m_closeButton = new ViewButton(":icons/icons/close.png", this);
92
	m_buttonArea->addButton(m_closeButton);
93
	connect(m_closeButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(cb_closeView(int, int, int, int)));
94 95 96 97 98 99

	qglviewer::Camera* c = this->camera();
	this->setCamera(m_currentCamera);
	delete c;

	this->setBackgroundColor(QColor(0,0,0));
100 101
}

102
void View::preDraw()
103
{
Pierre Kraemer's avatar
Pierre Kraemer committed
104 105
	m_currentCamera->setScreenWidthAndHeight(width(), height());

106 107 108 109 110 111
	glm::mat4 mm = getCurrentModelViewMatrix();
	glm::mat4 pm = getCurrentProjectionMatrix();
	for(std::set< std::pair<void*, CGoGN::Utils::GLSLShader*> >::iterator it = CGoGN::Utils::GLSLShader::m_registeredShaders.begin();
		it != CGoGN::Utils::GLSLShader::m_registeredShaders.end();
		++it)
	{
112 113
		if(it->first == this || it->first == NULL)
			it->second->updateMatrices(pm, mm);
114
	}
115 116

	QGLViewer::preDraw();
117 118 119 120
}

void View::draw()
{
121 122 123
	foreach(Plugin* plugin, l_plugins)
		plugin->redraw(this);
}
124

125 126
void View::postDraw()
{
127
	glPushAttrib(GL_ALL_ATTRIB_BITS);
128
	drawButtons();
129 130
	if(isCurrentView())
		drawFrame();
131
	glPopAttrib();
132 133 134 135 136 137 138 139 140

	QGLViewer::postDraw();
}

void View::resizeGL(int width, int height)
{
	QGLViewer::resizeGL(width, height);
	if(m_buttonArea)
		m_buttonArea->setTopRightPosition(width, 0);
141 142
}

Pierre Kraemer's avatar
Pierre Kraemer committed
143 144 145 146 147 148 149 150
void View::drawButtons()
{
	glEnable(GL_TEXTURE_2D);
	startScreenCoordinatesSystem();
	m_buttonArea->draw();
	stopScreenCoordinatesSystem();
	glDisable(GL_TEXTURE_2D);
}
151

152 153 154 155 156 157 158 159 160 161 162 163 164 165
void View::drawFrame()
{
	startScreenCoordinatesSystem();
	glColor3f(0.0f, 1.0f, 0.0f);
	glLineWidth(2.0);
	glBegin(GL_LINE_LOOP);
		glVertex2i(1, 1);
		glVertex2i(1, height()-1);
		glVertex2i(width()-1, height()-1);
		glVertex2i(width()-1, 1);
	glEnd();
	stopScreenCoordinatesSystem();
}

166 167
void View::keyPressEvent(QKeyEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
168
	foreach(Plugin* plugin, l_plugins)
169
		plugin->keyPress(this, event->key());
Pierre Kraemer's avatar
Pierre Kraemer committed
170
	QGLViewer::keyPressEvent(event);
171 172
}

Pierre Kraemer's avatar
Pierre Kraemer committed
173
void View::keyReleaseEvent(QKeyEvent *event)
174
{
Pierre Kraemer's avatar
Pierre Kraemer committed
175
	foreach(Plugin* plugin, l_plugins)
176
		plugin->keyRelease(this, event->key());
Pierre Kraemer's avatar
Pierre Kraemer committed
177
	QGLViewer::keyReleaseEvent(event);
178 179 180 181
}

void View::mousePressEvent(QMouseEvent* event)
{
182 183 184
	if(!isCurrentView())
		m_window->setCurrentView(this);

Pierre Kraemer's avatar
Pierre Kraemer committed
185
	if(m_buttonArea->isClicked(event->x(), event->y()))
186
		m_buttonArea->clickButton(event->x(), event->y(), event->globalX(), event->globalY());
Pierre Kraemer's avatar
Pierre Kraemer committed
187 188 189
	else
	{
		foreach(Plugin* plugin, l_plugins)
190
			plugin->mousePress(this, event->button(), event->pos().x(), event->pos().y());
191
		QGLViewer::mousePressEvent(event);
Pierre Kraemer's avatar
Pierre Kraemer committed
192
	}
193 194 195 196
}

void View::mouseReleaseEvent(QMouseEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
197
	foreach(Plugin* plugin, l_plugins)
198
		plugin->mouseRelease(this, event->button(), event->pos().x(), event->pos().y());
Pierre Kraemer's avatar
Pierre Kraemer committed
199
	QGLViewer::mouseReleaseEvent(event);
200 201 202 203
}

void View::mouseMoveEvent(QMouseEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
204
	foreach(Plugin* plugin, l_plugins)
205
		plugin->mouseMove(this, event->button(), event->pos().x(), event->pos().y());
Pierre Kraemer's avatar
Pierre Kraemer committed
206
	QGLViewer::mouseMoveEvent(event);
207 208 209 210
}

void View::wheelEvent(QWheelEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
211
	foreach(Plugin* plugin, l_plugins)
212
		plugin->wheelEvent(this, event->delta(), event->pos().x(), event->pos().y());
Pierre Kraemer's avatar
Pierre Kraemer committed
213
	QGLViewer::wheelEvent(event);
214 215
}

216 217 218 219
/*********************************************************
 * MANAGE LINKED CAMERA
 *********************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
220 221
void View::setCurrentCamera(Camera* c)
{
222
	if(c != m_currentCamera && c != NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
223 224 225 226 227 228 229
	{
		m_currentCamera = c;
		this->setCamera(m_currentCamera);
		updateGL();
	}
}

230 231 232 233
/*********************************************************
 * MANAGE LINKED PLUGINS
 *********************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
234 235 236
void View::linkPlugin(Plugin* plugin)
{
	if(plugin && !l_plugins.contains(plugin))
237
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
238
		l_plugins.push_back(plugin);
239 240 241
		if(isCurrentView())
			m_window->enablePluginTabWidgets(plugin);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
242 243 244 245 246
}

void View::unlinkPlugin(Plugin* plugin)
{
	l_plugins.removeOne(plugin);
247 248
	if(isCurrentView())
		m_window->disablePluginTabWidgets(plugin);
Pierre Kraemer's avatar
Pierre Kraemer committed
249 250
}

251 252 253 254
/*********************************************************
 * MANAGE LINKED MAPS
 *********************************************************/

255
void View::linkMap(MapHandlerGen* map)
Pierre Kraemer's avatar
Pierre Kraemer committed
256 257
{
	if(map && !l_maps.contains(map))
258
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
259
		l_maps.push_back(map);
260
		updateViewBB();
261
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
262 263
}

264
void View::unlinkMap(MapHandlerGen* map)
Pierre Kraemer's avatar
Pierre Kraemer committed
265 266
{
	l_maps.removeOne(map);
267
	updateViewBB();
Pierre Kraemer's avatar
Pierre Kraemer committed
268 269
}

270
void View::updateViewBB()
271
{
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	qglviewer::Vec bbMin(0,0,0);
	qglviewer::Vec bbMax(1,1,1);
	if(!l_maps.empty())
	{
		bbMin = l_maps[0]->getBBmin();
		bbMax = l_maps[0]->getBBmax();
		for(int i = 1; i < l_maps.size(); ++i)
		{
			MapHandlerGen* m = l_maps[i];
			qglviewer::Vec min = m->getBBmin();
			qglviewer::Vec max = m->getBBmax();
			for(unsigned int dim = 0; dim < 3; ++dim)
			{
				if(min[dim] < bbMin[dim])
					bbMin[dim] = min[dim];
				if(max[dim] > bbMax[dim])
					bbMax[dim] = max[dim];
			}
		}
	}
	setSceneBoundingBox(bbMin, bbMax);
	showEntireScene();
294 295
}

296 297 298 299
/*********************************************************
 * MANAGE MATRICES
 *********************************************************/

300
glm::mat4 View::getCurrentModelViewMatrix() const
301 302
{
	GLdouble gl_mvm[16];
303
	camera()->getModelViewMatrix(gl_mvm);
304 305 306 307 308 309 310 311 312
	glm::mat4 mvm;
	for(unsigned int i = 0; i < 4; ++i)
	{
		for(unsigned int j = 0; j < 4; ++j)
			mvm[i][j] = (float)gl_mvm[i*4+j];
	}
	return mvm;
}

313
glm::mat4 View::getCurrentProjectionMatrix() const
314 315
{
	GLdouble gl_pm[16];
316
	camera()->getProjectionMatrix(gl_pm);
317 318 319 320 321 322 323 324 325
	glm::mat4 pm;
	for(unsigned int i = 0; i < 4; ++i)
	{
		for(unsigned int j = 0; j < 4; ++j)
			pm[i][j] = (float)gl_pm[i*4+j];
	}
	return pm;
}

326
glm::mat4 View::getCurrentModelViewProjectionMatrix() const
327 328
{
	GLdouble gl_mvpm[16];
329
	camera()->getModelViewProjectionMatrix(gl_mvpm);
330 331 332 333 334 335 336 337 338
	glm::mat4 mvpm;
	for(unsigned int i = 0; i < 4; ++i)
	{
		for(unsigned int j = 0; j < 4; ++j)
			mvpm[i][j] = (float)gl_mvpm[i*4+j];
	}
	return mvpm;
}

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
//void View::setCurrentModelViewMatrix(const glm::mat4& mvm)
//{
//	GLdouble gl_mvm[16];
//	for(unsigned int i = 0; i < 4; ++i)
//	{
//		for(unsigned int j = 0; j < 4; ++j)
//			gl_mvm[i*4+j] = mvm[i][j];
//	}
//	camera()->setFromModelViewMatrix(gl_mvm);
//}
//
//void View::setCurrentProjectionMatrix(const glm::mat4& pm)
//{
//	float gl_pm[12];
//	for(unsigned int i = 0; i < 3; ++i)
//	{
//		for(unsigned int j = 0; j < 4; ++j)
//			gl_pm[i*3+j] = pm[i][j];
//	}
//	camera()->setFromProjectionMatrix(gl_pm);
//}
360

361
void View::cb_cameraView(int x, int y, int globalX, int globalY)
362
{
363
	m_cameraViewDialog->move(globalX, globalY);
364 365 366
	m_cameraViewDialog->show();
}

367
void View::cb_pluginsView(int x, int y, int globalX, int globalY)
368
{
369
	m_pluginsViewDialog->move(globalX, globalY);
370 371 372
	m_pluginsViewDialog->show();
}

373
void View::cb_mapsView(int x, int y, int globalX, int globalY)
374
{
375
	m_mapsViewDialog->move(globalX, globalY);
376 377 378
	m_mapsViewDialog->show();
}

379
void View::cb_closeView(int x, int y, int globalX, int globalY)
Pierre Kraemer's avatar
Pierre Kraemer committed
380
{
381
	m_window->removeView(m_name);
Pierre Kraemer's avatar
Pierre Kraemer committed
382
}
Pierre Kraemer's avatar
Pierre Kraemer committed
383 384 385 386 387 388 389 390 391 392

void View::cb_VsplitView(int x, int y, int globalX, int globalY)
{
	m_window->splitView(m_name, Qt::Horizontal);
}

void View::cb_HsplitView(int x, int y, int globalX, int globalY)
{
	m_window->splitView(m_name, Qt::Vertical);
}
Pierre Kraemer's avatar
Pierre Kraemer committed
393 394 395 396

} // namespace SCHNApps

} // namespace CGoGN