importObjTex.hpp 34.1 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 33 34 35

namespace CGoGN
{
namespace Algo
{
36 37
namespace Surface
{
Pierre Kraemer's avatar
Pierre Kraemer committed
38 39 40
namespace Import 
{

Sylvain Thery's avatar
Sylvain Thery committed
41

Sylvain Thery's avatar
Sylvain Thery committed
42 43
template <typename PFP>
OBJModel<PFP>::OBJModel(typename PFP::MAP& map):
Sylvain Thery's avatar
Sylvain Thery committed
44
	m_map(map), m_maxTextureSize(2048),
45 46
	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
47 48 49
{
}

Sylvain Thery's avatar
Sylvain Thery committed
50 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 64 65 66 67



template <typename PFP>
inline void OBJModel<PFP>::setMaxTextureSize(unsigned int mts)
{
	m_maxTextureSize = mts;
}


Sylvain Thery's avatar
Sylvain Thery committed
68 69 70 71 72
template <typename PFP>
inline typename PFP::VEC3 OBJModel<PFP>::getPosition(Dart d)
{
	return m_positions[d];
}
thery's avatar
thery committed
73

Sylvain Thery's avatar
Sylvain Thery committed
74

Sylvain Thery's avatar
Sylvain Thery committed
75

Sylvain Thery's avatar
Sylvain Thery committed
76 77 78 79 80 81 82 83 84 85 86
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
87
{
Sylvain Thery's avatar
Sylvain Thery committed
88 89 90 91 92 93
	if (m_specialVertices.isMarked(d))
		return m_texCoordsF[d];
	return m_texCoords[d];
}


94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
template <typename PFP>
void OBJModel<PFP>::setPositionAttribute(VertexAttribute<Geom::Vec3f> position)
{
	m_positions = position;
}

template <typename PFP>
void OBJModel<PFP>::setNormalAttribute(VertexAttribute<Geom::Vec3f> normal)
{
	m_normals = normal;
}

template <typename PFP>
void OBJModel<PFP>::setTexCoordAttribute(VertexAttribute<Geom::Vec2f>texcoord)
{
	m_texCoords = texcoord;
}


Sylvain Thery's avatar
Sylvain Thery committed
113 114


Sylvain Thery's avatar
Sylvain Thery committed
115 116 117



Sylvain Thery's avatar
Sylvain Thery committed
118
template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
119
void OBJModel<PFP>::readMaterials(const std::string& filename)
Sylvain Thery's avatar
Sylvain Thery committed
120
{
Sylvain Thery's avatar
Sylvain Thery committed
121 122 123 124 125 126 127
	m_materials.reserve(m_materialNames.size());

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

Sylvain Thery's avatar
Sylvain Thery committed
128
	// open file
Sylvain Thery's avatar
Sylvain Thery committed
129
	std::ifstream fp(m_matFileName.c_str());
Sylvain Thery's avatar
Sylvain Thery committed
130 131
	if (!fp.good())
	{
Sylvain Thery's avatar
Sylvain Thery committed
132
		CGoGNerr << "Unable to open file " << m_matFileName << CGoGNendl;
Thery Sylvain's avatar
Thery Sylvain committed
133
		return ;
Sylvain Thery's avatar
Sylvain Thery committed
134 135
	}
	
Sylvain Thery's avatar
Sylvain Thery committed
136 137 138
	MaterialOBJ* currentMat = NULL;

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

Sylvain Thery's avatar
Sylvain Thery committed
140 141 142 143
	std::string ligne;
	do
	{
		std::getline (fp, ligne);
Sylvain Thery's avatar
Sylvain Thery committed
144 145 146
		std::stringstream oss(ligne);
		std::string tag;
		oss >> tag;
Sylvain Thery's avatar
Sylvain Thery committed
147 148
		if (tag == "newmtl")
		{
Sylvain Thery's avatar
Sylvain Thery committed
149 150 151
			std::string name;
			oss >> name ;
			std::map<std::string,int>::iterator it = m_materialNames.find(name);
Sylvain Thery's avatar
Sylvain Thery committed
152 153
			if (it ==  m_materialNames.end())
			{
Sylvain Thery's avatar
Sylvain Thery committed
154
				CGoGNerr << "Skipping material "<< name << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
155 156 157 158 159 160 161 162
				do
				{
					fp >> tag;
					
				}while (!fp.eof() && (tag == "new mtl")); 
			}
			else
			{
Sylvain Thery's avatar
Sylvain Thery committed
163
//				CGoGNout << "Reading material "<< name << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
164 165 166
				currentMat = new MaterialOBJ();
				m_materials[it->second] = currentMat;
				currentMat->name = name;
Sylvain Thery's avatar
Sylvain Thery committed
167 168
			}
		}
Sylvain Thery's avatar
Sylvain Thery committed
169
		else if (currentMat != NULL)
Sylvain Thery's avatar
Sylvain Thery committed
170 171 172
		{
			if (tag == "Ka")
			{
Sylvain Thery's avatar
Sylvain Thery committed
173 174 175
				oss >> currentMat->ambiantColor[0];
				oss >> currentMat->ambiantColor[1];
				oss >> currentMat->ambiantColor[2];
Sylvain Thery's avatar
Sylvain Thery committed
176 177 178
			}
			if (tag == "Kd")
			{
Sylvain Thery's avatar
Sylvain Thery committed
179 180 181 182
				oss >> currentMat->diffuseColor[0];
				oss >> currentMat->diffuseColor[1];
				oss >> currentMat->diffuseColor[2];

Sylvain Thery's avatar
Sylvain Thery committed
183 184 185
			}
			if (tag == "Ks")
			{
Sylvain Thery's avatar
Sylvain Thery committed
186 187 188
				oss >> currentMat->specularColor[0];
				oss >> currentMat->specularColor[1];
				oss >> currentMat->specularColor[2];
Sylvain Thery's avatar
Sylvain Thery committed
189 190 191
			}
			if (tag == "Ns")
			{
Sylvain Thery's avatar
Sylvain Thery committed
192
				oss >> currentMat->shininess;
Sylvain Thery's avatar
Sylvain Thery committed
193
			}
Sylvain Thery's avatar
Sylvain Thery committed
194 195
			if (tag == "Tf")
			{
Sylvain Thery's avatar
Sylvain Thery committed
196 197 198
				oss >> currentMat->transparentFilter[0];
				oss >> currentMat->transparentFilter[1];
				oss >> currentMat->transparentFilter[2];
Sylvain Thery's avatar
Sylvain Thery committed
199 200
			}

Sylvain Thery's avatar
Sylvain Thery committed
201 202 203 204 205 206
			if ((tag == "illum"))
			{
				oss >> currentMat->illuminationModel;
			}


Sylvain Thery's avatar
Sylvain Thery committed
207 208
			if ((tag == "Tr") || (tag == "d"))
			{
Sylvain Thery's avatar
Sylvain Thery committed
209
				oss >> currentMat->transparency;
Sylvain Thery's avatar
Sylvain Thery committed
210
			}
Sylvain Thery's avatar
Sylvain Thery committed
211
			
Sylvain Thery's avatar
Sylvain Thery committed
212
			if (tag == "map_Kd") // diffuse texture
Sylvain Thery's avatar
Sylvain Thery committed
213
			{
Sylvain Thery's avatar
Sylvain Thery committed
214 215 216 217 218 219 220 221 222 223
				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
224
//					CGoGNout << "Loading texture "<< m_matPath+tname << " -> "<<std::hex << currentMat->textureDiffuse <<std::dec<<CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
225
					currentMat->textureDiffuse->scaleNearest( currentMat->textureDiffuse->newMaxSize(m_maxTextureSize));
Sylvain Thery's avatar
Sylvain Thery committed
226 227 228 229
					currentMat->textureDiffuse->setFiltering(GL_LINEAR);
					currentMat->textureDiffuse->setWrapping(GL_REPEAT);
					currentMat->textureDiffuse->update();
				}
Sylvain Thery's avatar
Sylvain Thery committed
230
			}
Sylvain Thery's avatar
Sylvain Thery committed
231 232

			if (tag == "map_Ka") // ambiant texture
Sylvain Thery's avatar
Sylvain Thery committed
233
			{
Sylvain Thery's avatar
Sylvain Thery committed
234 235 236 237 238 239 240 241 242 243 244
//				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
245
					currentMat->textureDiffuse->scaleNearest( currentMat->textureDiffuse->newMaxSize(m_maxTextureSize));
Sylvain Thery's avatar
Sylvain Thery committed
246 247 248 249
					currentMat->textureDiffuse->setFiltering(GL_LINEAR);
					currentMat->textureDiffuse->setWrapping(GL_REPEAT);
					currentMat->textureDiffuse->update();
				}
Sylvain Thery's avatar
Sylvain Thery committed
250

Sylvain Thery's avatar
Sylvain Thery committed
251
			}
Sylvain Thery's avatar
Sylvain Thery committed
252

Sylvain Thery's avatar
Sylvain Thery committed
253 254 255 256 257 258 259 260
			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
261
			tag="";
Sylvain Thery's avatar
Sylvain Thery committed
262 263 264
		}
	}while (!fp.eof());

Sylvain Thery's avatar
Sylvain Thery committed
265 266 267 268 269
	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
270

Sylvain Thery's avatar
Sylvain Thery committed
271
}
Sylvain Thery's avatar
Sylvain Thery committed
272 273 274


template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
275
unsigned int OBJModel<PFP>::getMaterialIndex(const std::string& name) const
Sylvain Thery's avatar
Sylvain Thery committed
276
{
Sylvain Thery's avatar
Sylvain Thery committed
277 278 279 280 281 282 283 284 285 286
	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)
//{
287 288 289
//	browsers.clear();
//	if (m_groupNames.empty())
//		return false;
Sylvain Thery's avatar
Sylvain Thery committed
290 291

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

294 295
//	for (unsigned int i = 1; i<m_groupNames.size(); ++i)
//	{
Sylvain Thery's avatar
Sylvain Thery committed
296
//		ContainerBrowser* MBptr = new ContainerBrowserLinked(*MBLptr);
297 298 299
//		browsers.push_back(MBptr);
//		m_groupMaterialID[i]= m_materialNames[m_groupMaterialNames[i]];
//	}
Sylvain Thery's avatar
Sylvain Thery committed
300

301 302 303
//	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
304 305
//		ContainerBrowserLinked* mb = static_cast<ContainerBrowserLinked*>(browsers[g]);
//		mb->pushBack(d.index);
306 307
//	}
//	return true;
Sylvain Thery's avatar
Sylvain Thery committed
308
//}
Sylvain Thery's avatar
Sylvain Thery committed
309 310 311 312 313 314 315 316 317 318




	
template <typename PFP>
short OBJModel<PFP>::readObjLine(std::stringstream& oss, std::vector<unsigned int>& indices)
{
	indices.clear();
	
thery's avatar
thery committed
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	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;
}


363 364 365 366 367 368
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
369

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	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
394

395 396 397 398 399 400 401 402 403 404 405 406

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
407
	{
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
		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
433 434
	}

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
	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
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 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522




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



523 524 525
template <typename PFP>
unsigned int OBJModel<PFP>::createSimpleVBO_PTN(Utils::VBO* positionVBO, Utils::VBO* texcoordVBO, Utils::VBO* normalVBO )
{
526 527 528 529 530 531 532 533 534 535 536 537
	if (!m_normals.isValid())
	{
		CGoGNerr << "no normal attribute "<< CGoGNendl;
		return 0;
	}
	if (!m_texCoords.isValid())
	{
		CGoGNerr << "no tex coords attribute "<< CGoGNendl;
		return 0;
	}


538 539 540
	TraversorF<typename PFP::MAP> traf(m_map);
	std::vector<Geom::Vec3f> posBuff;
	std::vector<Geom::Vec2f> TCBuff;
541
	std::vector<Geom::Vec3f> normalBuff;
542 543 544 545 546 547 548 549 550 551 552 553 554 555
	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))
			{
556 557 558 559 560 561 562 563
				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]);
564 565 566 567 568 569 570 571 572 573
			}
			else
			{
				TCBuff.push_back(m_texCoords[d]);
				normalBuff.push_back(m_normals[d]);
			}

			posBuff.push_back(m_positions[e]);
			if (m_specialVertices.isMarked(e))
			{
574 575 576 577 578 579 580 581
				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]);
582 583 584 585 586 587 588 589 590 591
			}
			else
			{
				TCBuff.push_back(m_texCoords[e]);
				normalBuff.push_back(m_normals[e]);
			}

			posBuff.push_back(m_positions[f]);
			if (m_specialVertices.isMarked(f))
			{
592 593 594 595 596 597 598 599
				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]);
600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
			}
			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>
Sylvain Thery's avatar
Sylvain Thery committed
635
bool OBJModel<PFP>::createGroupMatVBO_P(Utils::VBO* positionVBO)
636
{
Sylvain Thery's avatar
Sylvain Thery committed
637 638 639 640 641 642 643
	m_beginIndices.clear();
	m_nbIndices.clear();

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

	std::vector< std::vector<Dart> > group_faces(m_materialNames.size());
644
	TraversorF<typename PFP::MAP> traf(m_map);
Sylvain Thery's avatar
Sylvain Thery committed
645 646 647 648 649 650 651 652 653 654 655
	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);
Sylvain Thery's avatar
Sylvain Thery committed
656
	m_groupIdx.resize(sz);
Sylvain Thery's avatar
Sylvain Thery committed
657 658 659 660 661 662

	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
663 664 665 666 667
		if (m_tagG != 0)
			m_groupIdx[g] = m_groups[traf.front()];
		else
			m_groupIdx[g]=0;

Sylvain Thery's avatar
Sylvain Thery committed
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
		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();

Sylvain Thery's avatar
Sylvain Thery committed
693 694
	updateGroups();

Sylvain Thery's avatar
Sylvain Thery committed
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
	return true;
}



template <typename PFP>
bool OBJModel<PFP>::createGroupMatVBO_PT(Utils::VBO* positionVBO, Utils::VBO* texcoordVBO)
{
	m_beginIndices.clear();
	m_nbIndices.clear();

	std::vector<Geom::Vec3f> posBuff;
	std::vector<Geom::Vec2f> TCBuff;
	posBuff.reserve(16384);
	TCBuff.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);
Sylvain Thery's avatar
Sylvain Thery committed
724
	m_groupIdx.resize(sz);
Sylvain Thery's avatar
Sylvain Thery committed
725 726 727 728 729 730

	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
731 732 733 734 735
		if (m_tagG != 0)
			m_groupIdx[g] = m_groups[traf.front()];
		else
			m_groupIdx[g]=0;

Sylvain Thery's avatar
Sylvain Thery committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
		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]);
				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);
		}
		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();

Sylvain Thery's avatar
Sylvain Thery committed
783 784
	updateGroups();

Sylvain Thery's avatar
Sylvain Thery committed
785 786 787 788 789 790 791 792 793 794 795 796 797 798
	return true;
}






template <typename PFP>
bool OBJModel<PFP>::createGroupMatVBO_PN(Utils::VBO* positionVBO, Utils::VBO* normalVBO)
{
	m_beginIndices.clear();
	m_nbIndices.clear();

799 800 801 802 803
	std::vector<Geom::Vec3f> posBuff;
	std::vector<Geom::Vec2f> normalBuff;
	posBuff.reserve(16384);
	normalBuff.reserve(16384);

Sylvain Thery's avatar
Sylvain Thery committed
804 805
	std::vector< std::vector<Dart> > group_faces(m_materialNames.size());
	TraversorF<typename PFP::MAP> traf(m_map);
806 807
	for (Dart d=traf.begin(); d!= traf.end(); d = traf.next())
	{
Sylvain Thery's avatar
Sylvain Thery committed
808 809 810 811 812 813 814 815 816
		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);
Sylvain Thery's avatar
Sylvain Thery committed
817
	m_groupIdx.resize(sz);
Sylvain Thery's avatar
Sylvain Thery committed
818 819 820 821 822 823

	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
824 825 826 827 828
		if (m_tagG != 0)
			m_groupIdx[g] = m_groups[traf.front()];
		else
			m_groupIdx[g]=0;

Sylvain Thery's avatar
Sylvain Thery committed
829
		for (std::vector<Dart>::iterator id=traf.begin(); id!= traf.end(); ++id)
830
		{
Sylvain Thery's avatar
Sylvain Thery committed
831
			Dart d = *id;
832

Sylvain Thery's avatar
Sylvain Thery committed
833 834 835
			Dart e = m_map.phi1(d);
			Dart f = m_map.phi1(e);
			do
836
			{
Sylvain Thery's avatar
Sylvain Thery committed
837 838 839 840 841 842 843 844 845
				posBuff.push_back(m_positions[d]);
				if (m_specialVertices.isMarked(d))
				{
					normalBuff.push_back(m_normalsF[d]);
				}
				else
				{
					normalBuff.push_back(m_normals[d]);
				}
846

Sylvain Thery's avatar
Sylvain Thery committed
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
				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);
		}
		m_beginIndices[g] = firstIndex;
		m_nbIndices[g] = 3*nbtris;
		firstIndex += 3*nbtris;
874 875 876 877 878 879 880 881 882 883 884 885 886 887
	}

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

Sylvain Thery's avatar
Sylvain Thery committed
888 889
	updateGroups();

Sylvain Thery's avatar
Sylvain Thery committed
890 891 892
	return true;
}

893

Sylvain Thery's avatar
Sylvain Thery committed
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


template <typename PFP>
bool OBJModel<PFP>::createGroupMatVBO_PTN( Utils::VBO* positionVBO,
											Utils::VBO* texcoordVBO,
											Utils::VBO* normalVBO)
{
	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;
	}

	std::vector<Geom::Vec3f> posBuff;
	std::vector<Geom::Vec2f> TCBuff;
	std::vector<Geom::Vec3f> normalBuff;
	posBuff.reserve(16384);
	TCBuff.reserve(16384);
	normalBuff.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);

Sylvain Thery's avatar
Sylvain Thery committed
937
	m_groupIdx.resize(sz);
Sylvain Thery's avatar
Sylvain Thery committed
938 939 940 941 942 943

	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
944 945 946 947 948
		if (m_tagG != 0)
			m_groupIdx[g] = m_groups[traf.front()];
		else
			m_groupIdx[g]=0;

Sylvain Thery's avatar
Sylvain Thery committed
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 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
		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]);
//				if (m_specialVertices.isMarked(d))
				{
					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]);
				}
//				else
//				{
//					TCBuff.push_back(m_texCoords[d]);
//					normalBuff.push_back(m_normals[d]);
//				}

				posBuff.push_back(m_positions[e]);
//				if (m_specialVertices.isMarked(e))
				{
					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]);
				}
//				else
//				{
//					TCBuff.push_back(m_texCoords[e]);
//					normalBuff.push_back(m_normals[e]);
//				}

				posBuff.push_back(m_positions[f]);
//				if (m_specialVertices.isMarked(f))
				{
					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]);
				}
//				else
//				{
//					TCBuff.push_back(m_texCoords[f]);
//					normalBuff.push_back(m_normals[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();

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

Sylvain Thery's avatar
Sylvain Thery committed
1040 1041
	updateGroups(posBuff);

Sylvain Thery's avatar
Sylvain Thery committed
1042
	return true;
1043 1044 1045
}


Sylvain Thery's avatar
Sylvain Thery committed
1046

Sylvain Thery's avatar
Sylvain Thery committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
template <typename PFP>
void OBJModel<PFP>::updateGroups(const std::vector<Geom::Vec3f>& pos)
{
	unsigned int sz = m_beginIndices.size();
	unsigned int i=0;
	do
	{
		m_objGroups.push_back(i);
		m_groupBBs.resize(m_objGroups.size());
		Geom::BoundingBox<VEC3>& bb = m_groupBBs.back();

		unsigned int grp = m_groupIdx[i];
		while ((i<sz) && (m_groupIdx[i]==grp))
		{
			// update BB
			unsigned int beg = m_beginIndices[i];
			unsigned int end = m_beginIndices[i] + m_nbIndices[i];
			for (unsigned int j=beg; j!=end; ++j )
				bb.addPoint(pos[j]);
			++i;
		}
		bb.centeredScale(1.1);
	}while (i<sz);

	m_objGroups.push_back(sz);
}




thery's avatar
thery committed
1077
template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
1078
bool OBJModel<PFP>::import( const std::string& filename, std::vector<std::string>& attrNames)
thery's avatar
thery committed
1079 1080 1081 1082 1083 1084
{
	typedef  typename PFP::VEC3 VEC3;
	typedef Geom::Vec2f VEC2;

	attrNames.clear();
	// open file
Sylvain Thery's avatar
Sylvain Thery committed
1085
	std::ifstream fp(filename.c_str()/*, std::ios::binary*/);
thery's avatar
thery committed
1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	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
1097 1098
		std::stringstream oss(ligne);
		oss >> tag;
thery's avatar
thery committed
1099
		if (tag == "v")
1100
			m_tagV++;
thery's avatar
thery committed
1101
		if (tag == "vn")
1102
			m_tagVN++;
thery's avatar
thery committed
1103
		if (tag == "vt")
1104
			m_tagVT++;
Sylvain Thery's avatar
Sylvain Thery committed
1105
		if ((tag == "g") || (tag == "o"))
1106
			m_tagG++;
thery's avatar
thery committed
1107
		if (tag == "f")
1108
			m_tagF++;
Sylvain Thery's avatar
Sylvain Thery committed
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119

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

thery's avatar
thery committed
1120 1121 1122 1123
	}while (!fp.eof());



Sylvain Thery's avatar
Sylvain Thery committed
1124 1125 1126 1127
	m_positions =  m_map.template getAttribute<typename PFP::VEC3, VERTEX>("position") ;
	if (!m_positions.isValid())
		m_positions = m_map.template addAttribute<VEC3, VERTEX>("position") ;
	attrNames.push_back(m_positions.name()) ;
thery's avatar
thery committed
1128

Sylvain Thery's avatar
Sylvain Thery committed
1129 1130 1131 1132 1133 1134 1135


	m_texCoords =  m_map.template getAttribute<VEC2, VERTEX>("texCoord") ;
	if (!m_texCoords.isValid())
		m_texCoords = m_map.template addAttribute<VEC2, VERTEX>("texCoord") ;
	attrNames.push_back(m_texCoords.name()) ;

1136
	if (m_tagVT != 0)
thery's avatar
thery committed
1137
	{
Sylvain Thery's avatar
Sylvain Thery committed
1138 1139 1140
		m_texCoordsF =  m_map.template getAttribute<VEC2, VERTEX1>("texCoordF") ;
		if (!m_texCoordsF.isValid())
			m_texCoordsF = m_map.template addAttribute<VEC2, VERTEX1>("texCoordF") ;
thery's avatar
thery committed
1141 1142
	}

Sylvain Thery's avatar
Sylvain Thery committed
1143 1144 1145 1146 1147
	m_normals =  m_map.template getAttribute<typename PFP::VEC3, VERTEX>("normal") ;
	if (!m_normals.isValid())
		m_normals = m_map.template addAttribute<VEC3, VERTEX>("normal") ;
	attrNames.push_back(m_normals.name()) ;

1148
	if (m_tagVN != 0)
thery's avatar
thery committed
1149
	{
Sylvain Thery's avatar
Sylvain Thery committed
1150 1151 1152
		m_normalsF =  m_map.template getAttribute<VEC3, VERTEX1>("normalF") ;
		if (!m_normalsF.isValid())
			m_normalsF = m_map.template addAttribute<VEC3, VERTEX1>("normalF") ;
thery's avatar
thery committed
1153 1154
	}

Sylvain Thery's avatar
Sylvain Thery committed
1155
	
Sylvain Thery's avatar
Sylvain Thery committed
1156
//	if (m_tagG != 0) always use group even if not in the file
Sylvain Thery's avatar
Sylvain Thery committed
1157 1158 1159 1160
	{
		m_groups =  m_map.template getAttribute<unsigned int, FACE>("groups") ;
		if (!m_groups.isValid())
			m_groups = m_map.template addAttribute<unsigned int, FACE>("groups") ;
1161
		attrNames.push_back(m_groups.name()) ;
Sylvain Thery's avatar
Sylvain Thery committed
1162 1163
	}
	
Sylvain Thery's avatar
Sylvain Thery committed
1164 1165 1166 1167
	m_attMat =  m_map.template getAttribute<unsigned int, FACE>("material") ;
	if (!m_attMat.isValid())
		m_attMat = m_map.template addAttribute<unsigned int, FACE>("material") ;
	attrNames.push_back(m_attMat.name()) ;
thery's avatar
thery committed
1168 1169 1170 1171 1172 1173 1174 1175

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

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

	std::vector<VEC3> normalsBuffer;
1176
	normalsBuffer.reserve(m_tagVN);
Sylvain Thery's avatar
Sylvain Thery committed
1177
	
thery's avatar
thery committed
1178
	std::vector<VEC2> texCoordsBuffer;
1179
	texCoordsBuffer.reserve(m_tagVT);
Sylvain Thery's avatar
Sylvain Thery committed
1180
	
thery's avatar
thery committed
1181
	std::vector<unsigned int> verticesID;
1182
	verticesID.reserve(m_tagV);
Sylvain Thery's avatar
Sylvain Thery committed
1183 1184
	
	std::vector<unsigned int> normalsID;
1185
	normalsID.reserve(m_tagV);
thery's avatar
thery committed
1186
	std::vector<unsigned int> texCoordsID;
1187
	texCoordsID.reserve(m_tagV);
Sylvain Thery's avatar
Sylvain Thery committed
1188
	
thery's avatar
thery committed
1189 1190 1191

	std::vector<unsigned int> localIndices;
	localIndices.reserve(64*3);
Sylvain Thery's avatar
Sylvain Thery committed
1192
	FunctorInitEmb<typename PFP::MAP, VERTEX> fsetemb(m_map);
thery's avatar
thery committed
1193

1194 1195 1196
	VertexAutoAttribute< NoTypeNameAttribute< std::vector<Dart> > > vecDartsPerVertex(m_map, "incidents");
	VertexAutoAttribute< NoTypeNameAttribute< std::vector<unsigned int> > > vecNormIndPerVertex(m_map, "incidentsN");
	VertexAutoAttribute< NoTypeNameAttribute< std::vector<unsigned int> > > vecTCIndPerVertex(m_map, "incidentsTC");
thery's avatar
thery committed
1197

Sylvain Thery's avatar
Sylvain Thery committed
1198
	unsigned int currentGroup = 0;
Sylvain Thery's avatar
Sylvain Thery committed
1199 1200 1201
	unsigned int currentMat = 0;
	unsigned int nextMat = 0;

thery's avatar
thery committed
1202

Sylvain Thery's avatar
Sylvain Thery committed
1203
	DartMarkerNoUnmark mk(m_map) ;
thery's avatar
thery committed
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
	unsigned int i = 0;
	fp >> tag;
	std::getline(fp, ligne);
	do
	{
		if (tag == std::string("v"))
		{
			std::stringstream oss(ligne);

			float x,y,z;
			oss >> x;
			oss >> y;
			oss >> z;

			VEC3 pos(x,y,z);

			unsigned int id = container.insertLine();
Sylvain Thery's avatar
Sylvain Thery committed
1221
			m_positions[id] = pos;
thery's avatar
thery committed
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243

			verticesID.push_back(id);
			i++;
		}

		if (tag == std::string("vn"))
		{
			std::stringstream oss(ligne);

			VEC3 norm;
			oss >> norm[0];
			oss >> norm[1];
			oss >> norm[2];
			normalsBuffer.push_back(norm);
		}

		if (tag == std::string("vt"))
		{
			std::stringstream oss(ligne);
			VEC2 tc;
			oss >> tc[0];
			oss >> tc[1];
Sylvain Thery's avatar
Sylvain Thery committed
1244
			texCoordsBuffer.push_back(tc);
thery's avatar
thery committed
1245 1246
		}

Sylvain Thery's avatar
Sylvain Thery committed
1247 1248

		if (tag == std::string("usemtl"))
thery's avatar
thery committed
1249
		{
Sylvain Thery's avatar
Sylvain Thery committed
1250 1251 1252 1253 1254 1255 1256 1257 1258
			std::stringstream oss(ligne);
			std::string matName;
			oss >> matName;
			std::map<std::string, int>::iterator it = m_materialNames.find(matName);

			if (it==m_materialNames.end())
			{
				m_materialNames.insert(std::pair<std::string,int>(matName,nextMat));
				currentMat = nextMat++;
Sylvain Thery's avatar
Sylvain Thery committed
1259
//				std::cout << "New Material Name = "<< matName << "  index = "<< currentMat << std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
1260 1261 1262

			}
			else
Sylvain Thery's avatar
Sylvain Thery committed
1263
			{
Sylvain Thery's avatar
Sylvain Thery committed
1264
				currentMat = it->second;
Sylvain Thery's avatar
Sylvain Thery committed
1265
//				std::cout << "Using Material Name = "<<  matName << "  index = "<< currentMat << std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
1266
			}
thery's avatar
thery committed
1267 1268
		}

Sylvain Thery's avatar
Sylvain Thery committed
1269
		if ( (tag == std::string("g")) || (tag == std::string("o")) )
Sylvain Thery's avatar
Sylvain Thery committed
1270
		{
1271 1272
			std::string name = ligne.substr(1);
			m_groupNames.push_back(name);
Sylvain Thery's avatar
Sylvain Thery committed
1273 1274
			currentGroup++;
		}
Sylvain Thery's avatar
Sylvain Thery committed
1275

thery's avatar
thery committed
1276 1277 1278 1279 1280 1281
		if (tag == std::string("f"))
		{
			std::stringstream oss(ligne);

			short nbe = readObjLine(oss,localIndices);

Sylvain Thery's avatar
Sylvain Thery committed
1282
			Dart d = m_map.newFace(nbe, false);
1283
			if (m_tagG!=0)
1284
				m_groups[d] = currentGroup;
Sylvain Thery's avatar
Sylvain Thery committed
1285 1286

			m_attMat[d] = currentMat;
Sylvain Thery's avatar
Sylvain Thery committed
1287
			
thery's avatar
thery committed
1288 1289 1290 1291
			for (short j = 0; j < nbe; ++j)
			{
				unsigned int em = localIndices[3*j]-1;		// get embedding
				fsetemb.changeEmb(em) ;
Sylvain Thery's avatar
Sylvain Thery committed
1292 1293
				m_map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
				mk.mark(d) ;								// mark on the fly to unmark on second loop
thery's avatar
thery committed
1294 1295 1296
				vecDartsPerVertex[em].push_back(d);		// store incident darts for fast adjacency reconstruction
				vecTCIndPerVertex[em].push_back(localIndices[3*j+1]-1);
				vecNormIndPerVertex[em].push_back(localIndices[3*j+2]-1);
Sylvain Thery's avatar
Sylvain Thery committed
1297
				d = m_map.phi1(d);
thery's avatar
thery committed
1298 1299 1300 1301 1302
			}
		}
		fp >> tag;
		std::getline(fp, ligne);
	} while (!fp.eof());
Sylvain Thery's avatar
Sylvain Thery committed
1303
	fp.close ();
thery's avatar
thery committed
1304 1305 1306

	// reconstruct neighbourhood
	unsigned int nbBoundaryEdges = 0;
Sylvain Thery's avatar
Sylvain Thery committed
1307
	for (Dart d = m_map.begin(); d != m_map.end(); m_map.next(d))
thery's avatar
thery committed
1308
	{
Sylvain Thery's avatar
Sylvain Thery committed
1309
		if (mk.isMarked(d))
thery's avatar
thery committed
1310 1311
		{
			// darts incident to end vertex of edge
Sylvain Thery's avatar
Sylvain Thery committed
1312
			std::vector<Dart>& vec = vecDartsPerVertex[m_map.phi1(d)];
thery's avatar
thery committed
1313

Sylvain Thery's avatar
Sylvain Thery committed
1314
			unsigned int embd = m_map.template getEmbedding<VERTEX>(d);
thery's avatar
thery committed
1315 1316 1317
			Dart good_dart = NIL;
			for (typename std::vector<Dart>::iterator it = vec.begin(); it != vec.end() && good_dart == NIL; ++it)
			{
Sylvain Thery's avatar
Sylvain Thery committed
1318
				if (m_map.template getEmbedding<VERTEX>(m_map.phi1(*it)) == embd)
thery's avatar
thery committed
1319 1320 1321 1322 1323
					good_dart = *it;
			}

			if (good_dart != NIL)
			{
Sylvain Thery's avatar
Sylvain Thery committed
1324 1325 1326 1327 1328
				if (good_dart == m_map.phi2(good_dart) && (d == m_map.phi2(d)))
					m_map.sewFaces(d, good_dart, false);
				else
					m_dirtyEdges.mark(d);
				mk.unmarkOrbit<EDGE>(d);
thery's avatar
thery committed
1329 1330 1331
			}
			else
			{
Sylvain Thery's avatar
Sylvain Thery committed
1332 1333
				mk.unmark(d);
				m_dirtyEdges.mark(d);
thery's avatar
thery committed
1334 1335 1336 1337 1338
				++nbBoundaryEdges;
			}
		}
	}

Sylvain Thery's avatar
Sylvain Thery committed
1339 1340 1341

	// A SIMPLIFIER ???

Sylvain Thery's avatar
Sylvain Thery committed
1342
	TraversorV<typename PFP::MAP> tra(m_map);
thery's avatar
thery committed
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
	for (Dart d = tra.begin(); d != tra.end(); d = tra.next())
	{
		std::vector<Dart>& vec			= vecDartsPerVertex[d];
		std::vector<unsigned int>& vtc	= vecTCIndPerVertex[d];
		std::vector<unsigned int>& vn	= vecNormIndPerVertex[d];

		// test if normal vertex or multi-attrib vertex
		unsigned int nb = vtc.size();
		bool same=true;
		for (unsigned int j=1; (j<nb)&&(same); ++j)
		{
			if ( (vtc[j] != vtc[0]) || (vn[j] != vn[0]) )
				same = false;
		}

		// if not all faces the same embedding
1359 1360
//		if (!same)
//		{
thery's avatar
thery committed
1361 1362
			for (unsigned int j=0; j<nb; ++j)
			{
Sylvain Thery's avatar
Sylvain Thery committed
1363
				Dart e = vec[j];
1364
				if (m_tagVT)
Sylvain Thery's avatar
Sylvain Thery committed
1365 1366 1367 1368 1369 1370
				{
					if (vecTCIndPerVertex[e][j] != 0xffffffff)
						m_texCoordsF[e] = texCoordsBuffer[ vecTCIndPerVertex[e][j] ];
					else
						m_texCoordsF[e] = Geom::Vec2f(0);
				}
1371
				if (m_tagVN)
Sylvain Thery's avatar
Sylvain Thery committed
1372 1373 1374 1375 1376 1377
				{
					if (vecNormIndPerVertex[e][j] != 0xffffffff)
						m_normalsF[e] = normalsBuffer[ vecNormIndPerVertex[e][j] ];
					else
						m_normalsF[e] = VEC3(0);
				}
thery's avatar
thery committed
1378
			}
Sylvain Thery's avatar
Sylvain Thery committed
1379
			m_specialVertices.mark(d);
1380 1381 1382 1383 1384 1385 1386 1387 1388
//		}
//		else
//		{
//			if (tagVT)
//				m_texCoords[d] = texCoordsBuffer[ vecTCIndPerVertex[d][0] ];
//			if (tagVN)
//				m_normals[d] = normalsBuffer[ vecNormIndPerVertex[d][0] ];
//			m_specialVertices.unmark(d);
//		}
thery's avatar
thery committed
1389 1390
	}

Sylvain Thery's avatar
Sylvain Thery committed
1391 1392 1393

	readMaterials();

Sylvain Thery's avatar
Sylvain Thery committed
1394
	return true;
thery's avatar
thery committed
1395 1396 1397

}

Sylvain Thery's avatar
Sylvain Thery committed
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
template <typename PFP>
unsigned int OBJModel<PFP>::storeFacesOfGroup(unsigned int groupId, std::vector<Dart>& dartFaces)
{
	unsigned int nb=dartFaces.size();

	TraversorF<typename PFP::MAP> traf(m_map);
	for (Dart d=traf.begin(); d!= traf.end(); d = traf.next())
	{
		if (m_groups[d] == groupId)
		{
			dartFaces.push_back(d);
		}
	}

	return dartFaces.size()-nb;
}
thery's avatar
thery committed
1414 1415


1416
}
Pierre Kraemer's avatar
Pierre Kraemer committed
1417 1418 1419
}
} // end namespaces
}