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

3
#include "plugin.h"
4
#include "camera.h"
5
#include "viewButtonArea.h"
6
#include "mapHandler.h"
7

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

#include "Utils/GLSLShader.h"
13
#include "Algo/Geometry/boundingbox.h"
14 15 16 17

#include <QKeyEvent>
#include <QMouseEvent>
#include <QWheelEvent>
18

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

namespace SCHNApps
{

25 26
unsigned int View::viewCount = 0;

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

38
	m_currentCamera = m_window->addCamera();
39

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

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

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

52
	foreach(Plugin* plugin, l_plugins)
53 54
	{
		plugin->unlinkView(this);
55
//		unlinkPlugin(plugin);
56
	}
57

58
	foreach(MapHandlerGen* map, l_maps)
59 60
	{
		map->unlinkView(this);
61
//		unlinkMap(map);
62
	}
63 64

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

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

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
84 85 86 87 88 89 90 91 92
	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);
93
	m_buttonArea->addButton(m_closeButton);
94
	connect(m_closeButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(cb_closeView(int, int, int, int)));
95 96 97 98 99 100

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

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

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

107 108
	glm::mat4 mm = getCurrentModelViewMatrix();
	glm::mat4 pm = getCurrentProjectionMatrix();
109
	foreach(Plugin* plugin, l_plugins)
110
	{
111 112 113
		const QList<CGoGN::Utils::GLSLShader*>& shaders = plugin->getShaders();
		foreach(CGoGN::Utils::GLSLShader* shader, shaders)
			shader->updateMatrices(pm, mm);
114
	}
115 116

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

void View::draw()
{
121 122 123 124 125 126 127 128 129 130
	QList<Camera*> cameras = m_window->getCamerasList();
	foreach(Camera* camera, cameras)
	{
		if(camera != m_currentCamera)
		{
			if(camera->getDraw()) camera->draw();
			if(camera->getDrawPath()) camera->drawAllPaths();
		}
	}

131 132 133
	foreach(Plugin* plugin, l_plugins)
		plugin->redraw(this);
}
134

135 136
void View::postDraw()
{
137
	glPushAttrib(GL_ALL_ATTRIB_BITS);
138
	drawButtons();
139 140
	if(isCurrentView())
		drawFrame();
141
	glPopAttrib();
142 143 144 145 146 147 148 149 150

	QGLViewer::postDraw();
}

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

153 154 155 156 157 158 159 160
void View::drawButtons()
{
	glEnable(GL_TEXTURE_2D);
	startScreenCoordinatesSystem();
	m_buttonArea->draw();
	stopScreenCoordinatesSystem();
	glDisable(GL_TEXTURE_2D);
}
161

162 163 164 165 166 167 168 169 170 171 172 173 174 175
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();
}

176 177
void View::keyPressEvent(QKeyEvent* event)
{
178
	foreach(Plugin* plugin, l_plugins)
179
		plugin->keyPress(this, event);
Pierre Kraemer's avatar
Pierre Kraemer committed
180
	QGLViewer::keyPressEvent(event);
181 182
}

183
void View::keyReleaseEvent(QKeyEvent *event)
184
{
185
	foreach(Plugin* plugin, l_plugins)
186
		plugin->keyRelease(this, event);
187
	QGLViewer::keyReleaseEvent(event);
188 189 190 191
}

void View::mousePressEvent(QMouseEvent* event)
{
192 193 194
	if(!isCurrentView())
		m_window->setCurrentView(this);

195
	if(m_buttonArea->isClicked(event->x(), event->y()))
196
		m_buttonArea->clickButton(event->x(), event->y(), event->globalX(), event->globalY());
197 198 199
	else
	{
		foreach(Plugin* plugin, l_plugins)
200
			plugin->mousePress(this, event);
201
		QGLViewer::mousePressEvent(event);
202
	}
203 204 205 206
}

void View::mouseReleaseEvent(QMouseEvent* event)
{
207
	foreach(Plugin* plugin, l_plugins)
208
		plugin->mouseRelease(this, event);
Pierre Kraemer's avatar
Pierre Kraemer committed
209
	QGLViewer::mouseReleaseEvent(event);
210 211 212 213
}

void View::mouseMoveEvent(QMouseEvent* event)
{
214
	foreach(Plugin* plugin, l_plugins)
215
		plugin->mouseMove(this, event);
Pierre Kraemer's avatar
Pierre Kraemer committed
216
	QGLViewer::mouseMoveEvent(event);
217

218
	QList<View*> views = m_currentCamera->getLinkedViews();
219 220 221 222 223
	foreach(View* view, views)
	{
		if(view != this)
			view->updateGL();
	}
224 225 226 227
}

void View::wheelEvent(QWheelEvent* event)
{
228
	foreach(Plugin* plugin, l_plugins)
229
		plugin->wheelEvent(this, event);
Pierre Kraemer's avatar
Pierre Kraemer committed
230
	QGLViewer::wheelEvent(event);
231

232
	QList<View*> views = m_currentCamera->getLinkedViews();
233 234 235 236 237
	foreach(View* view, views)
	{
		if(view != this)
			view->updateGL();
	}
238 239
}

240 241 242 243
/*********************************************************
 * MANAGE LINKED CAMERA
 *********************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
244 245
void View::setCurrentCamera(Camera* c)
{
246
	if(c != m_currentCamera && c != NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
247 248 249
	{
		m_currentCamera = c;
		this->setCamera(m_currentCamera);
250
		emit(currentCameraChanged(c));
Pierre Kraemer's avatar
Pierre Kraemer committed
251 252 253 254
		updateGL();
	}
}

255 256 257 258
/*********************************************************
 * MANAGE LINKED PLUGINS
 *********************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
259 260 261
void View::linkPlugin(Plugin* plugin)
{
	if(plugin && !l_plugins.contains(plugin))
262
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
263
		l_plugins.push_back(plugin);
264
		emit(pluginLinked(plugin));
265
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
266 267 268 269
}

void View::unlinkPlugin(Plugin* plugin)
{
270 271
	if(l_plugins.removeOne(plugin))
		emit(pluginUnlinked(plugin));
Pierre Kraemer's avatar
Pierre Kraemer committed
272 273
}

274 275 276 277
/*********************************************************
 * MANAGE LINKED MAPS
 *********************************************************/

278
void View::linkMap(MapHandlerGen* map)
Pierre Kraemer's avatar
Pierre Kraemer committed
279 280
{
	if(map && !l_maps.contains(map))
281
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
282
		l_maps.push_back(map);
283
		emit(mapLinked(map));
284
		updateViewBB();
285
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
286 287
}

288
void View::unlinkMap(MapHandlerGen* map)
Pierre Kraemer's avatar
Pierre Kraemer committed
289
{
290 291 292 293 294
	if(l_maps.removeOne(map))
	{
		emit(mapUnlinked(map));
		updateViewBB();
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
295 296
}

297
void View::updateViewBB()
298
{
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	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];
			}
		}
	}
319 320
	camera()->setSceneBoundingBox(bbMin, bbMax);
	camera()->showEntireScene();
321 322
}

323 324 325 326
/*********************************************************
 * MANAGE MATRICES
 *********************************************************/

327
glm::mat4 View::getCurrentModelViewMatrix() const
328 329
{
	GLdouble gl_mvm[16];
330
	camera()->getModelViewMatrix(gl_mvm);
331 332 333 334 335 336 337 338 339
	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;
}

340
glm::mat4 View::getCurrentProjectionMatrix() const
341 342
{
	GLdouble gl_pm[16];
343
	camera()->getProjectionMatrix(gl_pm);
344 345 346 347 348 349 350 351 352
	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;
}

353
glm::mat4 View::getCurrentModelViewProjectionMatrix() const
354 355
{
	GLdouble gl_mvpm[16];
356
	camera()->getModelViewProjectionMatrix(gl_mvpm);
357 358 359 360 361 362 363 364 365
	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;
}

366
void View::cb_cameraView(int x, int y, int globalX, int globalY)
367
{
368
	m_cameraViewDialog->move(globalX, globalY);
369 370 371
	m_cameraViewDialog->show();
}

372
void View::cb_pluginsView(int x, int y, int globalX, int globalY)
373
{
374
	m_pluginsViewDialog->move(globalX, globalY);
375 376 377
	m_pluginsViewDialog->show();
}

378
void View::cb_mapsView(int x, int y, int globalX, int globalY)
379
{
380
	m_mapsViewDialog->move(globalX, globalY);
381 382 383
	m_mapsViewDialog->show();
}

384
void View::cb_closeView(int x, int y, int globalX, int globalY)
385
{
386
	m_window->removeView(m_name);
387
}
Pierre Kraemer's avatar
Pierre Kraemer committed
388 389 390 391 392 393 394 395 396 397

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
398 399 400 401

} // namespace SCHNApps

} // namespace CGoGN