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;
40 struct vertex_pos_color *varr = 0;
43 struct rbtree *rbtree = 0;
45 if(!(fp = fopen(fname, "rb"))) {
46 fprintf(stderr, "load_mesh: failed to open file: %s\n", fname);
50 if(!(rbtree = rb_create(cmp_facevert))) {
51 fprintf(stderr, "load_mesh: failed to create facevertex binary search tree\n");
54 rb_set_delete_func(rbtree, free_rbnode_key, 0);
56 if(!(varr = dynarr_alloc(0, sizeof *varr)) ||
57 !(narr = dynarr_alloc(0, sizeof *narr)) ||
58 !(tarr = dynarr_alloc(0, sizeof *tarr))) {
59 fprintf(stderr, "load_mesh: failed to allocate resizable vertex array\n");
63 while(fgets(buf, sizeof buf, fp)) {
64 char *line = clean_line(buf);
71 if(isspace(line[1])) {
73 struct vertex_pos_color v;
76 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);
78 fprintf(stderr, "%s:%d: invalid vertex definition: \"%s\"\n", fname, line_num, line);
91 if(!(varr = dynarr_push(varr, &v))) {
92 fprintf(stderr, "load_mesh: failed to resize vertex buffer\n");
96 } else if(line[1] == 't' && isspace(line[2])) {
99 if(sscanf(line + 3, "%f %f", &tc.x, &tc.y) != 2) {
100 fprintf(stderr, "%s:%d: invalid texcoord definition: \"%s\"\n", fname, line_num, line);
103 if(!(tarr = dynarr_push(tarr, &tc))) {
104 fprintf(stderr, "load_mesh: failed to resize texcoord buffer\n");
108 } else if(line[1] == 'n' && isspace(line[2])) {
111 if(sscanf(line + 3, "%f %f %f", &norm.x, &norm.y, &norm.z) != 3) {
112 fprintf(stderr, "%s:%d: invalid normal definition: \"%s\"\n", fname, line_num, line);
115 if(!(narr = dynarr_push(narr, &norm))) {
116 fprintf(stderr, "load_mesh: failed to resize normal buffer\n");
123 if(isspace(line[1])) {
125 char *ptr = line + 2;
126 struct facevertex fv;
128 int vsz = dynarr_size(varr);
129 int tsz = dynarr_size(tarr);
130 int nsz = dynarr_size(narr);
133 if(!(ptr = parse_face_vert(ptr, &fv, vsz, tsz, nsz))) {
134 if(i < 3 || found_quad) {
135 fprintf(stderr, "%s:%d: invalid face definition: \"%s\"\n", fname, line_num, line);
142 if((node = rb_find(rbtree, &fv))) {
143 unsigned int idx = (unsigned int)node->data;
144 if(cmesh_push_index(mesh, idx) == -1) {
145 fprintf(stderr, "load_mesh: failed to resize index array\n");
149 unsigned int newidx = cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX);
150 struct facevertex *newfv;
151 struct vertex_pos_color *vptr = varr + fv.vidx;
154 if(cmesh_push_attrib3f(mesh, CMESH_ATTR_VERTEX, vptr->x, vptr->y, vptr->z) == -1) {
155 fprintf(stderr, "load_mesh: failed to resize vertex array\n");
158 if(cmesh_push_attrib(mesh, CMESH_ATTR_COLOR, &vptr->r) == -1) {
159 fprintf(stderr, "load_mesh: failed to resize color array\n");
163 tu = tarr[fv.tidx].x;
164 tv = tarr[fv.tidx].y;
169 if(cmesh_push_attrib2f(mesh, CMESH_ATTR_TEXCOORD, tu, tv) == -1) {
170 fprintf(stderr, "load_mesh: failed to resize texcoord array\n");
174 if((newfv = malloc(sizeof *newfv))) {
177 if(!newfv || rb_insert(rbtree, newfv, (void*)newidx) == -1) {
178 fprintf(stderr, "load_mesh: failed to insert facevertex to the binary search tree\n");
183 if(i > 3) found_quad = 1;
192 result = 0; /* success */
194 printf("loaded %s mesh: %s: %d vertices, %d faces\n", found_quad ? "quad" : "triangle",
195 fname, cmesh_attrib_count(mesh, CMESH_ATTR_VERTEX), cmesh_poly_count(mesh));
206 static char *clean_line(char *s)
210 while(*s && isspace(*s)) ++s;
214 while(*end && *end != '#') ++end;
217 while(end > s && isspace(*end)) --end;
223 static char *parse_idx(char *ptr, int *idx, int arrsz)
226 int val = strtol(ptr, &endp, 10);
227 if(endp == ptr) return 0;
229 if(val < 0) { /* convert negative indices */
232 *idx = val - 1; /* indices in obj are 1-based */
237 /* possible face-vertex definitions:
241 * 4. vertex/texcoord/normal
243 static char *parse_face_vert(char *ptr, struct facevertex *fv, int numv, int numt, int numn)
245 if(!(ptr = parse_idx(ptr, &fv->vidx, numv)))
247 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
249 if(*++ptr == '/') { /* no texcoord */
253 if(!(ptr = parse_idx(ptr, &fv->tidx, numt)))
255 if(*ptr != '/') return (!*ptr || isspace(*ptr)) ? ptr : 0;
259 if(!(ptr = parse_idx(ptr, &fv->nidx, numn)))
261 return (!*ptr || isspace(*ptr)) ? ptr : 0;
264 static int cmp_facevert(const void *ap, const void *bp)
266 const struct facevertex *a = ap;
267 const struct facevertex *b = bp;
269 if(a->vidx == b->vidx) {
270 if(a->tidx == b->tidx) {
271 return a->nidx - b->nidx;
273 return a->tidx - b->tidx;
275 return a->vidx - b->vidx;
278 static void free_rbnode_key(struct rbnode *n, void *cls)