surface_render.cpp 9.21 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4 5 6 7 8 9 10
#include "surface_render.h"

#include "mapHandler.h"

namespace CGoGN
{

namespace SCHNApps
{

Pierre Kraemer's avatar
Pierre Kraemer committed
11
bool Surface_Render_Plugin::enable()
Pierre Kraemer's avatar
Pierre Kraemer committed
12
{
Pierre Kraemer's avatar
Pierre Kraemer committed
13 14
	m_dockTab = new Surface_Render_DockTab(m_schnapps, this);
	m_schnapps->addPluginDockTab(this, m_dockTab, "Surface_Render");
Pierre Kraemer's avatar
Pierre Kraemer committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

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

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

	m_simpleColorShader = new CGoGN::Utils::ShaderSimpleColor();

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

	registerShader(m_flatShader);
	registerShader(m_phongShader);
	registerShader(m_simpleColorShader);
	registerShader(m_pointSprite);

	connect(m_schnapps, SIGNAL(selectedViewChanged(View*, View*)), this, SLOT(selectedViewChanged(View*, View*)));
	connect(m_schnapps, SIGNAL(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)), this, SLOT(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)));
	connect(m_schnapps, SIGNAL(mapAdded(MapHandlerGen*)), this, SLOT(mapAdded(MapHandlerGen*)));
	connect(m_schnapps, SIGNAL(mapRemoved(MapHandlerGen*)), this, SLOT(mapRemoved(MapHandlerGen*)));

	foreach(MapHandlerGen* map, m_schnapps->getMapSet().values())
		mapAdded(map);

	m_dockTab->updateMapParameters();

	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
49
void Surface_Render_Plugin::disable()
Pierre Kraemer's avatar
Pierre Kraemer committed
50 51 52 53 54 55
{
	delete m_flatShader;
	delete m_phongShader;
	delete m_simpleColorShader;
	delete m_pointSprite;

56 57 58 59 60
	disconnect(m_schnapps, SIGNAL(selectedViewChanged(View*, View*)), this, SLOT(selectedViewChanged(View*, View*)));
	disconnect(m_schnapps, SIGNAL(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)), this, SLOT(selectedMapChanged(MapHandlerGen*, MapHandlerGen*)));
	disconnect(m_schnapps, SIGNAL(mapAdded(MapHandlerGen*)), this, SLOT(mapAdded(MapHandlerGen*)));
	disconnect(m_schnapps, SIGNAL(mapRemoved(MapHandlerGen*)), this, SLOT(mapRemoved(MapHandlerGen*)));

Pierre Kraemer's avatar
Pierre Kraemer committed
61 62 63 64
	foreach(MapHandlerGen* map, m_schnapps->getMapSet().values())
		mapRemoved(map);
}

Pierre Kraemer's avatar
Pierre Kraemer committed
65
void Surface_Render_Plugin::drawMap(View* view, MapHandlerGen* map)
Pierre Kraemer's avatar
Pierre Kraemer committed
66
{
67 68
	const MapParameters& p = h_viewParameterSet[view][map];
	if(p.positionVBO)
Pierre Kraemer's avatar
Pierre Kraemer committed
69
	{
70
		if(p.renderVertices)
Pierre Kraemer's avatar
Pierre Kraemer committed
71
		{
72 73 74 75 76 77 78 79
			m_pointSprite->setSize(map->getBBdiagSize() / 200.0f * p.verticesScaleFactor);
			m_pointSprite->setAttributePosition(p.positionVBO);
			m_pointSprite->setColor(CGoGN::Geom::Vec4f(0.0f, 0.0f, 1.0f, 1.0f));
			map->draw(m_pointSprite, CGoGN::Algo::Render::GL2::POINTS);
		}
		if(p.renderEdges)
		{
			glLineWidth(1.0f);
80 81
			CGoGN::Geom::Vec4f c(0.1f, 0.1f, 0.1f, 1.0f);
			m_simpleColorShader->setColor(c);
82 83 84 85 86 87 88 89 90 91
			m_simpleColorShader->setAttributePosition(p.positionVBO);
			map->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)
Pierre Kraemer's avatar
Pierre Kraemer committed
92
			{
93 94 95
				case MapParameters::FLAT :
					m_flatShader->setAttributePosition(p.positionVBO);
					map->draw(m_flatShader, CGoGN::Algo::Render::GL2::TRIANGLES);
Pierre Kraemer's avatar
Pierre Kraemer committed
96
					break;
97 98 99
				case MapParameters::PHONG :
					if(p.normalVBO != NULL)
					{
Pierre Kraemer's avatar
Pierre Kraemer committed
100 101
						m_phongShader->setAttributePosition(p.positionVBO);
						m_phongShader->setAttributeNormal(p.normalVBO);
102 103
						map->draw(m_phongShader, CGoGN::Algo::Render::GL2::TRIANGLES);
					}
Pierre Kraemer's avatar
Pierre Kraemer committed
104
					break;
Pierre Kraemer's avatar
Pierre Kraemer committed
105
			}
106
			glDisable(GL_POLYGON_OFFSET_FILL);
Pierre Kraemer's avatar
Pierre Kraemer committed
107
		}
108 109 110 111 112 113 114 115
		if(p.renderBoundary)
		{
			glLineWidth(5.0f);
			CGoGN::Geom::Vec4f c(0.8f, 0.8f, 0.1f, 1.0f);
			m_simpleColorShader->setColor(c);
			m_simpleColorShader->setAttributePosition(p.positionVBO);
			map->draw(m_simpleColorShader, CGoGN::Algo::Render::GL2::BOUNDARY);
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
116 117 118 119 120 121 122
	}
}





Pierre Kraemer's avatar
Pierre Kraemer committed
123
void Surface_Render_Plugin::selectedViewChanged(View *prev, View *cur)
Pierre Kraemer's avatar
Pierre Kraemer committed
124 125 126 127
{
	m_dockTab->updateMapParameters();
}

Pierre Kraemer's avatar
Pierre Kraemer committed
128
void Surface_Render_Plugin::selectedMapChanged(MapHandlerGen *prev, MapHandlerGen *cur)
Pierre Kraemer's avatar
Pierre Kraemer committed
129 130 131 132
{
	m_dockTab->updateMapParameters();
}

Pierre Kraemer's avatar
Pierre Kraemer committed
133
void Surface_Render_Plugin::mapAdded(MapHandlerGen *map)
Pierre Kraemer's avatar
Pierre Kraemer committed
134 135 136 137 138
{
	connect(map, SIGNAL(vboAdded(Utils::VBO*)), this, SLOT(vboAdded(Utils::VBO*)));
	connect(map, SIGNAL(vboRemoved(Utils::VBO*)), this, SLOT(vboRemoved(Utils::VBO*)));
}

Pierre Kraemer's avatar
Pierre Kraemer committed
139
void Surface_Render_Plugin::mapRemoved(MapHandlerGen *map)
Pierre Kraemer's avatar
Pierre Kraemer committed
140 141 142 143 144 145 146 147 148
{
	disconnect(map, SIGNAL(vboAdded(Utils::VBO*)), this, SLOT(vboAdded(Utils::VBO*)));
	disconnect(map, SIGNAL(vboRemoved(Utils::VBO*)), this, SLOT(vboRemoved(Utils::VBO*)));
}





Pierre Kraemer's avatar
Pierre Kraemer committed
149
void Surface_Render_Plugin::vboAdded(Utils::VBO *vbo)
Pierre Kraemer's avatar
Pierre Kraemer committed
150 151 152 153 154 155 156 157 158 159 160 161 162
{
	MapHandlerGen* map = static_cast<MapHandlerGen*>(QObject::sender());

	if(map == m_schnapps->getSelectedMap())
	{
		if(vbo->dataSize() == 3)
		{
			m_dockTab->addPositionVBO(QString::fromStdString(vbo->name()));
			m_dockTab->addNormalVBO(QString::fromStdString(vbo->name()));
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
163
void Surface_Render_Plugin::vboRemoved(Utils::VBO *vbo)
Pierre Kraemer's avatar
Pierre Kraemer committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
{
	MapHandlerGen* map = static_cast<MapHandlerGen*>(QObject::sender());

	if(map == m_schnapps->getSelectedMap())
	{
		if(vbo->dataSize() == 3)
		{
			m_dockTab->removePositionVBO(QString::fromStdString(vbo->name()));
			m_dockTab->removeNormalVBO(QString::fromStdString(vbo->name()));
		}
	}

	QSet<View*> viewsToUpdate;

	QHash<View*, QHash<MapHandlerGen*, MapParameters> >::iterator i;
	for (i = h_viewParameterSet.begin(); i != h_viewParameterSet.end(); ++i)
	{
		View* view = i.key();
		QHash<MapHandlerGen*, MapParameters>& viewParamSet = i.value();
		MapParameters& mapParam = viewParamSet[map];
		if(mapParam.positionVBO == vbo)
		{
			mapParam.positionVBO = NULL;
			if(view->isLinkedToMap(map)) viewsToUpdate.insert(view);
		}
		if(mapParam.normalVBO == vbo)
		{
			mapParam.normalVBO = NULL;
			if(view->isLinkedToMap(map)) viewsToUpdate.insert(view);
		}
	}

	foreach(View* v, viewsToUpdate)
		v->updateGL();
}





Pierre Kraemer's avatar
Pierre Kraemer committed
204
void Surface_Render_Plugin::changePositionVBO(const QString& view, const QString& map, const QString& vbo)
Pierre Kraemer's avatar
Pierre Kraemer committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
{
	View* v = m_schnapps->getView(view);
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(v && m)
	{
		Utils::VBO* vbuf = m->getVBO(vbo);
		h_viewParameterSet[v][m].positionVBO = vbuf;
		if(v->isSelectedView())
		{
			if(v->isLinkedToMap(m))	v->updateGL();
			if(m->isSelectedMap()) m_dockTab->updateMapParameters();
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
220
void Surface_Render_Plugin::changeNormalVBO(const QString& view, const QString& map, const QString& vbo)
Pierre Kraemer's avatar
Pierre Kraemer committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
{
	View* v = m_schnapps->getView(view);
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(v && m)
	{
		Utils::VBO* vbuf = m->getVBO(vbo);
		h_viewParameterSet[v][m].normalVBO = vbuf;
		if(v->isSelectedView())
		{
			if(v->isLinkedToMap(m))	v->updateGL();
			if(m->isSelectedMap()) m_dockTab->updateMapParameters();
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
236
void Surface_Render_Plugin::changeRenderVertices(const QString& view, const QString& map, bool b)
Pierre Kraemer's avatar
Pierre Kraemer committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250
{
	View* v = m_schnapps->getView(view);
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(v && m)
	{
		h_viewParameterSet[v][m].renderVertices = b;
		if(v->isSelectedView())
		{
			if(v->isLinkedToMap(m))	v->updateGL();
			if(m->isSelectedMap()) m_dockTab->updateMapParameters();
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
251
void Surface_Render_Plugin::changeVerticesScaleFactor(const QString& view, const QString& map, float f)
Pierre Kraemer's avatar
Pierre Kraemer committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265
{
	View* v = m_schnapps->getView(view);
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(v && m)
	{
		h_viewParameterSet[v][m].verticesScaleFactor = f;
		if(v->isSelectedView())
		{
			if(v->isLinkedToMap(m))	v->updateGL();
			if(m->isSelectedMap()) m_dockTab->updateMapParameters();
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
266
void Surface_Render_Plugin::changeRenderEdges(const QString& view, const QString& map, bool b)
Pierre Kraemer's avatar
Pierre Kraemer committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280
{
	View* v = m_schnapps->getView(view);
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(v && m)
	{
		h_viewParameterSet[v][m].renderEdges = b;
		if(v->isSelectedView())
		{
			if(v->isLinkedToMap(m))	v->updateGL();
			if(m->isSelectedMap()) m_dockTab->updateMapParameters();
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
281
void Surface_Render_Plugin::changeRenderFaces(const QString& view, const QString& map, bool b)
Pierre Kraemer's avatar
Pierre Kraemer committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295
{
	View* v = m_schnapps->getView(view);
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(v && m)
	{
		h_viewParameterSet[v][m].renderFaces = b;
		if(v->isSelectedView())
		{
			if(v->isLinkedToMap(m))	v->updateGL();
			if(m->isSelectedMap()) m_dockTab->updateMapParameters();
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
296
void Surface_Render_Plugin::changeFacesStyle(const QString& view, const QString& map, MapParameters::FaceShadingStyle style)
Pierre Kraemer's avatar
Pierre Kraemer committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310
{
	View* v = m_schnapps->getView(view);
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(v && m)
	{
		h_viewParameterSet[v][m].faceStyle = style;
		if(v->isSelectedView())
		{
			if(v->isLinkedToMap(m))	v->updateGL();
			if(m->isSelectedMap()) m_dockTab->updateMapParameters();
		}
	}
}

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
void Surface_Render_Plugin::changeRenderBoundary(const QString& view, const QString& map, bool b)
{
	View* v = m_schnapps->getView(view);
	MapHandlerGen* m = m_schnapps->getMap(map);
	if(v && m)
	{
		h_viewParameterSet[v][m].renderBoundary = b;
		if(v->isSelectedView())
		{
			if(v->isLinkedToMap(m))	v->updateGL();
			if(m->isSelectedMap()) m_dockTab->updateMapParameters();
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
326
#ifndef DEBUG
Pierre Kraemer's avatar
Pierre Kraemer committed
327
Q_EXPORT_PLUGIN2(Surface_Render_Plugin, Surface_Render_Plugin)
Pierre Kraemer's avatar
Pierre Kraemer committed
328
#else
Pierre Kraemer's avatar
Pierre Kraemer committed
329
Q_EXPORT_PLUGIN2(Surface_Render_PluginD, Surface_Render_Plugin)
Pierre Kraemer's avatar
Pierre Kraemer committed
330 331 332 333 334
#endif

} // namespace SCHNApps

} // namespace CGoGN