importObjTex.hpp 44.4 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

Sylvain Thery's avatar
Sylvain Thery committed
25
#include "Topology/generic/attributeHandler.h"
26
#include "Topology/generic/autoAttributeHandler.h"
Sylvain Thery's avatar
Sylvain Thery committed
27 28
#include "Container/fakeAttribute.h"
#include <fstream>
Sylvain Thery's avatar
Sylvain Thery committed
29
#include <algorithm>
thery's avatar
thery committed
30

Pierre Kraemer's avatar
Pierre Kraemer committed
31 32
namespace CGoGN
{
33

Pierre Kraemer's avatar
Pierre Kraemer committed
34 35
namespace Algo
{
36

37 38
namespace Surface
{
39

Pierre Kraemer's avatar
Pierre Kraemer committed
40 41 42
namespace Import 
{

Sylvain Thery's avatar
Sylvain Thery committed
43 44
template <typename PFP>
OBJModel<PFP>::OBJModel(typename PFP::MAP& map):
Sylvain Thery's avatar
Sylvain Thery committed
45
	m_map(map), m_maxTextureSize(2048),
46 47
	m_tagV(0),m_tagVT(0),m_tagVN(0),m_tagG(0),m_tagF(0),
	m_specialVertices(map),m_dirtyEdges(map)
Sylvain Thery's avatar
Sylvain Thery committed
48 49 50
{
}

Sylvain Thery's avatar
Sylvain Thery committed
51 52 53 54 55 56 57
template <typename PFP>
OBJModel<PFP>::~OBJModel()
{
	for (std::vector<MaterialOBJ*>::iterator it = m_materials.begin(); it != m_materials.end(); ++it)
		delete *it;
}

Sylvain Thery's avatar
Sylvain Thery committed
58 59 60 61 62 63
template <typename PFP>
inline void OBJModel<PFP>::setMaxTextureSize(unsigned int mts)
{
	m_maxTextureSize = mts;
}

Sylvain Thery's avatar
Sylvain Thery committed
64 65 66 67 68
template <typename PFP>
inline typename PFP::VEC3 OBJModel<PFP>::getPosition(Dart d)
{
	return m_positions[d];
}
thery's avatar
thery committed
69

Sylvain Thery's avatar
Sylvain Thery committed
70 71 72 73 74 75 76 77 78 79
template <typename PFP>
inline typename PFP::VEC3 OBJModel<PFP>::getNormal(Dart d)
{
	if (m_specialVertices.isMarked(d))
		return m_normalsF[d];
	return m_normals[d];
}

template <typename PFP>
inline Geom::Vec2f OBJModel<PFP>::getTexCoord(Dart d)
thery's avatar
thery committed
80
{
Sylvain Thery's avatar
Sylvain Thery committed
81 82 83 84 85
	if (m_specialVertices.isMarked(d))
		return m_texCoordsF[d];
	return m_texCoords[d];
}

86
template <typename PFP>
87
void OBJModel<PFP>::setPositionAttribute(VertexAttribute<Geom::Vec3f, typename PFP::MAP> position)
88 89 90 91 92
{
	m_positions = position;
}

template <typename PFP>
93
void OBJModel<PFP>::setNormalAttribute(VertexAttribute<Geom::Vec3f, typename PFP::MAP> normal)
94 95 96 97 98
{
	m_normals = normal;
}

template <typename PFP>
99
void OBJModel<PFP>::setTexCoordAttribute(VertexAttribute<Geom::Vec2f, typename PFP::MAP>texcoord)
100 101 102 103
{
	m_texCoords = texcoord;
}

Sylvain Thery's avatar
Sylvain Thery committed
104
template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
105
void OBJModel<PFP>::readMaterials(const std::string& filename)
Sylvain Thery's avatar
Sylvain Thery committed
106
{
Sylvain Thery's avatar
Sylvain Thery committed
107 108 109 110 111 112 113
	m_materials.reserve(m_materialNames.size());

	if (!filename.empty())
	{
		m_matFileName = filename;
	}

Sylvain Thery's avatar
Sylvain Thery committed
114
	// open file
Sylvain Thery's avatar
Sylvain Thery committed
115
	std::ifstream fp(m_matFileName.c_str());
Sylvain Thery's avatar
Sylvain Thery committed
116 117
	if (!fp.good())
	{
Sylvain Thery's avatar
Sylvain Thery committed
118
		CGoGNerr << "Unable to open file " << m_matFileName << CGoGNendl;
Thery Sylvain's avatar
Thery Sylvain committed
119
		return ;
Sylvain Thery's avatar
Sylvain Thery committed
120 121
	}
	
Sylvain Thery's avatar
Sylvain Thery committed
122 123 124
	MaterialOBJ* currentMat = NULL;

	m_materials.resize(m_materialNames.size(),NULL);
Sylvain Thery's avatar
Sylvain Thery committed
125

Sylvain Thery's avatar
Sylvain Thery committed
126 127 128 129
	std::string ligne;
	do
	{
		std::getline (fp, ligne);
Sylvain Thery's avatar
Sylvain Thery committed
130 131 132
		std::stringstream oss(ligne);
		std::string tag;
		oss >> tag;
Sylvain Thery's avatar
Sylvain Thery committed
133 134
		if (tag == "newmtl")
		{
Sylvain Thery's avatar
Sylvain Thery committed
135 136 137
			std::string name;
			oss >> name ;
			std::map<std::string,int>::iterator it = m_materialNames.find(name);
Sylvain Thery's avatar
Sylvain Thery committed
138 139
			if (it ==  m_materialNames.end())
			{
Sylvain Thery's avatar
Sylvain Thery committed
140
				CGoGNerr << "Skipping material "<< name << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
141 142 143 144 145 146 147 148
				do
				{
					fp >> tag;
					
				}while (!fp.eof() && (tag == "new mtl")); 
			}
			else
			{
Sylvain Thery's avatar
Sylvain Thery committed
149
//				CGoGNout << "Reading material "<< name << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
150 151 152
				currentMat = new MaterialOBJ();
				m_materials[it->second] = currentMat;
				currentMat->name = name;
Sylvain Thery's avatar
Sylvain Thery committed
153 154
			}
		}
Sylvain Thery's avatar
Sylvain Thery committed
155
		else if (currentMat != NULL)
Sylvain Thery's avatar
Sylvain Thery committed
156 157 158
		{
			if (tag == "Ka")
			{
Sylvain Thery's avatar
Sylvain Thery committed
159 160 161
				oss >> currentMat->ambiantColor[0];
				oss >> currentMat->ambiantColor[1];
				oss >> currentMat->ambiantColor[2];
Sylvain Thery's avatar
Sylvain Thery committed
162 163 164
			}
			if (tag == "Kd")
			{
Sylvain Thery's avatar
Sylvain Thery committed
165 166 167 168
				oss >> currentMat->diffuseColor[0];
				oss >> currentMat->diffuseColor[1];
				oss >> currentMat->diffuseColor[2];

Sylvain Thery's avatar
Sylvain Thery committed
169 170 171
			}
			if (tag == "Ks")
			{
Sylvain Thery's avatar
Sylvain Thery committed
172 173 174
				oss >> currentMat->specularColor[0];
				oss >> currentMat->specularColor[1];
				oss >> currentMat->specularColor[2];
Sylvain Thery's avatar
Sylvain Thery committed
175 176 177
			}
			if (tag == "Ns")
			{
Sylvain Thery's avatar
Sylvain Thery committed
178
				oss >> currentMat->shininess;
Sylvain Thery's avatar
Sylvain Thery committed
179
			}
Sylvain Thery's avatar
Sylvain Thery committed
180 181
			if (tag == "Tf")
			{
Sylvain Thery's avatar
Sylvain Thery committed
182 183 184
				oss >> currentMat->transparentFilter[0];
				oss >> currentMat->transparentFilter[1];
				oss >> currentMat->transparentFilter[2];
Sylvain Thery's avatar
Sylvain Thery committed
185 186
			}

Sylvain Thery's avatar
Sylvain Thery committed
187 188 189 190 191 192
			if ((tag == "illum"))
			{
				oss >> currentMat->illuminationModel;
			}


Sylvain Thery's avatar
Sylvain Thery committed
193 194
			if ((tag == "Tr") || (tag == "d"))
			{
Sylvain Thery's avatar
Sylvain Thery committed
195
				oss >> currentMat->transparency;
Sylvain Thery's avatar
Sylvain Thery committed
196
			}
Sylvain Thery's avatar
Sylvain Thery committed
197
			
Sylvain Thery's avatar
Sylvain Thery committed
198
			if (tag == "map_Kd") // diffuse texture
Sylvain Thery's avatar
Sylvain Thery committed
199
			{
Sylvain Thery's avatar
Sylvain Thery committed
200 201 202 203 204 205 206 207 208 209
				if (currentMat->textureDiffuse == NULL)
				{
					currentMat->textureDiffuse = new Utils::Texture<2,Geom::Vec3uc>(GL_UNSIGNED_BYTE);
					std::string buff;
					getline (oss, buff);
					std::string tname = buff.substr(buff.find_first_not_of(' '));
					if (tname[tname.length()-1] == '\r')
						tname = tname.substr(0,tname.length()-1);

					currentMat->textureDiffuse->load(m_matPath+tname);
Sylvain Thery's avatar
Sylvain Thery committed
210
//					CGoGNout << "Loading texture "<< m_matPath+tname << " -> "<<std::hex << currentMat->textureDiffuse <<std::dec<<CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
211
					currentMat->textureDiffuse->scaleNearest( currentMat->textureDiffuse->newMaxSize(m_maxTextureSize));
Sylvain Thery's avatar
Sylvain Thery committed
212 213 214 215
					currentMat->textureDiffuse->setFiltering(GL_LINEAR);
					currentMat->textureDiffuse->setWrapping(GL_REPEAT);
					currentMat->textureDiffuse->update();
				}
Sylvain Thery's avatar
Sylvain Thery committed
216
			}
Sylvain Thery's avatar
Sylvain Thery committed
217 218

			if (tag == "map_Ka") // ambiant texture
Sylvain Thery's avatar
Sylvain Thery committed
219
			{
Sylvain Thery's avatar
Sylvain Thery committed
220 221 222 223 224 225 226 227 228 229 230
//				CGoGNerr << tag << " not yet supported in OBJ material reading" << CGoGNendl;
				if (currentMat->textureDiffuse == NULL)
				{
					currentMat->textureDiffuse = new Utils::Texture<2,Geom::Vec3uc>(GL_UNSIGNED_BYTE);
					std::string buff;
					getline (oss, buff);
					std::string tname = buff.substr(buff.find_first_not_of(' '));
					if (tname[tname.length()-1] == '\r')
						tname = tname.substr(0,tname.length()-1);
					currentMat->textureDiffuse->load(m_matPath+tname);
					CGoGNout << "Loading texture "<< m_matPath+tname << " -> "<<std::hex << currentMat->textureDiffuse <<std::dec<<CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
231
					currentMat->textureDiffuse->scaleNearest( currentMat->textureDiffuse->newMaxSize(m_maxTextureSize));
Sylvain Thery's avatar
Sylvain Thery committed
232 233 234 235
					currentMat->textureDiffuse->setFiltering(GL_LINEAR);
					currentMat->textureDiffuse->setWrapping(GL_REPEAT);
					currentMat->textureDiffuse->update();
				}
Sylvain Thery's avatar
Sylvain Thery committed
236

Sylvain Thery's avatar
Sylvain Thery committed
237
			}
Sylvain Thery's avatar
Sylvain Thery committed
238

Sylvain Thery's avatar
Sylvain Thery committed
239 240 241 242 243 244 245 246
			if (tag == "map_d") // opacity texture
			{
				CGoGNerr << tag << " not yet supported in OBJ material reading" << CGoGNendl;
			}
			if ((tag == "map_bump") || (tag == "bump"))
			{
				CGoGNerr << tag << " not yet supported in OBJ material reading" << CGoGNendl;
			}
Sylvain Thery's avatar
Sylvain Thery committed
247
			tag="";
Sylvain Thery's avatar
Sylvain Thery committed
248 249 250
		}
	}while (!fp.eof());

Sylvain Thery's avatar
Sylvain Thery committed
251 252 253 254 255
	for (std::vector<MaterialOBJ*>::iterator it = m_materials.begin(); it != m_materials.end(); ++it)
	{
		if (*it == NULL)
			CGoGNerr << "Warning missing material in .mtl"<< CGoGNendl;
	}
Sylvain Thery's avatar
Sylvain Thery committed
256

Sylvain Thery's avatar
Sylvain Thery committed
257
}
Sylvain Thery's avatar
Sylvain Thery committed
258 259

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
260
unsigned int OBJModel<PFP>::getMaterialIndex(const std::string& name) const
Sylvain Thery's avatar
Sylvain Thery committed
261
{
Sylvain Thery's avatar
Sylvain Thery committed
262 263 264 265 266 267 268 269 270
	std::map<std::string, int>::iterator it = m_materialNames.find(name);
	if (it != m_materialNames.end())
		return it->second;
	return 0xffffffff;
}

//template <typename PFP>
//bool OBJModel<PFP>::generateBrowsers(std::vector<ContainerBrowser*>& browsers)
//{
271 272 273
//	browsers.clear();
//	if (m_groupNames.empty())
//		return false;
Sylvain Thery's avatar
Sylvain Thery committed
274 275

//	ContainerBrowserLinked* MBLptr = new ContainerBrowserLinked(m_map,DART);
276
//	browsers.push_back(MBLptr);
Sylvain Thery's avatar
Sylvain Thery committed
277

278 279
//	for (unsigned int i = 1; i<m_groupNames.size(); ++i)
//	{
Sylvain Thery's avatar
Sylvain Thery committed
280
//		ContainerBrowser* MBptr = new ContainerBrowserLinked(*MBLptr);
281 282 283
//		browsers.push_back(MBptr);
//		m_groupMaterialID[i]= m_materialNames[m_groupMaterialNames[i]];
//	}
Sylvain Thery's avatar
Sylvain Thery committed
284

285 286 287
//	for (Dart d=m_map.begin(); d!=m_map.end(); m_map.next(d))
//	{
//		unsigned int g = m_groups[d] -1 ; // groups are name from 1
Sylvain Thery's avatar
Sylvain Thery committed
288 289
//		ContainerBrowserLinked* mb = static_cast<ContainerBrowserLinked*>(browsers[g]);
//		mb->pushBack(d.index);
290 291
//	}
//	return true;
Sylvain Thery's avatar
Sylvain Thery committed
292
//}
Sylvain Thery's avatar
Sylvain Thery committed
293 294 295 296 297 298
	
template <typename PFP>
short OBJModel<PFP>::readObjLine(std::stringstream& oss, std::vector<unsigned int>& indices)
{
	indices.clear();
	
thery's avatar
thery committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	unsigned int nb=0;
	while (!oss.eof())  // lecture de tous les indices
	{
		int index;
		oss >> index;

		indices.push_back(index);

		int slash = 0;
		char sep='_';
		do
		{
			oss >> sep;
			if (sep =='/')
				++slash;
		} while ( ((sep=='/') || (sep ==' ')) && !oss.eof() ) ;

		if ((sep>='0') && (sep<='9'))
			oss.seekg(-1,std::ios_base::cur);

		if (slash == 0)
		{
			if (indices.size()%3 == 1)
			{
				indices.push_back(0);
				indices.push_back(0);
			}
			if (indices.size()%3 == 2)
			{
				indices.push_back(0);
			}
			nb++;
		}


		if (slash == 2)
		{
			indices.push_back(0);
		}
	}
	return nb;
}

342 343 344 345 346 347
template <typename PFP>
unsigned int OBJModel<PFP>::createSimpleVBO_P(Utils::VBO* positionVBO)
{
	TraversorF<typename PFP::MAP> traf(m_map);
	std::vector<Geom::Vec3f> posBuff;
	posBuff.reserve(16384);
Pierre Kraemer's avatar
Pierre Kraemer committed
348

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
	unsigned int nbtris = 0;
	for (Dart d=traf.begin(); d!= traf.end(); d = traf.next())
	{
		Dart e = m_map.phi1(d);
		Dart f = m_map.phi1(e);
		do
		{
			posBuff.push_back(m_positions[d]);
			posBuff.push_back(m_positions[e]);
			posBuff.push_back(m_positions[f]);
			e=f;
			f = m_map.phi1(e);
			nbtris++;
		}while (f!=d);
	}

	positionVBO->setDataSize(3);
	positionVBO->allocate(posBuff.size());
	Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
	memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
	positionVBO->releasePtr();

	return 3*nbtris;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
373

374 375 376 377 378 379 380 381 382 383 384
template <typename PFP>
unsigned int OBJModel<PFP>::createSimpleVBO_PT(Utils::VBO* positionVBO, Utils::VBO* texcoordVBO)
{
	TraversorF<typename PFP::MAP> traf(m_map);
	std::vector<Geom::Vec3f> posBuff;
	std::vector<Geom::Vec2f> TCBuff;
	posBuff.reserve(16384);
	TCBuff.reserve(16384);

	unsigned int nbtris = 0;
	for (Dart d=traf.begin(); d!= traf.end(); d = traf.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
385
	{
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
		Dart e = m_map.phi1(d);
		Dart f = m_map.phi1(e);
		do
		{
			posBuff.push_back(m_positions[d]);
			if (m_specialVertices.isMarked(d))
				TCBuff.push_back(m_texCoordsF[d]);
			else
				TCBuff.push_back(m_texCoords[d]);

			posBuff.push_back(m_positions[e]);
			if (m_specialVertices.isMarked(e))
				TCBuff.push_back(m_texCoordsF[e]);
			else
				TCBuff.push_back(m_texCoords[e]);

			posBuff.push_back(m_positions[f]);
			if (m_specialVertices.isMarked(f))
				TCBuff.push_back(m_texCoordsF[f]);
			else
				TCBuff.push_back(m_texCoords[f]);
			e=f;
			f = m_map.phi1(e);
			nbtris++;
		}while (f!=d);
Pierre Kraemer's avatar
Pierre Kraemer committed
411 412
	}

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
	positionVBO->setDataSize(3);
	positionVBO->allocate(posBuff.size());
	Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
	memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
	positionVBO->releasePtr();

	texcoordVBO->setDataSize(2);
	texcoordVBO->allocate(TCBuff.size());
	Geom::Vec2f* ptrTC = reinterpret_cast<Geom::Vec2f*>(texcoordVBO->lockPtr());
	memcpy(ptrTC,&TCBuff[0],TCBuff.size()*sizeof(Geom::Vec2f));
	texcoordVBO->releasePtr();

	return 3*nbtris;
}

Sylvain Thery's avatar
Sylvain Thery committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
template <typename PFP>
unsigned int OBJModel<PFP>::createSimpleVBO_PN(Utils::VBO* positionVBO, Utils::VBO* normalVBO )
{
	TraversorF<typename PFP::MAP> traf(m_map);
	std::vector<Geom::Vec3f> posBuff;
	std::vector<Geom::Vec2f> normalBuff;
	posBuff.reserve(16384);
	normalBuff.reserve(16384);

	unsigned int nbtris = 0;
	for (Dart d=traf.begin(); d!= traf.end(); d = traf.next())
	{
		Dart e = m_map.phi1(d);
		Dart f = m_map.phi1(e);
		do
		{
			posBuff.push_back(m_positions[d]);
			if (m_specialVertices.isMarked(d))
			{
				normalBuff.push_back(m_normalsF[d]);
			}
			else
			{
				normalBuff.push_back(m_normals[d]);
			}

			posBuff.push_back(m_positions[e]);
			if (m_specialVertices.isMarked(e))
			{
				normalBuff.push_back(m_normalsF[e]);
			}
			else
			{
				normalBuff.push_back(m_normals[e]);
			}

			posBuff.push_back(m_positions[f]);
			if (m_specialVertices.isMarked(f))
			{
				normalBuff.push_back(m_normalsF[f]);
			}
			else
			{
				normalBuff.push_back(m_normals[f]);
			}
			e=f;
			f = m_map.phi1(e);
			nbtris++;
		}while (f!=d);
	}

	positionVBO->setDataSize(3);
	positionVBO->allocate(posBuff.size());
	Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
	memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
	positionVBO->releasePtr();

	normalVBO->setDataSize(3);
	normalVBO->allocate(normalBuff.size());
	Geom::Vec3f* ptrNormal = reinterpret_cast<Geom::Vec3f*>(normalVBO->lockPtr());
	memcpy(ptrNormal, &normalBuff[0], normalBuff.size()*sizeof(Geom::Vec3f));
	normalVBO->releasePtr();


	return 3*nbtris;
}

495 496 497
template <typename PFP>
unsigned int OBJModel<PFP>::createSimpleVBO_PTN(Utils::VBO* positionVBO, Utils::VBO* texcoordVBO, Utils::VBO* normalVBO )
{
498 499 500 501 502 503 504 505 506 507 508
	if (!m_normals.isValid())
	{
		CGoGNerr << "no normal attribute "<< CGoGNendl;
		return 0;
	}
	if (!m_texCoords.isValid())
	{
		CGoGNerr << "no tex coords attribute "<< CGoGNendl;
		return 0;
	}

509 510 511
	TraversorF<typename PFP::MAP> traf(m_map);
	std::vector<Geom::Vec3f> posBuff;
	std::vector<Geom::Vec2f> TCBuff;
512
	std::vector<Geom::Vec3f> normalBuff;
513 514 515 516 517 518 519 520 521 522 523 524 525 526
	posBuff.reserve(16384);
	TCBuff.reserve(16384);
	normalBuff.reserve(16384);

	unsigned int nbtris = 0;
	for (Dart d=traf.begin(); d!= traf.end(); d = traf.next())
	{
		Dart e = m_map.phi1(d);
		Dart f = m_map.phi1(e);
		do
		{
			posBuff.push_back(m_positions[d]);
			if (m_specialVertices.isMarked(d))
			{
527 528 529 530 531 532 533 534
				if (hasTexCoords())
					TCBuff.push_back(m_texCoordsF[d]);
				else
					TCBuff.push_back(m_texCoords[d]);
				if (hasNormals())
					normalBuff.push_back(m_normalsF[d]);
				else
					normalBuff.push_back(m_normals[d]);
535 536 537 538 539 540 541 542 543 544
			}
			else
			{
				TCBuff.push_back(m_texCoords[d]);
				normalBuff.push_back(m_normals[d]);
			}

			posBuff.push_back(m_positions[e]);
			if (m_specialVertices.isMarked(e))
			{
545 546 547 548 549 550 551 552
				if (hasTexCoords())
					TCBuff.push_back(m_texCoordsF[e]);
				else
					TCBuff.push_back(m_texCoords[e]);
				if (hasNormals())
					normalBuff.push_back(m_normalsF[e]);
				else
					normalBuff.push_back(m_normals[e]);
553 554 555 556 557 558 559 560 561 562
			}
			else
			{
				TCBuff.push_back(m_texCoords[e]);
				normalBuff.push_back(m_normals[e]);
			}

			posBuff.push_back(m_positions[f]);
			if (m_specialVertices.isMarked(f))
			{
563 564 565 566 567 568 569 570
				if (hasTexCoords())
					TCBuff.push_back(m_texCoordsF[f]);
				else
					TCBuff.push_back(m_texCoords[f]);
				if (hasNormals())
					normalBuff.push_back(m_normalsF[f]);
				else
					normalBuff.push_back(m_normals[f]);
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
			}
			else
			{
				TCBuff.push_back(m_texCoords[f]);
				normalBuff.push_back(m_normals[f]);
			}
			e=f;
			f = m_map.phi1(e);
			nbtris++;
		}while (f!=d);
	}

	positionVBO->setDataSize(3);
	positionVBO->allocate(posBuff.size());
	Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
	memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
	positionVBO->releasePtr();

	texcoordVBO->setDataSize(2);
	texcoordVBO->allocate(TCBuff.size());
	Geom::Vec2f* ptrTC = reinterpret_cast<Geom::Vec2f*>(texcoordVBO->lockPtr());
	memcpy(ptrTC,&TCBuff[0],TCBuff.size()*sizeof(Geom::Vec2f));
	texcoordVBO->releasePtr();

	normalVBO->setDataSize(3);
	normalVBO->allocate(normalBuff.size());
	Geom::Vec3f* ptrNormal = reinterpret_cast<Geom::Vec3f*>(normalVBO->lockPtr());
	memcpy(ptrNormal, &normalBuff[0], normalBuff.size()*sizeof(Geom::Vec3f));
	normalVBO->releasePtr();

	return 3*nbtris;
}

template <typename PFP>
605
bool OBJModel<PFP>::createGroupMatVBO_P( Utils::VBO* positionVBO)
606
{
Sylvain Thery's avatar
Sylvain Thery committed
607 608 609
	m_beginIndices.clear();
	m_nbIndices.clear();

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
	if (!m_normals.isValid())
	{
		CGoGNerr << "no normal attribute "<< CGoGNendl;
		return false;
	}
	if (!m_texCoords.isValid())
	{
		CGoGNerr << "no tex coords attribute "<< CGoGNendl;
		return false;
	}

	std::vector< std::vector<Dart> > group_faces; //(m_materialNames.size());
	group_faces.reserve(16384);
	m_groupIdx.reserve(16384);
	m_sgMat.reserve(16384);

	unsigned int c_sg=0;
	group_faces.resize(1);
Sylvain Thery's avatar
Sylvain Thery committed
628

629
	TraversorF<typename PFP::MAP> traf(m_map);
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
	Dart d=traf.begin();

	unsigned int c_grp = m_groups[d];
	unsigned int c_mat = m_attMat[d];
	m_sgMat.push_back(c_mat);

	m_groupFirstSub.push_back(0);

	if (m_tagG != 0)
		m_groupIdx.push_back(c_grp);
	else
		m_groupIdx.push_back(0);

	while (d!= traf.end())
	{
		if ((m_groups[d] != c_grp) || (m_attMat[d] != c_mat))
		{
			c_sg++;

			if (m_groups[d] != c_grp)
			{
				m_groupNbSub.push_back(c_sg-m_groupFirstSub.back());
				m_groupFirstSub.push_back(c_sg);
			}

			c_grp = m_groups[d];
			c_mat = m_attMat[d];
			m_sgMat.push_back(c_mat);

			if (m_tagG != 0)
				m_groupIdx.push_back(c_grp);
			else
				m_groupIdx.push_back(0);

			group_faces.resize(c_sg+1);
		}

		group_faces[c_sg].push_back(d);
		d = traf.next();
	}

	m_groupNbSub.push_back(c_sg+1-m_groupFirstSub.back()); // nb sub-group of last group

	// merging same material sub-groups of same group
	for (unsigned int g=0; g<m_groupNbSub.size(); ++g)
	{
		unsigned int fsg = m_groupFirstSub[g];
		unsigned int lsg = m_groupFirstSub[g]+m_groupNbSub[g]-1;

		for (unsigned int s=fsg; s<=lsg; ++s)
		{
			if (m_sgMat[s] != 0xffffffff)
			{
				for (unsigned ss=s+1; ss<=lsg; ++ss)
				{
					if (m_sgMat[ss] == m_sgMat[s])
					{
						group_faces[s].insert(group_faces[s].end(),group_faces[ss].begin(),group_faces[ss].end());
						group_faces[ss].clear();
						m_sgMat[ss] = 0xffffffff;
					}
				}
			}
		}
	}

	// compact group_faces/m_groupIdx/m_sgMat
	unsigned int outSg=0;
	for (unsigned int inSg=0; inSg<m_sgMat.size(); ++inSg)
	{
		if (m_sgMat[inSg] != 0xffffffff)
		{
			if (outSg != inSg)
			{
				group_faces[outSg].swap(group_faces[inSg]);
				m_groupIdx[outSg] = m_groupIdx[inSg];
				m_sgMat[outSg] = m_sgMat[inSg];
			}
			outSg++;
		}
	}
	group_faces.resize(outSg);
	m_groupIdx.resize(outSg);
	m_sgMat.resize(outSg);

	// recreate m_groupFirstSub & m_groupNbSub
	unsigned int outGr=0;
	m_groupFirstSub[0] = m_groupIdx[0];
	for (unsigned int inSg=1; inSg<m_sgMat.size(); ++inSg)
Sylvain Thery's avatar
Sylvain Thery committed
719
	{
720 721 722 723 724 725
		if (m_groupIdx[inSg] != m_groupIdx[inSg-1])
		{
			m_groupNbSub[outGr] = inSg - m_groupFirstSub[outGr];
			outGr++;
			m_groupFirstSub[outGr] = inSg;
		}
Sylvain Thery's avatar
Sylvain Thery committed
726
	}
727 728 729 730 731 732
	m_groupNbSub[outGr+1] = m_sgMat.size() - m_groupNbSub[outGr];

	// now create VBOs

	std::vector<Geom::Vec3f> posBuff;
	posBuff.reserve(16384);
Sylvain Thery's avatar
Sylvain Thery committed
733 734 735 736 737 738

	unsigned int firstIndex = 0;

	unsigned int sz = group_faces.size();
	m_beginIndices.resize(sz);
	m_nbIndices.resize(sz);
Sylvain Thery's avatar
Sylvain Thery committed
739
	m_groupIdx.resize(sz);
Sylvain Thery's avatar
Sylvain Thery committed
740 741 742 743 744 745

	for (unsigned int g=0; g<sz; ++g)
	{
		unsigned int nbtris = 0;
		std::vector<Dart>& traf = group_faces[g];

Sylvain Thery's avatar
Sylvain Thery committed
746 747 748 749 750
		if (m_tagG != 0)
			m_groupIdx[g] = m_groups[traf.front()];
		else
			m_groupIdx[g]=0;

Sylvain Thery's avatar
Sylvain Thery committed
751 752 753
		for (std::vector<Dart>::iterator id=traf.begin(); id!= traf.end(); ++id)
		{
			Dart d = *id;
754

Sylvain Thery's avatar
Sylvain Thery committed
755 756 757 758 759
			Dart e = m_map.phi1(d);
			Dart f = m_map.phi1(e);
			do
			{
				posBuff.push_back(m_positions[d]);
760

Sylvain Thery's avatar
Sylvain Thery committed
761
				posBuff.push_back(m_positions[e]);
762

Sylvain Thery's avatar
Sylvain Thery committed
763
				posBuff.push_back(m_positions[f]);
764

Sylvain Thery's avatar
Sylvain Thery committed
765 766 767 768 769 770 771
				e=f;
				f = m_map.phi1(e);
				nbtris++;
			}while (f!=d);
		}
		m_beginIndices[g] = firstIndex;
		m_nbIndices[g] = 3*nbtris;
772

Sylvain Thery's avatar
Sylvain Thery committed
773 774
		firstIndex += 3*nbtris;
	}
775

Sylvain Thery's avatar
Sylvain Thery committed
776 777 778 779 780 781
	positionVBO->setDataSize(3);
	positionVBO->allocate(posBuff.size());
	Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
	memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
	positionVBO->releasePtr();

782 783 784

	// compute BBs ( TO DO: a method ?)
	computeBB(posBuff);
Sylvain Thery's avatar
Sylvain Thery committed
785

Sylvain Thery's avatar
Sylvain Thery committed
786 787 788
	return true;
}

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
//template <typename PFP>
//bool OBJModel<PFP>::createGroupMatVBO_P(Utils::VBO* positionVBO)
//{
//	m_beginIndices.clear();
//	m_nbIndices.clear();

//	std::vector<Geom::Vec3f> posBuff;
//	posBuff.reserve(16384);

//	std::vector< std::vector<Dart> > group_faces(m_materialNames.size());
//	TraversorF<typename PFP::MAP> traf(m_map);
//	for (Dart d=traf.begin(); d!= traf.end(); d = traf.next())
//	{
//		unsigned int g = m_attMat[d];
//		group_faces[g].push_back(d);
//	}

//	unsigned int firstIndex = 0;

//	unsigned int sz = group_faces.size();
//	m_beginIndices.resize(sz);
//	m_nbIndices.resize(sz);
//	m_groupIdx.resize(sz);

//	for (unsigned int g=0; g<sz; ++g)
//	{
//		unsigned int nbtris = 0;
//		std::vector<Dart>& traf = group_faces[g];

//		if (m_tagG != 0)
//			m_groupIdx[g] = m_groups[traf.front()];
//		else
//			m_groupIdx[g]=0;

//		for (std::vector<Dart>::iterator id=traf.begin(); id!= traf.end(); ++id)
//		{
//			Dart d = *id;
//			Dart e = m_map.phi1(d);
//			Dart f = m_map.phi1(e);
//			do
//			{
//				posBuff.push_back(m_positions[d]);
//				posBuff.push_back(m_positions[e]);
//				posBuff.push_back(m_positions[f]);
//				e=f;
//				f = m_map.phi1(e);
//				nbtris++;
//			}while (f!=d);
//		}
//		m_beginIndices[g] = firstIndex;
//		m_nbIndices[g] = 3*nbtris;
//		firstIndex += 3*nbtris;
//	}
//	positionVBO->setDataSize(3);
//	positionVBO->allocate(posBuff.size());
//	Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
//	memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
//	positionVBO->releasePtr();

//	updateGroups();

//	return true;
//}

Sylvain Thery's avatar
Sylvain Thery committed
853
template <typename PFP>
854 855
bool OBJModel<PFP>::createGroupMatVBO_PT( Utils::VBO* positionVBO,
											Utils::VBO* texcoordVBO)
Sylvain Thery's avatar
Sylvain Thery committed
856 857 858 859
{
	m_beginIndices.clear();
	m_nbIndices.clear();

860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
	if (!m_normals.isValid())
	{
		CGoGNerr << "no normal attribute "<< CGoGNendl;
		return false;
	}
	if (!m_texCoords.isValid())
	{
		CGoGNerr << "no tex coords attribute "<< CGoGNendl;
		return false;
	}

	std::vector< std::vector<Dart> > group_faces; //(m_materialNames.size());
	group_faces.reserve(16384);
	m_groupIdx.reserve(16384);
	m_sgMat.reserve(16384);

	unsigned int c_sg=0;
	group_faces.resize(1);

	TraversorF<typename PFP::MAP> traf(m_map);
	Dart d=traf.begin();

	unsigned int c_grp = m_groups[d];
	unsigned int c_mat = m_attMat[d];
	m_sgMat.push_back(c_mat);

	m_groupFirstSub.push_back(0);

	if (m_tagG != 0)
		m_groupIdx.push_back(c_grp);
	else
		m_groupIdx.push_back(0);

	while (d!= traf.end())
	{
		if ((m_groups[d] != c_grp) || (m_attMat[d] != c_mat))
		{
			c_sg++;

			if (m_groups[d] != c_grp)
			{
				m_groupNbSub.push_back(c_sg-m_groupFirstSub.back());
				m_groupFirstSub.push_back(c_sg);
			}

			c_grp = m_groups[d];
			c_mat = m_attMat[d];
			m_sgMat.push_back(c_mat);

			if (m_tagG != 0)
				m_groupIdx.push_back(c_grp);
			else
				m_groupIdx.push_back(0);

			group_faces.resize(c_sg+1);
		}

		group_faces[c_sg].push_back(d);
		d = traf.next();
	}

	m_groupNbSub.push_back(c_sg+1-m_groupFirstSub.back()); // nb sub-group of last group

	// merging same material sub-groups of same group
	for (unsigned int g=0; g<m_groupNbSub.size(); ++g)
	{
		unsigned int fsg = m_groupFirstSub[g];
		unsigned int lsg = m_groupFirstSub[g]+m_groupNbSub[g]-1;

		for (unsigned int s=fsg; s<=lsg; ++s)
		{
			if (m_sgMat[s] != 0xffffffff)
			{
				for (unsigned ss=s+1; ss<=lsg; ++ss)
				{
					if (m_sgMat[ss] == m_sgMat[s])
					{
						group_faces[s].insert(group_faces[s].end(),group_faces[ss].begin(),group_faces[ss].end());
						group_faces[ss].clear();
						m_sgMat[ss] = 0xffffffff;
					}
				}
			}
		}
	}

	// compact group_faces/m_groupIdx/m_sgMat
	unsigned int outSg=0;
	for (unsigned int inSg=0; inSg<m_sgMat.size(); ++inSg)
	{
		if (m_sgMat[inSg] != 0xffffffff)
		{
			if (outSg != inSg)
			{
				group_faces[outSg].swap(group_faces[inSg]);
				m_groupIdx[outSg] = m_groupIdx[inSg];
				m_sgMat[outSg] = m_sgMat[inSg];
			}
			outSg++;
		}
	}
	group_faces.resize(outSg);
	m_groupIdx.resize(outSg);
	m_sgMat.resize(outSg);

	// recreate m_groupFirstSub & m_groupNbSub
	unsigned int outGr=0;
	m_groupFirstSub[0] = m_groupIdx[0];
	for (unsigned int inSg=1; inSg<m_sgMat.size(); ++inSg)
	{
		if (m_groupIdx[inSg] != m_groupIdx[inSg-1])
		{
			m_groupNbSub[outGr] = inSg - m_groupFirstSub[outGr];
			outGr++;
			m_groupFirstSub[outGr] = inSg;
		}
	}
	m_groupNbSub[outGr+1] = m_sgMat.size() - m_groupNbSub[outGr];

	// now create VBOs

Sylvain Thery's avatar
Sylvain Thery committed
981 982 983 984 985 986 987 988 989 990
	std::vector<Geom::Vec3f> posBuff;
	std::vector<Geom::Vec2f> TCBuff;
	posBuff.reserve(16384);
	TCBuff.reserve(16384);

	unsigned int firstIndex = 0;

	unsigned int sz = group_faces.size();
	m_beginIndices.resize(sz);
	m_nbIndices.resize(sz);
Sylvain Thery's avatar
Sylvain Thery committed
991
	m_groupIdx.resize(sz);
Sylvain Thery's avatar
Sylvain Thery committed
992 993 994 995 996 997

	for (unsigned int g=0; g<sz; ++g)
	{
		unsigned int nbtris = 0;
		std::vector<Dart>& traf = group_faces[g];

Sylvain Thery's avatar
Sylvain Thery committed
998 999 1000 1001 1002
		if (m_tagG != 0)
			m_groupIdx[g] = m_groups[traf.front()];
		else
			m_groupIdx[g]=0;

Sylvain Thery's avatar
Sylvain Thery committed
1003 1004 1005 1006 1007 1008 1009 1010 1011
		for (std::vector<Dart>::iterator id=traf.begin(); id!= traf.end(); ++id)
		{
			Dart d = *id;

			Dart e = m_map.phi1(d);
			Dart f = m_map.phi1(e);
			do
			{
				posBuff.push_back(m_positions[d]);
1012 1013

				if (hasTexCoords())
Sylvain Thery's avatar
Sylvain Thery committed
1014 1015 1016 1017
					TCBuff.push_back(m_texCoordsF[d]);
				else
					TCBuff.push_back(m_texCoords[d]);

1018

Sylvain Thery's avatar
Sylvain Thery committed
1019
				posBuff.push_back(m_positions[e]);
1020 1021

				if (hasTexCoords())
Sylvain Thery's avatar
Sylvain Thery committed
1022 1023 1024 1025
					TCBuff.push_back(m_texCoordsF[e]);
				else
					TCBuff.push_back(m_texCoords[e]);

1026

Sylvain Thery's avatar
Sylvain Thery committed
1027
				posBuff.push_back(m_positions[f]);
1028 1029

				if (hasTexCoords())
Sylvain Thery's avatar
Sylvain Thery committed
1030 1031 1032
					TCBuff.push_back(m_texCoordsF[f]);
				else
					TCBuff.push_back(m_texCoords[f]);
1033 1034


Sylvain Thery's avatar
Sylvain Thery committed
1035 1036 1037 1038 1039 1040 1041
				e=f;
				f = m_map.phi1(e);
				nbtris++;
			}while (f!=d);
		}
		m_beginIndices[g] = firstIndex;
		m_nbIndices[g] = 3*nbtris;
1042

Sylvain Thery's avatar
Sylvain Thery committed
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
		firstIndex += 3*nbtris;
	}

	positionVBO->setDataSize(3);
	positionVBO->allocate(posBuff.size());
	Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
	memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
	positionVBO->releasePtr();

	texcoordVBO->setDataSize(2);
	texcoordVBO->allocate(TCBuff.size());
	Geom::Vec2f* ptrTC = reinterpret_cast<Geom::Vec2f*>(texcoordVBO->lockPtr());
	memcpy(ptrTC,&TCBuff[0],TCBuff.size()*sizeof(Geom::Vec2f));
	texcoordVBO->releasePtr();

1058 1059
	// compute BBs ( TO DO: a method ?)
	computeBB(posBuff);
Sylvain Thery's avatar
Sylvain Thery committed
1060

Sylvain Thery's avatar
Sylvain Thery committed
1061 1062 1063 1064
	return true;
}

template <typename PFP>
1065 1066
bool OBJModel<PFP>::createGroupMatVBO_PN( Utils::VBO* positionVBO,
											Utils::VBO* normalVBO)
Sylvain Thery's avatar
Sylvain Thery committed
1067 1068 1069 1070
{
	m_beginIndices.clear();
	m_nbIndices.clear();

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	if (!m_normals.isValid())
	{
		CGoGNerr << "no normal attribute "<< CGoGNendl;
		return false;
	}

	std::vector< std::vector<Dart> > group_faces; //(m_materialNames.size());
	group_faces.reserve(16384);
	m_groupIdx.reserve(16384);
	m_sgMat.reserve(16384);

	unsigned int c_sg=0;
	group_faces.resize(1);
1084

Sylvain Thery's avatar
Sylvain Thery committed
1085
	TraversorF<typename PFP::MAP> traf(m_map);
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
	Dart d=traf.begin();

	unsigned int c_grp = m_groups[d];
	unsigned int c_mat = m_attMat[d];
	m_sgMat.push_back(c_mat);

	m_groupFirstSub.push_back(0);

	if (m_tagG != 0)
		m_groupIdx.push_back(c_grp);
	else
		m_groupIdx.push_back(0);

	while (d!= traf.end())
	{
		if ((m_groups[d] != c_grp) || (m_attMat[d] != c_mat))
		{
			c_sg++;

			if (m_groups[d] != c_grp)
			{
				m_groupNbSub.push_back(c_sg-m_groupFirstSub.back());
				m_groupFirstSub.push_back(c_sg);
			}

			c_grp = m_groups[d];
			c_mat = m_attMat[d];
			m_sgMat.push_back(c_mat);

			if (m_tagG != 0)
				m_groupIdx.push_back(c_grp);
			else
				m_groupIdx.push_back(0);

			group_faces.resize(c_sg+1);
		}

		group_faces[c_sg].push_back(d);
		d = traf.next();
	}

	m_groupNbSub.push_back(c_sg+1-m_groupFirstSub.back()); // nb sub-group of last group

	// merging same material sub-groups of same group
	for (unsigned int g=0; g<m_groupNbSub.size(); ++g)
	{
		unsigned int fsg = m_groupFirstSub[g];
		unsigned int lsg = m_groupFirstSub[g]+m_groupNbSub[g]-1;

		for (unsigned int s=fsg; s<=lsg; ++s)
		{
			if (m_sgMat[s] != 0xffffffff)
			{
				for (unsigned ss=s+1; ss<=lsg; ++ss)
				{
					if (m_sgMat[ss] == m_sgMat[s])
					{
						group_faces[s].insert(group_faces[s].end(),group_faces[ss].begin(),group_faces[ss].end());
						group_faces[ss].clear();
						m_sgMat[ss] = 0xffffffff;
					}
				}
			}
		}
	}

	// compact group_faces/m_groupIdx/m_sgMat
	unsigned int outSg=0;
	for (unsigned int inSg=0; inSg<m_sgMat.size(); ++inSg)
1155
	{
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
		if (m_sgMat[inSg] != 0xffffffff)
		{
			if (outSg != inSg)
			{
				group_faces[outSg].swap(group_faces[inSg]);
				m_groupIdx[outSg] = m_groupIdx[inSg];
				m_sgMat[outSg] = m_sgMat[inSg];
			}
			outSg++;
		}
	}
	group_faces.resize(outSg);
	m_groupIdx.resize(outSg);
	m_sgMat.resize(outSg);

	// recreate m_groupFirstSub & m_groupNbSub
	unsigned int outGr=0;
	m_groupFirstSub[0] = m_groupIdx[0];
	for (unsigned int inSg=1; inSg<m_sgMat.size(); ++inSg)
	{
		if (m_groupIdx[inSg] != m_groupIdx[inSg-1])
		{
			m_groupNbSub[outGr] = inSg - m_groupFirstSub[outGr];
			outGr++;
			m_groupFirstSub[outGr] = inSg;
		}
Sylvain Thery's avatar
Sylvain Thery committed
1182
	}
1183 1184 1185 1186 1187 1188 1189 1190 1191
	m_groupNbSub[outGr+1] = m_sgMat.size() - m_groupNbSub[outGr];

	// now create VBOs

	std::vector<Geom::Vec3f> posBuff;
	std::vector<Geom::Vec3f> normalBuff;
	posBuff.reserve(16384);
	normalBuff.reserve(16384);

Sylvain Thery's avatar
Sylvain Thery committed
1192 1193 1194 1195 1196 1197

	unsigned int firstIndex = 0;

	unsigned int sz = group_faces.size();
	m_beginIndices.resize(sz);
	m_nbIndices.resize(sz);
Sylvain Thery's avatar
Sylvain Thery committed
1198
	m_groupIdx.resize(sz);
Sylvain Thery's avatar
Sylvain Thery committed
1199 1200 1201 1202 1203 1204

	for (unsigned int g=0; g<sz; ++g)
	{
		unsigned int nbtris = 0;
		std::vector<Dart>& traf = group_faces[g];

Sylvain Thery's avatar
Sylvain Thery committed
1205 1206 1207 1208 1209
		if (m_tagG != 0)
			m_groupIdx[g] = m_groups[traf.front()];
		else
			m_groupIdx[g]=0;

Sylvain Thery's avatar
Sylvain Thery committed
1210
		for (std::vector<Dart>::iterator id=traf.begin(); id!= traf.end(); ++id)
1211
		{
Sylvain Thery's avatar
Sylvain Thery committed
1212
			Dart d = *id;
1213

Sylvain Thery's avatar
Sylvain Thery committed
1214 1215 1216
			Dart e = m_map.phi1(d);
			Dart f = m_map.phi1(e);
			do
1217
			{
Sylvain Thery's avatar
Sylvain Thery committed
1218
				posBuff.push_back(m_positions[d]);
1219 1220

				if (hasNormals())
Sylvain Thery's avatar
Sylvain Thery committed
1221 1222 1223
					normalBuff.push_back(m_normalsF[d]);
				else
					normalBuff.push_back(m_normals[d]);
1224

1225

Sylvain Thery's avatar
Sylvain Thery committed
1226
				posBuff.push_back(m_positions[e]);
1227 1228

				if (hasNormals())
Sylvain Thery's avatar
Sylvain Thery committed
1229 1230 1231
					normalBuff.push_back(m_normalsF[e]);
				else
					normalBuff.push_back(m_normals[e]);
1232

Sylvain Thery's avatar
Sylvain Thery committed
1233 1234

				posBuff.push_back(m_positions[f]);
1235 1236

				if (hasNormals())
Sylvain Thery's avatar
Sylvain Thery committed
1237 1238 1239
					normalBuff.push_back(m_normalsF[f]);
				else
					normalBuff.push_back(m_normals[f]);
1240 1241


Sylvain Thery's avatar
Sylvain Thery committed
1242 1243 1244 1245 1246 1247 1248
				e=f;
				f = m_map.phi1(e);
				nbtris++;
			}while (f!=d);
		}
		m_beginIndices[g] = firstIndex;
		m_nbIndices[g] = 3*nbtris;
1249

Sylvain Thery's avatar
Sylvain Thery committed
1250
		firstIndex += 3*nbtris;
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
	}

	positionVBO->setDataSize(3);
	positionVBO->allocate(posBuff.size());
	Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
	memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
	positionVBO->releasePtr();

	normalVBO->setDataSize(3);
	normalVBO->allocate(normalBuff.size());
	Geom::Vec3f* ptrNormal = reinterpret_cast<Geom::Vec3f*>(normalVBO->lockPtr());
	memcpy(ptrNormal, &normalBuff[0], normalBuff.size()*sizeof(Geom::Vec3f));
	normalVBO->releasePtr();

1265 1266
	// compute BBs ( TO DO: a method ?)
	computeBB(posBuff);
Sylvain Thery's avatar
Sylvain Thery committed
1267

Sylvain Thery's avatar
Sylvain Thery committed
1268 1269 1270 1271
	return true;
}

template <typename PFP>
1272 1273 1274 1275
bool OBJModel<PFP>::createGroupMatVBO_PTN(
	Utils::VBO* positionVBO,
	Utils::VBO* texcoordVBO,
	Utils::VBO* normalVBO)
Sylvain Thery's avatar
Sylvain Thery committed
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
{
	m_beginIndices.clear();
	m_nbIndices.clear();

	if (!m_normals.isValid())
	{
		CGoGNerr << "no normal attribute "<< CGoGNendl;
		return false;
	}
	if (!m_texCoords.isValid())
	{
		CGoGNerr << "no tex coords attribute "<< CGoGNendl;
		return false;
	}

1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
	std::vector< std::vector<Dart> > group_faces; //(m_materialNames.size());
	group_faces.reserve(16384);
	m_groupIdx.reserve(16384);
	m_sgMat.reserve(16384);

	unsigned int c_sg=0;
	group_faces.resize(1);

	TraversorF<typename PFP::MAP> traf(m_map);
	Dart d=traf.begin();

	unsigned int c_grp = m_groups[d];
	unsigned int c_mat = m_attMat[d];
	m_sgMat.push_back(c_mat);

	m_groupFirstSub.push_back(0);

	if (m_tagG != 0)
		m_groupIdx.push_back(c_grp);
	else
		m_groupIdx.push_back(0);

	while (d!= traf.end())
	{
		if ((m_groups[d] != c_grp) || (m_attMat[d] != c_mat))
		{
			c_sg++;

			if (m_groups[d] != c_grp)
			{
				m_groupNbSub.push_back(c_sg-m_groupFirstSub.back());
				m_groupFirstSub.push_back(c_sg);
			}

			c_grp = m_groups[d];
			c_mat = m_attMat[d];
			m_sgMat.push_back(c_mat);

			if (m_tagG != 0)
				m_groupIdx.push_back(c_grp);
			else
				m_groupIdx.push_back(0);

			group_faces.resize(c_sg+1);
		}

		group_faces[c_sg].push_back(d);
		d = traf.next();
	}

	m_groupNbSub.push_back(c_sg+1-m_groupFirstSub.back()); // nb sub-group of last group

	// merging same material sub-groups of same group
	for (unsigned int g=0; g<m_groupNbSub.size(); ++g)
	{
		unsigned int fsg = m_groupFirstSub[g];
		unsigned int lsg = m_groupFirstSub[g]+m_groupNbSub[g]-1;

		for (unsigned int s=fsg; s<=lsg; ++s)
		{
			if (m_sgMat[s] != 0xffffffff)
			{
				for (unsigned ss=s+1; ss<=lsg; ++ss)
				{
					if (m_sgMat[ss] == m_sgMat[s])
					{
						group_faces[s].insert(group_faces[s].end(),group_faces[ss].begin(),group_faces[ss].end());
						group_faces[ss].clear();
						m_sgMat[ss] = 0xffffffff;
					}
				}
			}
		}
	}

	// compact group_faces/m_groupIdx/m_sgMat
	unsigned int outSg=0;
	for (unsigned int inSg=0; inSg<m_sgMat.size(); ++inSg)
	{
		if (m_sgMat[inSg] != 0xffffffff)
		{
			if (outSg != inSg)
			{
				group_faces[outSg].swap(group_faces[inSg]);
				m_groupIdx[outSg] = m_groupIdx[inSg];
				m_sgMat[outSg] = m_sgMat[inSg];
			}
			outSg++;
		}
	}
	group_faces.resize(outSg);
	m_groupIdx.resize(outSg);
	m_sgMat.resize(outSg);

	// recreate m_groupFirstSub & m_groupNbSub
	unsigned int outGr=0;
	m_groupFirstSub[0] = m_groupIdx[0];
	for (unsigned int inSg=1; inSg<m_sgMat.size(); ++inSg)
	{
		if (m_groupIdx[inSg] != m_groupIdx[inSg-1])
		{
			m_groupNbSub[outGr] = inSg - m_groupFirstSub[outGr];
			outGr++;
			m_groupFirstSub[outGr] = inSg;
		}
	}
	m_groupNbSub[outGr+1] = m_sgMat.size() - m_groupNbSub[outGr];

	// now create VBOs

Sylvain Thery's avatar
Sylvain Thery committed
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
	std::vector<Geom::Vec3f> posBuff;
	std::vector<Geom::Vec2f> TCBuff;
	std::vector<Geom::Vec3f> normalBuff;
	posBuff.reserve(16384);
	TCBuff.reserve(16384);
	normalBuff.reserve(16384);

	unsigned int firstIndex = 0;

	unsigned int sz = group_faces.size();
	m_beginIndices.resize(sz);
	m_nbIndices.resize(sz);
Sylvain Thery's avatar
Sylvain Thery committed
1413
	m_groupIdx.resize(sz);
Sylvain Thery's avatar
Sylvain Thery committed
1414 1415 1416 1417 1418 1419

	for (unsigned int g=0; g<sz; ++g)
	{
		unsigned int nbtris = 0;
		std::vector<Dart>& traf = group_faces[g];

Sylvain Thery's avatar
Sylvain Thery committed
1420 1421 1422 1423 1424
		if (m_tagG != 0)
			m_groupIdx[g] = m_groups[traf.front()];
		else
			m_groupIdx[g]=0;

Sylvain Thery's avatar
Sylvain Thery committed
1425 1426 1427 1428 1429 1430 1431 1432 1433
		for (std::vector<Dart>::iterator id=traf.begin(); id!= traf.end(); ++id)
		{
			Dart d = *id;

			Dart e = m_map.phi1(d);
			Dart f = m_map.phi1(e);
			do
			{
				posBuff.push_back(m_positions[d]);
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443

				if (hasTexCoords())
					TCBuff.push_back(m_texCoordsF[d]);
				else
					TCBuff.push_back(m_texCoords[d]);
				if (hasNormals())
					normalBuff.push_back(m_normalsF[d]);
				else
					normalBuff.push_back(m_normals[d]);

Sylvain Thery's avatar
Sylvain Thery committed
1444 1445

				posBuff.push_back(m_positions[e]);
1446 1447 1448 1449 1450 1451 1452 1453 1454 1455

				if (hasTexCoords())
					TCBuff.push_back(m_texCoordsF[e]);
				else
					TCBuff.push_back(m_texCoords[e]);
				if (hasNormals())
					normalBuff.push_back(m_normalsF[e]);
				else
					normalBuff.push_back(m_normals[e]);

Sylvain Thery's avatar
Sylvain Thery committed
1456 1457

				posBuff.push_back(m_positions[f]);
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467

				if (hasTexCoords())
					TCBuff.push_back(m_texCoordsF[f]);
				else
					TCBuff.push_back(m_texCoords[f]);
				if (hasNormals())
					normalBuff.push_back(m_normalsF[f]);
				else
					normalBuff.push_back(m_normals[f]);

Sylvain Thery's avatar
Sylvain Thery committed
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497

				e=f;
				f = m_map.phi1(e);
				nbtris++;
			}while (f!=d);
		}
		m_beginIndices[g] = firstIndex;
		m_nbIndices[g] = 3*nbtris;

		firstIndex += 3*nbtris;
	}

	positionVBO->setDataSize(3);
	positionVBO->allocate(posBuff.size());
	Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
	memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
	positionVBO->releasePtr();

	texcoordVBO->setDataSize(2);
	texcoordVBO->allocate(TCBuff.size());
	Geom::Vec2f* ptrTC = reinterpret_cast<Geom::Vec2f*>(texcoordVBO->lockPtr());
	memcpy(ptrTC,&TCBuff[0],TCBuff.size()*sizeof(Geom::Vec2f));
	texcoordVBO->releasePtr();

	normalVBO->setDataSize(3);
	normalVBO->allocate(normalBuff.size());
	Geom::Vec3f* ptrNormal = reinterpret_cast<Geom::Vec3f*>(normalVBO->lockPtr());
	memcpy(ptrNormal, &normalBuff[0], normalBuff.size()*sizeof(Geom::Vec3f));
	normalVBO->releasePtr();

1498 1499 1500

	// compute BBs ( TO DO: a method ?)
	computeBB(posBuff);
Sylvain Thery's avatar
Sylvain Thery committed
1501

Sylvain Thery's avatar
Sylvain Thery committed
1502
	return true;
1503 1504
}

Sylvain Thery's avatar
Sylvain Thery committed
1505
template <typename PFP>
1506
void OBJModel<PFP>::computeBB(const std::vector<Geom::Vec3f>& pos)
Sylvain Thery's avatar
Sylvain Thery committed
1507
{
1508 1509 1510
	m_groupBBs.resize(nbObjGroups());

	for (unsigned int i=0; i<nbObjGroups(); ++i )
Sylvain Thery's avatar
Sylvain Thery committed
1511
	{
1512 1513
		Geom::BoundingBox<VEC3>& bb = m_groupBBs[i];
		bb.reset();
Sylvain Thery's avatar
Sylvain Thery committed
1514

1515 1516 1517 1518 1519 1520
		unsigned int begInd = beginIndex(i,0);
		unsigned int endInd = begInd;
		for (unsigned int j= 0; j< nbSubGroup(i); ++j)
			endInd += nbIndices(i,j);

		for (unsigned int j=begInd; j<endInd; ++j )
Sylvain Thery's avatar
Sylvain Thery committed
1521
		{
1522
			bb.addPoint(pos[j]);
Sylvain Thery's avatar
Sylvain Thery committed
1523
		}
1524
	}
Sylvain Thery's avatar
Sylvain Thery committed
1525 1526
}

thery's avatar
thery committed
1527
template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
1528
bool OBJModel<PFP>::import( const std::string& filename, std::vector<std::string>& attrNames)
thery's avatar
thery committed
1529 1530 1531
{
	attrNames.clear();
	// open file
Sylvain Thery's avatar
Sylvain Thery committed
1532
	std::ifstream fp(filename.c_str()/*, std::ios::binary*/);
thery's avatar
thery committed
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
	if (!fp.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
		return false;
	}

	std::string ligne;
	std::string tag;
	do
	{
		std::getline (fp, ligne);
Sylvain Thery's avatar
Sylvain Thery committed
1544 1545
		std::stringstream oss(ligne);
		oss >> tag;
thery's avatar
thery committed
1546
		if (tag == "v")
1547
			m_tagV++;
thery's avatar
thery committed
1548
		if (tag == "vn")
1549
			m_tagVN++;
thery's avatar
thery committed
1550
		if (tag == "vt")
1551
			m_tagVT++;
Sylvain Thery's avatar
Sylvain Thery committed
1552
		if ((tag == "g") || (tag == "o"))
1553
			m_tagG++;
thery's avatar
thery committed
1554
		if (tag == "f")
1555
			m_tagF++;
Sylvain Thery's avatar
Sylvain Thery committed
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566

		if (tag == "mtllib")
		{
			unsigned found = filename.find_last_of("/\\");
			std::string mtfn;
			oss >> mtfn;
			m_matPath = filename.substr(0,found) + "/";
			m_matFileName = m_matPath + mtfn;
		}
		tag.clear();

1567
	} while (!fp.eof());
thery's avatar
thery committed
1568

1569
	m_positions =  m_map.template getAttribute<VEC3, VERTEX, MAP>("position") ;
Sylvain Thery's avatar
Sylvain Thery committed
1570
	if (!m_positions.isValid())
1571
		m_positions = m_map.template addAttribute<VEC3, VERTEX, MAP>("position") ;
Sylvain Thery's avatar
Sylvain Thery committed
1572
	attrNames.push_back(m_positions.name()) ;
thery's avatar
thery committed
1573

1574
	m_texCoords =  m_map.template getAttribute<VEC2, VERTEX, MAP>("texCoord") ;
Sylvain Thery's avatar
Sylvain Thery committed
1575
	if (!m_texCoords.isValid())
1576
		m_texCoords = m_map.template addAttribute<VEC2, VERTEX, MAP>("texCoord") ;
Sylvain Thery's avatar
Sylvain Thery committed
1577 1578
	attrNames.push_back(m_texCoords.name()) ;

1579
	if (m_tagVT != 0)
thery's avatar
thery committed
1580
	{
1581
		m_texCoordsF =  m_map.template getAttribute<VEC2, VERTEX1, MAP>("texCoordF") ;
Sylvain Thery's avatar
Sylvain Thery committed
1582
		if (!m_texCoordsF.isValid())
1583
			m_texCoordsF = m_map.template addAttribute<VEC2, VERTEX1, MAP>("texCoordF") ;
thery's avatar
thery committed
1584 1585
	}

1586
	m_normals =  m_map.template getAttribute<VEC3, VERTEX, MAP>("normal") ;
Sylvain Thery's avatar
Sylvain Thery committed
1587
	if (!m_normals.isValid())
1588
		m_normals = m_map.template addAttribute<VEC3, VERTEX, MAP>("normal") ;
Sylvain Thery's avatar
Sylvain Thery committed
1589 1590
	attrNames.push_back(m_normals.name()) ;

1591
	if (m_tagVN != 0)
thery's avatar
thery committed
1592
	{
1593
		m_normalsF =  m_map.template getAttribute<VEC3, VERTEX1, MAP>("normalF") ;
Sylvain Thery's avatar
Sylvain Thery committed
1594
		if (!m_normalsF.isValid())
1595
			m_normalsF = m_map.template addAttribute<VEC3, VERTEX1, MAP>("normalF") ;
thery's avatar
thery committed
1596
	}
Sylvain Thery's avatar
Sylvain Thery committed
1597
	
Sylvain Thery's avatar
Sylvain Thery committed
1598
//	if (m_tagG != 0) always use group even if not in the file
Sylvain Thery's avatar
Sylvain Thery committed
1599
	{
1600
		m_groups =  m_map.template getAttribute<unsigned int, FACE, MAP>("groups") ;
Sylvain Thery's avatar
Sylvain Thery committed
1601
		if (!m_groups.isValid())
1602
			m_groups = m_map.template addAttribute<unsigned int, FACE, MAP>("groups") ;
1603
		attrNames.push_back(m_groups.name()) ;
Sylvain Thery's avatar
Sylvain Thery committed
1604 1605
	}
	
1606
	m_attMat =  m_map.template getAttribute<unsigned int, FACE, MAP>("material") ;
Sylvain Thery's avatar
Sylvain Thery committed
1607
	if (!m_attMat.isValid())
1608
		m_attMat = m_map.template addAttribute<unsigned int, FACE, MAP>("material") ;
Sylvain Thery's avatar
Sylvain Thery committed
1609
	attrNames.push_back(m_attMat.name()) ;
thery's avatar
thery committed
1610 1611 1612 1613 1614 1615 1616 1617

	AttributeContainer& container = m_map.template getAttributeContainer<VERTEX>() ;

	fp.close();
	fp.clear();
	fp.open(filename.c_str());

	std::vector<VEC3> normalsBuffer;
1618
	normalsBuffer.reserve(m_tagVN);
Sylvain Thery's avatar
Sylvain Thery committed
1619
	
thery's avatar
thery committed
1620
	std::vector<VEC2> texCoordsBuffer;
1621
	texCoordsBuffer.reserve(m_tagVT);
Sylvain Thery's avatar
Sylvain Thery committed
1622
	
thery's avatar
thery committed
1623
	std::vector<unsigned int> verticesID;
1624
	verticesID.reserve(m_tagV);
Sylvain Thery's avatar
Sylvain Thery committed
1625 1626
	
	std::vector<unsigned int> normalsID;
1627
	normalsID.reserve(m_tagV);
thery's avatar