8 #include <assimp/cimport.h>
9 #include <assimp/postprocess.h>
10 #include <assimp/mesh.h>
11 #include <assimp/scene.h>
12 #include <assimp/types.h>
21 static int add_mesh(struct cmesh *mesh, struct aiMesh *aimesh, const struct aiNode *ainode);
22 static struct aiNode *find_node(struct aiNode *root, unsigned int midx);
25 (aiProcess_JoinIdenticalVertices | \
26 aiProcess_Triangulate | aiProcess_SortByPType | aiProcess_FlipUVs)
28 int cmesh_load(struct cmesh *mesh, const char *fname)
31 const struct aiScene *aiscn;
32 const struct aiNode *ainode;
34 if(!(aiscn = aiImportFile(fname, AIPPFLAGS))) {
35 fprintf(stderr, "failed to open mesh file: %s\n", fname);
39 printf("scene contains %d meshes\n", (int)aiscn->mNumMeshes);
40 for(i=0; i<(int)aiscn->mNumMeshes; i++) {
41 if(aiscn->mRootNode->mNumChildren) {
42 ainode = find_node(aiscn->mRootNode, i);
46 add_mesh(mesh, aiscn->mMeshes[i], ainode);
49 aiReleaseImport(aiscn);
53 static int add_mesh(struct cmesh *mesh, struct aiMesh *aim, const struct aiNode *ainode)
55 int i, j, voffs, foffs;
58 if(ainode && ainode->mName.length > 0) {
59 name = ainode->mName.data;
61 name = aim->mName.data;
63 printf("adding mesh: %s\n", name);
65 voffs = cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX);
66 foffs = cmesh_poly_count(mesh);
68 for(i=0; i<aim->mNumVertices; i++) {
69 struct aiVector3D *v = aim->mVertices + i;
70 cmesh_push_attrib3f(mesh, CMESH_ATTR_VERTEX, v->x, v->y, v->z);
73 v = aim->mNormals + i;
74 cmesh_push_attrib3f(mesh, CMESH_ATTR_NORMAL, v->x, v->y, v->z);
77 v = aim->mTangents + i;
78 cmesh_push_attrib3f(mesh, CMESH_ATTR_TANGENT, v->x, v->y, v->z);
81 struct aiColor4D *col = aim->mColors[0] + i;
82 cmesh_push_attrib4f(mesh, CMESH_ATTR_COLOR, col->r, col->g, col->b, col->a);
84 if(aim->mTextureCoords[0]) {
85 v = aim->mTextureCoords[0] + i;
86 cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD, v->x, v->y);
88 if(aim->mTextureCoords[1]) {
89 v = aim->mTextureCoords[1] + i;
90 cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD2, v->x, v->y);
95 for(i=0; i<aim->mNumFaces; i++) {
96 assert(aim->mFaces[i].mNumIndices == 3);
98 cmesh_push_index(mesh, aim->mFaces[i].mIndices[j] + voffs);
101 cmesh_submesh(mesh, name, foffs, aim->mNumFaces);
106 static struct aiNode *find_node(struct aiNode *node, unsigned int midx)
111 for(i=0; i<node->mNumMeshes; i++) {
112 if(node->mMeshes[i] == midx) {
117 for(i=0; i<node->mNumChildren; i++) {
118 if((n = find_node(node->mChildren[i], midx))) {
132 int vidx, tidx, nidx;
135 static char *clean_line(char *s);
136 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn);
137 static int cmp_facevert(const void *ap, const void *bp);
138 static void free_rbnode_key(struct rbnode *n, void *cls);
140 /* merge of different indices per attribute happens during face processing.
142 * A triplet of (vertex index/texcoord index/normal index) is used as the key
143 * to search in a balanced binary search tree for vertex buffer index assigned
144 * to the same triplet if it has been encountered before. That index is
145 * appended to the index buffer.
147 * If a particular triplet has not been encountered before, a new vertex is
148 * appended to the vertex buffer. The index of this new vertex is appended to
149 * the index buffer, and also inserted into the tree for future searches.
151 int cmesh_load(struct cmesh *mesh, const char *fname)
153 int i, line_num = 0, result = -1;
157 struct vertex_pos *varr = 0;
160 struct rbtree *rbtree = 0;
162 int substart = 0, subcount = 0;
164 if(!(fp = fopen(fname, "rb"))) {
165 fprintf(stderr, "load_mesh: failed to open file: %s\n", fname);
169 if(!(rbtree = rb_create(cmp_facevert))) {
170 fprintf(stderr, "load_mesh: failed to create facevertex binary search tree\n");
173 rb_set_delete_func(rbtree, free_rbnode_key, 0);
175 if(!(varr = dynarr_alloc(0, sizeof *varr)) ||
176 !(narr = dynarr_alloc(0, sizeof *narr)) ||
177 !(tarr = dynarr_alloc(0, sizeof *tarr))) {
178 fprintf(stderr, "load_mesh: failed to allocate resizable vertex array\n");
182 while(fgets(buf, sizeof buf, fp)) {
183 char *line = clean_line(buf);
190 if(isspace(line[1])) {
195 num = sscanf(line + 2, "%f %f %f", &v.x, &v.y, &v.z);
197 fprintf(stderr, "%s:%d: invalid vertex definition: \"%s\"\n", fname, line_num, line);
200 if(!(varr = dynarr_push(varr, &v))) {
201 fprintf(stderr, "load_mesh: failed to resize vertex buffer\n");
205 } else if(line[1] == 't' && isspace(line[2])) {
208 if(sscanf(line + 3, "%f %f", &tc.x, &tc.y) != 2) {
209 fprintf(stderr, "%s:%d: invalid texcoord definition: \"%s\"\n", fname, line_num, line);
213 if(!(tarr = dynarr_push(tarr, &tc))) {
214 fprintf(stderr, "load_mesh: failed to resize texcoord buffer\n");
218 } else if(line[1] == 'n' && isspace(line[2])) {
221 if(sscanf(line + 3, "%f %f %f", &norm.x, &norm.y, &norm.z) != 3) {
222 fprintf(stderr, "%s:%d: invalid normal definition: \"%s\"\n", fname, line_num, line);
225 if(!(narr = dynarr_push(narr, &norm))) {
226 fprintf(stderr, "load_mesh: failed to resize normal buffer\n");
233 if(isspace(line[1])) {
235 char *ptr = line + 2;
236 struct facevertex fv;
238 int vsz = dynarr_size(varr);
239 int tsz = dynarr_size(tarr);
240 int nsz = dynarr_size(narr);
243 if(!(ptr = parse_face_vert(ptr, &fv, vsz, tsz, nsz))) {
244 if(i < 3 || found_quad) {
245 fprintf(stderr, "%s:%d: invalid face definition: \"%s\"\n", fname, line_num, line);
252 if((node = rb_find(rbtree, &fv))) {
253 unsigned int idx = (unsigned int)node->data;
254 assert(idx < cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX));
255 if(cmesh_push_index(mesh, idx) == -1) {
256 fprintf(stderr, "load_mesh: failed to resize index array\n");
259 subcount++; /* inc number of submesh indices, in case we have submeshes */
261 unsigned int newidx = cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX);
262 struct facevertex *newfv;
263 struct vertex_pos *vptr = varr + fv.vidx;
265 if(cmesh_push_attrib3f(mesh, CMESH_ATTR_VERTEX, vptr->x, vptr->y, vptr->z) == -1) {
266 fprintf(stderr, "load_mesh: failed to resize vertex array\n");
270 float nx = narr[fv.nidx].x;
271 float ny = narr[fv.nidx].y;
272 float nz = narr[fv.nidx].z;
273 if(cmesh_push_attrib3f(mesh, CMESH_ATTR_NORMAL, nx, ny, nz) == -1) {
274 fprintf(stderr, "load_mesh: failed to resize normal array\n");
279 float tu = tarr[fv.tidx].x;
280 float tv = tarr[fv.tidx].y;
281 if(cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD, tu, tv) == -1) {
282 fprintf(stderr, "load_mesh: failed to resize texcoord array\n");
287 if(cmesh_push_index(mesh, newidx) == -1) {
288 fprintf(stderr, "load_mesh: failed to resize index array\n");
291 subcount++; /* inc number of submesh indices, in case we have submeshes */
293 if((newfv = malloc(sizeof *newfv))) {
296 if(!newfv || rb_insert(rbtree, newfv, (void*)newidx) == -1) {
297 fprintf(stderr, "load_mesh: failed to insert facevertex to the binary search tree\n");
302 if(i > 3) found_quad = 1;
308 printf("adding submesh: %s\n", subname);
309 cmesh_submesh(mesh, subname, substart / 3, subcount / 3);
312 if((subname = malloc(strlen(line)))) {
313 strcpy(subname, clean_line(line + 2));
315 substart += subcount;
325 /* don't add the final submesh if we never found another. an obj file with a
326 * single 'o' for the whole list of faces, is a single mesh without submeshes
328 if(cmesh_submesh_count(mesh) > 0) {
329 printf("adding submesh: %s\n", subname);
330 cmesh_submesh(mesh, subname, substart / 3, subcount / 3);
332 /* ... but use the 'o' name as the name of the mesh instead of the filename */
333 if(subname && *subname) {
334 cmesh_set_name(mesh, subname);
339 result = 0; /* success */
341 printf("loaded %s mesh: %s (%d submeshes): %d vertices, %d faces\n",
342 found_quad ? "quad" : "triangle", fname, cmesh_submesh_count(mesh),
343 cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX), cmesh_poly_count(mesh));
356 static char *clean_line(char *s)
360 while(*s && isspace(*s)) ++s;
364 while(*end && *end != '#') ++end;
367 while(end > s && isspace(*end)) {
374 static char *parse_idx(char *ptr, int *idx, int arrsz)
377 int val = strtol(ptr, &endp, 10);
378 if(endp == ptr) return 0;
380 if(val < 0) { /* convert negative indices */
383 *idx = val - 1; /* indices in obj are 1-based */
388 /* possible face-vertex definitions:
392 * 4. vertex/texcoord/normal
394 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn)
396 if(!(ptr = parse_idx(ptr, &fv->vidx, numv)))
398 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
400 if(*++ptr == '/') { /* no texcoord */
404 if(!(ptr = parse_idx(ptr, &fv->tidx, numt)))
406 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
410 if(!(ptr = parse_idx(ptr, &fv->nidx, numn)))
412 return (!*ptr || isspace(*ptr)) ? ptr : 0;
415 static int cmp_facevert(const void *ap, const void *bp)
417 const struct facevertex *a = ap;
418 const struct facevertex *b = bp;
420 if(a->vidx == b->vidx) {
421 if(a->tidx == b->tidx) {
422 return a->nidx - b->nidx;
424 return a->tidx - b->tidx;
426 return a->vidx - b->vidx;
429 static void free_rbnode_key(struct rbnode *n, void *cls)