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

#include "mapHandler.h"

namespace CGoGN
{

namespace SCHNApps
{

PerMapParameterSet::PerMapParameterSet(MapHandlerGen* map) :
12 13
	positionVBO(NULL),
	scalarVBO(NULL),
14
	colorMap(BlueWhiteRed),
15
	expansion(0)
Pierre Kraemer's avatar
Pierre Kraemer committed
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 49 50 51 52 53 54 55 56 57 58 59 60 61 62
{
	bool positionFound = false;

	QList<Utils::VBO*> vbos = map->getVBOList();
	for(int i = 0; i < vbos.count(); ++i)
	{
		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;
			}
		}
	}
}


bool RenderScalarPlugin::enable()
{
	m_dockTab = new RenderScalarDockTab(m_window, this);
	addTabInDock(m_dockTab, "RenderScalar");

	m_scalarShader = new Utils::ShaderScalarField();
	registerShader(m_scalarShader);

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

	return true;
}

void RenderScalarPlugin::disable()
{
	delete m_scalarShader;
}

void RenderScalarPlugin::redraw(View* view)
{
	ParameterSet* params = h_viewParams[view];

	const QList<MapHandlerGen*>& maps = view->getLinkedMaps();
	foreach(MapHandlerGen* m, maps)
	{
		PerMapParameterSet* p = params->perMap[m->getName()];
63
		if(p->positionVBO && p->scalarVBO)
Pierre Kraemer's avatar
Pierre Kraemer committed
64
		{
65 66 67 68
			m_scalarShader->setAttributePosition(p->positionVBO);
			m_scalarShader->setAttributeScalar(p->scalarVBO);
			m_scalarShader->setMinValue(p->scalarMin);
			m_scalarShader->setMaxValue(p->scalarMax);
69
			m_scalarShader->setColorMap(p->colorMap);
70
			m_scalarShader->setExpansion(p->expansion);
71 72 73 74

			glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
			glEnable(GL_POLYGON_OFFSET_FILL);
			glPolygonOffset(1.0f, 1.0f);
75
			m->draw(m_scalarShader, Algo::Render::GL2::TRIANGLES);
76
			glDisable(GL_POLYGON_OFFSET_FILL);
Pierre Kraemer's avatar
Pierre Kraemer committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 134 135 136 137 138
		}
	}
}

void RenderScalarPlugin::viewLinked(View* view, Plugin* plugin)
{
	if(plugin == this)
	{
		ParameterSet* params = new ParameterSet();
		h_viewParams.insert(view, params);

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

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

		if(view->isCurrentView())
			m_dockTab->refreshUI(params);
	}
}

void RenderScalarPlugin::viewUnlinked(View* view, Plugin* plugin)
{
	if(plugin == this)
	{
		const QList<MapHandlerGen*>& maps = view->getLinkedMaps();
		foreach(MapHandlerGen* mh, maps)
			removeManagedMap(view, mh);

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

		disconnect(view, SIGNAL(mapLinked(MapHandlerGen*)), this, SLOT(mapLinked(MapHandlerGen*)));
		disconnect(view, SIGNAL(mapUnlinked(MapHandlerGen*)), this, SLOT(mapUnlinked(MapHandlerGen*)));
	}
}

void RenderScalarPlugin::currentViewChanged(View* view)
{
	if(isLinkedToView(view))
		m_dockTab->refreshUI(h_viewParams[view]);
}

void RenderScalarPlugin::mapLinked(MapHandlerGen* m)
{
	View* view = static_cast<View*>(QObject::sender());
	assert(isLinkedToView(view));
	addManagedMap(view, m);
}

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

void RenderScalarPlugin::addManagedMap(View* v, MapHandlerGen *m)
{
139
	connect(m, SIGNAL(attributeModified(unsigned int, QString)), this, SLOT(attributeModified(unsigned int, QString)));
Pierre Kraemer's avatar
Pierre Kraemer committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
//	connect(m, SIGNAL(connectivityModified()), this, SLOT(connectivityModified()));

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

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

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

void RenderScalarPlugin::removeManagedMap(View *v, MapHandlerGen *m)
{
155
	disconnect(m, SIGNAL(attributeModified(unsigned int, QString)), this, SLOT(attributeModified(unsigned int, QString)));
Pierre Kraemer's avatar
Pierre Kraemer committed
156 157 158 159 160 161 162 163 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 204 205
//	disconnect(m, SIGNAL(connectivityModified()), this, SLOT(connectivityModified()));

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

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

	if(params->selectedMap == m)
	{
		if(!params->perMap.empty())
			changeSelectedMap(v, m_window->getMap(params->perMap.begin().key()));
		else
			changeSelectedMap(v, NULL);
	}
	else
		m_dockTab->refreshUI(params);
}

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

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

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

		m_dockTab->refreshUI(params);
	}
}

void RenderScalarPlugin::changePositionVBO(View* view, MapHandlerGen* map, Utils::VBO* vbo, bool fromUI)
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()]->positionVBO = vbo;

	if(view->isCurrentView())
	{
		if(!fromUI)
			m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

206 207 208 209 210 211 212 213 214 215 216
void RenderScalarPlugin::changeScalarVBO(View* view, MapHandlerGen* map, Utils::VBO* vbo, bool fromUI)
{
	ParameterSet* params = h_viewParams[view];
	PerMapParameterSet* perMap = params->perMap[map->getName()];

	perMap->scalarVBO = vbo;

	if(vbo != NULL)
	{
		const VertexAttribute<PFP2::REAL>& attr = map->getAttribute<PFP2::REAL, VERTEX>(QString::fromStdString(vbo->name()));
		perMap->scalarMin = 1e20;
217
		perMap->scalarMax = -1e20;
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
		for(unsigned int i = attr.begin(); i != attr.end(); attr.next(i))
		{
			perMap->scalarMin = attr[i] < perMap->scalarMin ? attr[i] : perMap->scalarMin;
			perMap->scalarMax = attr[i] > perMap->scalarMax ? attr[i] : perMap->scalarMax;
		}
	}

	if(view->isCurrentView())
	{
		if(!fromUI)
			m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

233 234 235 236 237 238 239 240 241 242 243 244 245
void RenderScalarPlugin::changeColorMap(View* view, MapHandlerGen* map, int c, bool fromUI)
{
	ParameterSet* params = h_viewParams[view];
	params->perMap[map->getName()]->colorMap = c;

	if(view->isCurrentView())
	{
		if(!fromUI)
			m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

246
void RenderScalarPlugin::changeExpansion(View* view, MapHandlerGen* map, int i, bool fromUI)
Pierre Kraemer's avatar
Pierre Kraemer committed
247 248
{
	ParameterSet* params = h_viewParams[view];
249
	params->perMap[map->getName()]->expansion = i;
Pierre Kraemer's avatar
Pierre Kraemer committed
250 251 252 253 254 255 256 257 258

	if(view->isCurrentView())
	{
		if(!fromUI)
			m_dockTab->refreshUI(params);
		view->updateGL();
	}
}

259 260 261 262 263 264 265 266 267 268 269 270 271 272
void RenderScalarPlugin::attributeModified(unsigned int orbit, QString nameAttr)
{
	if(orbit == VERTEX)
	{
		MapHandlerGen* map = static_cast<MapHandlerGen*>(QObject::sender());
		foreach(View* view, l_views)
		{
			ParameterSet* params = h_viewParams[view];
			if(params->perMap.contains(map->getName()))
			{
				PerMapParameterSet* perMap = params->perMap[map->getName()];
				if(perMap->scalarVBO && nameAttr == QString::fromStdString(perMap->scalarVBO->name()))
				{
					const VertexAttribute<PFP2::REAL>& attr = map->getAttribute<PFP2::REAL, VERTEX>(nameAttr);
273 274
					perMap->scalarMin = std::numeric_limits<float>::max();
					perMap->scalarMax = std::numeric_limits<float>::min();
275 276 277 278 279 280 281 282 283 284 285
					for(unsigned int i = attr.begin(); i != attr.end(); attr.next(i))
					{
						perMap->scalarMin = attr[i] < perMap->scalarMin ? attr[i] : perMap->scalarMin;
						perMap->scalarMax = attr[i] > perMap->scalarMax ? attr[i] : perMap->scalarMax;
					}
				}
			}
		}
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
286 287 288 289 290 291 292 293 294
#ifndef DEBUG
Q_EXPORT_PLUGIN2(RenderScalarPlugin, RenderScalarPlugin)
#else
Q_EXPORT_PLUGIN2(RenderScalarPluginD, RenderScalarPlugin)
#endif

} // namespace SCHNApps

} // namespace CGoGN