/* * VRML reading * 1996/11 KAMEDA Yoshinari */ #include #include #include "cg.h" #include "proto.h" extern int dv; /*----------* * CCKR P85 * *----------*/ static int strindex (char *s, char *t) { int i, j, k; for (i = 0; s[i] != '\0'; i++) { for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++) ; if (k > 0 && t[k] == '\0') return i; } return -1; } /*---------* * mu * *---------*/ static int getword (FILE *fd, char oneword[], int sl) { int i, c; while ((c = fgetc(fd)) != EOF && (isspace(c) || c == '#')) if (c == '#') { while ((c = fgetc(fd)) != EOF && c != '\n') ; if (c == EOF) return(0); } if (c == EOF) return(0); ungetc(c, fd); for (i = 0; i < sl - 1; i++) { oneword[i] = fgetc(fd); if (isspace(oneword[i])) break; } oneword[i] = '\0'; return(i); } static void read_material (FILE *fd, Oelem *oelem, char *b) { int c; if (dv) fprintf(stderr, "read_material\n"); while (getword(fd, b, MBS) > 0) { if (strindex(b, "{") >= 0) continue; if (strindex(b, "}") >= 0) break; else if (strindex(b, "diffuseColor") >= 0) { for (c = 0; getword(fd, b, MBS) > 0; ) { oelem->diff[c++] = atof(b); if (c >= 3) { if (dv) fprintf(stderr, "diffC: (%g, %g, %g)\n", oelem->diff[AR], oelem->diff[AG], oelem->diff[AB]); break; } } } else if (strindex(b, "specularColor") >= 0) { for (c = 0; getword(fd, b, MBS) > 0; ) { oelem->spec[c++] = atof(b); if (c >= 3) { if (dv) fprintf(stderr, "specC: (%g, %g, %g)\n", oelem->spec[AR], oelem->spec[AG], oelem->spec[AB]); break; } } } else if (strindex(b, "ambientColor") >= 0) { for (c = 0; getword(fd, b, MBS) > 0; ) { oelem->ambi[c++] = atof(b); if (c >= 3) { if (dv) fprintf(stderr, "ambiC: (%g, %g, %g)\n", oelem->ambi[AR], oelem->ambi[AG], oelem->ambi[AB]); break; } } } else if (strindex(b, "shininess") >= 0) { for (c = 0; getword(fd, b, MBS) > 0; ) { oelem->shine = atof(b); c++; if (c >= 1) { if (dv) fprintf(stderr, "shine: (%g)\n", oelem->shine); break; } } } } } static void read_coordinate (FILE *fd, Selem *selem, char *b) { int a; int n; n = 0; a = 0; if (dv) fprintf(stderr, "read_coordinate\n"); while (getword(fd, b, MBS) > 0) { if (strindex(b, "point") >= 0) { while (getword(fd, b, MBS) > 0) { if (strindex(b, "[") >= 0) continue; if (strindex(b, "]") >= 0) break; else { selem->v[n][a++] = atof(b); if (a >= 3) { selem->v[n][AA] = 1.0; a = 0; n++; if (n >= selem-> nv) { fprintf(stderr, "Too many vertices."); exit (1); } } } } break; } } selem->nv = n; if (dv) fprintf(stderr, "%d vertices found.\n", n); } static void read_faceset (FILE *fd, Selem *selem, char *b) { int i; int n; n = 0; i = 0; if (dv) fprintf(stderr, "read_faceset\n"); while (getword(fd, b, MBS) > 0) { if (strindex(b, "coordIndex") >= 0) { while (getword(fd, b, MBS) > 0) { if (strindex(b, "[") >= 0) continue; if (strindex(b, "]") >= 0) break; else { if (i < 3) selem->t[n][i] = atof(b); i++; if (i >= 4) { i = 0; n++; if (n >= selem-> nt) { fprintf(stderr, "Too many patches."); exit (1); } } } } break; } } selem->nt = n; if (dv) fprintf(stderr, "%d faces found.\n", n); } int read_one_obj (FILE *fd, Oelem *oelem, Selem *selem) { char b[MBS]; int s[3]; s[0] = s[1] = s[2] = 0; if (getword(fd, b, MBS) <= 0) return 0; while (s[0] == 0 || s[1] == 0 || s[2] == 0) { if (dv) fprintf(stderr, "read_one_obj: %s\n", b); if (strindex(b, "Material") >= 0) { read_material(fd, oelem, b); s[0] = 1; } else if (strindex(b, "Coordinate3") >= 0) { read_coordinate(fd, selem, b); s[1] = 1; } else if (strindex(b, "IndexedFaceSet") >= 0) { read_faceset(fd, selem, b); s[2] = 1; } else if (getword(fd, b, MBS) <= 0) return 0; } if (dv) fprintf(stderr, "vertex=%d triangle=%d\n", selem->nv, selem->nt); } #ifdef DEBUG_SAMPLE void main (int argc, char *argv[]){ FILE *fd; Selem selem; Oelem oelem; int n, i, a, c; selem.nv = 100; selem.v = (APos *)malloc(sizeof(APos) * selem.nv); selem.nt = 100; selem.t = (Tri *)malloc(sizeof(Tri) * selem.nt); fd = fopen("av.wrl", "r"); read_one_obj(fd, &oelem, &selem); fprintf(stderr, "C: diff: (%12g, %12g, %12g)\n", oelem.diff[AR], oelem.diff[AG], oelem.diff[AB]); fprintf(stderr, "C: spec: (%12g, %12g, %12g)\n", oelem.spec[AR], oelem.spec[AG], oelem.spec[AB]); fprintf(stderr, "C: ambi: (%12g, %12g, %12g)\n", oelem.ambi[AR], oelem.ambi[AG], oelem.ambi[AB]); fprintf(stderr, "C: shin: %12g\n", oelem.shine); for (n = 0; n < selem.nv; n++) fprintf(stderr, "V: %4d: (%12g, %12g, %12g)\n", n, selem.v[n][AX], selem.v[n][AY], selem.v[n][AZ]); for (n = 0; n < selem.nt; n++) fprintf(stderr, "T: %4d: (%3d, %3d, %3d)\n", n, selem.t[n][0], selem.t[n][1], selem.t[n][2]); } #endif