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

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

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

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

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

19
20
unsigned int View::viewCount = 0;

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

32
	m_currentCamera = m_window->addCamera();
33

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

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

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

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

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

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

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

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

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

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

	m_closeButton = new ViewButton(":icons/icons/close_32.png", this);
79
	m_buttonArea->addButton(m_closeButton);
80
	connect(m_closeButton, SIGNAL(clicked(int, int, int, int)), this, SLOT(cb_closeView(int, int, int, int)));
81
82
83
84
85
86
87
88

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

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

	updateTextInfo();
89
90
}

91
void View::preDraw()
92
{
93
94
95
96
97
98
99
100
	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)
	{
		it->second->updateMatrices(pm, mm);
	}
101
102

	QGLViewer::preDraw();
103
104
105
106
}

void View::draw()
{
107
108
109
	foreach(Plugin* plugin, l_plugins)
		plugin->redraw(this);
}
110

111
112
void View::postDraw()
{
113
	glPushAttrib(GL_ALL_ATTRIB_BITS);
114
	drawButtons();
Pierre Kraemer's avatar
Pierre Kraemer committed
115
	drawText();
116
	glPopAttrib();
117
118
119
120
121
122
123
124
125

	QGLViewer::postDraw();
}

void View::resizeGL(int width, int height)
{
	QGLViewer::resizeGL(width, height);
	if(m_buttonArea)
		m_buttonArea->setTopRightPosition(width, 0);
126
127
128
129
130
}

void View::drawText()
{
	glColor3f(1.0f, 1.0f, 1.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
131
132
133
	QGLViewer::drawText(10, 20, m_textInfo);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
134
135
136
137
138
139
140
141
void View::drawButtons()
{
	glEnable(GL_TEXTURE_2D);
	startScreenCoordinatesSystem();
	m_buttonArea->draw();
	stopScreenCoordinatesSystem();
	glDisable(GL_TEXTURE_2D);
}
142
143
144

void View::keyPressEvent(QKeyEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
145
	foreach(Plugin* plugin, l_plugins)
146
		plugin->keyPress(this, event->key());
Pierre Kraemer's avatar
Pierre Kraemer committed
147
	QGLViewer::keyPressEvent(event);
148
149
}

Pierre Kraemer's avatar
Pierre Kraemer committed
150
void View::keyReleaseEvent(QKeyEvent *event)
151
{
Pierre Kraemer's avatar
Pierre Kraemer committed
152
	foreach(Plugin* plugin, l_plugins)
153
		plugin->keyRelease(this, event->key());
Pierre Kraemer's avatar
Pierre Kraemer committed
154
	QGLViewer::keyReleaseEvent(event);
155
156
157
158
}

void View::mousePressEvent(QMouseEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
159
	if(m_buttonArea->isClicked(event->x(), event->y()))
160
		m_buttonArea->clickButton(event->x(), event->y(), event->globalX(), event->globalY());
Pierre Kraemer's avatar
Pierre Kraemer committed
161
162
163
	else
	{
		foreach(Plugin* plugin, l_plugins)
164
			plugin->mousePress(this, event->button(), event->pos().x(), event->pos().y());
165
		QGLViewer::mousePressEvent(event);
Pierre Kraemer's avatar
Pierre Kraemer committed
166
	}
167
168
169
170
}

void View::mouseReleaseEvent(QMouseEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
171
	foreach(Plugin* plugin, l_plugins)
172
		plugin->mouseRelease(this, event->button(), event->pos().x(), event->pos().y());
Pierre Kraemer's avatar
Pierre Kraemer committed
173
	QGLViewer::mouseReleaseEvent(event);
174
175
176
177
}

void View::mouseMoveEvent(QMouseEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
178
	foreach(Plugin* plugin, l_plugins)
179
		plugin->mouseMove(this, event->button(), event->pos().x(), event->pos().y());
Pierre Kraemer's avatar
Pierre Kraemer committed
180
	QGLViewer::mouseMoveEvent(event);
181
182
183
184
}

void View::wheelEvent(QWheelEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
185
	foreach(Plugin* plugin, l_plugins)
186
		plugin->wheelEvent(this, event->delta(), event->pos().x(), event->pos().y());
Pierre Kraemer's avatar
Pierre Kraemer committed
187
	QGLViewer::wheelEvent(event);
188
189
190
191
192
193
194
195
196
}

void View::drawOverpaint(QPainter *painter)
{
	painter->save();
	painter->setOpacity(0.8);
    painter->restore();
}

197
198
199
200
/*********************************************************
 * MANAGE LINKED CAMERA
 *********************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
201
202
void View::setCurrentCamera(Camera* c)
{
203
	if(c != m_currentCamera && c != NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
204
205
206
207
208
209
210
211
	{
		m_currentCamera = c;
		this->setCamera(m_currentCamera);
		updateTextInfo();
		updateGL();
	}
}

212
213
214
215
/*********************************************************
 * MANAGE LINKED PLUGINS
 *********************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
216
217
218
219
220
221
222
223
224
225
226
void View::linkPlugin(Plugin* plugin)
{
	if(plugin && !l_plugins.contains(plugin))
		l_plugins.push_back(plugin);
}

void View::unlinkPlugin(Plugin* plugin)
{
	l_plugins.removeOne(plugin);
}

227
228
229
230
/*********************************************************
 * MANAGE LINKED MAPS
 *********************************************************/

231
void View::linkMap(MapHandlerGen* map)
Pierre Kraemer's avatar
Pierre Kraemer committed
232
233
{
	if(map && !l_maps.contains(map))
234
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
235
		l_maps.push_back(map);
236
237
238
239

		// TODO : update view global BB
		setSceneBoundingBox(map->getBBmin(), map->getBBmax());
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
240
241
}

242
void View::unlinkMap(MapHandlerGen* map)
Pierre Kraemer's avatar
Pierre Kraemer committed
243
244
{
	l_maps.removeOne(map);
245
246

	// TODO : update view global BB
Pierre Kraemer's avatar
Pierre Kraemer committed
247
248
}

249
250
251



252
253
void View::updateTextInfo()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
254
	m_textInfo =
255
256
		QString("view: ") + m_name +
		QString(" / camera: ") + m_currentCamera->getName();
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
}

glm::mat4 View::getCurrentModelViewMatrix()
{
	GLdouble gl_mvm[16];
	this->camera()->getModelViewMatrix(gl_mvm);
	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;
}

glm::mat4 View::getCurrentProjectionMatrix()
{
	GLdouble gl_pm[16];
	this->camera()->getProjectionMatrix(gl_pm);
	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;
}

glm::mat4 View::getCurrentModelViewProjectionMatrix()
{
	GLdouble gl_mvpm[16];
	this->camera()->getModelViewProjectionMatrix(gl_mvpm);
	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
298
void View::setCurrentModelViewMatrix(const glm::mat4& mvm)
299
300
301
302
303
304
305
306
307
308
{
	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];
	}
	this->camera()->setFromModelViewMatrix(gl_mvm);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
309
void View::setCurrentProjectionMatrix(const glm::mat4& pm)
310
311
312
313
314
315
316
317
318
319
{
	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];
	}
	this->camera()->setFromProjectionMatrix(gl_pm);
}

320
void View::cb_cameraView(int x, int y, int globalX, int globalY)
321
{
322
	m_cameraViewDialog->move(globalX, globalY);
323
324
325
	m_cameraViewDialog->show();
}

326
void View::cb_pluginsView(int x, int y, int globalX, int globalY)
327
{
328
	m_pluginsViewDialog->move(globalX, globalY);
329
330
331
	m_pluginsViewDialog->show();
}

332
void View::cb_mapsView(int x, int y, int globalX, int globalY)
333
{
334
	m_mapsViewDialog->move(globalX, globalY);
335
336
337
	m_mapsViewDialog->show();
}

338
void View::cb_closeView(int x, int y, int globalX, int globalY)
Pierre Kraemer's avatar
Pierre Kraemer committed
339
{
340
	m_window->removeView(m_name);
Pierre Kraemer's avatar
Pierre Kraemer committed
341
}