Coupure prévue mardi 3 Août au matin pour maintenance du serveur. Nous faisons au mieux pour que celle-ci soit la plus brève possible.

render.cpp 13.8 KB
Newer Older
1
2
3
4
5
6
#include "render.h"

#include "mapHandler.h"

#include "Algo/Import/import.h"

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

PerMapParameterSet::PerMapParameterSet(MapHandlerGen* map) :
	positionVBO(NULL),
	normalVBO(NULL),
	verticesScaleFactor(1.0f),
	renderVertices(false),
	renderEdges(false),
	renderFaces(true),
	faceStyle(FLAT)
{
	QList<Utils::VBO*> vbos = map->getVBOList();
	for(int i = 0; i < vbos.count(); ++i)
	{
		if(vbos[i]->name() == "position") // try to select a VBO named "position"
			positionVBO = vbos[i];
		if(vbos[i]->name() == "normal")	// try to select a VBO named "normal"
			normalVBO = vbos[i];
	}

	if(positionVBO == NULL && vbos.count() > 0)
		positionVBO = vbos[0];
	if(normalVBO == NULL && vbos.count() > 0)
		normalVBO = vbos[0];
}


33
34
bool RenderPlugin::enable()
{
35
	m_dockTab = new RenderDockTab(this);
36
37
38
39
40
41
42
	addTabInDock(m_dockTab, "Render");

	m_flatShader = new CGoGN::Utils::ShaderFlat();
	m_flatShader->setAmbiant(CGoGN::Geom::Vec4f(0.2f, 0.2f, 0.2f, 0.1f));
	m_flatShader->setDiffuse(CGoGN::Geom::Vec4f(0.8f, 0.9f, 0.7f, 1.0f));
	m_flatShader->setExplode(1.0f);

Pierre Kraemer's avatar
Pierre Kraemer committed
43
44
45
46
47
48
	m_phongShader = new CGoGN::Utils::ShaderPhong() ;
	m_phongShader->setAmbiant(CGoGN::Geom::Vec4f(0.2f, 0.2f, 0.2f, 0.1f)) ;
	m_phongShader->setDiffuse(CGoGN::Geom::Vec4f(0.8f, 0.9f, 0.7f, 1.0f)) ;
	m_phongShader->setSpecular(CGoGN::Geom::Vec4f(0.9f, 0.9f, 0.9f, 1.0f)) ;
	m_phongShader->setShininess(80.0f) ;

49
50
51
52
53
54
	m_simpleColorShader = new CGoGN::Utils::ShaderSimpleColor();
	CGoGN::Geom::Vec4f c(0.1f, 0.1f, 0.1f, 1.0f);
	m_simpleColorShader->setColor(c);

	m_pointSprite = new CGoGN::Utils::PointSprite();

55
56
57
58
59
	registerShader(m_flatShader);
	registerShader(m_phongShader);
	registerShader(m_simpleColorShader);
	registerShader(m_pointSprite);

60
61
62
	connect(m_dockTab->mapList, SIGNAL(itemSelectionChanged()), this, SLOT(cb_selectedMapChanged()));
	connect(m_dockTab->combo_positionVBO, SIGNAL(currentIndexChanged(int)), this, SLOT(cb_positionVBOChanged(int)));
	connect(m_dockTab->combo_normalVBO, SIGNAL(currentIndexChanged(int)), this, SLOT(cb_normalVBOChanged(int)));
63
64
65
66
	connect(m_dockTab->check_renderVertices, SIGNAL(toggled(bool)), this, SLOT(cb_renderVerticesChanged(bool)));
	connect(m_dockTab->slider_verticesScaleFactor, SIGNAL(valueChanged(int)), this, SLOT(cb_verticesScaleFactorChanged(int)));
	connect(m_dockTab->check_renderEdges, SIGNAL(toggled(bool)), this, SLOT(cb_renderEdgesChanged(bool)));
	connect(m_dockTab->check_renderFaces, SIGNAL(toggled(bool)), this, SLOT(cb_renderFacesChanged(bool)));
Pierre Kraemer's avatar
Pierre Kraemer committed
67
	connect(m_dockTab->group_faceShading, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(cb_faceStyleChanged(QAbstractButton*)));
68

69
70
71
72
	connect(m_window, SIGNAL(viewAndPluginLinked(View*, Plugin*)), this, SLOT(viewLinked(View*, Plugin*)));
	connect(m_window, SIGNAL(viewAndPluginUnlinked(View*, Plugin*)), this, SLOT(viewUnlinked(View*, Plugin*)));
	connect(m_window, SIGNAL(currentViewChanged(View*)), this, SLOT(currentViewChanged(View*)));

73
74
75
76
77
78
	return true;
}

void RenderPlugin::disable()
{
	delete m_flatShader;
Pierre Kraemer's avatar
Pierre Kraemer committed
79
	delete m_phongShader;
80
81
82
83
84
85
	delete m_simpleColorShader;
	delete m_pointSprite;
}

void RenderPlugin::redraw(View* view)
{
86
	ParameterSet* params = h_viewParams[view];
87
88
89
90

	const QList<MapHandlerGen*>& maps = view->getLinkedMaps();
	foreach(MapHandlerGen* m, maps)
	{
91
92
		const PerMapParameterSet& p = params->perMap[m->getName()];
		if(p.positionVBO != NULL)
93
		{
94
95
96
97
98
99
100
101
102
			if(p.renderVertices)
			{
				m_pointSprite->setSize(m->getBBdiagSize() / 200.0f * p.verticesScaleFactor);
				m_pointSprite->setAttributePosition(p.positionVBO);
				m_pointSprite->predraw(CGoGN::Geom::Vec3f(0.0f, 0.0f, 1.0f));
				m->draw(m_pointSprite, CGoGN::Algo::Render::GL2::POINTS);
				m_pointSprite->postdraw();
			}
			if(p.renderEdges)
Pierre Kraemer's avatar
Pierre Kraemer committed
103
			{
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
				glLineWidth(1.0f);
				m_simpleColorShader->setAttributePosition(p.positionVBO);
				m->draw(m_simpleColorShader, CGoGN::Algo::Render::GL2::LINES);
			}
			if(p.renderFaces)
			{
				glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
				glEnable(GL_LIGHTING);
				glEnable(GL_POLYGON_OFFSET_FILL);
				glPolygonOffset(1.0f, 1.0f);
				switch(p.faceStyle)
				{
					case FLAT :
						m_flatShader->setAttributePosition(p.positionVBO);
						m->draw(m_flatShader, CGoGN::Algo::Render::GL2::TRIANGLES);
						break ;
					case PHONG :
						if(p.normalVBO != NULL)
						{
							m_phongShader->setAttributePosition(p.positionVBO) ;
							m_phongShader->setAttributeNormal(p.normalVBO) ;
							m->draw(m_phongShader, CGoGN::Algo::Render::GL2::TRIANGLES);
						}
						break ;
				}
				glDisable(GL_POLYGON_OFFSET_FILL);
Pierre Kraemer's avatar
Pierre Kraemer committed
130
			}
131
132
133
134
		}
	}
}

135
void RenderPlugin::viewLinked(View* view, Plugin* plugin)
136
{
137
	if(plugin == this)
138
	{
139
140
141
142
143
144
145
146
147
148
		ParameterSet* params = new ParameterSet();
		h_viewParams.insert(view, params);
		const QList<MapHandlerGen*>& maps = view->getLinkedMaps();
		foreach(MapHandlerGen* map, maps)
		{
			PerMapParameterSet p(map);
			params->perMap.insert(map->getName(), p);
		}
		if (!maps.empty())
			changeSelectedMap(view, maps[0]);
149

150
151
		connect(view, SIGNAL(mapLinked(MapHandlerGen*)), this, SLOT(mapLinked(MapHandlerGen*)));
		connect(view, SIGNAL(mapUnlinked(MapHandlerGen*)), this, SLOT(mapUnlinked(MapHandlerGen*)));
152

153
154
155
		if(view->isCurrentView())
			m_dockTab->refreshUI(params);
	}
156
157
}

158
void RenderPlugin::viewUnlinked(View* view, Plugin* plugin)
159
{
160
161
162
	if(plugin == this)
	{
		h_viewParams.remove(view);
163

164
165
166
		disconnect(view, SIGNAL(mapLinked(MapHandlerGen*)), this, SLOT(mapLinked(MapHandlerGen*)));
		disconnect(view, SIGNAL(mapUnlinked(MapHandlerGen*)), this, SLOT(mapUnlinked(MapHandlerGen*)));
	}
167
168
169
170
}

void RenderPlugin::currentViewChanged(View* view)
{
171
172
	if(isLinkedToView(view))
		m_dockTab->refreshUI(h_viewParams[view]);
173
174
}

175
void RenderPlugin::mapLinked(MapHandlerGen* m)
176
{
177
	View* view = static_cast<View*>(QObject::sender());
178
	assert(isLinkedToView(view));
179

180
	ParameterSet* params = h_viewParams[view];
181
	PerMapParameterSet p(m);
182
	params->perMap.insert(m->getName(), p);
183
184
185
186
	if(params->selectedMap == NULL || params->perMap.count() == 1)
		changeSelectedMap(view, m);
	else
		m_dockTab->refreshUI(params);
187
188
}

189
void RenderPlugin::mapUnlinked(MapHandlerGen* m)
190
{
191
	View* view = static_cast<View*>(QObject::sender());
192
	assert(isLinkedToView(view));
193

194
195
	ParameterSet* params = h_viewParams[view];
	params->perMap.remove(m->getName());
196

197
198
	if(params->selectedMap == m)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
199
		if(!params->perMap.empty())
200
			changeSelectedMap(view, m_window->getMap(params->perMap.begin().key()));
Pierre Kraemer's avatar
Pierre Kraemer committed
201
		else
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
			changeSelectedMap(view, NULL);
	}
	else
		m_dockTab->refreshUI(params);
}

void RenderPlugin::vboAdded(Utils::VBO* vbo)
{
	m_dockTab->refreshUI(h_viewParams[m_window->getCurrentView()]);
}

void RenderPlugin::vboRemoved(Utils::VBO* vbo)
{
	MapHandlerGen* map = static_cast<MapHandlerGen*>(QObject::sender());

	View* view = m_window->getCurrentView();
	ParameterSet* params = h_viewParams[view];
	if(params->perMap[map->getName()].positionVBO == vbo)
		changePositionVBO(view, map, NULL);
	if(params->perMap[map->getName()].normalVBO == vbo)
		changeNormalVBO(view, map, NULL);
223
224

	m_dockTab->refreshUI(h_viewParams[view]);
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
292
293
294
295
296
297
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
}

void RenderPlugin::changeSelectedMap(View* view, MapHandlerGen* map)
{
	ParameterSet* params = h_viewParams[view];

	MapHandlerGen* prev = params->selectedMap;
	params->selectedMap = map;

	if(view->isCurrentView())
	{
		if(prev)
			disconnect(map, SIGNAL(vboAdded(Utils::VBO*)), this, SLOT(vboAdded(Utils::VBO*)));
		if(map)
			connect(map, SIGNAL(vboAdded(Utils::VBO*)), this, SLOT(vboAdded(Utils::VBO*)));

		m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

void RenderPlugin::changePositionVBO(View* view, MapHandlerGen* map, Utils::VBO* vbo)
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].positionVBO = vbo;

	if(view->isCurrentView())
	{
		m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

void RenderPlugin::changeNormalVBO(View* view, MapHandlerGen* map, Utils::VBO* vbo)
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].normalVBO = vbo;

	if(view->isCurrentView())
	{
		m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

void RenderPlugin::changeRenderVertices(View* view, MapHandlerGen* map, bool b)
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].renderVertices = b;

	if(view->isCurrentView())
	{
		m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

void RenderPlugin::changeVerticesScaleFactor(View* view, MapHandlerGen* map, int i)
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].verticesScaleFactor = i / 50.0;

	if(view->isCurrentView())
	{
		m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

void RenderPlugin::changeRenderEdges(View* view, MapHandlerGen* map, bool b)
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].renderEdges = b;

	if(view->isCurrentView())
	{
		m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

void RenderPlugin::changeRenderFaces(View* view, MapHandlerGen* map, bool b)
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].renderFaces = b;

	if(view->isCurrentView())
	{
		m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

void RenderPlugin::changeFacesStyle(View* view, MapHandlerGen* map, FaceShadingStyle style)
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].faceStyle = style;

	if(view->isCurrentView())
	{
325
		m_dockTab->refreshUI(params);
326
		view->updateGL();
327
	}
328
329
}

330
void RenderPlugin::cb_selectedMapChanged()
331
{
332
333
334
335
	if(!b_refreshingUI)
	{
		QList<QListWidgetItem*> currentItems = m_dockTab->mapList->selectedItems();
		if(!currentItems.empty())
336
			changeSelectedMap(m_window->getCurrentView(), m_window->getMap(currentItems[0]->text()));
337
338
	}
}
339

340
341
342
343
void RenderPlugin::cb_positionVBOChanged(int index)
{
	if(!b_refreshingUI)
	{
344
345
346
		View* view = m_window->getCurrentView();
		MapHandlerGen* map = h_viewParams[view]->selectedMap;
		changePositionVBO(view, map, map->getVBO(m_dockTab->combo_positionVBO->currentText()));
347
	}
348
349
}

350
351
352
353
void RenderPlugin::cb_normalVBOChanged(int index)
{
	if(!b_refreshingUI)
	{
354
355
356
		View* view = m_window->getCurrentView();
		MapHandlerGen* map = h_viewParams[view]->selectedMap;
		changeNormalVBO(view, map, map->getVBO(m_dockTab->combo_normalVBO->currentText()));
357
358
359
360
361
362
363
	}
}

void RenderPlugin::cb_renderVerticesChanged(bool b)
{
	if(!b_refreshingUI)
	{
364
365
366
		View* view = m_window->getCurrentView();
		MapHandlerGen* map = h_viewParams[view]->selectedMap;
		changeRenderVertices(view, map, b);
367
	}
368
369
}

370
void RenderPlugin::cb_verticesScaleFactorChanged(int i)
Pierre Kraemer's avatar
Pierre Kraemer committed
371
{
372
373
	if(!b_refreshingUI)
	{
374
375
376
		View* view = m_window->getCurrentView();
		MapHandlerGen* map = h_viewParams[view]->selectedMap;
		changeVerticesScaleFactor(view, map, i);
377
378
	}
}
Pierre Kraemer's avatar
Pierre Kraemer committed
379

380
381
382
383
void RenderPlugin::cb_renderEdgesChanged(bool b)
{
	if(!b_refreshingUI)
	{
384
385
386
		View* view = m_window->getCurrentView();
		MapHandlerGen* map = h_viewParams[view]->selectedMap;
		changeRenderEdges(view, map, b);
387
388
389
390
391
392
393
	}
}

void RenderPlugin::cb_renderFacesChanged(bool b)
{
	if(!b_refreshingUI)
	{
394
395
396
		View* view = m_window->getCurrentView();
		MapHandlerGen* map = h_viewParams[view]->selectedMap;
		changeRenderFaces(view, map, b);
397
398
399
400
401
402
403
	}
}

void RenderPlugin::cb_faceStyleChanged(QAbstractButton* b)
{
	if(!b_refreshingUI)
	{
404
405
		View* view = m_window->getCurrentView();
		MapHandlerGen* map = h_viewParams[view]->selectedMap;
406
		if(m_dockTab->radio_flatShading->isChecked())
407
			changeFacesStyle(view, map, FLAT);
408
		else if(m_dockTab->radio_phongShading->isChecked())
409
			changeFacesStyle(view, map, PHONG);
410
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
411
412
}

413
414
415
416


void RenderDockTab::refreshUI(ParameterSet* params)
{
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
	plugin->setRefreshingUI(true);

	mapList->clear();
	combo_positionVBO->clear();
	combo_normalVBO->clear();

	MapHandlerGen* map = params->selectedMap;

	QHash<QString, PerMapParameterSet>::const_iterator i = params->perMap.constBegin();
	while (i != params->perMap.constEnd())
	{
		mapList->addItem(i.key());
		if(map != NULL && i.key() == map->getName())
		{
			QList<QListWidgetItem*> item = mapList->findItems(map->getName(), Qt::MatchExactly);
			item[0]->setSelected(true);

			PerMapParameterSet& p = params->perMap[map->getName()];

			QList<Utils::VBO*> vbos = map->getVBOList();
437
			unsigned int j = 0;
438
439
			for(int i = 0; i < vbos.count(); ++i)
			{
440
441
442
443
444
445
446
447
448
449
				if(vbos[i]->dataSize() == 3)
				{
					combo_positionVBO->addItem(QString::fromStdString(vbos[i]->name()));
					if(p.positionVBO == NULL)
					{										// if nothing is specified in the parameter set
						if(vbos[i]->name() == "position")	// try to select a VBO named "position"
						{
							p.positionVBO = vbos[i];
							combo_positionVBO->setCurrentIndex(j);
						}
450
					}
451
452
					else if(vbos[i] == p.positionVBO)
						combo_positionVBO->setCurrentIndex(j);
453

454
455
456
457
458
459
460
461
					combo_normalVBO->addItem(QString::fromStdString(vbos[i]->name()));
					if(p.normalVBO == NULL)
					{									// if nothing is specified in the parameter set
						if(vbos[i]->name() == "normal")	// try to select a VBO named "normal"
						{
							p.normalVBO = vbos[i];
							combo_normalVBO->setCurrentIndex(j);
						}
462
					}
463
464
465
466
					else if(vbos[i] == p.normalVBO)
						combo_normalVBO->setCurrentIndex(j);

					++j;
467
				}
468
			}
469

470
471
			if(p.positionVBO == NULL && vbos.count() > 0)
			{
472
473
474
475
476
477
478
479
480
481
482
483
				int i = 0;
				bool found = false;
				while(i < vbos.count() && !found)
				{
					if(vbos[i]->dataSize() == 3)
					{
						p.positionVBO = vbos[i];
						combo_positionVBO->setCurrentIndex(i);
						found = true;
					}
					++i;
				}
484
485
486
			}
			if(p.normalVBO == NULL && vbos.count() > 0)
			{
487
488
489
490
491
492
493
494
495
496
497
498
				int i = 0;
				bool found = false;
				while(i < vbos.count() && !found)
				{
					if(vbos[i]->dataSize() == 3)
					{
						p.normalVBO = vbos[i];
						combo_normalVBO->setCurrentIndex(i);
						found = true;
					}
					++i;
				}
499
500
501
502
503
504
505
506
507
508
509
510
511
			}

			check_renderVertices->setChecked(p.renderVertices);
			slider_verticesScaleFactor->setSliderPosition(p.verticesScaleFactor * 50.0);
			check_renderEdges->setChecked(p.renderEdges);
			check_renderFaces->setChecked(p.renderFaces);
			radio_flatShading->setChecked(p.faceStyle == FLAT);
			radio_phongShading->setChecked(p.faceStyle == PHONG);
		}
		++i;
	}

	plugin->setRefreshingUI(false);
512
513
514
515
}



516
517
518
519
520
#ifndef DEBUG
Q_EXPORT_PLUGIN2(RenderPlugin, RenderPlugin)
#else
Q_EXPORT_PLUGIN2(RenderPluginD, RenderPlugin)
#endif