5 static char *cleanline(char *s)
9 if((ptr = strchr(s, '#'))) *ptr = 0;
11 while(*s && isspace(*s)) s++;
12 ptr = s + strlen(s) - 1;
13 while(ptr >= s && isspace(*s)) *ptr-- = 0;
18 #define APPEND(prefix) \
20 if(prefix##count >= prefix##max) { \
21 int newsz = prefix##max ? prefix##max * 2 : 8; \
22 void *ptr = realloc(prefix##arr, newsz * sizeof(cgm_vec3)); \
24 fprintf(stderr, "load_mesh: failed to resize array to %d elements\n", newsz); \
28 prefix##max = newsz; \
33 int load_mesh(struct mesh *m, const char *fname)
38 cgm_vec3 *varr, *narr, *tarr;
39 int vcount, ncount, tcount, vmax, nmax, tmax;
40 char linebuf[256], *line;
42 varr = narr = tarr = 0;
43 vcount = ncount = tcount = vmax = nmax = tmax = 0;
45 if(!(fp = fopen(fname, "rb"))) {
46 fprintf(stderr, "load_mesh: failed to open: %s\n", fname);
51 while(fgets(linebuf, sizeof linebuf, fp)) {
53 if(!(line = cleanline(linebuf))) {
60 if((num = sscanf(line + 1, "%f %f %f", &v.x, &v.y, &v.z)) < 2) {
61 verr: fprintf(stderr, "load_mesh: ignoring malformed attribute at %d: %s\n", nline, line);
64 if(isspace(line[1])) {
67 } else if(line[1] == 'n') {
70 } else if(line[1] == 't') {
73 tarr[ncount++].y = v.y;
88 void draw_mesh(struct mesh *m)
90 glEnableClientState(GL_VERTEX_ARRAY);
91 glEnableClientState(GL_NORMAL_ARRAY);
92 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
94 glVertexPointer(3, GL_FLOAT, sizeof *m->faces, &m->faces->v[0].pos.x);
95 glNormalPointer(GL_FLOAT, sizeof *m->faces, &m->faces->v[0].norm.x);
96 glTexCoordPointer(2, GL_FLOAT, sizeof *m->faces, &m->faces->v[0].tex.x);
98 glDrawArrays(GL_TRIANGLES, 0, m->num_faces * 3);
100 glDisableClientState(GL_VERTEX_ARRAY);
101 glDisableClientState(GL_NORMAL_ARRAY);
102 glDisableClientState(GL_TEXTURE_COORD_ARRAY);