11 struct vertex_pos_color {
20 static char *clean_line(char *s);
21 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn);
22 static int cmp_facevert(const void *ap, const void *bp);
23 static void free_rbnode_key(struct rbnode *n, void *cls);
25 /* merge of different indices per attribute happens during face processing.
27 * A triplet of (vertex index/texcoord index/normal index) is used as the key
28 * to search in a balanced binary search tree for vertex buffer index assigned
29 * to the same triplet if it has been encountered before. That index is
30 * appended to the index buffer.
32 * If a particular triplet has not been encountered before, a new g3d_vertex is
33 * appended to the vertex buffer. The index of this new vertex is appended to
34 * the index buffer, and also inserted into the tree for future searches.
36 int load_mesh(struct g3d_mesh *mesh, const char *fname)
38 int i, line_num = 0, result = -1;
42 struct vertex_pos_color *varr = 0;
45 struct rbtree *rbtree = 0;
47 if(!(fp = fopen(fname, "rb"))) {
48 fprintf(stderr, "load_mesh: failed to open file: %s\n", fname);
52 if(!(rbtree = rb_create(cmp_facevert))) {
53 fprintf(stderr, "load_mesh: failed to create facevertex binary search tree\n");
56 rb_set_delete_func(rbtree, free_rbnode_key, 0);
58 if(!(mesh->varr = dynarr_alloc(0, sizeof *mesh->varr)) ||
59 !(mesh->iarr = dynarr_alloc(0, sizeof *mesh->iarr))) {
60 fprintf(stderr, "load_mesh: failed to allocate resizable mesh arrays\n");
63 if(!(varr = dynarr_alloc(0, sizeof *varr)) ||
64 !(narr = dynarr_alloc(0, sizeof *narr)) ||
65 !(tarr = dynarr_alloc(0, sizeof *tarr))) {
66 fprintf(stderr, "load_mesh: failed to allocate resizable vertex array\n");
70 while(fgets(buf, sizeof buf, fp)) {
71 char *line = clean_line(buf);
78 if(isspace(line[1])) {
80 struct vertex_pos_color v;
83 num = sscanf(line + 2, "%f %f %f %f %f %f %f", &v.x, &v.y, &v.z, &v.r, &v.g, &v.b, &v.a);
85 fprintf(stderr, "%s:%d: invalid vertex definition: \"%s\"\n", fname, line_num, line);
98 if(!(varr = dynarr_push(varr, &v))) {
99 fprintf(stderr, "load_mesh: failed to resize vertex buffer\n");
103 } else if(line[1] == 't' && isspace(line[2])) {
106 if(sscanf(line + 3, "%f %f", &tc.x, &tc.y) != 2) {
107 fprintf(stderr, "%s:%d: invalid texcoord definition: \"%s\"\n", fname, line_num, line);
110 if(!(tarr = dynarr_push(tarr, &tc))) {
111 fprintf(stderr, "load_mesh: failed to resize texcoord buffer\n");
115 } else if(line[1] == 'n' && isspace(line[2])) {
118 if(sscanf(line + 3, "%f %f %f", &norm.x, &norm.y, &norm.z) != 3) {
119 fprintf(stderr, "%s:%d: invalid normal definition: \"%s\"\n", fname, line_num, line);
122 if(!(narr = dynarr_push(narr, &norm))) {
123 fprintf(stderr, "load_mesh: failed to resize normal buffer\n");
130 if(isspace(line[1])) {
132 char *ptr = line + 2;
133 struct facevertex fv;
135 int vsz = dynarr_size(varr);
136 int tsz = dynarr_size(tarr);
137 int nsz = dynarr_size(narr);
140 if(!(ptr = parse_face_vert(ptr, &fv, vsz, tsz, nsz))) {
141 if(i < 3 || found_quad) {
142 fprintf(stderr, "%s:%d: invalid face definition: \"%s\"\n", fname, line_num, line);
149 if((node = rb_find(rbtree, &fv))) {
150 uint16_t idx = (int)(intptr_t)node->data;
151 if(!(mesh->iarr = dynarr_push(mesh->iarr, &idx))) {
152 fprintf(stderr, "load_mesh: failed to resize index array\n");
156 uint16_t newidx = dynarr_size(mesh->varr);
158 struct facevertex *newfv;
160 v.x = varr[fv.vidx].x;
161 v.y = varr[fv.vidx].y;
162 v.z = varr[fv.vidx].z;
164 v.r = cround64(varr[fv.vidx].r * 255.0);
165 v.g = cround64(varr[fv.vidx].g * 255.0);
166 v.b = cround64(varr[fv.vidx].b * 255.0);
167 v.a = cround64(varr[fv.vidx].a * 255.0);
169 v.u = tarr[fv.tidx].x;
170 v.v = tarr[fv.tidx].y;
176 v.nx = narr[fv.nidx].x;
177 v.ny = narr[fv.nidx].y;
178 v.nz = narr[fv.nidx].z;
184 if(!(mesh->varr = dynarr_push(mesh->varr, &v))) {
185 fprintf(stderr, "load_mesh: failed to resize combined vertex array\n");
188 if(!(mesh->iarr = dynarr_push(mesh->iarr, &newidx))) {
189 fprintf(stderr, "load_mesh: failed to resize index array\n");
193 if((newfv = malloc(sizeof *newfv))) {
196 if(!newfv || rb_insert(rbtree, newfv, (void*)(intptr_t)newidx) == -1) {
197 fprintf(stderr, "load_mesh: failed to insert facevertex to the binary search tree\n");
202 if(i > 3) found_quad = 1;
211 mesh->prim = found_quad ? G3D_QUADS : G3D_TRIANGLES;
212 mesh->vcount = dynarr_size(mesh->varr);
213 mesh->icount = dynarr_size(mesh->iarr);
214 mesh->varr = dynarr_finalize(mesh->varr);
215 mesh->iarr = dynarr_finalize(mesh->iarr);
216 result = 0; /* success */
218 printf("loaded %s mesh: %s: %d vertices, %d faces\n", found_quad ? "quad" : "triangle",
219 fname, mesh->vcount, mesh->icount / mesh->prim);
227 dynarr_free(mesh->varr);
228 dynarr_free(mesh->iarr);
234 int save_mesh(struct g3d_mesh *mesh, const char *fname)
239 if(!(fp = fopen(fname, "wb"))) {
240 fprintf(stderr, "save_mesh: failed to open %s for writing\n", fname);
243 fprintf(fp, "# Wavefront OBJ file shoved in your FACE by Mindlapse. Deal with it\n");
245 for(i=0; i<mesh->vcount; i++) {
246 struct g3d_vertex *v = mesh->varr + i;
247 fprintf(fp, "v %f %f %f %f %f %f %f\n", v->x, v->y, v->z, v->r / 255.0f, v->g / 255.0f,
248 v->b / 255.0f, v->a / 255.0f);
250 for(i=0; i<mesh->vcount; i++) {
251 fprintf(fp, "vn %f %f %f\n", mesh->varr[i].nx, mesh->varr[i].ny, mesh->varr[i].nz);
253 for(i=0; i<mesh->vcount; i++) {
254 fprintf(fp, "vt %f %f\n", mesh->varr[i].u, mesh->varr[i].v);
257 fvcount = mesh->prim;
258 for(i=0; i<mesh->icount; i++) {
259 int idx = mesh->iarr[i] + 1;
261 if(fvcount == mesh->prim) {
265 fprintf(fp, " %d/%d/%d", idx, idx, idx);
274 static char *clean_line(char *s)
278 while(*s && isspace(*s)) ++s;
282 while(*end && *end != '#') ++end;
285 while(end > s && isspace(*end)) --end;
291 static char *parse_idx(char *ptr, int *idx, int arrsz)
294 int val = strtol(ptr, &endp, 10);
295 if(endp == ptr) return 0;
297 if(val < 0) { /* convert negative indices */
300 *idx = val - 1; /* indices in obj are 1-based */
305 /* possible face-vertex definitions:
309 * 4. vertex/texcoord/normal
311 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn)
313 if(!(ptr = parse_idx(ptr, &fv->vidx, numv)))
315 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
317 if(*++ptr == '/') { /* no texcoord */
321 if(!(ptr = parse_idx(ptr, &fv->tidx, numt)))
323 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
327 if(!(ptr = parse_idx(ptr, &fv->nidx, numn)))
329 return (!*ptr || isspace(*ptr)) ? ptr : 0;
332 static int cmp_facevert(const void *ap, const void *bp)
334 const struct facevertex *a = ap;
335 const struct facevertex *b = bp;
337 if(a->vidx == b->vidx) {
338 if(a->tidx == b->tidx) {
339 return a->nidx - b->nidx;
341 return a->tidx - b->tidx;
343 return a->vidx - b->vidx;
346 static void free_rbnode_key(struct rbnode *n, void *cls)