view.cpp 5.87 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
9
10
11
12
#include "cameraViewDialog.h"

#include <QKeyEvent>
#include <QMouseEvent>
#include <QWheelEvent>
13

14
15
unsigned int View::viewCount = 0;

16
View::View(const QString& name, Window* w, QWidget* parent, const QGLWidget* shareWidget) :
Pierre Kraemer's avatar
Pierre Kraemer committed
17
//	QGLViewer(new Context(NULL, QGLFormat(QGL::Rgba | QGL::DoubleBuffer | QGL::DepthBuffer))),
18
	QGLViewer(parent, shareWidget),
19
	m_name(name),
Pierre Kraemer's avatar
Pierre Kraemer committed
20
	m_window(w),
Pierre Kraemer's avatar
Pierre Kraemer committed
21
	m_currentCamera(NULL),
Pierre Kraemer's avatar
Pierre Kraemer committed
22
23
24
25
	m_cameraButton(NULL),
	m_pluginsButton(NULL),
	m_mapsButton(NULL),
	m_closeButton(NULL)
Pierre Kraemer's avatar
Pierre Kraemer committed
26
{
27
28
	++viewCount;

29
30
31
	Camera* c = m_window->addCamera();
	setCurrentCamera(c);

Pierre Kraemer's avatar
Pierre Kraemer committed
32
33
	m_buttonArea = new ViewButtonArea(this);
	m_buttonArea->setTopRightPosition(this->width(), 0);
34
35
36
37
38
39

	updateTextInfo();
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
42
43
44
45
46
47
48
	foreach(Plugin* plugin, l_plugins)
		unlinkPlugin(plugin);

	foreach(MapHandler* map, l_maps)
		unlinkMap(map);

	delete m_buttonArea;
Pierre Kraemer's avatar
Pierre Kraemer committed
49
50
51
52
}

void View::initGL()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
53
54
55
56
57
58
59
60
	m_cameraButton = new ViewButton(":icons/icons/camera_32.png", this);
	connect(m_cameraButton, SIGNAL(clicked()), this, SLOT(cb_cameraView()));

	m_pluginsButton = new ViewButton(":icons/icons/plugins_32.png", this);

	m_mapsButton = new ViewButton(":icons/icons/maps_32.png", this);

	m_closeButton = new ViewButton(":icons/icons/close_32.png", this);
61
62
63
64
65
66
67
68
69
}

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

void View::draw()
{
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	const float nbSteps = 200.0;
	glBegin(GL_QUAD_STRIP);
	for (float i = 0; i < nbSteps; ++i)
	{
		float ratio = i/nbSteps;
		float angle = 21.0*ratio;
		float c = cos(angle);
		float s = sin(angle);
		float r1 = 1.0 - 0.8f*ratio;
		float r2 = 0.8f - 0.8f*ratio;
		float alt = ratio - 0.5f;
		const float nor = 0.5f;
		const float up = sqrt(1.0-nor*nor);
		glColor3f(1.0-ratio, 0.2f , ratio);
		glNormal3f(nor*c, up, nor*s);
		glVertex3f(r1*c, alt, r1*s);
		glVertex3f(r2*c, alt+0.05f, r2*s);
	}
	glEnd();

90
	glPushAttrib(GL_ALL_ATTRIB_BITS);
Pierre Kraemer's avatar
Pierre Kraemer committed
91
//	drawButtons();
Pierre Kraemer's avatar
Pierre Kraemer committed
92
	drawText();
93
94
95
96
97
98
	glPopAttrib();
}

void View::drawText()
{
	glColor3f(1.0f, 1.0f, 1.0f);
Pierre Kraemer's avatar
Pierre Kraemer committed
99
100
101
	QGLViewer::drawText(10, 20, m_textInfo);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
102
103
104
105
106
107
108
109
110
void View::drawButtons()
{
	glColor3f(1.0f,1.0f,1.0f);
	glEnable(GL_TEXTURE_2D);
	startScreenCoordinatesSystem();
	m_buttonArea->draw();
	stopScreenCoordinatesSystem();
	glDisable(GL_TEXTURE_2D);
}
111
112
113

void View::keyPressEvent(QKeyEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
114
115
	foreach(Plugin* plugin, l_plugins)
		plugin->cb_keyPress(this, event->key());
Pierre Kraemer's avatar
Pierre Kraemer committed
116
	QGLViewer::keyPressEvent(event);
117
118
}

Pierre Kraemer's avatar
Pierre Kraemer committed
119
void View::keyReleaseEvent(QKeyEvent *event)
120
{
Pierre Kraemer's avatar
Pierre Kraemer committed
121
122
123
	foreach(Plugin* plugin, l_plugins)
		plugin->cb_keyRelease(this, event->key());
	QGLViewer::keyReleaseEvent(event);
124
125
126
127
}

void View::mousePressEvent(QMouseEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
128
129
130
131
132
133
	if(m_buttonArea->isClicked(event->x(), event->y()))
		m_buttonArea->clickButton(event->x(), event->y());
	else
	{
		foreach(Plugin* plugin, l_plugins)
			plugin->cb_mousePress(this, event->button(), event->pos().x(), event->pos().y());
134
		QGLViewer::mousePressEvent(event);
Pierre Kraemer's avatar
Pierre Kraemer committed
135
	}
136
137
138
139
}

void View::mouseReleaseEvent(QMouseEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
140
141
	foreach(Plugin* plugin, l_plugins)
		plugin->cb_mouseRelease(this, event->button(), event->pos().x(), event->pos().y());
Pierre Kraemer's avatar
Pierre Kraemer committed
142
	QGLViewer::mouseReleaseEvent(event);
143
144
145
146
}

void View::mouseMoveEvent(QMouseEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
147
148
	foreach(Plugin* plugin, l_plugins)
		plugin->cb_mouseMove(this, event->button(), event->pos().x(), event->pos().y());
Pierre Kraemer's avatar
Pierre Kraemer committed
149
	QGLViewer::mouseMoveEvent(event);
150
151
152
153
}

void View::wheelEvent(QWheelEvent* event)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
154
155
	foreach(Plugin* plugin, l_plugins)
		plugin->cb_mouseMove(this, event->delta(), event->pos().x(), event->pos().y());
Pierre Kraemer's avatar
Pierre Kraemer committed
156
	QGLViewer::wheelEvent(event);
157
158
159
160
161
}

void View::resizeGL(int width, int height)
{
	QGLViewer::resizeGL(width, height);
Pierre Kraemer's avatar
Pierre Kraemer committed
162
163
	if(m_buttonArea)
		m_buttonArea->setTopRightPosition(width, 0);
164
165
166
167
168
169
170
171
172
}

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

Pierre Kraemer's avatar
Pierre Kraemer committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
void View::setCurrentCamera(Camera* c)
{
	if(c != m_currentCamera)
	{
		if(m_currentCamera != NULL)
			m_currentCamera->unlinkView(this);
		m_currentCamera = c;
		m_currentCamera->linkView(this);
		this->setCamera(m_currentCamera);
		updateTextInfo();
		updateGL();
	}
}

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

void View::linkMap(MapHandler* map)
{
	if(map && !l_maps.contains(map))
		l_maps.push_back(map);
}

void View::unlinkMap(MapHandler* map)
{
	l_maps.removeOne(map);
}

209
210
void View::updateTextInfo()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
211
	m_textInfo =
212
213
		QString("view: ") + m_name +
		QString(" / camera: ") + m_currentCamera->getName();
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
}

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
255
void View::setCurrentModelViewMatrix(const glm::mat4& mvm)
256
257
258
259
260
261
262
263
264
265
{
	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
266
void View::setCurrentProjectionMatrix(const glm::mat4& pm)
267
268
269
270
271
272
273
274
275
276
{
	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
277
278
279
280
281
void View::cb_cameraView()
{
	CameraViewDialog cvd(m_window, this);
	cvd.exec();
}