colorPerFaceRender.hpp 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
#include <cmath>

#include "Geometry/vector_gen.h"
#include "Topology/generic/dartmarker.h"
#include "Topology/generic/cellmarker.h"
#include "Algo/Geometry/centroid.h"

namespace CGoGN
{

namespace Algo
{

namespace Render
{

namespace GL2
{


Sylvain Thery's avatar
Sylvain Thery committed
44 45

inline ColorPerFaceRender::ColorPerFaceRender():
46
m_nbTris(0)
47 48 49 50
{
}


Sylvain Thery's avatar
Sylvain Thery committed
51

52
template<typename PFP, unsigned int ORBIT>
Sylvain Thery's avatar
Sylvain Thery committed
53
void ColorPerFaceRender::updateVBO(Utils::VBO& vboPosition, Utils::VBO& vboColor, typename PFP::MAP& map,
54
            const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const AttributeHandler<typename PFP::VEC3,ORBIT, typename PFP::MAP::IMPL>& colorPerXXX)
55 56 57
{
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;
58
	typedef Geom::Vec3f VEC3F;
59

60
	std::vector<VEC3F> buffer;
61 62
	buffer.reserve(16384);

63
	std::vector<VEC3F> bufferColors;
64 65
	bufferColors.reserve(16384);

66
	TraversorCell<typename PFP::MAP, FACE> traFace(map);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

	for (Dart d=traFace.begin(); d!=traFace.end(); d=traFace.next())
	{
		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(positions[d]);
			bufferColors.push_back(colorPerXXX[d]);
			buffer.push_back(positions[b]);
			bufferColors.push_back(colorPerXXX[b]);
			buffer.push_back(positions[c]);
			bufferColors.push_back(colorPerXXX[c]);
			b = c;
			c = map.phi1(b);
		} while (c != d);
	}

Sylvain Thery's avatar
Sylvain Thery committed
87
	m_nbTris = buffer.size()/3;
88

Sylvain Thery's avatar
Sylvain Thery committed
89 90
	vboPosition.setDataSize(3);
	vboPosition.allocate(buffer.size());
91 92
	VEC3F* ptrPos = reinterpret_cast<VEC3F*>(vboPosition.lockPtr());
	memcpy(ptrPos,&buffer[0],buffer.size()*sizeof(VEC3F));
Sylvain Thery's avatar
Sylvain Thery committed
93
	vboPosition.releasePtr();
94

Sylvain Thery's avatar
Sylvain Thery committed
95 96
	vboColor.setDataSize(3);
	vboColor.allocate(bufferColors.size());
97 98
	VEC3F* ptrCol = reinterpret_cast<VEC3F*>(vboColor.lockPtr());
	memcpy(ptrCol,&bufferColors[0],bufferColors.size()*sizeof(VEC3F));
Sylvain Thery's avatar
Sylvain Thery committed
99 100 101
	vboColor.releasePtr();
}

102
template<typename PFP, unsigned int ORBIT>
Sylvain Thery's avatar
Sylvain Thery committed
103
void ColorPerFaceRender::updateVBO(Utils::VBO& vboPosition, Utils::VBO& vboNormal, Utils::VBO& vboColor, typename PFP::MAP& map,
104 105
            const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& normals,
            const AttributeHandler<typename PFP::VEC3,ORBIT, typename PFP::MAP::IMPL>& colorPerXXX)
Sylvain Thery's avatar
Sylvain Thery committed
106 107 108
{
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;
109
	typedef Geom::Vec3f VEC3F;
Sylvain Thery's avatar
Sylvain Thery committed
110

111
	std::vector<VEC3F> buffer;
Sylvain Thery's avatar
Sylvain Thery committed
112 113
	buffer.reserve(16384);

114
	std::vector<VEC3F> bufferNormals;
Sylvain Thery's avatar
Sylvain Thery committed
115 116
	bufferNormals.reserve(16384);

117
	std::vector<VEC3F> bufferColors;
Sylvain Thery's avatar
Sylvain Thery committed
118 119
	bufferColors.reserve(16384);

120
	TraversorCell<typename PFP::MAP, FACE> traFace(map);
121

Sylvain Thery's avatar
Sylvain Thery committed
122 123 124 125 126 127 128 129
	for (Dart d=traFace.begin(); d!=traFace.end(); d=traFace.next())
	{
		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
		{
130 131 132 133
			buffer.push_back(PFP::toVec3f(positions[d]));
			bufferNormals.push_back(PFP::toVec3f(normals[d]));
			bufferColors.push_back(PFP::toVec3f(colorPerXXX[d]));
			buffer.push_back(PFP::toVec3f(positions[b]));
134
			bufferNormals.push_back(PFP::toVec3f(normals[b]));
135
			bufferColors.push_back(PFP::toVec3f(colorPerXXX[b]));
136
			buffer.push_back(PFP::toVec3f(positions[c]));
137 138
			bufferNormals.push_back(PFP::toVec3f(normals[c]));
			bufferColors.push_back(PFP::toVec3f(colorPerXXX[c]));
Sylvain Thery's avatar
Sylvain Thery committed
139 140 141 142 143 144 145 146 147
			b = c;
			c = map.phi1(b);
		} while (c != d);
	}

	m_nbTris = buffer.size()/3;

	vboPosition.setDataSize(3);
	vboPosition.allocate(buffer.size());
148 149
	VEC3F* ptrPos = reinterpret_cast<VEC3F*>(vboPosition.lockPtr());
	memcpy(ptrPos, &buffer[0], buffer.size()*sizeof(VEC3F));
Sylvain Thery's avatar
Sylvain Thery committed
150 151 152 153
	vboPosition.releasePtr();

	vboNormal.setDataSize(3);
	vboNormal.allocate(bufferColors.size());
154 155
	VEC3F* ptrNorm = reinterpret_cast<VEC3F*>(vboNormal.lockPtr());
	memcpy(ptrNorm, &bufferColors[0], bufferColors.size()*sizeof(VEC3F));
Sylvain Thery's avatar
Sylvain Thery committed
156 157 158 159
	vboNormal.releasePtr();

	vboColor.setDataSize(3);
	vboColor.allocate(bufferColors.size());
160 161
	VEC3F* ptrCol = reinterpret_cast<VEC3F*>(vboColor.lockPtr());
	memcpy(ptrCol, &bufferColors[0], bufferColors.size()*sizeof(VEC3F));
Sylvain Thery's avatar
Sylvain Thery committed
162
	vboColor.releasePtr();
163 164 165
}


Sylvain Thery's avatar
Sylvain Thery committed
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
/*

template<typename PFP, unsigned int ORBIT>
void ColorPerFaceRender::updateVBO(Utils::VBO& vboPosition, Utils::VBO& vboColor, Utils::VBO& vboTexCoord,
								   typename PFP::MAP& map,
								   const VertexAttribute<typename PFP::VEC3>& positions,
								   const AttributeHandler<typename PFP::VEC3,ORBIT>& colorPerXXX,
								   CellMarker<VERTEX>& specialVertices,
								   const VertexAttribute<typename PFP::VEC3>& texCoordPerVertex,
								   const AttributeHandler<typename PFP::VEC3,VERTEX1>& texCoordPerDart)

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

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

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


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

	TraversorCell<typename PFP::MAP, FACE> traFace(map);

	for (Dart d=traFace.begin(); d!=traFace.end(); d=traFace.next())
	{
		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(positions[d]);
			bufferColors.push_back(colorPerXXX[d]);
			if (specialVertices.isMarked(d))
				bufferTC.push_back(texCoordPerDart[d]);
			else
				bufferTC.push_back(texCoordPerVertex[d]);

			buffer.push_back(positions[b]);
			bufferColors.push_back(colorPerXXX[b]);
			if (specialVertices.isMarked(c))
				bufferTC.push_back(texCoordPerDart[b]);
			else
				bufferTC.push_back(texCoordPerVertex[b]);

			buffer.push_back(positions[c]);
			bufferColors.push_back(colorPerXXX[c]);
			if (specialVertices.isMarked(c))
				bufferTC.push_back(texCoordPerDart[c]);
			else
				bufferTC.push_back(texCoordPerVertex[c]);

			b = c;
			c = map.phi1(b);
		} while (c != d);
	}

	m_nbTris = buffer.size()/3;

	vboPosition.setDataSize(3);
	vboPosition.allocate(buffer.size());
	VEC3* ptrPos = reinterpret_cast<VEC3*>(vboPosition.lockPtr());
	memcpy(ptrPos, &buffer[0], buffer.size()*sizeof(VEC3));
	vboPosition.releasePtr();

	vboTexCoord.setDataSize(2);
	vboTexCoord.allocate(bufferTC.size());
	VEC3* ptrTC = reinterpret_cast<VEC3*>(vboTexCoord.lockPtr());
	memcpy(ptrTC, &bufferTC[0], bufferTC.size()*sizeof(VEC2));
	vboTexCoord.releasePtr();

	vboColor.setDataSize(3);
	vboColor.allocate(bufferColors.size());
	VEC3* ptrCol = reinterpret_cast<VEC3*>(vboColor.lockPtr());
	memcpy(ptrCol, &bufferColors[0], bufferColors.size()*sizeof(VEC3));
	vboColor.releasePtr();
}

*/


Sylvain Thery's avatar
Sylvain Thery committed
252
inline void ColorPerFaceRender::draw(Utils::GLSLShader* sh)
253 254
{
	sh->enableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
255
	glDrawArrays(GL_TRIANGLES , 0 , m_nbTris*3 );
256 257 258 259 260 261 262 263 264 265 266 267 268
	sh->disableVertexAttribs();
}




}//end namespace VBO

}//end namespace Algo

}//end namespace Render

}//end namespace CGoGN