explodeVolumeRender.hpp 9.83 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
#include <cmath>

#include "Geometry/vector_gen.h"
#include "Topology/generic/dartmarker.h"
#include "Topology/generic/cellmarker.h"
29
#include "Algo/Geometry/centroid.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
30 31 32 33 34 35 36 37 38 39

namespace CGoGN
{

namespace Algo
{

namespace Render
{

Sylvain Thery's avatar
Sylvain Thery committed
40
namespace GL2
Pierre Kraemer's avatar
Pierre Kraemer committed
41 42
{

43 44
inline ExplodeVolumeRender::ExplodeVolumeRender(bool withColorPerFace, bool withExplodeFace):
		m_cpf(withColorPerFace),m_ef(withExplodeFace),m_globalColor(0.7f,0.7f,0.7f)
Pierre Kraemer's avatar
Pierre Kraemer committed
45
{
46 47 48
	m_vboPos = new Utils::VBO();
	m_vboPos->setDataSize(3);

49 50 51 52
	m_vboColors = new Utils::VBO();
	m_vboColors->setDataSize(3);


53 54 55
	m_vboPosLine = new Utils::VBO();
	m_vboPosLine->setDataSize(3);

56
	m_shader = new Utils::ShaderExplodeVolumes(withColorPerFace,withExplodeFace);
57
	m_shaderL = new Utils::ShaderExplodeVolumesLines();
Pierre Kraemer's avatar
Pierre Kraemer committed
58

59 60
//	m_shader->setAmbiant(Geom::Vec4f(0.1f,0.1f,0.1f,0.0f));
//	m_shader->setDiffuse(Geom::Vec4f(1.0f,1.0f,0.1f,0.0f));
61 62
	//m_shaderL->setColor(Geom::Vec4f(1.0f,1.0f,1.0f,0.0f));

untereiner's avatar
untereiner committed
63
	//m_shaderL->setColor(Geom::Vec4f(0.113f,0.337f,0.0f,0.113f));
64

untereiner's avatar
untereiner committed
65
	m_shaderL->setColor(Geom::Vec4f(1.0f,1.0f,1.0f,0.0f));
66 67

}
Pierre Kraemer's avatar
Pierre Kraemer committed
68

69
inline ExplodeVolumeRender::~ExplodeVolumeRender()
Pierre Kraemer's avatar
Pierre Kraemer committed
70
{
71
	delete m_vboPos;
72
	delete m_vboColors;
73 74 75
	delete m_vboPosLine;
	delete m_shader;
	delete m_shaderL;
Pierre Kraemer's avatar
Pierre Kraemer committed
76 77
}

78
template<typename PFP>
79
void ExplodeVolumeRender::updateData(typename PFP::MAP& map, const AttributeHandler<typename PFP::VEC3, VERTEX>& positions, const FunctorSelect& good)
80 81 82 83 84 85 86 87 88 89
{
	if (m_cpf)
	{
		CGoGNerr<< "ExplodeVolumeRender: problem wrong update fonction use the other" << CGoGNendl;
		return;
	}

	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

90 91
	AutoAttributeHandler<VEC3, VOLUME> centerVolumes(map, "centerVolumes");
	Algo::Geometry::computeCentroidVolumes<PFP>(map, positions, centerVolumes, good);
92 93 94 95

	std::vector<VEC3> buffer;
	buffer.reserve(16384);

96
	TraversorCell<typename PFP::MAP, FACE + PFP::MAP::IN_PARENT> traFace(map, good);
97

98
	for (Dart d = traFace.begin(); d != traFace.end(); d = traFace.next())
99 100 101
	{
		if (m_ef)
		{
102
			VEC3 centerFace = Algo::Geometry::faceCentroid<PFP>(map, d, positions);
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 139 140 141 142 143 144 145 146 147 148 149 150
			Dart a = d;
			Dart b = map.phi1(a);
			Dart c = map.phi1(b);

			// loop to cut a polygon in triangle on the fly (works only with convex faces)
			do
			{
				buffer.push_back(centerVolumes[d]);
				buffer.push_back(positions[d]);
				buffer.push_back(positions[b]);
				buffer.push_back(positions[c]);
				buffer.push_back(centerFace); // not used
				buffer.push_back(centerFace);
				b = c;
				c = map.phi1(b);

			} while (c != d);
		}
		else
		{
			Dart a = d;
			Dart b = map.phi1(a);
			Dart c = map.phi1(b);

			// loop to cut a polygon in triangle on the fly (works only with convex faces)
			do
			{
				buffer.push_back(centerVolumes[d]);
				buffer.push_back(positions[d]);
				buffer.push_back(positions[b]);
				buffer.push_back(positions[c]);
				b = c;
				c = map.phi1(b);

			} while (c != d);
		}
	}

	m_nbTris = buffer.size()/4;

	m_vboPos->allocate(buffer.size());
	VEC3* ptrPos = reinterpret_cast<VEC3*>(m_vboPos->lockPtr());
	memcpy(ptrPos,&buffer[0],buffer.size()*sizeof(VEC3));
	m_vboPos->releasePtr();
	m_shader->setAttributePosition(m_vboPos);

	buffer.clear();

151 152
	TraversorCell<typename PFP::MAP, EDGE + PFP::MAP::IN_PARENT> traEdge(map, good);
	for (Dart d = traEdge.begin(); d != traEdge.end(); d = traEdge.next())
153
	{
154 155 156
		buffer.push_back(centerVolumes[d]);
		buffer.push_back(positions[d]);
		buffer.push_back(positions[map.phi1(d)]);
157 158 159 160 161 162 163 164 165 166 167 168
	}

	m_nbLines = buffer.size()/3;

	m_vboPosLine->allocate(buffer.size());

	ptrPos = reinterpret_cast<VEC3*>(m_vboPosLine->lockPtr());
	memcpy(ptrPos,&buffer[0],buffer.size()*sizeof(VEC3));

	m_vboPosLine->releasePtr();
	m_shaderL->setAttributePosition(m_vboPosLine);
}
169 170

template<typename PFP>
171
void ExplodeVolumeRender::updateData(typename PFP::MAP& map, const AttributeHandler<typename PFP::VEC3, VERTEX>& positions, const AttributeHandler<typename PFP::VEC3, VOLUME>& colorPerXXX, const FunctorSelect& good)
172 173 174 175 176 177 178 179 180 181
{
	if (!m_cpf)
	{
		CGoGNerr<< "ExplodeVolumeRender: problem wrong update fonction use the other" << CGoGNendl;
		return;
	}

	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

182 183
	AutoAttributeHandler<VEC3, VOLUME> centerVolumes(map, "centerVolumes");
	Algo::Geometry::computeCentroidVolumes<PFP>(map, positions, centerVolumes, good);
184 185 186 187

	std::vector<VEC3> buffer;
	buffer.reserve(16384);

188 189 190 191 192 193 194
	std::vector<VEC3> bufferColors;

//	bool withColors = (positions == colorPerXXX);
	bool withColors = true;
	if (withColors)
		bufferColors.reserve(16384);

195
	TraversorCell<typename PFP::MAP, FACE + PFP::MAP::IN_PARENT> traFace(map, good);
196

197
	for (Dart d = traFace.begin(); d != traFace.end(); d = traFace.next())
198
	{
199
		VEC3 centerFace = Algo::Geometry::faceCentroid<PFP>(map, d, positions);
200

201 202 203 204 205 206 207
		Dart a = d;
		Dart b = map.phi1(a);
		Dart c = map.phi1(b);
		// loop to cut a polygon in triangle on the fly (works only with convex faces)
		do
		{
			buffer.push_back(centerVolumes[d]);
208
			bufferColors.push_back(centerFace);
209
			buffer.push_back(positions[d]);
210 211 212 213
			if (m_cpf)
				bufferColors.push_back(colorPerXXX[d]);
			else
				bufferColors.push_back(m_globalColor);
214
			buffer.push_back(positions[b]);
215 216 217 218
			if (m_cpf)
				bufferColors.push_back(colorPerXXX[b]);
			else
				bufferColors.push_back(m_globalColor);
219
			buffer.push_back(positions[c]);
220 221 222 223
			if (m_cpf)
				bufferColors.push_back(colorPerXXX[c]);
			else
				bufferColors.push_back(m_globalColor);
224 225 226 227 228 229 230 231 232 233 234 235 236 237
			b = c;
			c = map.phi1(b);

		} while (c != d);
	}

	m_nbTris = buffer.size()/4;

	m_vboPos->allocate(buffer.size());
	VEC3* ptrPos = reinterpret_cast<VEC3*>(m_vboPos->lockPtr());
	memcpy(ptrPos,&buffer[0],buffer.size()*sizeof(VEC3));
	m_vboPos->releasePtr();
	m_shader->setAttributePosition(m_vboPos);

238 239 240 241 242 243 244 245 246
	if (withColors)
	{
		m_vboColors->allocate(bufferColors.size());
		VEC3* ptrCol = reinterpret_cast<VEC3*>(m_vboColors->lockPtr());
		memcpy(ptrCol,&bufferColors[0],bufferColors.size()*sizeof(VEC3));
		m_vboColors->releasePtr();
		m_shader->setAttributeColor(m_vboColors);
	}

247

248
	buffer.clear();
Sylvain Thery's avatar
Sylvain Thery committed
249

250 251
	TraversorCell<typename PFP::MAP, EDGE + PFP::MAP::IN_PARENT> traEdge(map, good);
	for (Dart d = traEdge.begin(); d != traEdge.end(); d = traEdge.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
252
	{
253 254
			buffer.push_back(centerVolumes[d]);
			buffer.push_back(positions[d]);
255
			buffer.push_back(positions[map.phi1(d)]);
Pierre Kraemer's avatar
Pierre Kraemer committed
256 257
	}

258 259 260 261 262 263 264 265 266
	m_nbLines = buffer.size()/3;

	m_vboPosLine->allocate(buffer.size());

	ptrPos = reinterpret_cast<VEC3*>(m_vboPosLine->lockPtr());
	memcpy(ptrPos,&buffer[0],buffer.size()*sizeof(VEC3));

	m_vboPosLine->releasePtr();
	m_shaderL->setAttributePosition(m_vboPosLine);
Pierre Kraemer's avatar
Pierre Kraemer committed
267 268
}

269
inline void ExplodeVolumeRender::drawFaces()
Pierre Kraemer's avatar
Pierre Kraemer committed
270
{
271
	m_shader->enableVertexAttribs();
272 273 274
//	if (m_cpf)
//		glDrawArrays(GL_TRIANGLES_ADJACENCY_EXT , 0 , m_nbTris*6 );
//	else
275
		glDrawArrays(GL_LINES_ADJACENCY_EXT , 0 , m_nbTris*4 );
276 277
	m_shader->disableVertexAttribs();
}
Pierre Kraemer's avatar
Pierre Kraemer committed
278

279 280
inline void ExplodeVolumeRender::drawEdges()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
281

282 283 284
	m_shaderL->enableVertexAttribs();
	glDrawArrays(GL_TRIANGLES , 0 , m_nbLines*3 );
	m_shaderL->disableVertexAttribs();
Pierre Kraemer's avatar
Pierre Kraemer committed
285 286
}

Sylvain Thery's avatar
Sylvain Thery committed
287 288 289 290 291 292
inline void ExplodeVolumeRender::setExplodeVolumes(float explode)
{
	m_shader->setExplodeVolumes(explode);
	m_shaderL->setExplodeVolumes(explode);
}

293 294 295 296 297
inline void ExplodeVolumeRender::setExplodeFaces(float explode)
{
	m_shader->setExplodeFaces(explode);
}

Sylvain Thery's avatar
Sylvain Thery committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
inline void ExplodeVolumeRender::setClippingPlane(const Geom::Vec4f& p)
{
	m_shader->setClippingPlane(p);
	m_shaderL->setClippingPlane(p);
}

inline void ExplodeVolumeRender::setNoClippingPlane()
{
	Geom::Vec4f p(1.0f,1.0f,1.0f,-99999999.9f);
	m_shader->setClippingPlane(p);
	m_shaderL->setClippingPlane(p);
}

inline void ExplodeVolumeRender::setAmbiant(const Geom::Vec4f& ambiant)
{
	m_shader->setAmbiant(ambiant);
}

316
inline void ExplodeVolumeRender::setBackColor(const Geom::Vec4f& color)
Sylvain Thery's avatar
Sylvain Thery committed
317
{
318
	m_shader->setBackColor(color);
Sylvain Thery's avatar
Sylvain Thery committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
}

inline void ExplodeVolumeRender::setLightPosition(const Geom::Vec3f& lp)
{
	m_shader->setLightPosition(lp);
}

inline void ExplodeVolumeRender::setColorLine(const Geom::Vec4f& col)
{
	m_shaderL->setColor(col);
}

inline Utils::GLSLShader* ExplodeVolumeRender::shaderFaces()
{
	return m_shader;
}

inline Utils::GLSLShader* ExplodeVolumeRender::shaderLines()
{
	return m_shaderL;
}
340

Pierre Kraemer's avatar
Pierre Kraemer committed
341 342 343 344 345 346 347
}//end namespace VBO

}//end namespace Algo

}//end namespace Render

}//end namespace CGoGN