11 typedef struct { float x, y; } vec2_t;
12 typedef struct { float x, y, z; } vec3_t;
13 typedef struct { float x, y, z, w; } vec4_t;
16 struct vertex_pos_color {
25 static char *clean_line(char *s);
26 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn);
27 static int cmp_facevert(const void *ap, const void *bp);
28 static void free_rbnode_key(struct rbnode *n, void *cls);
30 /* merge of different indices per attribute happens during face processing.
32 * A triplet of (vertex index/texcoord index/normal index) is used as the key
33 * to search in a balanced binary search tree for vertex buffer index assigned
34 * to the same triplet if it has been encountered before. That index is
35 * appended to the index buffer.
37 * If a particular triplet has not been encountered before, a new g3d_vertex is
38 * appended to the vertex buffer. The index of this new vertex is appended to
39 * the index buffer, and also inserted into the tree for future searches.
41 int load_mesh(struct g3d_mesh *mesh, const char *fname)
43 int i, line_num = 0, result = -1;
47 struct vertex_pos_color *varr = 0;
50 struct rbtree *rbtree = 0;
52 if(!(fp = fopen(fname, "rb"))) {
53 fprintf(stderr, "load_mesh: failed to open file: %s\n", fname);
57 if(!(rbtree = rb_create(cmp_facevert))) {
58 fprintf(stderr, "load_mesh: failed to create facevertex binary search tree\n");
61 rb_set_delete_func(rbtree, free_rbnode_key, 0);
63 if(!(mesh->varr = dynarr_alloc(0, sizeof *mesh->varr)) ||
64 !(mesh->iarr = dynarr_alloc(0, sizeof *mesh->iarr))) {
65 fprintf(stderr, "load_mesh: failed to allocate resizable mesh arrays\n");
68 if(!(varr = dynarr_alloc(0, sizeof *varr)) ||
69 !(narr = dynarr_alloc(0, sizeof *narr)) ||
70 !(tarr = dynarr_alloc(0, sizeof *tarr))) {
71 fprintf(stderr, "load_mesh: failed to allocate resizable vertex array\n");
75 while(fgets(buf, sizeof buf, fp)) {
76 char *line = clean_line(buf);
83 if(isspace(line[1])) {
85 struct vertex_pos_color v;
88 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);
90 fprintf(stderr, "%s:%d: invalid vertex definition: \"%s\"\n", fname, line_num, line);
103 if(!(varr = dynarr_push(varr, &v))) {
104 fprintf(stderr, "load_mesh: failed to resize vertex buffer\n");
108 } else if(line[1] == 't' && isspace(line[2])) {
111 if(sscanf(line + 3, "%f %f", &tc.x, &tc.y) != 2) {
112 fprintf(stderr, "%s:%d: invalid texcoord definition: \"%s\"\n", fname, line_num, line);
115 if(!(tarr = dynarr_push(tarr, &tc))) {
116 fprintf(stderr, "load_mesh: failed to resize texcoord buffer\n");
120 } else if(line[1] == 'n' && isspace(line[2])) {
123 if(sscanf(line + 3, "%f %f %f", &norm.x, &norm.y, &norm.z) != 3) {
124 fprintf(stderr, "%s:%d: invalid normal definition: \"%s\"\n", fname, line_num, line);
127 if(!(narr = dynarr_push(narr, &norm))) {
128 fprintf(stderr, "load_mesh: failed to resize normal buffer\n");
135 if(isspace(line[1])) {
137 char *ptr = line + 2;
138 struct facevertex fv;
140 int vsz = dynarr_size(varr);
141 int tsz = dynarr_size(tarr);
142 int nsz = dynarr_size(narr);
145 if(!(ptr = parse_face_vert(ptr, &fv, vsz, tsz, nsz))) {
146 if(i < 3 || found_quad) {
147 fprintf(stderr, "%s:%d: invalid face definition: \"%s\"\n", fname, line_num, line);
154 if((node = rb_find(rbtree, &fv))) {
155 uint16_t idx = (int)(intptr_t)node->data;
156 if(!(mesh->iarr = dynarr_push(mesh->iarr, &idx))) {
157 fprintf(stderr, "load_mesh: failed to resize index array\n");
161 uint16_t newidx = dynarr_size(mesh->varr);
163 struct facevertex *newfv;
165 v.x = varr[fv.vidx].x;
166 v.y = varr[fv.vidx].y;
167 v.z = varr[fv.vidx].z;
169 v.r = cround64(varr[fv.vidx].r * 255.0);
170 v.g = cround64(varr[fv.vidx].g * 255.0);
171 v.b = cround64(varr[fv.vidx].b * 255.0);
172 v.a = cround64(varr[fv.vidx].a * 255.0);
174 v.u = tarr[fv.tidx].x;
175 v.v = tarr[fv.tidx].y;
181 v.nx = narr[fv.nidx].x;
182 v.ny = narr[fv.nidx].y;
183 v.nz = narr[fv.nidx].z;
189 if(!(mesh->varr = dynarr_push(mesh->varr, &v))) {
190 fprintf(stderr, "load_mesh: failed to resize combined vertex array\n");
193 if(!(mesh->iarr = dynarr_push(mesh->iarr, &newidx))) {
194 fprintf(stderr, "load_mesh: failed to resize index array\n");
198 if((newfv = malloc(sizeof *newfv))) {
201 if(!newfv || rb_insert(rbtree, newfv, (void*)(intptr_t)newidx) == -1) {
202 fprintf(stderr, "load_mesh: failed to insert facevertex to the binary search tree\n");
207 if(i > 3) found_quad = 1;
216 mesh->prim = found_quad ? G3D_QUADS : G3D_TRIANGLES;
217 mesh->vcount = dynarr_size(mesh->varr);
218 mesh->icount = dynarr_size(mesh->iarr);
219 mesh->varr = dynarr_finalize(mesh->varr);
220 mesh->iarr = dynarr_finalize(mesh->iarr);
221 result = 0; /* success */
223 printf("loaded %s mesh: %s: %d vertices, %d faces\n", found_quad ? "quad" : "triangle",
224 fname, mesh->vcount, mesh->icount / mesh->prim);
232 dynarr_free(mesh->varr);
233 dynarr_free(mesh->iarr);
239 int save_mesh(struct g3d_mesh *mesh, const char *fname)
244 if(!(fp = fopen(fname, "wb"))) {
245 fprintf(stderr, "save_mesh: failed to open %s for writing\n", fname);
248 fprintf(fp, "# Wavefront OBJ file shoved in your FACE by Mindlapse. Deal with it\n");
250 for(i=0; i<mesh->vcount; i++) {
251 struct g3d_vertex *v = mesh->varr + i;
252 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,
253 v->b / 255.0f, v->a / 255.0f);
255 for(i=0; i<mesh->vcount; i++) {
256 fprintf(fp, "vn %f %f %f\n", mesh->varr[i].nx, mesh->varr[i].ny, mesh->varr[i].nz);
258 for(i=0; i<mesh->vcount; i++) {
259 fprintf(fp, "vt %f %f\n", mesh->varr[i].u, mesh->varr[i].v);
262 fvcount = mesh->prim;
263 for(i=0; i<mesh->icount; i++) {
264 int idx = mesh->iarr[i] + 1;
266 if(fvcount == mesh->prim) {
270 fprintf(fp, " %d/%d/%d", idx, idx, idx);
279 static char *clean_line(char *s)
283 while(*s && isspace(*s)) ++s;
287 while(*end && *end != '#') ++end;
290 while(end > s && isspace(*end)) --end;
296 static char *parse_idx(char *ptr, int *idx, int arrsz)
299 int val = strtol(ptr, &endp, 10);
300 if(endp == ptr) return 0;
302 if(val < 0) { /* convert negative indices */
305 *idx = val - 1; /* indices in obj are 1-based */
310 /* possible face-vertex definitions:
314 * 4. vertex/texcoord/normal
316 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn)
318 if(!(ptr = parse_idx(ptr, &fv->vidx, numv)))
320 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
322 if(*++ptr == '/') { /* no texcoord */
326 if(!(ptr = parse_idx(ptr, &fv->tidx, numt)))
328 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
332 if(!(ptr = parse_idx(ptr, &fv->nidx, numn)))
334 return (!*ptr || isspace(*ptr)) ? ptr : 0;
337 static int cmp_facevert(const void *ap, const void *bp)
339 const struct facevertex *a = ap;
340 const struct facevertex *b = bp;
342 if(a->vidx == b->vidx) {
343 if(a->tidx == b->tidx) {
344 return a->nidx - b->nidx;
346 return a->tidx - b->tidx;
348 return a->vidx - b->vidx;
351 static void free_rbnode_key(struct rbnode *n, void *cls)