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.97 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
		const PerMapParameterSet& p = params->perMap[m->getName()];
		if(p.positionVBO != NULL)
97
		{
98
99
100
101
102
103
104
105
106
			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
107
			{
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
				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
134
			}
135
136
137
138
		}
	}
}

139
void RenderPlugin::viewLinked(View* view, Plugin* plugin)
140
{
141
	if(plugin == this)
142
	{
143
144
145
146
147
148
149
150
151
152
		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]);
153

154
155
		connect(view, SIGNAL(mapLinked(MapHandlerGen*)), this, SLOT(mapLinked(MapHandlerGen*)));
		connect(view, SIGNAL(mapUnlinked(MapHandlerGen*)), this, SLOT(mapUnlinked(MapHandlerGen*)));
156

157
158
159
		if(view->isCurrentView())
			m_dockTab->refreshUI(params);
	}
160
161
}

162
void RenderPlugin::viewUnlinked(View* view, Plugin* plugin)
163
{
164
165
166
	if(plugin == this)
	{
		h_viewParams.remove(view);
167

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

void RenderPlugin::currentViewChanged(View* view)
{
175
	if(isLinkedToView(view))
176
177
178
	{
		ParameterSet* params = h_viewParams[view];
		changeSelectedMap(view, params->selectedMap);
179
		m_dockTab->refreshUI(h_viewParams[view]);
180
	}
181
182
}

183
void RenderPlugin::mapLinked(MapHandlerGen* m)
184
{
185
	View* view = static_cast<View*>(QObject::sender());
186
	assert(isLinkedToView(view));
187

188
	ParameterSet* params = h_viewParams[view];
189
	PerMapParameterSet p(m);
190
	params->perMap.insert(m->getName(), p);
191
192
193
194
	if(params->selectedMap == NULL || params->perMap.count() == 1)
		changeSelectedMap(view, m);
	else
		m_dockTab->refreshUI(params);
195
196
}

197
void RenderPlugin::mapUnlinked(MapHandlerGen* m)
198
{
199
	View* view = static_cast<View*>(QObject::sender());
200
	assert(isLinkedToView(view));
201

202
203
	ParameterSet* params = h_viewParams[view];
	params->perMap.remove(m->getName());
204

205
206
	if(params->selectedMap == m)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
207
		if(!params->perMap.empty())
208
			changeSelectedMap(view, m_window->getMap(params->perMap.begin().key()));
Pierre Kraemer's avatar
Pierre Kraemer committed
209
		else
210
211
212
213
214
215
216
217
			changeSelectedMap(view, NULL);
	}
	else
		m_dockTab->refreshUI(params);
}

void RenderPlugin::vboAdded(Utils::VBO* vbo)
{
218
	assert(h_viewParams[m_window->getCurrentView()]->selectedMap == static_cast<MapHandlerGen*>(QObject::sender()));
219
220
	if(vbo->dataSize() == 3)
		m_dockTab->addVBOToList(QString::fromStdString(vbo->name()));
221
222
223
224
225
226
227
228
229
230
231
232
}

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);
233
234

	m_dockTab->refreshUI(h_viewParams[view]);
235
236
}

237
void RenderPlugin::changeSelectedMap(View* view, MapHandlerGen* map, bool fromUI)
238
239
240
241
242
243
244
245
246
{
	ParameterSet* params = h_viewParams[view];

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

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

251
252
		if(!fromUI)
			m_dockTab->refreshUI(params);
253
254
255
	}
}

256
void RenderPlugin::changePositionVBO(View* view, MapHandlerGen* map, Utils::VBO* vbo, bool fromUI)
257
258
259
260
261
262
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].positionVBO = vbo;

	if(view->isCurrentView())
	{
263
264
		if(!fromUI)
			m_dockTab->refreshUI(params);
265
266
267
268
		view->updateGL();
	}
}

269
void RenderPlugin::changeNormalVBO(View* view, MapHandlerGen* map, Utils::VBO* vbo, bool fromUI)
270
271
272
273
274
275
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].normalVBO = vbo;

	if(view->isCurrentView())
	{
276
277
		if(!fromUI)
			m_dockTab->refreshUI(params);
278
279
280
281
		view->updateGL();
	}
}

282
void RenderPlugin::changeRenderVertices(View* view, MapHandlerGen* map, bool b, bool fromUI)
283
284
285
286
287
288
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].renderVertices = b;

	if(view->isCurrentView())
	{
289
290
		if(!fromUI)
			m_dockTab->refreshUI(params);
291
292
293
294
		view->updateGL();
	}
}

295
void RenderPlugin::changeVerticesScaleFactor(View* view, MapHandlerGen* map, int i, bool fromUI)
296
297
298
299
300
301
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].verticesScaleFactor = i / 50.0;

	if(view->isCurrentView())
	{
302
303
		if(!fromUI)
			m_dockTab->refreshUI(params);
304
305
306
307
		view->updateGL();
	}
}

308
void RenderPlugin::changeRenderEdges(View* view, MapHandlerGen* map, bool b, bool fromUI)
309
310
311
312
313
314
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].renderEdges = b;

	if(view->isCurrentView())
	{
315
316
		if(!fromUI)
			m_dockTab->refreshUI(params);
317
318
319
320
		view->updateGL();
	}
}

321
void RenderPlugin::changeRenderFaces(View* view, MapHandlerGen* map, bool b, bool fromUI)
322
323
324
325
326
327
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].renderFaces = b;

	if(view->isCurrentView())
	{
328
329
		if(!fromUI)
			m_dockTab->refreshUI(params);
330
331
332
333
		view->updateGL();
	}
}

334
void RenderPlugin::changeFacesStyle(View* view, MapHandlerGen* map, FaceShadingStyle style, bool fromUI)
335
336
337
338
339
340
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()].faceStyle = style;

	if(view->isCurrentView())
	{
341
342
		if(!fromUI)
			m_dockTab->refreshUI(params);
343
		view->updateGL();
344
	}
345
346
347
348
349
350
351
}

#ifndef DEBUG
Q_EXPORT_PLUGIN2(RenderPlugin, RenderPlugin)
#else
Q_EXPORT_PLUGIN2(RenderPluginD, RenderPlugin)
#endif
352
353
354
355

} // namespace SCHNApps

} // namespace CGoGN