-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmesh3ds.cpp
328 lines (283 loc) · 9.69 KB
/
mesh3ds.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
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
/* Copyright 2011 Pyarelal Knowles, under GNU LGPL (see LICENCE.txt) */
#include "prec.h"
#include <stdio.h>
#include <string.h>
#include <string>
#include <map>
#include "mesh/lib3ds/lib3ds.h"
#include "fileutil.h"
#include "matstack.h"
#include "vbomesh.h"
#include "material.h"
#include "imgpng.h"
#include "mesh3ds.h"
using namespace QI;
struct MeshNode
{
Lib3dsNode* node;
mat44 matrix;
MeshNode(Lib3dsNode* n, mat44 m = mat44::identity())
{
node = n;
matrix = m;
}
};
bool VBOMesh3DS::registerLoader()
{
return VBOMesh::registerLoader(".3ds", load);
}
bool VBOMesh3DS::load(VBOMesh& mesh, const char* filename)
{
mesh.release();
mesh.error = false;
std::string path = basefilepath(filename);
Lib3dsFile* f = lib3ds_file_open(filename);
if (!f)
{
printf("Error loading %s\n", filename);
mesh.error = true;
return false;
}
int vertexOffset = 0;
int indexOffset = 0;
mesh.numVertices = 0;
mesh.numIndices = 0;
bool hasTexCoords = false;
//extract all instances
typedef std::pair<std::string, mat44> Instance;
std::vector<Instance> instances;
std::set<std::string> isInstanced;
std::vector<MeshNode> nodes;
for (Lib3dsNode* p = f->nodes; p != NULL; p = p->next)
nodes.push_back(MeshNode(p, mat44(p->matrix))); //add root
while (nodes.size() > 0)
{
//get next
MeshNode n = nodes.back();
nodes.pop_back();
//add children
for (Lib3dsNode* p = n.node->childs; p != NULL; p = p->next)
nodes.push_back(MeshNode(p, n.matrix * mat44(p->matrix)));
//if mesh instance, extract
if (n.node->type == LIB3DS_NODE_MESH_INSTANCE)
{
Lib3dsMeshInstanceNode *inst = (Lib3dsMeshInstanceNode*)n.node;
printf("\n");
PRINTVEC3(*(vec4f*)inst->pos);
PRINTVEC4(*(vec4f*)inst->rot);
PRINTVEC3(*(vec4f*)inst->scl);
PRINTVEC3(*(vec4f*)inst->pivot);
//printf("%i\n", inst->hide);
instances.push_back(Instance(n.node->name, n.matrix * mat44::translate(-*(vec3f*)inst->pivot)));
isInstanced.insert(n.node->name);
}
}
std::map<std::string, Lib3dsMesh*> meshByName;
//perhaps some meshes were not referenced in the node hierarchy. chuck them in too :)
for (int i = 0; i < f->nmeshes; ++i)
{
if (isInstanced.find(f->meshes[i]->name) == isInstanced.end())
{
//printf("3ds mesh %s UNINSTANCED\n", f->meshes[i]->name);
instances.push_back(Instance(f->meshes[i]->name, mat44::identity()));
}
assert(meshByName.find(f->meshes[i]->name) == meshByName.end()); //I'd assume the mesh name must be unique
meshByName[f->meshes[i]->name] = f->meshes[i];
}
//count num verts/indices
for (std::vector<Instance>::iterator it = instances.begin(); it != instances.end(); ++it)
{
std::map<std::string, Lib3dsMesh*>::iterator found;
found = meshByName.find(it->first);
if (found == meshByName.end())
{
printf("missing mesh %s\n", (*it).first.c_str());
continue;
}
Lib3dsMesh* m = found->second;
mesh.numVertices += m->nvertices;
mesh.numIndices += m->nfaces * 3;
hasTexCoords = hasTexCoords || (m->texcos != NULL);
}
//extract materials
//FIXME: currently, only one material per mesh is supported - the material of the first face
for (int i = 0; i < f->nmaterials; ++i)
{
Lib3dsTextureMap& texture = f->materials[i]->texture1_map;
Lib3dsTextureMap& normalmap = f->materials[i]->bump_map;
Material* mat;
if (!MaterialCache::getMaterial(std::string(filename) + f->materials[i]->name, mat))
{
mat->colour = vec4f(f->materials[i]->diffuse[0], f->materials[i]->diffuse[1], f->materials[i]->diffuse[2], 1.0);
//printf("%s -> %s\n", f->materials[i]->name, (path + "/" + texture.name).c_str());
if (strlen(texture.name))
{
mat->imgColour.load((path + texture.name).c_str());
//mat->imgColour->anisotropy = 4.0f;
}
if (strlen(normalmap.name))
{
mat->imgNormal.load((path + normalmap.name).c_str());
//mat->imgNormal->anisotropy = 4.0f;
}
else
{
//assume normal map is basename + dd
std::string bn = basefilename(texture.name);
std::string assumedNormalsImage = path + bn + "dd.png";
if (fileExists(assumedNormalsImage.c_str()))
mat->imgNormal.load(assumedNormalsImage.c_str());
//mat->imgNormal->anisotropy = 4.0f;
//if (mat->imgNormal.texture)
// printf("%s\n", (path + bn + "dd.png").c_str());
}
}
mesh.addMaterial(mat, f->materials[i]->name);
}
//sort meshes by material, to reduce facesets and hence bind/unbind calls
typedef std::map<int, std::vector<int> > MaterialInstances;
MaterialInstances materialInstances;
for (int i = 0; i < (int)instances.size(); ++i)
{
std::map<std::string, Lib3dsMesh*>::iterator found;
found = meshByName.find(instances[i].first);
if (found == meshByName.end())
continue;
Lib3dsMesh* m = found->second;
int material = -1;
if (m->nfaces > 0)
material = m->faces[0].material;
//append mesh to material instance list. create if one doesn't exist for this material
if (materialInstances.find(material) == materialInstances.end())
materialInstances[material] = std::vector<int>();
materialInstances[material].push_back(i);
}
mesh.sub[VERTICES] = new float[mesh.numVertices*3];
if (hasTexCoords)
mesh.sub[TEXCOORDS] = new float[mesh.numVertices*2];
mesh.dataIndices = new unsigned int[mesh.numIndices];
int lastMaterialIndex = 0;
for (MaterialInstances::iterator it = materialInstances.begin(); it != materialInstances.end(); ++it)
{
for (int i = 0; i < (int)it->second.size(); ++i)
{
Instance& inst = instances[it->second[i]];
std::map<std::string, Lib3dsMesh*>::iterator found;
found = meshByName.find(inst.first);
if (found == meshByName.end())
continue;
Lib3dsMesh* m = found->second;
//printf("3ds mesh %s\n", m->name);
assert(m->vertices);
//swivel x/y/z
mat44 mat = mat44::zero();
mat.d[1][0] = 1.0;
mat.d[0][2] = 1.0;
mat.d[2][1] = 1.0;
mat *= mat44(m->matrix);
mat *= inst.second;
mat *= mat44(m->matrix).inverse();
//mat *= mat44::translate(m->matrix[3][0], m->matrix[3][1], m->matrix[3][2]);
//PRINTMAT44(mat44(m->matrix));
//printf("\tverts=%i\n", m->nvertices);
for (int v = 0; v < m->nvertices; ++v)
{
*(vec3f*)&mesh.sub[VERTICES][(vertexOffset+v)*3] = mat *
vec4f(m->vertices[v][0], m->vertices[v][1], m->vertices[v][2], 1.0f);
if (m->texcos)
{
mesh.sub[TEXCOORDS][(vertexOffset+v)*2+0] = m->texcos[v][0];
mesh.sub[TEXCOORDS][(vertexOffset+v)*2+1] = m->texcos[v][1];
}
else if (hasTexCoords)
{
mesh.sub[TEXCOORDS][(vertexOffset+v)*2+0] = 0.0f;
mesh.sub[TEXCOORDS][(vertexOffset+v)*2+1] = 0.0f;
}
}
mesh.indexed = true;
for (int n = 0; n < m->nfaces; ++n)
{
mesh.dataIndices[(indexOffset+n*3)+0] = vertexOffset + m->faces[n].index[0];
mesh.dataIndices[(indexOffset+n*3)+1] = vertexOffset + m->faces[n].index[1];
mesh.dataIndices[(indexOffset+n*3)+2] = vertexOffset + m->faces[n].index[2];
}
vertexOffset += m->nvertices;
indexOffset += m->nfaces * 3;
}
//add a faceset for the material group
if (it->first >= 0 && indexOffset > lastMaterialIndex)
{
mesh.useMaterial(lastMaterialIndex, indexOffset, f->materials[it->first]->name);
//printf("%s: %i->%i\n", f->materials[it->first]->name, lastMaterialIndex, indexOffset);
lastMaterialIndex = indexOffset;
}
}
//printf("Grouped to %i facesets\n", (int)materialInstances.size());
lib3ds_file_free(f);
mesh.generateNormals();
mesh.numPolygons = mesh.numIndices / 3;
#if 0
//http://openctm.sourceforge.net/media/DevelopersManual.pdf
3DScontext context;
// Create a new importer context
context = ctmNewContext(3DS_IMPORT);
// Load the Open3DS file
ctmLoad(context, filename);
3DSenum err = ctmGetError(context);
if(err != 3DS_NONE)
{
printf("Error opening %s\n", filename);
ctmFreeContext(context);
mesh.error = true;
return false;
}
// Access the mesh data
mesh.numVertices = ctmGetInteger(context, 3DS_VERTEX_COUNT);
int norms = ctmGetInteger(context, 3DS_HAS_NORMALS);
int texcs = ctmGetInteger(context, 3DS_UV_MAP_COUNT);
int other = ctmGetInteger(context, 3DS_ATTRIB_MAP_COUNT);
mesh.numPolygons = ctmGetInteger(context, 3DS_TRIANGLE_COUNT);
mesh.numIndices = 3 * mesh.numPolygons;
mesh.sub[VERTICES] = (float*)ctmGetFloatArray(context, 3DS_VERTICES);
if (norms)
mesh.sub[NORMALS] = (float*)ctmGetFloatArray(context, 3DS_NORMALS);
if (texcs)
mesh.sub[TEXCOORDS] = (float*)ctmGetFloatArray(context, 3DS_UV_MAP_1);
if (other)
mesh.sub[TANGENTS] = (float*)ctmGetFloatArray(context, 3DS_ATTRIB_MAP_1);
delete[] mesh.dataIndices;
mesh.dataIndices = new unsigned int[mesh.numIndices];
mesh.indexed = true;
memcpy(mesh.dataIndices, ctmGetIntegerArray(context, 3DS_INDICES), mesh.numIndices * sizeof(unsigned int));
if (!norms)
{
printf("Generating normals\n");
float* tmp = NULL;
if (smoothGeneratedNormals)
{
//generate normals as though vertices were smoothed a little
tmp = mesh.sub[VERTICES];
mesh.sub[VERTICES] = new float[mesh.numVertices*3];
memcpy(mesh.sub[VERTICES], tmp, mesh.numVertices*3*sizeof(float));
mesh.averageVertices();
}
mesh.generateNormals();
if (tmp)
{
//free tmp data - use actual vertices
delete[] mesh.sub[VERTICES];
mesh.sub[VERTICES] = tmp;
}
}
mesh.interleave(false);
//important - remove pointers. we didn't allocate that data
mesh.sub[VERTICES] = NULL;
mesh.sub[NORMALS] = NULL;
mesh.sub[TEXCOORDS] = NULL;
mesh.sub[TANGENTS] = NULL;
// Free the context
ctmFreeContext(context);
#endif
return true;
}