view.cpp 8.71 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
#include "cameraViewDialog.h"
8
9
10
11
#include "pluginsViewDialog.h"
#include "mapsViewDialog.h"

#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

18
19
unsigned int View::viewCount = 0;

Pierre Kraemer's avatar
Pierre Kraemer committed
20
21
View::View(const QString& name, Window* w, const QGLWidget* shareWidget) :
	QGLViewer(NULL, shareWidget),
22
	m_name(name),
Pierre Kraemer's avatar
Pierre Kraemer committed
23
	m_window(w),
Pierre Kraemer's avatar
Pierre Kraemer committed
24
25
26
27
	m_cameraButton(NULL),
	m_pluginsButton(NULL),
	m_mapsButton(NULL),
	m_closeButton(NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
28
{
29
30
	++viewCount;

31
	m_currentCamera = m_window->addCamera();
32

33
34
35
	m_cameraViewDialog = new CameraViewDialog(m_window, this);
	m_pluginsViewDialog = new PluginsViewDialog(m_window, this);
	m_mapsViewDialog = new MapsViewDialog(m_window, this);
36
37
38
39
}

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

42
43
44
	qglviewer::Camera* c = new qglviewer::Camera();
	this->setCamera(c);

Pierre Kraemer's avatar
Pierre Kraemer committed
45
	foreach(Plugin* plugin, l_plugins)
46
47
	{
		plugin->unlinkView(this);
48
//		unlinkPlugin(plugin);
49
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
50

51
	foreach(MapHandlerGen* map, l_maps)
52
53
	{
		map->unlinkView(this);
54
//		unlinkMap(map);
55
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
56
57

	delete m_buttonArea;
Pierre Kraemer's avatar
Pierre Kraemer committed
58
59
}

60
void View::init()
Pierre Kraemer's avatar
Pierre Kraemer committed
61
{
62
63
64
	m_buttonArea = new ViewButtonArea(this);
	m_buttonArea->setTopRightPosition(this->width(), 0);

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
77
78
79
80
81
82
83
84
85
	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);
86
	m_buttonArea->addButton(m_closeButton);
87
	connect(m_closeButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(cb_closeView(int, int, int, int)));
88
89
90
91
92
93
94
95

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

	this->setBackgroundColor(QColor(0,0,0));

	updateTextInfo();
96
97
}

98
void View::preDraw()
99
{
100
101
102
103
104
105
	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)
	{
106
107
		if(it->first == this || it->first == NULL)
			it->second->updateMatrices(pm, mm);
108
	}
109
110

	QGLViewer::preDraw();
111
112
113
114
}

void View::draw()
{
115
116
117
	foreach(Plugin* plugin, l_plugins)
		plugin->redraw(this);
}
118

119
120
void View::postDraw()
{
121
	glPushAttrib(GL_ALL_ATTRIB_BITS);
122
	drawButtons();
Pierre Kraemer's avatar
Pierre Kraemer committed
123
	drawText();
124
125
	if(isCurrentView())
		drawFrame();
126
	glPopAttrib();
127
128
129
130
131
132
133
134
135

	QGLViewer::postDraw();
}

void View::resizeGL(int width, int height)
{
	QGLViewer::resizeGL(width, height);
	if(m_buttonArea)
		m_buttonArea->setTopRightPosition(width, 0);
136
137
138
139
140
}

void View::drawText()
{
	glColor3f(1.0f, 1.0f, 1.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
141
142
143
	QGLViewer::drawText(10, 20, m_textInfo);
}

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

153
154
155
156
157
158
159
160
161
162
163
164
165
166
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();
}

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

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

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

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

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

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

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

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

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

232
233
234
235
/*********************************************************
 * MANAGE LINKED PLUGINS
 *********************************************************/

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

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

253
254
255
256
/*********************************************************
 * MANAGE LINKED MAPS
 *********************************************************/

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

		// TODO : update view global BB
		setSceneBoundingBox(map->getBBmin(), map->getBBmax());
265
		showEntireScene();
266
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
267
268
}

269
void View::unlinkMap(MapHandlerGen* map)
Pierre Kraemer's avatar
Pierre Kraemer committed
270
271
{
	l_maps.removeOne(map);
272
273

	// TODO : update view global BB
Pierre Kraemer's avatar
Pierre Kraemer committed
274
275
}

276
277
278



279
280
void View::updateTextInfo()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
281
	m_textInfo =
282
283
		QString("view: ") + m_name +
		QString(" / camera: ") + m_currentCamera->getName();
284
285
}

286
glm::mat4 View::getCurrentModelViewMatrix() const
287
288
{
	GLdouble gl_mvm[16];
289
	camera()->getModelViewMatrix(gl_mvm);
290
291
292
293
294
295
296
297
298
	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;
}

299
glm::mat4 View::getCurrentProjectionMatrix() const
300
301
{
	GLdouble gl_pm[16];
302
	camera()->getProjectionMatrix(gl_pm);
303
304
305
306
307
308
309
310
311
	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;
}

312
glm::mat4 View::getCurrentModelViewProjectionMatrix() const
313
314
{
	GLdouble gl_mvpm[16];
315
	camera()->getModelViewProjectionMatrix(gl_mvpm);
316
317
318
319
320
321
322
323
324
	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;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
325
void View::setCurrentModelViewMatrix(const glm::mat4& mvm)
326
327
328
329
330
331
332
{
	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];
	}
333
	camera()->setFromModelViewMatrix(gl_mvm);
334
335
}

Pierre Kraemer's avatar
Pierre Kraemer committed
336
void View::setCurrentProjectionMatrix(const glm::mat4& pm)
337
338
339
340
341
342
343
{
	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];
	}
344
	camera()->setFromProjectionMatrix(gl_pm);
345
346
}

347
void View::cb_cameraView(int x, int y, int globalX, int globalY)
348
{
349
	m_cameraViewDialog->move(globalX, globalY);
350
351
352
	m_cameraViewDialog->show();
}

353
void View::cb_pluginsView(int x, int y, int globalX, int globalY)
354
{
355
	m_pluginsViewDialog->move(globalX, globalY);
356
357
358
	m_pluginsViewDialog->show();
}

359
void View::cb_mapsView(int x, int y, int globalX, int globalY)
360
{
361
	m_mapsViewDialog->move(globalX, globalY);
362
363
364
	m_mapsViewDialog->show();
}

365
void View::cb_closeView(int x, int y, int globalX, int globalY)
Pierre Kraemer's avatar
Pierre Kraemer committed
366
{
367
	m_window->removeView(m_name);
Pierre Kraemer's avatar
Pierre Kraemer committed
368
}
Pierre Kraemer's avatar
Pierre Kraemer committed
369
370
371
372
373
374
375
376
377
378

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);
}