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

#include "system.h"
4
5
#include "window.h"
#include "plugin.h"
6
#include "scene.h"
7
#include "camera.h"
8
9
10
#include "cameraViewDialog.h"
#include "cameraSceneDialog.h"

11
12
13
unsigned int View::viewCount = 0;

View::View(const QString& name, Window* w, QWidget* parent) :
Pierre Kraemer's avatar
Pierre Kraemer committed
14
//	QGLViewer(new Context(NULL, QGLFormat(QGL::Rgba | QGL::DoubleBuffer | QGL::DepthBuffer))),
15
	QGLViewer(w->getContext(), parent),
16
	m_name(name),
Pierre Kraemer's avatar
Pierre Kraemer committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
	m_window(w),
	m_context(w->getContext()),
	b_drawText(true)
//	m_linkButton(NULL),
//	m_linkViewEnabled(false),
//	m_unlinkButton(NULL),
//	m_unlinkViewEnabled(false),
//	m_cameraButton(NULL),
//	m_cameraEnabled(false),
//	m_cameraSceneButton(NULL),
//	m_cameraSceneEnabled(false),
//	m_closeViewButton(NULL),
//	m_closeViewEnabled(false),
//	b_showButtons(true)
{
32
33
	++viewCount;

Pierre Kraemer's avatar
Pierre Kraemer committed
34
35
36
37
38
//	((Context*)(this->context()))->setDevice(this);
//	((Context*)(this->context()))->create(context);

//	makeCurrent();
//	glewInit();
39

40
41
42
43
44
45
46
47
48
49
50
51
52
53
//	qglviewer::Camera* defaultCamera = this->camera();
//
//	if(c)
//	{
//		m_currentCamera = c;
//		this->setCamera(c);
//		c->linkView(this);
//	}
//	else
//	{
//		m_currentCamera = new Camera(m_window, this, *defaultCamera);
//	}
//
//	delete defaultCamera;
54

Pierre Kraemer's avatar
Pierre Kraemer committed
55
56
//	m_buttonArea = new ViewButtonArea(this);
//	m_buttonArea->setTopRightPosition(this->width(), 0);
57
58
59
60
61
62

	updateTextInfo();
}

View::~View()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
63
64
65
66
67
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
	m_scene->unlinkView(this);
	m_currentCamera->unlinkView(this);

//	if(m_buttonArea)
//		delete m_buttonArea;
}

void View::initGL()
{
	if(m_scene)
		m_scene->initGL();

//	m_linkButton = new ViewButton(":icons/icons/link.png", this);
//	if(m_linkViewEnabled)
//	{
//		m_linkViewEnabled = false;
//		enableLinking(true);
//	}
//
//	m_cameraButton = new ViewButton(":icons/icons/camera.png", this);
//	if(m_cameraEnabled)
//	{
//		m_cameraEnabled = false;
//		enableCameraGesture(true);
//	}
//
//	m_unlinkButton = new ViewButton(":icons/icons/broken_link.png", this);
//	if(m_unlinkViewEnabled)
//	{
//		m_unlinkViewEnabled = false;
//		enableUnlinking(true);
//	}
//
//	m_cameraSceneButton = new ViewButton(":icons/icons/separate_camera.png", this);
//	if(m_cameraSceneEnabled)
//	{
//		m_cameraSceneEnabled = false;
//		enableSceneCameraGesture(true);
//	}
//
//	m_closeViewButton = new ViewButton(":icons/icons/close.png", this);
//	if(m_closeViewEnabled)
//	{
//		m_closeViewEnabled = false;
//		enableViewClose(true);
//	}
109
110
111
112
113
114
115
116
117
118
119
120
121
}

void View::updateGL()
{
	QGLViewer::updateGL();
}

void View::draw()
{
	if(m_scene && m_currentCamera)
		m_scene->draw(this);

	glPushAttrib(GL_ALL_ATTRIB_BITS);
Pierre Kraemer's avatar
Pierre Kraemer committed
122
123
124
125
//	if(b_drawButtons)
//		drawButtons();
	if(b_drawText)
		drawText();
126
127
128
129
130
131
	glPopAttrib();
}

void View::drawText()
{
	glColor3f(1.0f, 1.0f, 1.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
	QGLViewer::drawText(10, 20, m_textInfo);
}

//void View::drawButtons()
//{
//	glColor3f(1.0f,1.0f,1.0f);
//	glEnable(GL_TEXTURE_2D);
//	if(b_showButtons)
//	{
//		startScreenCoordinatesSystem();
//		m_buttonArea->draw();
//		stopScreenCoordinatesSystem();
//	}
//	glDisable(GL_TEXTURE_2D);
//}
147
148
149

void View::keyPressEvent(QKeyEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
150
151
	m_scene->keyPressEvent(event);
	QGLViewer::keyPressEvent(event);
152
153
154
155
}

void View::keyReleaseEvent(QKeyEvent *e)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
156
157
	m_scene->keyReleaseEvent(e);
	QGLViewer::keyReleaseEvent(e);
158
159
160
161
}

void View::mousePressEvent(QMouseEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
162
163
164
165
166
//	if(m_buttonArea->isClicked(event->x(), event->y()))
//		m_buttonArea->clickButton(event->x(), event->y());
//	else
//	{
		m_scene->mousePressEvent(event);
167
		QGLViewer::mousePressEvent(event);
Pierre Kraemer's avatar
Pierre Kraemer committed
168
//	}
169
170
171
172
}

void View::mouseReleaseEvent(QMouseEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
173
174
	m_scene->mouseReleaseEvent(event);
	QGLViewer::mouseReleaseEvent(event);
175
176
177
178
}

void View::mouseMoveEvent(QMouseEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
179
180
	m_scene->mouseMoveEvent(event);
	QGLViewer::mouseMoveEvent(event);
181
182
183
184
}

void View::wheelEvent(QWheelEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
185
186
	m_scene->wheelEvent(event);
	QGLViewer::wheelEvent(event);
187
188
189
190
191
}

void View::resizeGL(int width, int height)
{
	QGLViewer::resizeGL(width, height);
Pierre Kraemer's avatar
Pierre Kraemer committed
192
193
//	if(m_buttonArea)
//		m_buttonArea->setTopRightPosition(width, 0);
194
195
196
197
198
199
200
201
202
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
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
//void View::enableLinking(bool b)
//{
//	if(m_linkButton)
//	{
//		if(b && !m_linkViewEnabled)
//		{
//			m_buttonArea->addButton(m_linkButton);
//			connect(m_linkButton, SIGNAL(clicked()), this, SLOT(linkView()));
//		}
//		else if(!b && m_linkViewEnabled)
//		{
//			disconnect(m_linkButton, SIGNAL(clicked()), this, SLOT(linkView()));
//			m_buttonArea->removeButton(m_linkButton);
//		}
//	}
//	m_linkViewEnabled = b;
//}
//
//void View::enableUnlinking(bool b)
//{
//	if(m_unlinkButton)
//	{
//		if(b && !m_unlinkViewEnabled)
//		{
//			m_buttonArea->addButton(m_unlinkButton);
//			connect(m_unlinkButton, SIGNAL(clicked()), this, SLOT(unlinkView()));
//		}
//		else if(!b && m_unlinkViewEnabled)
//		{
//			disconnect(m_unlinkButton, SIGNAL(clicked()), this, SLOT(unlinkView()));
//			m_buttonArea->removeButton(m_unlinkButton);
//		}
//	}
//	m_unlinkViewEnabled = b;
//}
//
//void View::enableCameraGesture(bool b)
//{
//	if(m_cameraButton)
//	{
//		if(b && !m_cameraEnabled)
//		{
//			m_buttonArea->addButton(m_cameraButton);
//			connect(m_cameraButton, SIGNAL(clicked()), this, SLOT(cameraGesture()));
//		}
//		else if(!b && m_cameraEnabled)
//		{
//			disconnect(m_cameraButton, SIGNAL(clicked()), this, SLOT(cameraGesture()));
//			m_buttonArea->removeButton(m_cameraButton);
//		}
//	}
//	m_cameraEnabled = b;
//}
//
//void View::enableSceneCameraGesture(bool b)
//{
//	if(m_cameraSceneButton)
//	{
//		if(b && !m_cameraSceneEnabled)
//		{
//			m_buttonArea->addButton(m_cameraSceneButton);
//			connect(m_cameraSceneButton, SIGNAL(clicked()), this, SLOT(cameraSceneGesture()));
//		}
//		else if(!b && m_cameraSceneEnabled)
//		{
//			disconnect(m_cameraSceneButton, SIGNAL(clicked()), this, SLOT(cameraSceneGesture()));
//			m_buttonArea->removeButton(m_cameraSceneButton);
//		}
//	}
//	m_cameraSceneEnabled = b;
//}
//
//void View::enableViewClose(bool b)
//{
//	if(m_closeViewButton)
//	{
//		if(b && !m_closeViewEnabled)
//		{
//			m_buttonArea->addButton(m_closeViewButton);
//			connect(m_closeViewButton, SIGNAL(clicked()), this, SLOT(closeView()));
//		}
//		else if(!b && m_closeViewEnabled)
//		{
//			disconnect(m_closeViewButton, SIGNAL(clicked()), this, SLOT(closeView()));
//			m_buttonArea->removeButton(m_closeViewButton);
//		}
//	}
//	m_closeViewEnabled = b;
//}
292
293
294
295

void View::updateTextInfo()
{
	m_textInfo = QString("Scene: ") + m_scene->getName() + " > "
Pierre Kraemer's avatar
Pierre Kraemer committed
296
297
			   + QString("View: ") + this->m_name + " > "
			   + QString("Camera: ") + m_currentCamera->getName() + (m_currentCamera->isShared() ? " (shared)" : "");
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
327
328
329
330
331
332
333
334
335
336
337
338
}

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
339
void View::setCurrentModelViewMatrix(const glm::mat4& mvm)
340
341
342
343
344
345
346
347
348
349
{
	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
350
void View::setCurrentProjectionMatrix(const glm::mat4& pm)
351
352
353
354
355
356
357
358
359
360
{
	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);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
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
//void View::linkView()
//{
//	m_scene->linkWithPlugin();
//}
//
//void View::unlinkView()
//{
//	m_scene->unlinkPlugin();
//}
//
//void View::cameraGesture()
//{
//	CameraViewDialog cvd(this, this);
//	cvd.exec();
//	updateTextInfo();
//	enableSceneCameraGesture(l_camera.size() > 1 || m_scene->countViews() > 1);
//}
//
//void View::cameraSceneGesture()
//{
//	CameraSceneDialog csd(m_scene);
//	csd.exec();
//	updateTextInfo();
//}
//
//void View::closeView()
//{
//	b_destroyView = true;
//}

//void View::addViewButton(ViewButton* viewButton)
//{
//	m_buttonArea->addButton(viewButton);
//}
//
//void View::removeViewButton(ViewButton* viewButton)
//{
//	m_buttonArea->takeButton(viewButton);
//}