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

27
28
//#include "Topology/generic/mapBrowser.h"
#include "Container/containerBrowser.h"
Sylvain Thery's avatar
Sylvain Thery committed
29
#include "Topology/generic/cellmarker.h"
Sylvain Thery's avatar
Sylvain Thery committed
30
#include "Utils/textures.h"
Sylvain Thery's avatar
Sylvain Thery committed
31
#include "Geometry/bounding_box.h"
Sylvain Thery's avatar
Sylvain Thery committed
32
33
34

namespace CGoGN
{
35

Sylvain Thery's avatar
Sylvain Thery committed
36
37
namespace Algo
{
38

Sylvain Thery's avatar
Sylvain Thery committed
39
40
namespace Surface
{
41

Sylvain Thery's avatar
Sylvain Thery committed
42
43
44
namespace Import
{

Sylvain Thery's avatar
Sylvain Thery committed
45
class MaterialOBJ
Sylvain Thery's avatar
Sylvain Thery committed
46
{
Sylvain Thery's avatar
Sylvain Thery committed
47
public:
Sylvain Thery's avatar
Sylvain Thery committed
48
49
50
51
52
53
54
55
56
57
58
	MaterialOBJ():
		illuminationModel(0),
		ambiantColor(0.0f,0.0f,0.0f),
		diffuseColor(0.0f,0.0f,0.0f),
		specularColor(0.0f,0.0f,0.0f),
		shininess(100.0f),
		transparentFilter(0.0f,0.0f,0.0f),
		transparency(0.0f),
		textureDiffuse(NULL)
	{}

Sylvain Thery's avatar
Sylvain Thery committed
59
60
61
62
63
64
65
	~MaterialOBJ()
	{
		if (textureDiffuse!=NULL)
			delete textureDiffuse;
	}

	std::string name;
Sylvain Thery's avatar
Sylvain Thery committed
66
	int illuminationModel; // 0 Diffu / 1 D+Ambiant / 3 A+D+Specular
Sylvain Thery's avatar
Sylvain Thery committed
67
68
69
70
	Geom::Vec3f ambiantColor;
	Geom::Vec3f diffuseColor;
	Geom::Vec3f specularColor;
	float shininess;
Sylvain Thery's avatar
Sylvain Thery committed
71
72
73
	Geom::Vec3f transparentFilter;
	float transparency;
	Utils::Texture<2,Geom::Vec3uc>* textureDiffuse;
Sylvain Thery's avatar
Sylvain Thery committed
74
75
76
77
78
79
80
81
82

//	static bool compare(MaterialOBJ* m1, MaterialOBJ* m2)
//	{
//		if (m1->textureDiffuse > m2->textureDiffuse)
//			return true;
//		if (m1->textureDiffuse == m2->textureDiffuse)
//			return m1->illuminationModel > m2->illuminationModel;
//		return false;
//	}
Sylvain Thery's avatar
Sylvain Thery committed
83
84
85
86
87
};

template <typename PFP>
class OBJModel
{
88
89
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::MAP MAP;
Sylvain Thery's avatar
Sylvain Thery committed
90
91
92
	typedef Geom::Vec2f VEC2;

protected:
93
	MAP& m_map;
Sylvain Thery's avatar
Sylvain Thery committed
94

95
	// infof of sub-groups (group/material)
Sylvain Thery's avatar
Sylvain Thery committed
96
97
	std::vector<unsigned int> m_beginIndices;
	std::vector<unsigned int> m_nbIndices;
Sylvain Thery's avatar
Sylvain Thery committed
98
	std::vector<unsigned int> m_groupIdx;
99
	std::vector<unsigned int> m_sgMat;
100

Sylvain Thery's avatar
Sylvain Thery committed
101
	std::vector<unsigned int> m_objGroups;
102
103
104
105
106


	std::vector<unsigned int> m_groupFirstSub;
	std::vector<unsigned int> m_groupNbSub;

Sylvain Thery's avatar
Sylvain Thery committed
107
108
	std::vector<std::string> m_groupNames;
	std::vector< Geom::BoundingBox<VEC3> > m_groupBBs;
Sylvain Thery's avatar
Sylvain Thery committed
109

Sylvain Thery's avatar
Sylvain Thery committed
110
	unsigned int m_maxTextureSize;
111

Sylvain Thery's avatar
Sylvain Thery committed
112
	/// map of material names -> group id
Sylvain Thery's avatar
Sylvain Thery committed
113
114
	std::map<std::string,int> m_materialNames;

Sylvain Thery's avatar
Sylvain Thery committed
115
116
117
118
119
	/// filename of mtllib
	std::string m_matFileName;

	/// path of mtllib
	std::string m_matPath;
120
121

	/// vector of material struct
Sylvain Thery's avatar
Sylvain Thery committed
122
	std::vector<MaterialOBJ*> m_materials;
123
124
125
126

	/// read face line with different indices v  v/t v//n v/t/n
	short readObjLine(std::stringstream& oss, std::vector<unsigned int>& indices);

127
128
129
130
131
132
	unsigned int m_tagV ;
	unsigned int m_tagVT ;
	unsigned int m_tagVN ;
	unsigned int m_tagG ;
	unsigned int m_tagF ;

133
	void computeBB(const std::vector<Geom::Vec3f>& pos);
Sylvain Thery's avatar
Sylvain Thery committed
134

135
136
public:

Sylvain Thery's avatar
Sylvain Thery committed
137
	/// marker for special vertices (with several normals & tex coords)
138
	CellMarker<MAP, VERTEX> m_specialVertices;
Sylvain Thery's avatar
Sylvain Thery committed
139
140

	/// marker for darts with phi2 reconstruction face
141
	DartMarker<MAP> m_dirtyEdges;
Sylvain Thery's avatar
Sylvain Thery committed
142
143

	/// Face Attribute for group ID storage
144
145
	FaceAttribute<unsigned int, MAP> m_groups;
	FaceAttribute<unsigned int, MAP> m_attMat;
Sylvain Thery's avatar
Sylvain Thery committed
146
147

	/// Vertex Attribute Handlers
148
149
150
	VertexAttribute<VEC3, MAP> m_positions;
	VertexAttribute<VEC3, MAP> m_normals;
	VertexAttribute<Geom::Vec2f, MAP> m_texCoords;
Sylvain Thery's avatar
Sylvain Thery committed
151
152

	/// Vertex of face Attribute Handlers
153
154
	AttributeHandler<VEC3, VERTEX1, MAP> m_normalsF;
	AttributeHandler<Geom::Vec2f, VERTEX1, MAP> m_texCoordsF;
Sylvain Thery's avatar
Sylvain Thery committed
155
156
157
158
159
160
161

	/**
	 * @brief Constructeur
	 * @param map
	 */
	OBJModel(typename PFP::MAP& map);

Sylvain Thery's avatar
Sylvain Thery committed
162
163
	~OBJModel();

Sylvain Thery's avatar
Sylvain Thery committed
164
165
166
167
168
169
	/**
	 * @brief resize texture (at import) to max size
	 * @param mts max texture size in x & y
	 */
	void setMaxTextureSize(unsigned int mts);

170
171
172
173
	/**
	 * @brief set position attribute
	 * @param position attribute
	 */
174
	void setPositionAttribute(VertexAttribute<Geom::Vec3f, MAP> position);
175
176
177
178
179

	/**
	 * @brief set position attribute
	 * @param position attribute
	 */
180
	void setNormalAttribute(VertexAttribute<Geom::Vec3f, MAP> normal);
181
182
183
184
185

	/**
	 * @brief set texture coordinate attribute
	 * @param texcoord attribute
	 */
186
	void setTexCoordAttribute(VertexAttribute<Geom::Vec2f, MAP>texcoord);
187

188
	bool hasTexCoords() const { return m_tagVT != 0; }
189

190
	bool hasNormals() const { return m_tagVN != 0; }
191

192
	bool hasGroups() const { return m_tagG != 0; }
193

Sylvain Thery's avatar
Sylvain Thery committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
	/**
	 * @brief import
	 * @param filename
	 * @param attrNames
	 * @return
	 */
	bool import(const std::string& filename, std::vector<std::string>& attrNames);

	// Faire un handler ?
	/**
	 * @brief getNormal
	 * @param d
	 * @return
	 */
208
	VEC3 getNormal(Dart d);
Sylvain Thery's avatar
Sylvain Thery committed
209
210
211
212
213
214
215
216
217
218
219
220
221

	/**
	 * @brief getTexCoord
	 * @param d
	 * @return
	 */
	Geom::Vec2f getTexCoord(Dart d);

	/**
	 * @brief getPosition
	 * @param d
	 * @return
	 */
222
	VEC3 getPosition(Dart d);
Sylvain Thery's avatar
Sylvain Thery committed
223
224
225
226
227
228

	/**
	 * @brief Generate one browser per group
	 * @param browsers vector of MapBrowers representing the groups
	 * @return ok or not
	 */
229
	bool generateBrowsers(std::vector<ContainerBrowser*>& browsers);
Sylvain Thery's avatar
Sylvain Thery committed
230
231
232
233
234
235
236
237

	/**
	 * @brief getMaterialNames
	 * @return
	 */
	std::vector<std::string>& getMaterialNames();

	/**
Sylvain Thery's avatar
Sylvain Thery committed
238
239
240
241
242
243
244
245
	 * @brief getMaterialIndex
	 * @param name name of material
	 * @return index in generated vector of material by readMaterials
	 */
	unsigned int getMaterialIndex(const std::string& name) const;

	/**
	 * @brief read materials from files. Call after creating VBOs !!
Sylvain Thery's avatar
Sylvain Thery committed
246
247
	 * @param filename name of file
	 */
Sylvain Thery's avatar
Sylvain Thery committed
248
	void readMaterials(const std::string& filename="");
Sylvain Thery's avatar
Sylvain Thery committed
249
250
251
252
253
254
255
256
257
258
259

	/**
	 * @brief getMaterials
	 * @return the vector of MaterialObj*
	 */
	const std::vector<MaterialOBJ*>& getMaterials() const { return m_materials;}

	/**
	 * @brief nb group of indices created by createGroupMatVBO_XXX
	 * @return
	 */
260
261
262
263
264
265
266
267
//	unsigned int nbMatGroups() { return m_beginIndices.size(); }

	/**
	 * @brief number of sub-group in group
	 * @param grp id of group
	 * @return
	 */
	inline unsigned int nbSubGroup(unsigned int grp) const { return m_groupNbSub[grp];}
Sylvain Thery's avatar
Sylvain Thery committed
268
269

	/**
270
	 * @brief get the begin index of a sub-group in VBOs (for glDrawArrays)
Sylvain Thery's avatar
Sylvain Thery committed
271
	 * @param i id of group
272
	 * @param j id of subgroup in group
Sylvain Thery's avatar
Sylvain Thery committed
273
274
	 * @return begin index
	 */
275
	inline unsigned int beginIndex(unsigned int i, unsigned int j) const { return m_beginIndices[ m_groupFirstSub[i]+j ]; }
Sylvain Thery's avatar
Sylvain Thery committed
276
277

	/**
278
	 * @brief get the number of indices of a sub-group in VBOs (for glDrawArrays)
Sylvain Thery's avatar
Sylvain Thery committed
279
	 * @param i id of group
280
	 * @param j id of subgroup in group
Sylvain Thery's avatar
Sylvain Thery committed
281
282
	 * @return number of indices
	 */
283
	inline unsigned int nbIndices(unsigned int i, unsigned int j) const { return m_nbIndices[ m_groupFirstSub[i]+j ]; }
Sylvain Thery's avatar
Sylvain Thery committed
284

Sylvain Thery's avatar
Sylvain Thery committed
285
	/**
286
	 * @brief material id of a sub-group
Sylvain Thery's avatar
Sylvain Thery committed
287
	 * @param i id of group
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
	 * @param j id of subgroup in group
	 * @return id of material
	 */
	inline unsigned int materialIdOf(unsigned int i, unsigned int j) const { return m_sgMat[ m_groupFirstSub[i]+j ]; }

	/**
	 * @brief material of a sub-group
	 * @param i id of group
	 * @param j id of subgroup in group
	 * @return material ptr
	 */
	inline const MaterialOBJ* materialOf(unsigned int i, unsigned int j) const { return m_materials[materialIdOf(i,j)]; }

	/**
	 * @brief get the id of group in OBJ file os sub-group
	 * @param i id of sub-group
Sylvain Thery's avatar
Sylvain Thery committed
304
305
	 * @return obj group index
	 */
306
	inline unsigned int groupIdx(unsigned int i) const { return m_groupIdx[i]; }
Sylvain Thery's avatar
Sylvain Thery committed
307
308
309
310
311

	/**
	 * @brief get the number of groups in OBJ file
	 * @return number of groups
	 */
312
	unsigned int nbObjGroups() { return m_groupFirstSub.size(); }
Sylvain Thery's avatar
Sylvain Thery committed
313
314
315
316
317
318

	/**
	 * @brief get the index of first group mat of obj
	 * @param i id of obj group
	 * @return id of first group mat
	 */
319
320
//	unsigned int objGroup(unsigned int i) const { return m_objGroups[i]; }

Sylvain Thery's avatar
Sylvain Thery committed
321

322
	const Geom::BoundingBox<VEC3>& getGroupBB(unsigned int i) const { return m_groupBBs[i]; }
Sylvain Thery's avatar
Sylvain Thery committed
323

boustila's avatar
boustila committed
324
325
	Geom::BoundingBox<VEC3>& getGroupBB(unsigned int i) { return m_groupBBs[i];}

Sylvain Thery's avatar
Sylvain Thery committed
326
327
	const std::string& objGroupName(unsigned int i) const { return m_groupNames[i];}

328
329
330
331
332
333
	/**
	 * @brief create simple VBO for separated triangles
	 * @param positionVBO
	 * @param normalVBO
	 * @return number of indices to draw
	 */
Sylvain Thery's avatar
Sylvain Thery committed
334
	unsigned int createSimpleVBO_P(Utils::VBO* positionVBO);
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354

	/**
	 * @brief create simple VBO for separated triangles
	 * @param positionVBO
	 * @param texcoordVBO
	 * @return number of indices to draw
	 */
	unsigned int createSimpleVBO_PT(Utils::VBO* positionVBO, Utils::VBO* texcoordVBO);

	/**
	 * @brief create simple VBO for separated triangles
	 * @param positionVBO
	 * @param normalVBO
	 * @return number of indices to draw
	 */
	unsigned int createSimpleVBO_PN(Utils::VBO* positionVBO, Utils::VBO* normalVBO);

	/**
	 * @brief create simple VBO for separated triangles
	 * @param positionVBO
Sylvain Thery's avatar
Sylvain Thery committed
355
	 * @param texcoordVBO
356
357
358
	 * @param normalVBO
	 * @return number of indices to draw
	 */
Sylvain Thery's avatar
Sylvain Thery committed
359
360
361
362
363
364
365
366
367
368
	unsigned int createSimpleVBO_PTN(Utils::VBO* positionVBO, Utils::VBO* texcoordVBO, Utils::VBO* normalVBO);

	/**
	 * @brief create VBOs with group by material
	 * @param positionVBO
	 * @param beginIndices
	 * @param nbIndices
	 * @return
	 */
	bool createGroupMatVBO_P(Utils::VBO* positionVBO);
369

Sylvain Thery's avatar
Sylvain Thery committed
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
	/**
	 * @brief create VBOs with group by material
	 * @param positionVBO
	 * @param texcoordVBO
	 * @param beginIndices
	 * @param nbIndices
	 * @return
	 */
	bool createGroupMatVBO_PT(Utils::VBO* positionVBO, Utils::VBO* texcoordVBO);

	/**
	 * @brief create VBOs with group by material
	 * @param positionVBO
	 * @param normalVBO
	 * @param beginIndices
	 * @param nbIndices
	 * @return
	 */
	bool createGroupMatVBO_PN(Utils::VBO* positionVBO, Utils::VBO* normalVBO);

	/**
	 * @brief create VBOs with group by material
	 * @param positionVBO
	 * @param texcoordVBO
	 * @param normalVBO
	 * @param nbIndices
	 * @return
	 */
	bool createGroupMatVBO_PTN( Utils::VBO* positionVBO, Utils::VBO* texcoordVBO, Utils::VBO* normalVBO);

Sylvain Thery's avatar
Sylvain Thery committed
400
401
402
403
404
405
406
	/**
	 * @brief add a dart by each face of group in a vector
	 * @param groupId the group to add
	 * @param dartFaces the vector in which we want to add
	 * @return the number of faces added.
	 */
	unsigned int storeFacesOfGroup(unsigned int groupId, std::vector<Dart>& dartFaces);
Sylvain Thery's avatar
Sylvain Thery committed
407
408
};

409
410
411
412
413
} // namespace Import

} // namepsace Surface

} // namespace Algo
Sylvain Thery's avatar
Sylvain Thery committed
414

415
} // namespace CGoGN
Sylvain Thery's avatar
Sylvain Thery committed
416

417
#include "Algo/Import/importObjTex.hpp"
Sylvain Thery's avatar
Sylvain Thery committed
418
419

#endif // IMPORTOBJTEX_H