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);
24 (aiProcess_JoinIdenticalVertices | aiProcess_PreTransformVertices | \
25 aiProcess_Triangulate | aiProcess_SortByPType | aiProcess_FlipUVs)
27 int cmesh_load(struct cmesh *mesh, const char *fname)
30 const struct aiScene *aiscn;
32 if(!(aiscn = aiImportFile(fname, AIPPFLAGS))) {
33 fprintf(stderr, "failed to open mesh file: %s\n", fname);
37 for(i=0; i<(int)aiscn->mNumMeshes; i++) {
38 add_mesh(mesh, aiscn->mMeshes[i]);
41 aiReleaseImport(aiscn);
45 static int add_mesh(struct cmesh *mesh, struct aiMesh *aim)
47 int i, j, voffs, foffs;
49 voffs = cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX);
50 foffs = cmesh_poly_count(mesh);
52 for(i=0; i<aim->mNumVertices; i++) {
53 struct aiVector3D *v = aim->mVertices + i;
54 cmesh_push_attrib3f(mesh, CMESH_ATTR_VERTEX, v->x, v->y, v->z);
57 v = aim->mNormals + i;
58 cmesh_push_attrib3f(mesh, CMESH_ATTR_NORMAL, v->x, v->y, v->z);
61 v = aim->mTangents + i;
62 cmesh_push_attrib3f(mesh, CMESH_ATTR_TANGENT, v->x, v->y, v->z);
65 struct aiColor4D *col = aim->mColors[0] + i;
66 cmesh_push_attrib4f(mesh, CMESH_ATTR_COLOR, col->r, col->g, col->b, col->a);
68 if(aim->mTextureCoords[0]) {
69 v = aim->mTextureCoords[0] + i;
70 cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD, v->x, v->y);
72 if(aim->mTextureCoords[1]) {
73 v = aim->mTextureCoords[1] + i;
74 cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD2, v->x, v->y);
79 for(i=0; i<aim->mNumFaces; i++) {
80 assert(aim->mFaces[i].mNumIndices == 3);
82 cmesh_push_index(mesh, aim->mFaces[i].mIndices[j] + voffs);
85 cmesh_submesh(mesh, aim->mName.data, foffs, aim->mNumFaces);
100 static char *clean_line(char *s);
101 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn);
102 static int cmp_facevert(const void *ap, const void *bp);
103 static void free_rbnode_key(struct rbnode *n, void *cls);
105 /* merge of different indices per attribute happens during face processing.
107 * A triplet of (vertex index/texcoord index/normal index) is used as the key
108 * to search in a balanced binary search tree for vertex buffer index assigned
109 * to the same triplet if it has been encountered before. That index is
110 * appended to the index buffer.
112 * If a particular triplet has not been encountered before, a new vertex is
113 * appended to the vertex buffer. The index of this new vertex is appended to
114 * the index buffer, and also inserted into the tree for future searches.
116 int cmesh_load(struct cmesh *mesh, const char *fname)
118 int i, line_num = 0, result = -1;
122 struct vertex_pos *varr = 0;
125 struct rbtree *rbtree = 0;
127 int substart = 0, subcount = 0;
129 if(!(fp = fopen(fname, "rb"))) {
130 fprintf(stderr, "load_mesh: failed to open file: %s\n", fname);
134 if(!(rbtree = rb_create(cmp_facevert))) {
135 fprintf(stderr, "load_mesh: failed to create facevertex binary search tree\n");
138 rb_set_delete_func(rbtree, free_rbnode_key, 0);
140 if(!(varr = dynarr_alloc(0, sizeof *varr)) ||
141 !(narr = dynarr_alloc(0, sizeof *narr)) ||
142 !(tarr = dynarr_alloc(0, sizeof *tarr))) {
143 fprintf(stderr, "load_mesh: failed to allocate resizable vertex array\n");
147 while(fgets(buf, sizeof buf, fp)) {
148 char *line = clean_line(buf);
155 if(isspace(line[1])) {
160 num = sscanf(line + 2, "%f %f %f", &v.x, &v.y, &v.z);
162 fprintf(stderr, "%s:%d: invalid vertex definition: \"%s\"\n", fname, line_num, line);
165 if(!(varr = dynarr_push(varr, &v))) {
166 fprintf(stderr, "load_mesh: failed to resize vertex buffer\n");
170 } else if(line[1] == 't' && isspace(line[2])) {
173 if(sscanf(line + 3, "%f %f", &tc.x, &tc.y) != 2) {
174 fprintf(stderr, "%s:%d: invalid texcoord definition: \"%s\"\n", fname, line_num, line);
178 if(!(tarr = dynarr_push(tarr, &tc))) {
179 fprintf(stderr, "load_mesh: failed to resize texcoord buffer\n");
183 } else if(line[1] == 'n' && isspace(line[2])) {
186 if(sscanf(line + 3, "%f %f %f", &norm.x, &norm.y, &norm.z) != 3) {
187 fprintf(stderr, "%s:%d: invalid normal definition: \"%s\"\n", fname, line_num, line);
190 if(!(narr = dynarr_push(narr, &norm))) {
191 fprintf(stderr, "load_mesh: failed to resize normal buffer\n");
198 if(isspace(line[1])) {
200 char *ptr = line + 2;
201 struct facevertex fv;
203 int vsz = dynarr_size(varr);
204 int tsz = dynarr_size(tarr);
205 int nsz = dynarr_size(narr);
208 if(!(ptr = parse_face_vert(ptr, &fv, vsz, tsz, nsz))) {
209 if(i < 3 || found_quad) {
210 fprintf(stderr, "%s:%d: invalid face definition: \"%s\"\n", fname, line_num, line);
217 if((node = rb_find(rbtree, &fv))) {
218 unsigned int idx = (unsigned int)node->data;
219 assert(idx < cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX));
220 if(cmesh_push_index(mesh, idx) == -1) {
221 fprintf(stderr, "load_mesh: failed to resize index array\n");
224 subcount++; /* inc number of submesh indices, in case we have submeshes */
226 unsigned int newidx = cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX);
227 struct facevertex *newfv;
228 struct vertex_pos *vptr = varr + fv.vidx;
230 if(cmesh_push_attrib3f(mesh, CMESH_ATTR_VERTEX, vptr->x, vptr->y, vptr->z) == -1) {
231 fprintf(stderr, "load_mesh: failed to resize vertex array\n");
235 float nx = narr[fv.nidx].x;
236 float ny = narr[fv.nidx].y;
237 float nz = narr[fv.nidx].z;
238 if(cmesh_push_attrib3f(mesh, CMESH_ATTR_NORMAL, nx, ny, nz) == -1) {
239 fprintf(stderr, "load_mesh: failed to resize normal array\n");
244 float tu = tarr[fv.tidx].x;
245 float tv = tarr[fv.tidx].y;
246 if(cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD, tu, tv) == -1) {
247 fprintf(stderr, "load_mesh: failed to resize texcoord array\n");
252 if(cmesh_push_index(mesh, newidx) == -1) {
253 fprintf(stderr, "load_mesh: failed to resize index array\n");
256 subcount++; /* inc number of submesh indices, in case we have submeshes */
258 if((newfv = malloc(sizeof *newfv))) {
261 if(!newfv || rb_insert(rbtree, newfv, (void*)newidx) == -1) {
262 fprintf(stderr, "load_mesh: failed to insert facevertex to the binary search tree\n");
267 if(i > 3) found_quad = 1;
273 printf("adding submesh: %s\n", subname);
274 cmesh_submesh(mesh, subname, substart / 3, subcount / 3);
277 if((subname = malloc(strlen(line)))) {
278 strcpy(subname, clean_line(line + 2));
280 substart += subcount;
290 /* don't add the final submesh if we never found another. an obj file with a
291 * single 'o' for the whole list of faces, is a single mesh without submeshes
293 if(cmesh_submesh_count(mesh) > 0) {
294 printf("adding submesh: %s\n", subname);
295 cmesh_submesh(mesh, subname, substart / 3, subcount / 3);
297 /* ... but use the 'o' name as the name of the mesh instead of the filename */
298 if(subname && *subname) {
299 cmesh_set_name(mesh, subname);
304 result = 0; /* success */
306 printf("loaded %s mesh: %s (%d submeshes): %d vertices, %d faces\n",
307 found_quad ? "quad" : "triangle", fname, cmesh_submesh_count(mesh),
308 cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX), cmesh_poly_count(mesh));
321 static char *clean_line(char *s)
325 while(*s && isspace(*s)) ++s;
329 while(*end && *end != '#') ++end;
332 while(end > s && isspace(*end)) {
339 static char *parse_idx(char *ptr, int *idx, int arrsz)
342 int val = strtol(ptr, &endp, 10);
343 if(endp == ptr) return 0;
345 if(val < 0) { /* convert negative indices */
348 *idx = val - 1; /* indices in obj are 1-based */
353 /* possible face-vertex definitions:
357 * 4. vertex/texcoord/normal
359 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn)
361 if(!(ptr = parse_idx(ptr, &fv->vidx, numv)))
363 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
365 if(*++ptr == '/') { /* no texcoord */
369 if(!(ptr = parse_idx(ptr, &fv->tidx, numt)))
371 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
375 if(!(ptr = parse_idx(ptr, &fv->nidx, numn)))
377 return (!*ptr || isspace(*ptr)) ? ptr : 0;
380 static int cmp_facevert(const void *ap, const void *bp)
382 const struct facevertex *a = ap;
383 const struct facevertex *b = bp;
385 if(a->vidx == b->vidx) {
386 if(a->tidx == b->tidx) {
387 return a->nidx - b->nidx;
389 return a->tidx - b->tidx;
391 return a->vidx - b->vidx;
394 static void free_rbnode_key(struct rbnode *n, void *cls)