Debian Codegraph @

src/vcglib/wrap/io_trimesh/import_fbx.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2018 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23
24 /**
25 @name Open FBX format
26 */
27 //@{
28
29 #ifndef VCGLIB_IMPORT_FBX
30 #define VCGLIB_IMPORT_FBX
31 #include <wrap/openfbx/src/ofbx.h>
32
33 #include <fstream>
34 #include <vcg/complex/complex.h>
35 #include <vcg/complex/algorithms/clean.h>
36 #include <wrap/io_trimesh/io_mask.h>
37
38 /**
39 \brief ImporterFBX is the class devoted to import the info contained in a FBX file inside a mesh defined following the vcg standards.
40
41 To use it you need to add the following two files to your project:
42
43 wrap/openfbx/src/ofbx.cpp
44 wrap/openfbx/src/miniz.c
45
46
47 */
48 namespace vcg {
49 namespace tri {
50 namespace io {
51
52 template <class OpenMeshType>
53 class ImporterFBX
54 {
55 public:
56
57 typedef typename OpenMeshType::VertexPointer VertexPointer;
58 typedef typename OpenMeshType::ScalarType ScalarType;
59 typedef typename OpenMeshType::VertexType VertexType;
60 typedef typename OpenMeshType::FaceType FaceType;
61 typedef typename OpenMeshType::VertexIterator VertexIterator;
62 typedef typename OpenMeshType::FaceIterator FaceIterator;
63 typedef typename OpenMeshType::CoordType CoordType;
64 typedef typename OpenMeshType::VertexType::ColorType ColorType;
65 typedef typename OpenMeshType::VertexType::ColorType::ScalarType CSType;
66 typedef typename vcg::Matrix44<ScalarType> Matrix44Type;
67
68 public:
69 /*****
70 Enum containing possible error codes you can get opening an FBX file.
71 */
72 enum FBXError
73 {
74 // Successfull opening
75 E_NOERROR = 0,
76
77 // Critical Opening Errors (only even numbers)
78 };
79
80 /****
81 Function devoted to check if a given error is critical or the elaboration can go on.
82 */
83 static bool ErrorCritical(int err)
84 {
85 if(err == E_NOERROR)
86 return false;
87 return true;
88 }
89
90 /****
91 The function return the sting info associated with an error code.
92 */
93 static const char* ErrorMsg(int error)
94 {
95 static const char* fbx_error_msg[] =
96 {
97 "No errors",
98 };
99
100 if(error>1 || error<0)
101 return "Unknown error";
102 else
103 return fbx_error_msg[error];
104 }
105
106 /****
107 The open function import info contained in a FBX file into a mesh defined using the VCG standards.
108 Parameters:
109 - m is the mesh in which you want to load the file info
110 - filename is the filename path of a valid FBX file. It could be a relative path or an absolute one.
111 - oi is a small structure devoted to contain some feedbacks about the loaded file (like vertex number or face number). Typically you can ignore it.
112
113 Semantics:
114 If the file has been correctly loaded (i.e. Open function returned a E_NOERROR code, you will find in the mesh m at least:
115 - a vertex position for each vertex
116 - the triangles componing the mesh surface
117
118 Depending on which type mesh you passed to the function you could also have:
119 - a vertex normal for each vertex
120 - a vertex color (ONLY IF THE FBX FILE HAS ALL THE MATERIALS WITHOUT TEXTURES AT ALL)
121 - a single material for each triangle. We are using the one containing a diffuse texture and/or a diffuse color.
122 - a UV parameterization for each vertex componing a triangle
123
124 Please note that if the file is composed by a set of meshes we compact all these meshes in a single one.
125 */
126
127 static int Open( OpenMeshType &m, const char * filename, CallBackPos *cb = nullptr)
128 {
129
130 FILE* fp = fopen(filename, "rb");
131 if (!fp) return false;
132
133 fseek(fp, 0, SEEK_END);
134 long file_size = ftell(fp);
135 fseek(fp, 0, SEEK_SET);
136 ofbx::u8* content = new ofbx::u8[file_size];
137 fread(content, 1, size_t(file_size), fp);
138 fclose(fp);
139
140 ofbx::IScene* scene =ofbx::load(content, int(file_size));
141
142 m.Clear();
143 int mesh_count = scene->getMeshCount();
144 printf("mesh count %i\n",mesh_count);
145 int totVert=0;
146 for (int i = 0; i < mesh_count; ++i)
147 totVert += scene->getMesh(i)->getGeometry()->getVertexCount();
148
149 int baseVertexCount;
150 printf("Scene has %i meshes\n",mesh_count);
151 for (int i = 0; i < mesh_count; ++i)
152 {
153 baseVertexCount=m.vert.size();
154 int baseTextureIndexCount = m.textures.size();
155
156 const ofbx::Mesh& mesh = *scene->getMesh(i);
157 const ofbx::Geometry& geom = *mesh.getGeometry();
158 const int *materials = geom.getMaterials();
159
160 // Load textured materials. The mesh object has pointers to the materials,
161 // the geometry object instead has a vector of FN material indices
162 for (int mat_i = 0; mat_i < mesh.getMaterialCount(); ++mat_i) {
163 const ofbx::Material *mat = mesh.getMaterial(mat_i);
164 assert(mat);
165 const ofbx::Texture *tex = mat->getTexture(ofbx::Texture::DIFFUSE);
166 char buf[1024];
167 if (tex) {
168 tex->getRelativeFileName().toString(buf); buf[1023] = 0;
169 if (std::string(buf) == "") {
170 tex->getFileName().toString(buf); buf[1023] = 0;
171 }
172 printf("Texture %s\n", buf);
173 m.textures.push_back(buf);
174 }
175 }
176
177 Matrix44d globTransfd(mesh.getGlobalTransform().m);
178 /*
179 Matrix44f globTransf;
180 globTransf.SetIdentity();
181 globTransf.Import(globTransfd);
182 Transpose(globTransf);
183 */
184 Transpose(globTransfd);
185 int vertex_count = geom.getVertexCount();
186 const ofbx::Vec3* vertices = geom.getVertices();
187 const ofbx::Vec2* texcoords = geom.getUVs();
188 printf(" Mesh %i has %i vertices\n",i,vertex_count);
189
190 for (int j = 0; j < vertex_count; ++j)
191 {
192 ofbx::Vec3 vt = vertices[j];
193 Point3d vd = globTransfd*Point3d(vt.x, vt.y, vt.z);
194 CoordType v(vd.X(), vd.Y(), vd.Z());
195 tri::Allocator<OpenMeshType>::AddVertex(m,v);
196 if(cb && (m.vert.size()%1000) == 0 ) cb(m.vert.size() * 100 / totVert, "Vertex Loading");
197 }
198
199 // bool has_normals = geom.getNormals() != nullptr;
200 bool has_uvs = texcoords != nullptr;
201
202 const ofbx::Skin* skin = geom.getSkin();
203 if(!skin)
204 {
205 printf("Geom has no skin\n");
206 for (int j = 0; j < vertex_count/3; ++j)
207 {
208 int curTriBase = baseVertexCount + j*3;
209 tri::Allocator<OpenMeshType>::AddFace(m,curTriBase,curTriBase+1,curTriBase+2);
210 if(has_uvs)
211 {
212 for(int k=0;k<3;++k)
213 {
214 m.face.back().WT(k).u() = texcoords[j*3+k].x;
215 m.face.back().WT(k).v() = texcoords[j*3+k].y;
216 if (materials != nullptr)
217 m.face.back().WT(k).n() = baseTextureIndexCount + materials[j];
218 }
219 }
220 }
221 }
222 else
223 {
224 int cluster_count = skin->getClusterCount();
225
226 printf("Mesh %i has %i clusters\n",i,cluster_count);
227 for (int k = 0; k < cluster_count; ++k)
228 {
229 // const ofbx::Cluster *cluster=skin->getCluster(k);
230 }
231 }
232 }
233
234 tri::Clean<OpenMeshType>::RemoveDuplicateVertex(m);
235 tri::Allocator<OpenMeshType>::CompactEveryVector(m);
236 return E_NOERROR;
237
238 }
239
240 /****
241 The LoadMask function gives you a preview of which attributes you will find inside the fbx file.
242 It's useful only if you are planning to use a mesh with dynamic attributes.
243
244 Parameters:
245 - filename is the filename path of a valid FBX file. It could be a relative path or an absolute one.
246 - oi is a small structure devoted to contain some feedbacks about the loaded file (like vertex number or face number).
247 In oi.mask you will find the mask containing the attributes contained in the file. You can query the presence of a specific attribute using the operators on bits.
248 ( for example if I want to check if the file contains the vertex normal layer I have to call the LoadMask function and after check if (oi.mask & vcg::tri::io::Mask::IOM_VERTNORMAL) is true).
249 */
250
251 static int LoadMask(const char * /*filename*/)
252 {
253 return tri::io::Mask::IOM_WEDGTEXCOORD;
254 }
255
256 }; // end class
257 } // end namespace tri
258 } // end namespace io
259 } // end namespace vcg
260
261
262 #endif