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 8.96 KB
Newer Older
1
2
3
4
#include "render.h"

#include "mapHandler.h"

5
6
namespace CGoGN
{
7

8
9
namespace SCHNApps
{
10
11
12
13
14
15
16
17
18
19

PerMapParameterSet::PerMapParameterSet(MapHandlerGen* map) :
	positionVBO(NULL),
	normalVBO(NULL),
	verticesScaleFactor(1.0f),
	renderVertices(false),
	renderEdges(false),
	renderFaces(true),
	faceStyle(FLAT)
{
20
21
22
	bool positionFound = false;
	bool normalFound = false;

23
24
25
	QList<Utils::VBO*> vbos = map->getVBOList();
	for(int i = 0; i < vbos.count(); ++i)
	{
26
27
28
29
30
31
32
33
		if(vbos[i]->dataSize() == 3)
		{
			if(!positionFound) positionVBO = vbos[i];
			if(vbos[i]->name() == "position") // try to select a VBO named "position"
			{
				positionVBO = vbos[i];
				positionFound = true;
			}
34

35
36
37
38
39
40
41
42
			if(!normalFound) normalVBO = vbos[i];
			if(vbos[i]->name() == "normal")	// try to select a VBO named "normal"
			{
				normalVBO = vbos[i];
				normalFound = true;
			}
		}
	}
43
44
45
}


46
47
bool RenderPlugin::enable()
{
48
	m_dockTab = new RenderDockTab(m_window, this);
49
50
51
52
53
54
55
	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
56
57
58
59
60
61
	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) ;

62
63
64
65
66
67
	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();

68
69
70
71
72
	registerShader(m_flatShader);
	registerShader(m_phongShader);
	registerShader(m_simpleColorShader);
	registerShader(m_pointSprite);

73
74
75
76
	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*)));

77
78
79
80
81
82
	return true;
}

void RenderPlugin::disable()
{
	delete m_flatShader;
Pierre Kraemer's avatar
Pierre Kraemer committed
83
	delete m_phongShader;
84
85
86
87
88
89
	delete m_simpleColorShader;
	delete m_pointSprite;
}

void RenderPlugin::redraw(View* view)
{
90
	ParameterSet* params = h_viewParams[view];
91
92
93
94

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

139
void RenderPlugin::viewLinked(View* view, Plugin* plugin)
140
{
141
	if(plugin == this)
142
	{
143
144
		ParameterSet* params = new ParameterSet();
		h_viewParams.insert(view, params);
145

146
		const QList<MapHandlerGen*>& maps = view->getLinkedMaps();
147
148
		foreach(MapHandlerGen* mh, maps)
			addManagedMap(view, mh);
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
	if(plugin == this)
	{
162
163
164
165
166
167
		const QList<MapHandlerGen*>& maps = view->getLinkedMaps();
		foreach(MapHandlerGen* mh, maps)
			removeManagedMap(view, mh);

		ParameterSet* params = h_viewParams[view];
		delete params;
168
		h_viewParams.remove(view);
169

170
171
172
		disconnect(view, SIGNAL(mapLinked(MapHandlerGen*)), this, SLOT(mapLinked(MapHandlerGen*)));
		disconnect(view, SIGNAL(mapUnlinked(MapHandlerGen*)), this, SLOT(mapUnlinked(MapHandlerGen*)));
	}
173
174
175
176
}

void RenderPlugin::currentViewChanged(View* view)
{
177
178
	if(isLinkedToView(view))
		m_dockTab->refreshUI(h_viewParams[view]);
179
180
}

181
void RenderPlugin::mapLinked(MapHandlerGen* m)
182
{
183
	View* view = static_cast<View*>(QObject::sender());
184
	assert(isLinkedToView(view));
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
	addManagedMap(view, m);
}

void RenderPlugin::mapUnlinked(MapHandlerGen* m)
{
	View* view = static_cast<View*>(QObject::sender());
	assert(isLinkedToView(view));
	removeManagedMap(view, m);
}

void RenderPlugin::addManagedMap(View* v, MapHandlerGen *m)
{
//	connect(m, SIGNAL(attributeModified(unsigned int, QString)), this, SLOT(attributeModified(unsigned int, QString)));
//	connect(m, SIGNAL(connectivityModified()), this, SLOT(connectivityModified()));

	ParameterSet* params = h_viewParams[v];
	PerMapParameterSet* perMap = new PerMapParameterSet(m);

	params->perMap.insert(m->getName(), perMap);
204
205

	if(params->selectedMap == NULL || params->perMap.count() == 1)
206
		changeSelectedMap(v, m);
207
208
	else
		m_dockTab->refreshUI(params);
209
210
}

211
void RenderPlugin::removeManagedMap(View *v, MapHandlerGen *m)
212
{
213
214
//	disconnect(m, SIGNAL(attributeModified(unsigned int, QString)), this, SLOT(attributeModified(unsigned int, QString)));
//	disconnect(m, SIGNAL(connectivityModified()), this, SLOT(connectivityModified()));
215

216
217
218
219
	ParameterSet* params = h_viewParams[v];
	PerMapParameterSet* perMap = params->perMap[m->getName()];

	delete perMap;
220
	params->perMap.remove(m->getName());
221

222
223
	if(params->selectedMap == m)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
224
		if(!params->perMap.empty())
225
			changeSelectedMap(v, m_window->getMap(params->perMap.begin().key()));
Pierre Kraemer's avatar
Pierre Kraemer committed
226
		else
227
			changeSelectedMap(v, NULL);
228
229
230
231
232
	}
	else
		m_dockTab->refreshUI(params);
}

233
void RenderPlugin::changeSelectedMap(View* view, MapHandlerGen* map)
234
235
236
237
238
239
240
241
242
{
	ParameterSet* params = h_viewParams[view];

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

	if(view->isCurrentView())
	{
		if(prev)
243
			disconnect(prev, SIGNAL(vboAdded(Utils::VBO*)), m_dockTab, SLOT(addVBOToList(Utils::VBO*)));
244
		if(map)
245
			connect(map, SIGNAL(vboAdded(Utils::VBO*)), m_dockTab, SLOT(addVBOToList(Utils::VBO*)));
246

247
		m_dockTab->refreshUI(params);
248
249
250
	}
}

251
void RenderPlugin::changePositionVBO(View* view, MapHandlerGen* map, Utils::VBO* vbo, bool fromUI)
252
253
{
	ParameterSet* params = h_viewParams[view];
254
	params->perMap[map->getName()]->positionVBO = vbo;
255
256
257

	if(view->isCurrentView())
	{
258
259
		if(!fromUI)
			m_dockTab->refreshUI(params);
260
261
262
263
		view->updateGL();
	}
}

264
void RenderPlugin::changeNormalVBO(View* view, MapHandlerGen* map, Utils::VBO* vbo, bool fromUI)
265
266
{
	ParameterSet* params = h_viewParams[view];
267
	params->perMap[map->getName()]->normalVBO = vbo;
268
269
270

	if(view->isCurrentView())
	{
271
272
		if(!fromUI)
			m_dockTab->refreshUI(params);
273
274
275
276
		view->updateGL();
	}
}

277
void RenderPlugin::changeRenderVertices(View* view, MapHandlerGen* map, bool b, bool fromUI)
278
279
{
	ParameterSet* params = h_viewParams[view];
280
	params->perMap[map->getName()]->renderVertices = b;
281
282
283

	if(view->isCurrentView())
	{
284
285
		if(!fromUI)
			m_dockTab->refreshUI(params);
286
287
288
289
		view->updateGL();
	}
}

290
void RenderPlugin::changeVerticesScaleFactor(View* view, MapHandlerGen* map, float f, bool fromUI)
291
292
{
	ParameterSet* params = h_viewParams[view];
293
	params->perMap[map->getName()]->verticesScaleFactor = f;
294
295
296

	if(view->isCurrentView())
	{
297
298
		if(!fromUI)
			m_dockTab->refreshUI(params);
299
300
301
302
		view->updateGL();
	}
}

303
void RenderPlugin::changeRenderEdges(View* view, MapHandlerGen* map, bool b, bool fromUI)
304
305
{
	ParameterSet* params = h_viewParams[view];
306
	params->perMap[map->getName()]->renderEdges = b;
307
308
309

	if(view->isCurrentView())
	{
310
311
		if(!fromUI)
			m_dockTab->refreshUI(params);
312
313
314
315
		view->updateGL();
	}
}

316
void RenderPlugin::changeRenderFaces(View* view, MapHandlerGen* map, bool b, bool fromUI)
317
318
{
	ParameterSet* params = h_viewParams[view];
319
	params->perMap[map->getName()]->renderFaces = b;
320
321
322

	if(view->isCurrentView())
	{
323
324
		if(!fromUI)
			m_dockTab->refreshUI(params);
325
326
327
328
		view->updateGL();
	}
}

329
void RenderPlugin::changeFacesStyle(View* view, MapHandlerGen* map, FaceShadingStyle style, bool fromUI)
330
331
{
	ParameterSet* params = h_viewParams[view];
332
	params->perMap[map->getName()]->faceStyle = style;
333
334
335

	if(view->isCurrentView())
	{
336
337
		if(!fromUI)
			m_dockTab->refreshUI(params);
338
		view->updateGL();
339
	}
340
341
342
343
344
345
346
}

#ifndef DEBUG
Q_EXPORT_PLUGIN2(RenderPlugin, RenderPlugin)
#else
Q_EXPORT_PLUGIN2(RenderPluginD, RenderPlugin)
#endif
347
348
349
350

} // namespace SCHNApps

} // namespace CGoGN