9 struct vertex_pos_color {
18 static char *clean_line(char *s);
19 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn);
20 static int cmp_facevert(const void *ap, const void *bp);
21 static void free_rbnode_key(struct rbnode *n, void *cls);
23 /* merge of different indices per attribute happens during face processing.
25 * A triplet of (vertex index/texcoord index/normal index) is used as the key
26 * to search in a balanced binary search tree for vertex buffer index assigned
27 * to the same triplet if it has been encountered before. That index is
28 * appended to the index buffer.
30 * If a particular triplet has not been encountered before, a new vertex is
31 * appended to the vertex buffer. The index of this new vertex is appended to
32 * the index buffer, and also inserted into the tree for future searches.
34 int cmesh_load(struct cmesh *mesh, const char *fname)
36 int i, line_num = 0, result = -1;
41 struct vertex_pos_color *varr = 0;
44 struct rbtree *rbtree = 0;
46 if(!(fp = fopen(fname, "rb"))) {
47 fprintf(stderr, "load_mesh: failed to open file: %s\n", fname);
51 if(!(rbtree = rb_create(cmp_facevert))) {
52 fprintf(stderr, "load_mesh: failed to create facevertex binary search tree\n");
55 rb_set_delete_func(rbtree, free_rbnode_key, 0);
57 if(!(varr = dynarr_alloc(0, sizeof *varr)) ||
58 !(narr = dynarr_alloc(0, sizeof *narr)) ||
59 !(tarr = dynarr_alloc(0, sizeof *tarr))) {
60 fprintf(stderr, "load_mesh: failed to allocate resizable vertex array\n");
64 while(fgets(buf, sizeof buf, fp)) {
65 char *line = clean_line(buf);
72 if(isspace(line[1])) {
74 struct vertex_pos_color v;
77 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);
79 fprintf(stderr, "%s:%d: invalid vertex definition: \"%s\"\n", fname, line_num, line);
82 if(num > 3) found_color = 1;
93 if(!(varr = dynarr_push(varr, &v))) {
94 fprintf(stderr, "load_mesh: failed to resize vertex buffer\n");
98 } else if(line[1] == 't' && isspace(line[2])) {
101 if(sscanf(line + 3, "%f %f", &tc.x, &tc.y) != 2) {
102 fprintf(stderr, "%s:%d: invalid texcoord definition: \"%s\"\n", fname, line_num, line);
105 if(!(tarr = dynarr_push(tarr, &tc))) {
106 fprintf(stderr, "load_mesh: failed to resize texcoord buffer\n");
110 } else if(line[1] == 'n' && isspace(line[2])) {
113 if(sscanf(line + 3, "%f %f %f", &norm.x, &norm.y, &norm.z) != 3) {
114 fprintf(stderr, "%s:%d: invalid normal definition: \"%s\"\n", fname, line_num, line);
117 if(!(narr = dynarr_push(narr, &norm))) {
118 fprintf(stderr, "load_mesh: failed to resize normal buffer\n");
125 if(isspace(line[1])) {
127 char *ptr = line + 2;
128 struct facevertex fv;
130 int vsz = dynarr_size(varr);
131 int tsz = dynarr_size(tarr);
132 int nsz = dynarr_size(narr);
135 if(!(ptr = parse_face_vert(ptr, &fv, vsz, tsz, nsz))) {
136 if(i < 3 || found_quad) {
137 fprintf(stderr, "%s:%d: invalid face definition: \"%s\"\n", fname, line_num, line);
144 if((node = rb_find(rbtree, &fv))) {
145 unsigned int idx = (unsigned int)node->data;
146 assert(idx < cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX));
147 if(cmesh_push_index(mesh, idx) == -1) {
148 fprintf(stderr, "load_mesh: failed to resize index array\n");
152 unsigned int newidx = cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX);
153 struct facevertex *newfv;
154 struct vertex_pos_color *vptr = varr + fv.vidx;
156 if(cmesh_push_attrib3f(mesh, CMESH_ATTR_VERTEX, vptr->x, vptr->y, vptr->z) == -1) {
157 fprintf(stderr, "load_mesh: failed to resize vertex array\n");
161 if(cmesh_push_attrib(mesh, CMESH_ATTR_COLOR, &vptr->r) == -1) {
162 fprintf(stderr, "load_mesh: failed to resize color array\n");
167 float nx = narr[fv.nidx].x;
168 float ny = narr[fv.nidx].y;
169 float nz = narr[fv.nidx].z;
170 if(cmesh_push_attrib3f(mesh, CMESH_ATTR_NORMAL, nx, ny, nz) == -1) {
171 fprintf(stderr, "load_mesh: failed to resize normal array\n");
176 float tu = tarr[fv.tidx].x;
177 float tv = tarr[fv.tidx].y;
178 if(cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD, tu, tv) == -1) {
179 fprintf(stderr, "load_mesh: failed to resize texcoord array\n");
184 if(cmesh_push_index(mesh, newidx) == -1) {
185 fprintf(stderr, "load_mesh: failed to resize index array\n");
189 if((newfv = malloc(sizeof *newfv))) {
192 if(!newfv || rb_insert(rbtree, newfv, (void*)newidx) == -1) {
193 fprintf(stderr, "load_mesh: failed to insert facevertex to the binary search tree\n");
198 if(i > 3) found_quad = 1;
207 result = 0; /* success */
209 printf("loaded %s mesh: %s: %d vertices, %d faces\n", found_quad ? "quad" : "triangle",
210 fname, cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX), cmesh_poly_count(mesh));
221 static char *clean_line(char *s)
225 while(*s && isspace(*s)) ++s;
229 while(*end && *end != '#') ++end;
232 while(end > s && isspace(*end)) --end;
238 static char *parse_idx(char *ptr, int *idx, int arrsz)
241 int val = strtol(ptr, &endp, 10);
242 if(endp == ptr) return 0;
244 if(val < 0) { /* convert negative indices */
247 *idx = val - 1; /* indices in obj are 1-based */
252 /* possible face-vertex definitions:
256 * 4. vertex/texcoord/normal
258 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn)
260 if(!(ptr = parse_idx(ptr, &fv->vidx, numv)))
262 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
264 if(*++ptr == '/') { /* no texcoord */
268 if(!(ptr = parse_idx(ptr, &fv->tidx, numt)))
270 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
274 if(!(ptr = parse_idx(ptr, &fv->nidx, numn)))
276 return (!*ptr || isspace(*ptr)) ? ptr : 0;
279 static int cmp_facevert(const void *ap, const void *bp)
281 const struct facevertex *a = ap;
282 const struct facevertex *b = bp;
284 if(a->vidx == b->vidx) {
285 if(a->tidx == b->tidx) {
286 return a->nidx - b->nidx;
288 return a->tidx - b->tidx;
290 return a->vidx - b->vidx;
293 static void free_rbnode_key(struct rbnode *n, void *cls)