| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#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 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 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 |
|
| 71 |
|
| 72 |
enum FBXError |
| 73 |
{ |
| 74 |
|
| 75 |
E_NOERROR = 0, |
| 76 |
|
| 77 |
|
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
static bool ErrorCritical(int err) |
| 84 |
{ |
| 85 |
if(err == E_NOERROR) |
| 86 |
return false; |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 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 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 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 |
|
| 161 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
static int LoadMask(const char * ) |
| 252 |
{ |
| 253 |
return tri::io::Mask::IOM_WEDGTEXCOORD; |
| 254 |
} |
| 255 |
|
| 256 |
}; |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
#endif |