render.cpp 8.93 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->setColor(CGoGN::Geom::Vec4f(0.0f, 0.0f, 1.0f, 1.0f));
103 104
				m->draw(m_pointSprite, CGoGN::Algo::Render::GL2::POINTS);
			}
105
			if(p->renderEdges)
Pierre Kraemer's avatar
Pierre Kraemer committed
106
			{
107
				glLineWidth(1.0f);
108
				m_simpleColorShader->setAttributePosition(p->positionVBO);
109 110
				m->draw(m_simpleColorShader, CGoGN::Algo::Render::GL2::LINES);
			}
111
			if(p->renderFaces)
112 113 114 115 116
			{
				glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
				glEnable(GL_LIGHTING);
				glEnable(GL_POLYGON_OFFSET_FILL);
				glPolygonOffset(1.0f, 1.0f);
117
				switch(p->faceStyle)
118 119
				{
					case FLAT :
120
						m_flatShader->setAttributePosition(p->positionVBO);
121 122 123
						m->draw(m_flatShader, CGoGN::Algo::Render::GL2::TRIANGLES);
						break ;
					case PHONG :
124
						if(p->normalVBO != NULL)
125
						{
126 127
							m_phongShader->setAttributePosition(p->positionVBO) ;
							m_phongShader->setAttributeNormal(p->normalVBO) ;
128 129 130 131 132
							m->draw(m_phongShader, CGoGN::Algo::Render::GL2::TRIANGLES);
						}
						break ;
				}
				glDisable(GL_POLYGON_OFFSET_FILL);
Pierre Kraemer's avatar
Pierre Kraemer committed
133
			}
134 135 136 137
		}
	}
}

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

145
		const QList<MapHandlerGen*>& maps = view->getLinkedMaps();
146 147
		foreach(MapHandlerGen* mh, maps)
			addManagedMap(view, mh);
148

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

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

157
void RenderPlugin::viewUnlinked(View* view, Plugin* plugin)
158
{
159 160
	if(plugin == this)
	{
161 162 163 164 165 166
		const QList<MapHandlerGen*>& maps = view->getLinkedMaps();
		foreach(MapHandlerGen* mh, maps)
			removeManagedMap(view, mh);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

} // namespace SCHNApps

} // namespace CGoGN