9 #include <assimp/cimport.h>
10 #include <assimp/postprocess.h>
11 #include <assimp/mesh.h>
12 #include <assimp/scene.h>
13 #include <assimp/types.h>
22 static int add_mesh(struct cmesh *mesh, struct aiMesh *aimesh);
25 (aiProcess_JoinIdenticalVertices | aiProcess_PreTransformVertices | \
26 aiProcess_Triangulate | aiProcess_SortByPType | aiProcess_FlipUVs)
28 int cmesh_load(struct cmesh *mesh, const char *fname)
31 const struct aiScene *aiscn;
33 if(!(aiscn = aiImportFile(fname, AIPPFLAGS))) {
34 fprintf(stderr, "failed to open mesh file: %s\n", fname);
38 for(i=0; i<(int)aiscn->mNumMeshes; i++) {
39 add_mesh(mesh, aiscn->mMeshes[i]);
42 aiReleaseImport(aiscn);
46 static int add_mesh(struct cmesh *mesh, struct aiMesh *aim)
48 int i, j, voffs, foffs;
50 voffs = cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX);
51 foffs = cmesh_poly_count(mesh);
53 for(i=0; i<aim->mNumVertices; i++) {
54 struct aiVector3D *v = aim->mVertices + i;
55 cmesh_push_attrib3f(mesh, CMESH_ATTR_VERTEX, v->x, v->y, v->z);
58 v = aim->mNormals + i;
59 cmesh_push_attrib3f(mesh, CMESH_ATTR_NORMAL, v->x, v->y, v->z);
62 v = aim->mTangents + i;
63 cmesh_push_attrib3f(mesh, CMESH_ATTR_TANGENT, v->x, v->y, v->z);
66 struct aiColor4D *col = aim->mColors[0] + i;
67 cmesh_push_attrib4f(mesh, CMESH_ATTR_COLOR, col->r, col->g, col->b, col->a);
69 if(aim->mTextureCoords[0]) {
70 v = aim->mTextureCoords[0] + i;
71 cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD, v->x, v->y);
73 if(aim->mTextureCoords[1]) {
74 v = aim->mTextureCoords[1] + i;
75 cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD2, v->x, v->y);
80 for(i=0; i<aim->mNumFaces; i++) {
81 assert(aim->mFaces[i].mNumIndices == 3);
83 cmesh_push_index(mesh, aim->mFaces[i].mIndices[j] + voffs);
86 cmesh_submesh(mesh, aim->mName.data, foffs, aim->mNumFaces);
101 static char *clean_line(char *s);
102 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn);
103 static int cmp_facevert(const void *ap, const void *bp);
104 static void free_rbnode_key(struct rbnode *n, void *cls);
106 /* merge of different indices per attribute happens during face processing.
108 * A triplet of (vertex index/texcoord index/normal index) is used as the key
109 * to search in a balanced binary search tree for vertex buffer index assigned
110 * to the same triplet if it has been encountered before. That index is
111 * appended to the index buffer.
113 * If a particular triplet has not been encountered before, a new vertex is
114 * appended to the vertex buffer. The index of this new vertex is appended to
115 * the index buffer, and also inserted into the tree for future searches.
117 int cmesh_load(struct cmesh *mesh, const char *fname)
119 int i, line_num = 0, result = -1;
123 struct vertex_pos *varr = 0;
126 struct rbtree *rbtree = 0;
128 int substart = 0, subcount = 0;
130 if(!(fp = fopen(fname, "rb"))) {
131 fprintf(stderr, "load_mesh: failed to open file: %s\n", fname);
135 if(!(rbtree = rb_create(cmp_facevert))) {
136 fprintf(stderr, "load_mesh: failed to create facevertex binary search tree\n");
139 rb_set_delete_func(rbtree, free_rbnode_key, 0);
141 if(!(varr = darr_alloc(0, sizeof *varr)) ||
142 !(narr = darr_alloc(0, sizeof *narr)) ||
143 !(tarr = darr_alloc(0, sizeof *tarr))) {
144 fprintf(stderr, "load_mesh: failed to allocate resizable vertex array\n");
148 while(fgets(buf, sizeof buf, fp)) {
149 char *line = clean_line(buf);
156 if(isspace(line[1])) {
161 num = sscanf(line + 2, "%f %f %f", &v.x, &v.y, &v.z);
163 fprintf(stderr, "%s:%d: invalid vertex definition: \"%s\"\n", fname, line_num, line);
166 if(!(varr = darr_push_impl(varr, &v))) {
167 fprintf(stderr, "load_mesh: failed to resize vertex buffer\n");
171 } else if(line[1] == 't' && isspace(line[2])) {
174 if(sscanf(line + 3, "%f %f", &tc.x, &tc.y) != 2) {
175 fprintf(stderr, "%s:%d: invalid texcoord definition: \"%s\"\n", fname, line_num, line);
179 if(!(tarr = darr_push_impl(tarr, &tc))) {
180 fprintf(stderr, "load_mesh: failed to resize texcoord buffer\n");
184 } else if(line[1] == 'n' && isspace(line[2])) {
187 if(sscanf(line + 3, "%f %f %f", &norm.x, &norm.y, &norm.z) != 3) {
188 fprintf(stderr, "%s:%d: invalid normal definition: \"%s\"\n", fname, line_num, line);
191 if(!(narr = darr_push_impl(narr, &norm))) {
192 fprintf(stderr, "load_mesh: failed to resize normal buffer\n");
199 if(isspace(line[1])) {
201 char *ptr = line + 2;
202 struct facevertex fv;
204 int vsz = darr_size(varr);
205 int tsz = darr_size(tarr);
206 int nsz = darr_size(narr);
209 if(!(ptr = parse_face_vert(ptr, &fv, vsz, tsz, nsz))) {
210 if(i < 3 || found_quad) {
211 fprintf(stderr, "%s:%d: invalid face definition: \"%s\"\n", fname, line_num, line);
218 if((node = rb_find(rbtree, &fv))) {
219 unsigned int idx = (unsigned int)(intptr_t)node->data;
220 assert(idx < cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX));
221 if(cmesh_push_index(mesh, idx) == -1) {
222 fprintf(stderr, "load_mesh: failed to resize index array\n");
225 subcount++; /* inc number of submesh indices, in case we have submeshes */
227 unsigned int newidx = cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX);
228 struct facevertex *newfv;
229 struct vertex_pos *vptr = varr + fv.vidx;
231 if(cmesh_push_attrib3f(mesh, CMESH_ATTR_VERTEX, vptr->x, vptr->y, vptr->z) == -1) {
232 fprintf(stderr, "load_mesh: failed to resize vertex array\n");
236 float nx = narr[fv.nidx].x;
237 float ny = narr[fv.nidx].y;
238 float nz = narr[fv.nidx].z;
239 if(cmesh_push_attrib3f(mesh, CMESH_ATTR_NORMAL, nx, ny, nz) == -1) {
240 fprintf(stderr, "load_mesh: failed to resize normal array\n");
245 float tu = tarr[fv.tidx].x;
246 float tv = tarr[fv.tidx].y;
247 if(cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD, tu, tv) == -1) {
248 fprintf(stderr, "load_mesh: failed to resize texcoord array\n");
253 if(cmesh_push_index(mesh, newidx) == -1) {
254 fprintf(stderr, "load_mesh: failed to resize index array\n");
257 subcount++; /* inc number of submesh indices, in case we have submeshes */
259 if((newfv = malloc(sizeof *newfv))) {
262 if(!newfv || rb_insert(rbtree, newfv, (void*)(intptr_t)newidx) == -1) {
263 fprintf(stderr, "load_mesh: failed to insert facevertex to the binary search tree\n");
268 if(i > 3) found_quad = 1;
274 printf("adding submesh: %s\n", subname);
275 cmesh_submesh(mesh, subname, substart / 3, subcount / 3);
278 if((subname = malloc(strlen(line)))) {
279 strcpy(subname, clean_line(line + 2));
281 substart += subcount;
291 /* don't add the final submesh if we never found another. an obj file with a
292 * single 'o' for the whole list of faces, is a single mesh without submeshes
294 if(cmesh_submesh_count(mesh) > 0) {
295 printf("adding submesh: %s\n", subname);
296 cmesh_submesh(mesh, subname, substart / 3, subcount / 3);
298 /* ... but use the 'o' name as the name of the mesh instead of the filename */
299 if(subname && *subname) {
300 cmesh_set_name(mesh, subname);
305 result = 0; /* success */
307 printf("loaded %s mesh: %s (%d submeshes): %d vertices, %d faces\n",
308 found_quad ? "quad" : "triangle", fname, cmesh_submesh_count(mesh),
309 cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX), cmesh_poly_count(mesh));
322 static char *clean_line(char *s)
326 while(*s && isspace(*s)) ++s;
330 while(*end && *end != '#') ++end;
333 while(end > s && isspace(*end)) {
340 static char *parse_idx(char *ptr, int *idx, int arrsz)
343 int val = strtol(ptr, &endp, 10);
344 if(endp == ptr) return 0;
346 if(val < 0) { /* convert negative indices */
349 *idx = val - 1; /* indices in obj are 1-based */
354 /* possible face-vertex definitions:
358 * 4. vertex/texcoord/normal
360 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn)
362 if(!(ptr = parse_idx(ptr, &fv->vidx, numv)))
364 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
366 if(*++ptr == '/') { /* no texcoord */
370 if(!(ptr = parse_idx(ptr, &fv->tidx, numt)))
372 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
376 if(!(ptr = parse_idx(ptr, &fv->nidx, numn)))
378 return (!*ptr || isspace(*ptr)) ? ptr : 0;
381 static int cmp_facevert(const void *ap, const void *bp)
383 const struct facevertex *a = ap;
384 const struct facevertex *b = bp;
386 if(a->vidx == b->vidx) {
387 if(a->tidx == b->tidx) {
388 return a->nidx - b->nidx;
390 return a->tidx - b->tidx;
392 return a->vidx - b->vidx;
395 static void free_rbnode_key(struct rbnode *n, void *cls)