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.63 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
218
219
220
221
222
223
224
225
226
227
228
229
230
			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);
231
232

	m_dockTab->refreshUI(h_viewParams[view]);
233
234
235
236
237
238
239
240
241
242
243
244
}

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

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

	if(view->isCurrentView())
	{
		if(prev)
245
			disconnect(prev, SIGNAL(vboAdded(Utils::VBO*)), this, SLOT(vboAdded(Utils::VBO*)));
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
325
326
327
328
329
330
331
332
		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())
	{
333
		m_dockTab->refreshUI(params);
334
		view->updateGL();
335
	}
336
337
338
339
340
341
342
}

#ifndef DEBUG
Q_EXPORT_PLUGIN2(RenderPlugin, RenderPlugin)
#else
Q_EXPORT_PLUGIN2(RenderPluginD, RenderPlugin)
#endif
343
344
345
346

} // namespace SCHNApps

} // namespace CGoGN